Skip to content

Commit

Permalink
Issue #926 fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
skattel49 authored and Brylie Christopher Oxley committed Sep 14, 2021
1 parent 0b8e587 commit e360d48
Show file tree
Hide file tree
Showing 22 changed files with 205 additions and 178 deletions.
32 changes: 16 additions & 16 deletions project/accounts/api.py
Original file line number Diff line number Diff line change
@@ -1,53 +1,53 @@
from rest_framework.viewsets import ModelViewSet
from rest_framework.decorators import action
from rest_framework.response import Response
from api.permissions import IsAccountOwnerOrDuringRegistrationOrReadOnly
from api.permissions import IsProfileOwnerOrDuringRegistrationOrReadOnly
from api.utils import get_account
from api.models import Thread
from accounts.models import Account
from accounts.models import Profile
from api.serializers import (
ThreadSerializer,
CategorySerializer,
CiviSerializer,
AccountSerializer,
AccountListSerializer,
ProfileSerializer,
ProfileListSerializer,
)


class AccountViewSet(ModelViewSet):
class ProfileViewSet(ModelViewSet):

"""
REST API viewset for an Account
REST API viewset for an Profile
retrieve:
Return the given user based a username.
list:
Return a list of all the existing users. Only with privileged access.
"""

queryset = Account.objects.all()
queryset = Profile.objects.all()
lookup_field = "user__username"
serializer_class = AccountSerializer
serializer_class = ProfileSerializer
http_method_names = ["get", "head", "put", "patch"]
permission_classes = (IsAccountOwnerOrDuringRegistrationOrReadOnly,)
permission_classes = (IsProfileOwnerOrDuringRegistrationOrReadOnly,)
authentication_classes = ()

def list(self, request):
""" """
if self.request.user.is_staff:
accounts = Account.objects.all()
accounts = Profile.objects.all()
else:
accounts = Account.objects.filter(user=self.request.user)
serializer = AccountListSerializer(accounts, many=True)
accounts = Profile.objects.filter(user=self.request.user)
serializer = ProfileListSerializer(accounts, many=True)
return Response(serializer.data)

def retrieve(self, request, user__username=None):
""" """
account = get_account(username=user__username)
if self.request.user == account.user:
serializer = AccountSerializer(account)
serializer = ProfileSerializer(account)
else:
serializer = AccountListSerializer(account)
serializer = ProfileListSerializer(account)
return Response(serializer.data)

@action(detail=True)
Expand All @@ -69,7 +69,7 @@ def followers(self, request, user__username=None):
"""
account = get_account(username=user__username)
account_followers = account.followers.all()
serializer = AccountListSerializer(account_followers, many=True)
serializer = ProfileListSerializer(account_followers, many=True)
return Response(serializer.data)

@action(detail=True)
Expand All @@ -80,7 +80,7 @@ def following(self, request, user__username=None):
"""
account = get_account(username=user__username)
account_followings = account.following.all()
serializer = AccountListSerializer(account_followings, many=True)
serializer = ProfileListSerializer(account_followings, many=True)
return Response(serializer.data)

@action(detail=True)
Expand Down
20 changes: 10 additions & 10 deletions project/accounts/authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@


from accounts.utils import send_email
from accounts.models import Account
from .forms import AccountRegistrationForm, PasswordResetForm, RecoverUserForm
from accounts.models import Profile
from .forms import ProfileRegistrationForm, PasswordResetForm, RecoverUserForm
from core.custom_decorators import require_post_params


User = get_user_model()


class AccountActivationTokenGenerator(PasswordResetTokenGenerator):
class ProfileActivationTokenGenerator(PasswordResetTokenGenerator):
"""Token Generator for Email Confirmation"""


Expand All @@ -45,7 +45,7 @@ def _make_token_with_timestamp(self, user, timestamp):
return "%s-%s" % (ts_b36, hash)


account_activation_token = AccountActivationTokenGenerator()
account_activation_token = ProfileActivationTokenGenerator()


