fix: scope users API to end-users and tag new registrations as customers
- UserListView and UserMetricsView now filter is_superuser=False so only end-user accounts appear in the admin Users page (not admin/staff) - _serialize_user now returns avatarUrl from profile_picture field so the grid view renders profile images instead of broken img tags - RegisterForm and WebRegisterForm now set is_customer=True and role='customer' on save so future registrants are correctly classified
This commit is contained in:
@@ -491,12 +491,18 @@ _USER_ROLE_MAP = {
|
|||||||
def _serialize_user(u):
|
def _serialize_user(u):
|
||||||
full_name = f'{u.first_name} {u.last_name}'.strip() or u.username
|
full_name = f'{u.first_name} {u.last_name}'.strip() or u.username
|
||||||
role_key = u.role if u.role else ('customer' if getattr(u, 'is_customer', False) else 'staff')
|
role_key = u.role if u.role else ('customer' if getattr(u, 'is_customer', False) else 'staff')
|
||||||
|
try:
|
||||||
|
pic = u.profile_picture
|
||||||
|
avatar = pic.url if pic and pic.name and pic.name != 'default.png' else ''
|
||||||
|
except Exception:
|
||||||
|
avatar = ''
|
||||||
return {
|
return {
|
||||||
'id': str(u.id),
|
'id': str(u.id),
|
||||||
'name': full_name,
|
'name': full_name,
|
||||||
'email': u.email,
|
'email': u.email,
|
||||||
'phone': getattr(u, 'phone_number', '') or '',
|
'phone': getattr(u, 'phone_number', '') or '',
|
||||||
'countryCode': '+91',
|
'countryCode': '+91',
|
||||||
|
'avatarUrl': avatar,
|
||||||
'role': _USER_ROLE_MAP.get(role_key, 'User'),
|
'role': _USER_ROLE_MAP.get(role_key, 'User'),
|
||||||
'status': _user_status(u),
|
'status': _user_status(u),
|
||||||
'tier': 'Bronze',
|
'tier': 'Bronze',
|
||||||
@@ -527,16 +533,19 @@ class UserMetricsView(APIView):
|
|||||||
|
|
||||||
def get(self, request):
|
def get(self, request):
|
||||||
from django.contrib.auth import get_user_model
|
from django.contrib.auth import get_user_model
|
||||||
|
from django.db.models import Q
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
import datetime
|
import datetime
|
||||||
User = get_user_model()
|
User = get_user_model()
|
||||||
today = timezone.now().date()
|
today = timezone.now().date()
|
||||||
week_ago = today - datetime.timedelta(days=7)
|
week_ago = today - datetime.timedelta(days=7)
|
||||||
|
# Customers = all non-superuser accounts (end users registered via mobile/web)
|
||||||
|
customer_qs = User.objects.filter(is_superuser=False)
|
||||||
return Response({
|
return Response({
|
||||||
'total': User.objects.count(),
|
'total': customer_qs.count(),
|
||||||
'active': User.objects.filter(is_active=True).count(),
|
'active': customer_qs.filter(is_active=True).count(),
|
||||||
'suspended': User.objects.filter(is_active=False).count(),
|
'suspended': customer_qs.filter(is_active=False).count(),
|
||||||
'newThisWeek': User.objects.filter(date_joined__date__gte=week_ago).count(),
|
'newThisWeek': customer_qs.filter(date_joined__date__gte=week_ago).count(),
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
@@ -547,7 +556,8 @@ class UserListView(APIView):
|
|||||||
from django.contrib.auth import get_user_model
|
from django.contrib.auth import get_user_model
|
||||||
from django.db.models import Q
|
from django.db.models import Q
|
||||||
User = get_user_model()
|
User = get_user_model()
|
||||||
qs = User.objects.all()
|
# Customers = all non-superuser accounts (end users registered via mobile/web)
|
||||||
|
qs = User.objects.filter(is_superuser=False)
|
||||||
if s := request.GET.get('status'):
|
if s := request.GET.get('status'):
|
||||||
if s == 'Active':
|
if s == 'Active':
|
||||||
qs = qs.filter(is_active=True)
|
qs = qs.filter(is_active=True)
|
||||||
|
|||||||
@@ -31,6 +31,9 @@ class RegisterForm(forms.ModelForm):
|
|||||||
# Set username equal to email to avoid separate username errors
|
# Set username equal to email to avoid separate username errors
|
||||||
user.username = self.cleaned_data['email']
|
user.username = self.cleaned_data['email']
|
||||||
user.set_password(self.cleaned_data['password'])
|
user.set_password(self.cleaned_data['password'])
|
||||||
|
# Mark as a customer / end-user
|
||||||
|
user.is_customer = True
|
||||||
|
user.role = 'customer'
|
||||||
if commit:
|
if commit:
|
||||||
user.save()
|
user.save()
|
||||||
return user
|
return user
|
||||||
@@ -70,9 +73,9 @@ class WebRegisterForm(forms.ModelForm):
|
|||||||
# Set username equal to email to avoid separate username errors
|
# Set username equal to email to avoid separate username errors
|
||||||
user.username = self.cleaned_data['email']
|
user.username = self.cleaned_data['email']
|
||||||
user.set_password(self.cleaned_data['password'])
|
user.set_password(self.cleaned_data['password'])
|
||||||
print('*' * 100)
|
# Mark as a customer / end-user
|
||||||
print(user.username)
|
user.is_customer = True
|
||||||
print('*' * 100)
|
user.role = 'customer'
|
||||||
if commit:
|
if commit:
|
||||||
user.save()
|
user.save()
|
||||||
return user
|
return user
|
||||||
|
|||||||
Reference in New Issue
Block a user