From 3b73fe78bd819011e9416941df4cd7969e7c2c62 Mon Sep 17 00:00:00 2001 From: j Date: Thu, 16 Nov 2017 18:05:50 +0100 Subject: [PATCH] update user references if username gets changed --- pandora/user/utils.py | 42 ++++++++++++++++++++++++++++++++++++++++++ pandora/user/views.py | 9 ++++++--- 2 files changed, 48 insertions(+), 3 deletions(-) diff --git a/pandora/user/utils.py b/pandora/user/utils.py index d32d0536c..e7b7c38a0 100644 --- a/pandora/user/utils.py +++ b/pandora/user/utils.py @@ -27,3 +27,45 @@ def get_location(ip): except: country = city = None return city, country + + +def rename_user(user, new): + import itemlist.models + import item.models + import user.models + + old = user.username + old_prefix = old + ':' + new_prefix = new + ':' + + # update list queries matching user + for l in itemlist.models.List.objects.filter(query__contains=old_prefix): + changed = False + for q in l.query['conditions']: + if old_prefix in q['value']: + q['value'] = q['value'].replace(old_prefix, new_prefix) + changed = True + if changed: + l.save() + + # update profile settings matching user + for p in user.models.UserProfile.objects.all(): + changed = False + lists = list(p.ui.get('lists', {})) + for name in lists: + if name.startswith(old_prefix): + nname = name.replace(old_prefix, new_prefix) + p.ui['lists'][nname] = p.ui['lists'].pop(name) + changed = True + collections = list(p.ui.get('collections', {})) + for name in collections: + if name.startswith(old_prefix): + nname = name.replace(old_prefix, new_prefix) + p.ui['collections'][nname] = p.ui['collections'].pop(name) + changed = True + if changed: + p.save() + + # update user item find + item.models.ItemFind.objects.filter(key='user', value=old).update(value=new) + user.username = new diff --git a/pandora/user/views.py b/pandora/user/views.py index 8c0a3cf2c..83432bf1c 100644 --- a/pandora/user/views.py +++ b/pandora/user/views.py @@ -29,6 +29,7 @@ from changelog.models import add_changelog from . import models from .decorators import capability_required_json +from .utils import rename_user def get_user_or_404(data): @@ -357,7 +358,7 @@ def editUser(request, data): if 'email' in data: if 'email' in data: data['email'] = ox.escape_html(data['email']) - if User.objects.filter(email__iexact=data['email']).exclude(id=user.id).count()>0: + if User.objects.filter(email__iexact=data['email']).exclude(id=user.id).count() > 0: response = json_response(status=403, text='email already in use') return render_to_json_response(response) user.email = data['email'] @@ -380,10 +381,12 @@ def editUser(request, data): user.groups.add(group) if 'username' in data: if User.objects.filter( - username__iexact=data['username']).exclude(id=user.id).count()>0: + username__iexact=data['username']).exclude(id=user.id).count() > 0: response = json_response(status=403, text='username already in use') return render_to_json_response(response) - user.username = data['username'] + else: + rename_user(user, data['username']) + profile = models.UserProfile.objects.get(pk=user.pk) user.save() profile.save() add_changelog(request, data, user.username)