From 655a05fb66a15b76fc34b00436e56c689b4a2c7b Mon Sep 17 00:00:00 2001 From: Natay Aberra Date: Tue, 25 May 2021 14:57:51 -0400 Subject: [PATCH 1/6] profile merging interface added --- biostar/forum/auth.py | 31 ++++++++- biostar/forum/forms.py | 45 +++++++++++++ .../templates/accounts/merge_profile.html | 64 +++++++++++++++++++ .../forum/templates/banners/menu-header.html | 13 +++- biostar/forum/tests/test_moderate.py | 15 ++++- biostar/forum/tests/test_navigation.py | 4 +- biostar/forum/urls.py | 1 + biostar/forum/views.py | 22 ++++++- biostar/server/test_settings.py | 2 +- biostar/utils/decorators.py | 14 ++++ 10 files changed, 202 insertions(+), 9 deletions(-) create mode 100644 biostar/forum/templates/accounts/merge_profile.html diff --git a/biostar/forum/auth.py b/biostar/forum/auth.py index f4f779386..a16e13d72 100644 --- a/biostar/forum/auth.py +++ b/biostar/forum/auth.py @@ -21,7 +21,7 @@ from biostar.utils.helpers import get_ip from . import util, awards from .const import * -from .models import Post, Vote, Subscription, Badge, delete_post_cache, Log +from .models import Post, Vote, Subscription, Badge, delete_post_cache, Log, Award User = get_user_model() @@ -228,6 +228,35 @@ def create_post(author, title, content, request, root=None, parent=None, ptype=P return post +def merge_profiles(main, alias): + """ + Merge alias profile into main + """ + + # Transfer posts + Post.objects.filter(author=alias).update(author=main) + + Post.objects.filter(lastedit_user=alias).update(lastedit_user=main) + + # Transfer votes + Vote.objects.filter(author=alias).update(author=main) + + # Transfer subscriptions + Subscription.objects.filter(user=alias).update(user=main) + + # Transfer awards + Award.objects.filter(user=alias).update(user=main) + + # Transfer messages + Message.objects.filter(sender=alias).update(sender=main) + Message.objects.filter(recipient=alias).update(recipient=main) + + # Remove alias profile. + #Profile.objects.filter(user=alias).update(state=Profile.SUSPENDED) + + return + + def create_subscription(post, user, sub_type=None, update=False): """ Creates subscription to a post. Returns a list of subscriptions. diff --git a/biostar/forum/forms.py b/biostar/forum/forms.py index 1c37c6918..760b5f501 100644 --- a/biostar/forum/forms.py +++ b/biostar/forum/forms.py @@ -206,3 +206,48 @@ def clean(self): if self.user.is_anonymous: raise forms.ValidationError("You need to be logged in.") return cleaned_data + + +class MergeProfiles(forms.Form): + + main = forms.CharField(label='Main user email', max_length=100, required=True) + alias = forms.CharField(label='Alias email to merge to main', max_length=100, required=True) + + def clean_main(self): + cleaned_data = super(MergeProfiles, self).clean() + main = cleaned_data['main'] + if not User.objects.filter(email=main).first(): + raise forms.ValidationError(f'{main} email does not exist.') + + return main + + def clean_alias(self): + cleaned_data = super(MergeProfiles, self).clean() + alias = cleaned_data['alias'] + if not User.objects.filter(email=alias).first(): + raise forms.ValidationError(f'{alias} email does not exist.') + + return alias + + def clean(self): + cleaned_data = super(MergeProfiles, self).clean() + alias = cleaned_data['alias'] + main = cleaned_data['main'] + + if main == alias: + raise forms.ValidationError('Main and alias profiles are the same.') + + return cleaned_data + + def save(self): + + alias = self.cleaned_data['alias'] + main = self.cleaned_data['main'] + + main = User.objects.filter(email=main).first() + alias = User.objects.filter(email=alias).first() + + # Merge the two accounts. + auth.merge_profiles(main=main, alias=alias) + + return main \ No newline at end of file diff --git a/biostar/forum/templates/accounts/merge_profile.html b/biostar/forum/templates/accounts/merge_profile.html new file mode 100644 index 000000000..75080a646 --- /dev/null +++ b/biostar/forum/templates/accounts/merge_profile.html @@ -0,0 +1,64 @@ +{% extends "forum_base.html" %} +{% load forum_tags %} +{% load socialaccount %} +{% block headtitle %}Login{% endblock %} + +{% block content %} +
+ +
+ +
+ Merge Profiles +
+ + {% csrf_token %} + + {{ form.errors }} + +
+ +
+ + + {{ form.alias }} +
+ Email to the alias profile +
+ +
+
+ +
+ +
+ + + {{ form.main }} +
+ Email to main profile +
+ +
+ + +
+ +
+ + + + Back + +
+ +
+ + +
+ + + +{% endblock %} diff --git a/biostar/forum/templates/banners/menu-header.html b/biostar/forum/templates/banners/menu-header.html index 2e55e967d..7b3af5749 100644 --- a/biostar/forum/templates/banners/menu-header.html +++ b/biostar/forum/templates/banners/menu-header.html @@ -33,10 +33,17 @@ About - + FAQ + {% if request.user.is_staff or request.user.is_superuser %} + + Merge + + + {% endif %} + diff --git a/biostar/forum/templates/accounts/profile_tab.html b/biostar/forum/templates/accounts/profile_tab.html index 6e2f1a239..287b40505 100644 --- a/biostar/forum/templates/accounts/profile_tab.html +++ b/biostar/forum/templates/accounts/profile_tab.html @@ -29,7 +29,7 @@ {% if request.user.is_superuser or request.user.is_staff %}
- Disable Messages + Silence
{% endif %} diff --git a/biostar/forum/templates/banners/menu-header.html b/biostar/forum/templates/banners/menu-header.html index 7b3af5749..592ec42fb 100644 --- a/biostar/forum/templates/banners/menu-header.html +++ b/biostar/forum/templates/banners/menu-header.html @@ -37,12 +37,6 @@ FAQ
- {% if request.user.is_staff or request.user.is_superuser %} - - Merge - - - {% endif %} diff --git a/biostar/forum/views.py b/biostar/forum/views.py index 1ca1572b8..b9a913746 100644 --- a/biostar/forum/views.py +++ b/biostar/forum/views.py @@ -633,10 +633,11 @@ def merge_profile(request): Merge two profiles into one. """ - form = forms.MergeProfiles() + user = request.user + form = forms.MergeProfiles(user=user) if request.method == 'POST': - form = forms.MergeProfiles(data=request.POST) + form = forms.MergeProfiles(user=user, data=request.POST) if form.is_valid(): merged = form.save() From 67112867871b76d4acea78e2bd897ed7d5bf532b Mon Sep 17 00:00:00 2001 From: Natay Aberra Date: Fri, 28 May 2021 08:49:38 -0400 Subject: [PATCH 5/6] update --- biostar/forum/auth.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/biostar/forum/auth.py b/biostar/forum/auth.py index 66b81d5e4..10283ca0a 100644 --- a/biostar/forum/auth.py +++ b/biostar/forum/auth.py @@ -241,8 +241,10 @@ def merge_profiles(main, alias): Message.objects.filter(sender=alias).update(sender=main) Message.objects.filter(recipient=alias).update(recipient=main) - # Do not delete alias is older than main. - if alias.profile.is_moderator or alias.profile.high_rep or (alias.profile.date_joined < main.profile.date_joined): + # Do not delete 'alias' if it is older than main profile. + older = (alias.profile.date_joined < main.profile.date_joined) + + if alias.profile.is_moderator or alias.profile.high_rep or older: return alias.delete() From 2ca7f04d7df3c2b83c8ba357680baaf30b1d74a3 Mon Sep 17 00:00:00 2001 From: Natay Aberra Date: Fri, 28 May 2021 08:55:10 -0400 Subject: [PATCH 6/6] update --- biostar/forum/auth.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/biostar/forum/auth.py b/biostar/forum/auth.py index 10283ca0a..89cac4acb 100644 --- a/biostar/forum/auth.py +++ b/biostar/forum/auth.py @@ -241,7 +241,7 @@ def merge_profiles(main, alias): Message.objects.filter(sender=alias).update(sender=main) Message.objects.filter(recipient=alias).update(recipient=main) - # Do not delete 'alias' if it is older than main profile. + # Do not delete older accounts. older = (alias.profile.date_joined < main.profile.date_joined) if alias.profile.is_moderator or alias.profile.high_rep or older: