Skip to content

Commit

Permalink
29038 remove user profile model
Browse files Browse the repository at this point in the history
  • Loading branch information
domdinicola committed Apr 12, 2022
1 parent 0c00287 commit 950588c
Show file tree
Hide file tree
Showing 7 changed files with 7 additions and 96 deletions.
3 changes: 1 addition & 2 deletions django_api/etools_prp/apps/account/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from django.contrib.auth.admin import UserAdmin

from etools_prp.apps.account.forms import CustomUserCreationForm, UserAdminForm
from etools_prp.apps.account.models import User, UserProfile
from etools_prp.apps.account.models import User


class CustomUserAdmin(UserAdmin):
Expand Down Expand Up @@ -56,4 +56,3 @@ class CustomUserAdmin(UserAdmin):


admin.site.register(User, CustomUserAdmin)
admin.site.register(UserProfile)
28 changes: 0 additions & 28 deletions django_api/etools_prp/apps/account/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
from django.db.models.signals import post_save
from django.utils.functional import cached_property

from model_utils.models import TimeStampedModel

from etools_prp.apps.core.common import PRP_ROLE_TYPES, USER_TYPES
from etools_prp.apps.utils.emails import send_email_from_template

Expand Down Expand Up @@ -112,30 +110,4 @@ def save(self, *args, **kwargs):
super().save(*args, **kwargs)


class UserProfile(TimeStampedModel):
"""
User Profile model related with user as profile.
related models:
account.User (OneToOne): "user"
"""
user = models.OneToOneField(
User,
related_name="profile",
on_delete=models.CASCADE,
)

def __str__(self):
return "{} - Profile".format(self.user.get_fullname())

@classmethod
def create_user_profile(cls, sender, instance, created, **kwargs):
"""
Signal handler to create user profiles automatically
"""
if created:
cls.objects.create(user=instance)


post_save.connect(UserProfile.create_user_profile, sender=User)
post_save.connect(User.lock_password_if_new, sender=User)
8 changes: 2 additions & 6 deletions django_api/etools_prp/apps/account/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,9 @@ def get_access(self, obj):
class Meta:
model = User
fields = (
'id', 'email', 'first_name',
'last_name', 'profile',
'partner', 'organization',
'access', 'prp_roles',
'position'
'id', 'email', 'first_name', 'last_name', 'partner', 'organization', 'access', 'prp_roles', 'position'
)
read_only_fields = ('id', 'profile', 'partner', 'organization', 'access', 'prp_roles')
read_only_fields = ('id', 'partner', 'organization', 'access', 'prp_roles')
depth = 1


Expand Down
27 changes: 0 additions & 27 deletions django_api/etools_prp/apps/account/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,33 +12,6 @@
from etools_prp.apps.core.tests.factories import faker


class UserProfileAPIViewTestCase(BaseAPITestCase):

def setUp(self):
self.partner = factories.PartnerFactory()
self.user = factories.PartnerUserFactory(partner=self.partner)

super().setUp()

def test_user_profile(self):
"""Test if the user profile is created when user is created
"""
url = reverse('user-profile')
response = self.client.get(url)

self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data['profile']['id'], self.user.profile.id)

def test_unauthenticated_user(self):
"""Test if the user profile is not accessible to unauthenticated user
"""
self.client.logout()
url = reverse('user-profile')
response = self.client.get(url)

self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)


class UserLogoutAPIViewTestCase(BaseAPITestCase):

def setUp(self):
Expand Down
3 changes: 1 addition & 2 deletions django_api/etools_prp/apps/account/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

from drfpasswordless.views import ObtainEmailCallbackToken

from .views import LoginUserWithTokenAPIView, UserLogoutAPIView, UserProfileAPIView
from .views import LoginUserWithTokenAPIView, UserLogoutAPIView

urlpatterns = [
re_path(r'^user-profile/$', UserProfileAPIView.as_view(), name="user-profile"),
re_path(r'^user-logout/$', UserLogoutAPIView.as_view(), name="user-logout"),

re_path(r'^auth/login-with-token/$', LoginUserWithTokenAPIView.as_view(), name='user-passwordless-login'),
Expand Down
23 changes: 2 additions & 21 deletions django_api/etools_prp/apps/account/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from rest_framework import status as statuses
from rest_framework.exceptions import PermissionDenied, ValidationError
from rest_framework.filters import OrderingFilter
from rest_framework.generics import ListCreateAPIView, RetrieveAPIView
from rest_framework.generics import ListCreateAPIView
from rest_framework.response import Response
from rest_framework.views import APIView

Expand All @@ -19,26 +19,7 @@

from .filters import UserFilter
from .models import User
from .serializers import UserSerializer, UserWithPRPRolesSerializer


class UserProfileAPIView(RetrieveAPIView):
"""
User Profile API - GET
Authentication required.
Returns:
UserSerializer object.
"""
serializer_class = UserSerializer
permission_classes = (IsAuthenticated, )

def get_object(self):
prefetch_queryset = PRPRole.objects.select_related('workspace', 'cluster', 'cluster__response_plan',
'cluster__response_plan__workspace')
prefetch_prp_roles = Prefetch('prp_roles', queryset=prefetch_queryset)
queryset = User.objects.select_related('profile', 'partner').prefetch_related(prefetch_prp_roles)
return queryset.get(id=self.request.user.id)
from .serializers import UserWithPRPRolesSerializer


class UserLogoutAPIView(APIView):
Expand Down
11 changes: 1 addition & 10 deletions django_api/etools_prp/apps/core/tests/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from faker import Faker
from unicef_locations.models import CartoDBTable

from etools_prp.apps.account.models import User, UserProfile
from etools_prp.apps.account.models import User
from etools_prp.apps.cluster.models import Cluster, ClusterActivity, ClusterObjective
from etools_prp.apps.core.common import (
CLUSTER_TYPES,
Expand Down Expand Up @@ -158,22 +158,13 @@ class AbstractUserFactory(factory.django.DjangoModelFactory):
position = factory.LazyFunction(faker.job)
username = factory.LazyFunction(faker.user_name)
password = factory.PostGenerationMethodCall('set_password', 'test')
profile = factory.RelatedFactory('etools_prp.apps.core.tests.factories.UserProfileFactory', 'user')

class Meta:
model = User
django_get_or_create = ('email', 'username')
abstract = True


@factory.django.mute_signals(signals.post_save)
class UserProfileFactory(factory.django.DjangoModelFactory):
class Meta:
model = UserProfile

user = factory.SubFactory('etools_prp.apps.core.tests.factories.AbstractUserFactory', profile=None)


@factory.django.mute_signals(signals.post_save)
class PartnerUserFactory(AbstractUserFactory):
"""
Expand Down

0 comments on commit 950588c

Please sign in to comment.