From bfb40f2dab0285a07d4e8e563c9cf10cac247a2e Mon Sep 17 00:00:00 2001 From: "J.-S. Caux" <J.S.Caux@uva.nl> Date: Tue, 26 Jan 2016 20:41:23 +0100 Subject: [PATCH] Simplify register by using utils.py and classmethod decorators --- .../commentaries/commentary_detail.html | 3 +- scipost/global_methods.py | 9 +++ scipost/templates/scipost/FAQ.html | 6 +- scipost/templates/scipost/base.html | 2 + .../templates/scipost/change_password.html | 8 ++ .../scipost/change_password_ack.html | 11 --- scipost/templates/scipost/header.html | 8 +- scipost/urls.py | 2 +- scipost/utils.py | 75 +++++++++++++++++++ scipost/views.py | 36 +++++++-- .../submissions/submission_detail.html | 2 +- 11 files changed, 135 insertions(+), 27 deletions(-) create mode 100644 scipost/global_methods.py delete mode 100644 scipost/templates/scipost/change_password_ack.html create mode 100644 scipost/utils.py diff --git a/commentaries/templates/commentaries/commentary_detail.html b/commentaries/templates/commentaries/commentary_detail.html index dbfd7b14f..48ff4b50d 100644 --- a/commentaries/templates/commentaries/commentary_detail.html +++ b/commentaries/templates/commentaries/commentary_detail.html @@ -3,7 +3,8 @@ {% block pagetitle %}: Commentary detail{% endblock pagetitle %} {% block headsup %} -<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> + +<!-- <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> --> <script> $(document).ready(function(){ diff --git a/scipost/global_methods.py b/scipost/global_methods.py new file mode 100644 index 000000000..bbdf43ddd --- /dev/null +++ b/scipost/global_methods.py @@ -0,0 +1,9 @@ +from django.contrib.auth.models import User + +from .models import * + +class Global(object): + + @classmethod + def get_user(cls, request): + Contributor.objects.get(user=request.user) diff --git a/scipost/templates/scipost/FAQ.html b/scipost/templates/scipost/FAQ.html index 586b00200..aa19b009a 100644 --- a/scipost/templates/scipost/FAQ.html +++ b/scipost/templates/scipost/FAQ.html @@ -30,7 +30,7 @@ <br/> <h3>Who is behind SciPost?</h3> <ul> - <li>SciPost was founded by <a href="staff.fnwi.uva.nl/j.s.caux">J.-S. Caux</a>, Professor of Theoretical Physics at the University of Amsterdam.</li> + <li>SciPost was founded by <a href="http://staff.fnwi.uva.nl/j.s.caux">J.-S. Caux</a>, Professor of Theoretical Physics at the University of Amsterdam.</li> <li>The legal entity behind SciPost is Stichting SciPost, a not-for-profit Foundation established in Amsterdam. Its (non-remunerated) personnel consists in Prof. J.-S. Caux (chairman), Dr J. van Mameren (secretary) and Dr J. van Wezel (treasurer). </li> </ul> <br/> @@ -51,11 +51,11 @@ <br/> <h3>How is SciPost funded?</h3> <p>SciPost operates non-commercially, encurring minimal costs. Contributors provide all the content and perform all editorial tasks as part of their normal institutional academic duties. Operations are kept running by a team of officers, themselves academically employed.</p> - <p>Support for operational costs is initially provided by the <a href="http://www.nwo.nl/en">NWO</a> in the Netherlands. Long-term operations will be financed by donations from national funding agencies, universities, foundations and individuals</p> + <p>Support for operational costs is initially provided by the <a href="http://www.nwo.nl/en">NWO</a> in the Netherlands. Long-term operations will be financed by donations from national funding agencies, universities, foundations and individuals.</p> <br/> <h3>Which fields of science does SciPost cater to?</h3> <p>The initial rollout of SciPost offers a Physics portal.</p> - <p>Portals in Other fields of science will be opened if the SciPost model proves to be successful.</p> + <p>Portals in other fields of science will be opened if the SciPost model proves to be successful.</p> </div> </div> </section> diff --git a/scipost/templates/scipost/base.html b/scipost/templates/scipost/base.html index 367631077..c10153ebb 100644 --- a/scipost/templates/scipost/base.html +++ b/scipost/templates/scipost/base.html @@ -4,6 +4,8 @@ {% load staticfiles %} <link rel="stylesheet" type="text/css" href="{% static 'scipost/SciPost.css' %}" /> + <script src="https://code.jquery.com/jquery-2.2.0.min.js"></script> + <title>SciPost{% block pagetitle %}{% endblock pagetitle %}</title> {% block headsup %} diff --git a/scipost/templates/scipost/change_password.html b/scipost/templates/scipost/change_password.html index 29846710f..9ff0c9081 100644 --- a/scipost/templates/scipost/change_password.html +++ b/scipost/templates/scipost/change_password.html @@ -4,6 +4,13 @@ {% block bodysup %} +{% if ack %} +<section> + <h1>Your SciPost password has been successfully changed</h1> +</section> + +{% else %} + <section> <h1>Change your SciPost password</h1> <form action="{% url 'scipost:change_password' %}" method="post"> @@ -19,5 +26,6 @@ <p>{{ errormessage }}</p> {% endif %} </section> +{% endif %} {% endblock bodysup %} diff --git a/scipost/templates/scipost/change_password_ack.html b/scipost/templates/scipost/change_password_ack.html deleted file mode 100644 index 8b194e605..000000000 --- a/scipost/templates/scipost/change_password_ack.html +++ /dev/null @@ -1,11 +0,0 @@ -{% extends 'scipost/base.html' %} - -{% block pagetitle %}: password changed{% endblock pagetitle %} - -{% block bodysup %} - -<section> - <h1>Your SciPost password has been successfully changed</h1> -</section> - -{% endblock bodysup %} diff --git a/scipost/templates/scipost/header.html b/scipost/templates/scipost/header.html index 2c0a8203e..25575743b 100644 --- a/scipost/templates/scipost/header.html +++ b/scipost/templates/scipost/header.html @@ -1,22 +1,22 @@ {% load staticfiles %} <header> <div class="row"> - <div class="col-3"> + <div class="col-4"> <!-- <a href="{% url 'scipost:index' %}"><h1>SciPost.org</h1></a> --> <h1> <font style="color: rgb(212,237,252)">Sci</font> <font style="color: rgb(247,164,0)">|</font> <font style="color: rgb(104,132,194)">Post</font> <font style="color: rgb(104,132,194)"></font></h1> </div> - <div class="col-3"> + <div class="col-4"> </div> {% if user.is_authenticated %} - <div class="col-6"> + <div class="col-4"> <p> <i>The complete scientific publication portal</i><br /> <i>Managed by professional scientists</i><br /> - <i>For freely, openly, globally and perpetually accessible science</i> + <i>For open, global and perpetual access science</i> </p> </div> {% endif %} diff --git a/scipost/urls.py b/scipost/urls.py index 7cbe5ea09..7f5232190 100644 --- a/scipost/urls.py +++ b/scipost/urls.py @@ -30,7 +30,7 @@ urlpatterns = [ url(r'^logout$', views.logout_view, name='logout'), url(r'^personal_page$', views.personal_page, name='personal_page'), url(r'^change_password$', views.change_password, name='change_password'), - url(r'^change_password_ack$', views.change_password_ack, name='change_password_ack'), + #url(r'^change_password_ack$', views.change_password_ack, name='change_password_ack'), url(r'^update_personal_data$', views.update_personal_data, name='update_personal_data'), url(r'^update_personal_data_ack$', views.update_personal_data_ack, name='update_personal_data_ack'), ] diff --git a/scipost/utils.py b/scipost/utils.py new file mode 100644 index 000000000..26be73d5f --- /dev/null +++ b/scipost/utils.py @@ -0,0 +1,75 @@ +import datetime +import hashlib +import random +import string + +from django.contrib.auth.models import User +from django.core.mail import EmailMessage +from django.utils import timezone + +from .models import * + + +class Utils(object): + + @classmethod + def load(cls, dict): + for var_name in dict: + setattr(cls, var_name, dict[var_name]) + + @classmethod + def password_mismatch(cls): + if cls.form.cleaned_data['password'] != cls.form.cleaned_data['password_verif']: + return True + else: + return False + + @classmethod + def username_already_taken(cls): + if User.objects.filter(username=cls.form.cleaned_data['username']).exists(): + return True + else: + return False + + @classmethod + def create_and_save_contributor(cls): + user = User.objects.create_user ( + first_name = cls.form.cleaned_data['first_name'], + last_name = cls.form.cleaned_data['last_name'], + email = cls.form.cleaned_data['email'], + username = cls.form.cleaned_data['username'], + password = cls.form.cleaned_data['password'] + ) + # Set to inactive until activation via email link + user.is_active = False + user.save() + contributor = Contributor ( + user=user, + title = cls.form.cleaned_data['title'], + orcid_id = cls.form.cleaned_data['orcid_id'], + country_of_employment = cls.form.cleaned_data['country_of_employment'], + address = cls.form.cleaned_data['address'], + affiliation = cls.form.cleaned_data['affiliation'], + personalwebpage = cls.form.cleaned_data['personalwebpage'], + ) + contributor.save() + Utils.load({'contributor': contributor}) + + @classmethod + def send_registration_email(cls): + # Generate email activation key and link + salt = "" + for i in range(5): + salt = salt + random.choice(string.ascii_letters) + salt = salt.encode('utf8') + usernamesalt = cls.contributor.user.username + usernamesalt = usernamesalt.encode('utf8') + cls.contributor.activation_key = hashlib.sha1(salt+usernamesalt).hexdigest() + cls.contributor.key_expires = datetime.datetime.strftime(datetime.datetime.now() + datetime.timedelta(days=2), "%Y-%m-%d %H:%M:%S") + cls.contributor.save() + email_text = 'Dear ' + title_dict[cls.contributor.title] + ' ' + cls.contributor.user.last_name + ', \n\nYour request for registration to the SciPost publication portal has been received. You now need to validate your email by visiting this link within the next 48 hours: \n\n' + 'https://scipost.org/activation/' + cls.contributor.activation_key + '\n\nYour registration will thereafter be vetted. Many thanks for your interest. \n\nThe SciPost Team.' + emailmessage = EmailMessage('SciPost registration request received', email_text, 'registration@scipost.org', [cls.contributor.user.email, 'registration@scipost.org'], reply_to=['registration@scipost.org']) + emailmessage.send(fail_silently=False) + + + diff --git a/scipost/views.py b/scipost/views.py index 2ac041b85..7de287c1d 100644 --- a/scipost/views.py +++ b/scipost/views.py @@ -16,6 +16,10 @@ from django.db.models import Avg from .models import * from .forms import * +from .global_methods import * +from .utils import * + + from commentaries.models import Commentary from comments.models import Comment, AuthorReply from submissions.models import Submission, Report @@ -58,7 +62,7 @@ def peer_witnessed_refereeing(request): title_dict = dict(TITLE_CHOICES) reg_ref_dict = dict(REGISTRATION_REFUSAL_CHOICES) -def register(request): +def register_old(request): if request.user.is_authenticated(): return HttpResponseRedirect('personal_page') # If POST, process the form data @@ -99,7 +103,6 @@ def register(request): salt = "" for i in range(5): salt = salt + random.choice(string.ascii_letters) - #salt = hashlib.sha1(str(random.random()).encode('utf8')).hexdigest()[:5] salt = salt.encode('utf8') usernamesalt = contributor.user.username usernamesalt = usernamesalt.encode('utf8') @@ -117,6 +120,25 @@ def register(request): errormessage = '' return render(request, 'scipost/register.html', {'form': form, 'errormessage': errormessage}) +def register(request): + if request.user.is_authenticated(): + return HttpResponseRedirect('personal_page') + if request.method == 'POST': + form = RegistrationForm(request.POST) + Utils.load({'form': form}) + if form.is_valid(): + if Utils.password_mismatch(): + return render(request, 'scipost/register.html', {'form': form, 'errormessage': 'Your passwords must match'}) + if Utils.username_already_taken(): + return render(request, 'scipost/register.html', {'form': form, 'errormessage': 'This username is already in use'}) + Utils.create_and_save_contributor() + Utils.send_registration_email() + return HttpResponseRedirect('thanks_for_registering') + else: + form = RegistrationForm() + errormessage = '' + return render(request, 'scipost/register.html', {'form': form, 'errormessage': errormessage}) + def thanks_for_registering(request): return render(request, 'scipost/thanks_for_registering.html') @@ -282,15 +304,17 @@ def change_password(request): # otherwise simply change the pwd: request.user.set_password(form.cleaned_data['password_new']) request.user.save() - return render(request, 'scipost/change_password_ack.html') + #return render(request, 'scipost/change_password_ack.html') + context = {'acknowledgment': True, 'form': PasswordChangeForm()} + return render(request, 'scipost/change_password.html', context) else: form = PasswordChangeForm() return render (request, 'scipost/change_password.html', {'form': form}) -def change_password_ack(request): - return render (request, 'scipost/change_password_ack.html') - +#def change_password_ack(request): +# return render (request, 'scipost/change_password_ack.html') +# def update_personal_data(request): if request.user.is_authenticated: diff --git a/submissions/templates/submissions/submission_detail.html b/submissions/templates/submissions/submission_detail.html index 735ed8f49..b2db52aca 100644 --- a/submissions/templates/submissions/submission_detail.html +++ b/submissions/templates/submissions/submission_detail.html @@ -3,7 +3,7 @@ {% block pagetitle %}: submission detail{% endblock pagetitle %} {% block headsup %} -<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> +<!-- <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> --> <script> $(document).ready(function(){ -- GitLab