From 59c2045ac6d7276db0346c3447f6228aa30f627e Mon Sep 17 00:00:00 2001 From: j Date: Thu, 7 Nov 2024 15:57:16 +0000 Subject: [PATCH] move first signup code into function for reuse --- pandora/user/utils.py | 32 ++++++++++++++++++++++++++++++++ pandora/user/views.py | 23 ++--------------------- 2 files changed, 34 insertions(+), 21 deletions(-) diff --git a/pandora/user/utils.py b/pandora/user/utils.py index db557dfe..9abecaaa 100644 --- a/pandora/user/utils.py +++ b/pandora/user/utils.py @@ -3,6 +3,38 @@ from django.contrib.gis.geoip2 import GeoIP2, GeoIP2Exception import ox +def prepare_user(user): + from django.contrib.auth import get_user_model + from django.conf import settings + from itemlist.models import List, Position + from django.db.models import Max + + User = get_user_model() + + first_user_qs = User.objects.all() + if user.id: + first_user_qs = first_user_qs.exclude(id=user.id) + if first_user_qs.count() == 0: + user.is_superuser = True + user.is_staff = True + user.save() + + for l in settings.CONFIG['personalLists']: + list = List(name=l['title'], user=user) + for key in ('query', 'public', 'featured'): + if key in l: + setattr(list, key, l[key]) + if key == 'query': + for c in list.query['conditions']: + if c['key'] == 'user': + c['value'] = c['value'].format(username=user.username) + list.save() + pos = Position(list=list, section='personal', user=user) + qs = Position.objects.filter(user=user, section='personal') + pos.position = (qs.aggregate(Max('position'))['position__max'] or 0) + 1 + pos.save() + + def get_ip(request): if 'HTTP_X_FORWARDED_FOR' in request.META: ip = request.META['HTTP_X_FORWARDED_FOR'].split(',')[0] diff --git a/pandora/user/views.py b/pandora/user/views.py index a2a678f1..588fcbf0 100644 --- a/pandora/user/views.py +++ b/pandora/user/views.py @@ -28,7 +28,7 @@ from user.models import Group from . import models from .decorators import capability_required_json -from .utils import rename_user +from .utils import rename_user, prepare_user User = get_user_model() @@ -177,28 +177,9 @@ def signup(request, data): } }) else: - first_user = User.objects.count() == 0 user = User(username=data['username'], email=data['email']) user.set_password(data['password']) - #make first user admin - user.is_superuser = first_user - user.is_staff = first_user - user.save() - #create default user lists: - for l in settings.CONFIG['personalLists']: - list = List(name=l['title'], user=user) - for key in ('query', 'public', 'featured'): - if key in l: - setattr(list, key, l[key]) - if key == 'query': - for c in list.query['conditions']: - if c['key'] == 'user': - c['value'] = c['value'].format(username=user.username) - list.save() - pos = Position(list=list, section='personal', user=user) - qs = Position.objects.filter(user=user, section='personal') - pos.position = (qs.aggregate(Max('position'))['position__max'] or 0) + 1 - pos.save() + prepare_user(user) if request.session.session_key: models.SessionData.objects.filter(session_key=request.session.session_key).update(user=user) ui = json.loads(request.session.get('ui', 'null'))