def send_activation_email(user, domain):
Expand All @@ -68,7 +68,7 @@ def send_activation_email(user, domain):
html_message = render_to_string("email/base_email_template.html", context)
sender = settings.EMAIL_HOST_USER
send_email(
subject="CiviWiki Account Setup",
subject="CiviWiki Profile Setup",
message=message,
sender=sender,
recipient_list=[user.email],
Expand Down Expand Up @@ -99,7 +99,7 @@ def cw_login(request):

if user.is_active:

account = get_object_or_404(Account, user=user)
account = get_object_or_404(Profile, user=user)
request.session["login_user_firstname"] = account.first_name
request.session["login_user_image"] = account.profile_image_thumb_url

Expand Down Expand Up @@ -135,19 +135,19 @@ def cw_register(request):
Return:
(200, ok) (500, Internal Error)
"""
form = AccountRegistrationForm(request.POST or None)
form = ProfileRegistrationForm(request.POST or None)
if request.method == "POST":
# Form Validation
if form.is_valid():
username = form.clean_username()
password = form.clean_password()
email = form.clean_email()

# Create a New Account
# Create a New Profile
try:
user = User.objects.create_user(username, email, password)

account = Account(user=user)
account = Profile(user=user)
account.save()

user.is_active = True
Expand Down Expand Up @@ -187,7 +187,7 @@ def activate_view(request, uidb64, token):
user = None

if user is not None and account_activation_token.check_token(user, token):
account = Account.objects.get(user=user)
account = Profile.objects.get(user=user)
if account.is_verified:
redirect_link = {"href": "/", "label": "Back to Main"}
template_var = {
Expand Down
18 changes: 9 additions & 9 deletions project/accounts/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@

from accounts.utils import send_email
from .reserved_usernames import RESERVED_USERNAMES
from accounts.models import Account
from accounts.models import Profile

User = get_user_model()


class AccountRegistrationForm(ModelForm):
class ProfileRegistrationForm(ModelForm):
"""
This class is used to register new account in Civiwiki
Expand Down Expand Up @@ -189,7 +189,7 @@ def save(
email_body = body_txt.format(domain=domain, username=user.username)

context = {
"title": "Account Recovery for CiviWiki",
"title": "Profile Recovery for CiviWiki",
"greeting": "Recover your account on CiviWiki",
"body": email_body,
"link": url_with_code,
Expand All @@ -202,22 +202,22 @@ def save(
html_message = render_to_string(html_message_template, context)
sender = settings.EMAIL_HOST_USER
send_email(
subject="Account Recovery for CiviWiki",
subject="Profile Recovery for CiviWiki",
message=message,
sender=settings.EMAIL_HOST_USER,
recipient_list=[email],
html_message=html_message,
)


class UpdateAccount(forms.ModelForm):
class UpdateProfile(forms.ModelForm):
"""
Form for updating Account data
Form for updating Profile data
"""

def __init__(self, *args, **kwargs):
readonly = kwargs.pop("readonly", False)
super(UpdateAccount, self).__init__(*args, **kwargs)
super(UpdateProfile, self).__init__(*args, **kwargs)
if readonly:
self.disable_fields()

Expand All @@ -226,7 +226,7 @@ def disable_fields(self):
field.disabled = True

class Meta:
model = Account
model = Profile
fields = [
"first_name",
"last_name",
Expand Down Expand Up @@ -297,7 +297,7 @@ class UpdateProfileImage(forms.ModelForm):
"""

class Meta:
model = Account
model = Profile
fields = ["profile_image"]

profile_image = forms.ImageField()
Expand Down
24 changes: 24 additions & 0 deletions project/accounts/migrations/0004_auto_20210914_0739.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Generated by Django 3.2.7 on 2021-09-14 07:39

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('api', '0041_auto_20210906_0802'),
('taggit', '0003_taggeditem_add_unique_index'),
('accounts', '0003_account'),
]

operations = [
migrations.RenameModel(
old_name='Account',
new_name='Profile',
),
migrations.RenameField(
model_name='profile',
old_name='full_account',
new_name='full_profile',
),
]
Loading

0 comments on commit e360d48

Please sign in to comment.