From 52a91e9d8d0d70babc0d8f011c2d7b5e0f996407 Mon Sep 17 00:00:00 2001 From: Kira Miller Date: Wed, 13 Nov 2024 23:33:40 +0000 Subject: [PATCH] fix: more in progress stuff --- .../v1/views/enterprise_customer_members.py | 62 +++++-------------- enterprise/models.py | 1 - 2 files changed, 16 insertions(+), 47 deletions(-) diff --git a/enterprise/api/v1/views/enterprise_customer_members.py b/enterprise/api/v1/views/enterprise_customer_members.py index d6e7fb83c..c105db06a 100644 --- a/enterprise/api/v1/views/enterprise_customer_members.py +++ b/enterprise/api/v1/views/enterprise_customer_members.py @@ -9,7 +9,7 @@ from django.contrib import auth from django.core.exceptions import ValidationError -from django.db.models import Case, CharField, Q, Value, When +from django.db.models import Case, CharField, F, Q, Value, When from enterprise import models from enterprise.api.v1 import serializers @@ -73,36 +73,9 @@ def filter_queryset_by_user_query(self, queryset): user_query = self.request.query_params.get("user_query", None) if user_query: queryset = queryset.filter( - # Q(user_email__icontains=user_query) #Q(name__icontains=user_query) | + Q(user_email__icontains=user_query) | Q(name__icontains=user_query) ) return queryset - - - # user_id__in=User.objects.filter( - # Q(first_name__icontains=user_query) | Q(last_name__icontains=user_query) | - # Q(email__icontains=user_query) - # ) - # ) - # user_profiles = queryset.filter( - # user_id__in=UserProfile.objects.filter( - # Q(name__icontains=user_query) - # ) - # ) - # return users | user_profiles - return queryset - - # queryset1 = models.EnterpriseCustomerUser.objects.filter( - # user_id__in=User.objects.filter( - # Q(first_name__icontains=user_query) | Q(last_name__icontains=user_query) | - # Q(email__icontains=user_query) - # ) - # ) - # queryset2 = models.EnterpriseCustomerUser.objects.filter( - # user_id__in=UserProfile.objects.filter( - # Q(name__icontains=user_query) - # ) - # ) - # return queryset1 | queryset2 def get_members(self, request, *args, **kwargs): """ @@ -114,7 +87,8 @@ def get_members(self, request, *args, **kwargs): # Because our main User table is not in this repo, and isn't connected through # a foreign key (like enterprise_customer) and only the user_id attribute, we # have to use django's extra() function in order to sort on the name before - # pagination. + # pagination. Not everyone has first and last filled out, so we also have to + # fetch the name from userprofile as well enterprise_customer_queryset = models.EnterpriseCustomerUser.objects.filter( enterprise_customer__uuid=enterprise_uuid ).extra( @@ -127,22 +101,23 @@ def get_members(self, request, *args, **kwargs): where=['auth_userprofile.user_id = enterprise_enterprisecustomeruser.user_id'] ) - # print(enterprise_customer_queryset[0].first_name) - # raise Exception(enterprise_customer_queryset[0].first_name) - - enterprise_customer_queryset = enterprise_customer_queryset.annotate( - # linkable=Case( - # When(is_relinkable=True, then=Value(1)), - # default=Value(0), + # raise Exception(enterprise_customer_queryset[0].__dir__()) + print('hi kira!') + print('first name', enterprise_customer_queryset[0].first_name) + print('last name', enterprise_customer_queryset[0].last_name) + print('full name', enterprise_customer_queryset[0].full_name) + - # ) + # We need to combine users with first/last and full into one 'name' + # column, so then we can order by that + enterprise_customer_queryset = enterprise_customer_queryset.annotate( name=Case( - When(full_name=None, then=Value('first_name' + ' ' + 'last_name')), - default=Value(0), + When(full_name__isnull=True,first_name__isnull=False, then=F('first_name') + ' ' + F('last_name')), + When(first_name__isnull=True,full_name__isnull=False, then=F('full_name')), + default=Value(None), output_field=CharField(), ) ).order_by('name') - print(enterprise_customer_queryset[0]['name']) enterprise_customer_queryset = self.filter_queryset_by_user_query( enterprise_customer_queryset @@ -156,11 +131,6 @@ def get_members(self, request, *args, **kwargs): status=status.HTTP_404_NOT_FOUND, ) - #alred g - #zane b - #alan - - # paginate the queryset users_page = self.paginator.paginate_queryset(users, request, view=self) diff --git a/enterprise/models.py b/enterprise/models.py index aeb5473b2..2d477e8fd 100644 --- a/enterprise/models.py +++ b/enterprise/models.py @@ -114,7 +114,6 @@ LOGGER = getEnterpriseLogger(__name__) User = auth.get_user_model() mark_safe_lazy = lazy(mark_safe, str) -FAKER = FakerFactory.create() class EnterpriseCustomerManager(models.Manager):