diff --git a/biostar/accounts/models.py b/biostar/accounts/models.py index 77b955dc9..9d406d58c 100644 --- a/biostar/accounts/models.py +++ b/biostar/accounts/models.py @@ -313,6 +313,12 @@ def low_rep(self): """ return self.score <= settings.LOW_REP_THRESHOLD and not self.is_moderator + @property + def high_rep(self): + """ + """ + + return not self.low_rep class UserLog(models.Model): DEFAULT, ACTION = 1, 2 diff --git a/biostar/forum/auth.py b/biostar/forum/auth.py index f4f779386..89cac4acb 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() @@ -213,13 +213,13 @@ def create_post(author, title, content, request, root=None, parent=None, ptype=P post = Post.objects.filter(content=content, author=author).order_by('-creation_date').first() # How many seconds since the last post should we disallow duplicates. - since = 60 + time_frame = 60 if post: - # Check to see if this post was made within the last minute. + # Check to see if this post was made within given timeframe delta_secs = (util.now() - post.creation_date).seconds - if delta_secs < since: + if delta_secs < time_frame: messages.warning(request, "Post with this content was created recently.") - return post.root + return post post = Post.objects.create(title=title, content=content, root=root, parent=parent, type=ptype, tag_val=tag_val, author=author) @@ -228,6 +228,30 @@ 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 messages + Message.objects.filter(sender=alias).update(sender=main) + Message.objects.filter(recipient=alias).update(recipient=main) + + # 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: + return + + alias.delete() + + 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..fde043c78 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 __init__(self, user=None, *args, **kwargs): + self.user = user + super().__init__(*args, **kwargs) + + def clean(self): + cleaned_data = super(MergeProfiles, self).clean() + alias = cleaned_data['alias'] + main = cleaned_data['main'] + + to_delete = User.objects.filter(email=alias).first() + merge_to = User.objects.filter(email=main).first() + + if self.user and not (self.user.is_staff or self.user.is_superuser): + raise forms.ValidationError(f'Only staff member can perform this action.') + + if not to_delete: + raise forms.ValidationError(f'{alias} email does not exist.') + + if not merge_to: + raise forms.ValidationError(f'{main} email does not exist.') + + 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/edit_profile.html b/biostar/forum/templates/accounts/edit_profile.html index 2675a31d0..5c1f41b3d 100644 --- a/biostar/forum/templates/accounts/edit_profile.html +++ b/biostar/forum/templates/accounts/edit_profile.html @@ -15,7 +15,6 @@ {% endblock %} {% block js %} - diff --git a/biostar/forum/templates/accounts/merge_profile.html b/biostar/forum/templates/accounts/merge_profile.html new file mode 100644 index 000000000..e6a51ec72 --- /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 %} +
+ + + +{% endblock %} 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 %} {% endif %} diff --git a/biostar/forum/templates/banners/menu-header.html b/biostar/forum/templates/banners/menu-header.html index 2e55e967d..592ec42fb 100644 --- a/biostar/forum/templates/banners/menu-header.html +++ b/biostar/forum/templates/banners/menu-header.html @@ -33,10 +33,11 @@ About - + FAQ +