Skip to content

Commit

Permalink
Create back-office account management templates #4380
Browse files Browse the repository at this point in the history
  • Loading branch information
joemull committed Nov 15, 2024
1 parent f4dfc67 commit 042b33e
Show file tree
Hide file tree
Showing 11 changed files with 653 additions and 95 deletions.
99 changes: 99 additions & 0 deletions src/core/tests/test_views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
__copyright__ = "Copyright 2024 Birkbeck, University of London"
__author__ = "Open Library of Humanities"
__license__ = "AGPL v3"
__maintainer__ = "Open Library of Humanities"

from mock import patch
from uuid import uuid4

from django.test import Client, TestCase, override_settings

from utils.testing import helpers

from core import models as core_models


class CoreViewTestsWithData(TestCase):

@classmethod
def setUpTestData(cls):
cls.press = helpers.create_press()
cls.journal_one, cls.journal_two = helpers.create_journals()
helpers.create_roles(['author'])
cls.user_email = 'sukv8golcvwervs0y7e5@example.org'
cls.user_password = 'xUMXW1oXn2l8L26Kixi2'
cls.user = core_models.Account.objects.create_user(
cls.user_email,
password=cls.user_password,
)
cls.user.confirmation_code = uuid4()
cls.user.is_active = True
cls.user_orcid = 'https://orcid.org/0000-0001-2345-6789'
cls.user.orcid = cls.user_orcid
cls.orcid_token_uuid = uuid4()
cls.orcid_token = core_models.OrcidToken.objects.create(
token=cls.orcid_token_uuid,
orcid=cls.user_orcid,
)
cls.reset_token_uuid = uuid4()
cls.reset_token = core_models.PasswordResetToken.objects.create(
account=cls.user,
token=cls.reset_token_uuid,
)
cls.user.save()

def setUp(self):
self.client = Client()


class AccountManagementTemplateTests(CoreViewTestsWithData):

def test_user_login(self):
url = '/login/'
data = {}
template = 'admin/core/accounts/login.html'
response = self.client.get(url, data)
self.assertTemplateUsed(response, template)

def test_get_reset_token(self):
url = '/reset/step/1/'
data = {}
template = 'admin/core/accounts/get_reset_token.html'
response = self.client.get(url, data)
self.assertTemplateUsed(response, template)

def test_reset_password(self):
url = f'/reset/step/2/{self.reset_token_uuid}/'
data = {}
template = 'admin/core/accounts/reset_password.html'
response = self.client.get(url, data)
self.assertTemplateUsed(response, template)

def test_register(self):
url = '/register/step/1/'
data = {}
template = 'admin/core/accounts/register.html'
response = self.client.get(url, data)
self.assertTemplateUsed(response, template)

def test_orcid_registration(self):
url = f'/register/step/orcid/{self.orcid_token_uuid}/'
data = {}
template = 'admin/core/accounts/orcid_registration.html'
response = self.client.get(url, data)
self.assertTemplateUsed(response, template)

def test_activate_account(self):
url = f'/register/step/2/{self.user.confirmation_code}/'
data = {}
template = 'admin/core/accounts/activate_account.html'
response = self.client.get(url, data)
self.assertTemplateUsed(response, template)

def test_edit_profile(self):
self.client.login(username=self.user_email, password=self.user_password)
url = '/profile/'
data = {}
template = 'admin/core/accounts/edit_profile.html'
response = self.client.get(url, data)
self.assertTemplateUsed(response, template)
14 changes: 7 additions & 7 deletions src/core/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ def user_login(request):
context = {
'form': form,
}
template = 'core/login.html'
template = 'admin/core/accounts/login.html'

return render(request, template, context)

Expand Down Expand Up @@ -255,7 +255,7 @@ def get_reset_token(request):
except models.Account.DoesNotExist:
return redirect(reverse('core_login'))

template = 'core/accounts/get_reset_token.html'
template = 'admin/core/accounts/get_reset_token.html'
context = {
'new_reset_token': new_reset_token,
'form': form,
Expand Down Expand Up @@ -297,7 +297,7 @@ def reset_password(request, token):
messages.add_message(request, messages.SUCCESS, 'Your password has been reset.')
return redirect(reverse('core_login'))

template = 'core/accounts/reset_password.html'
template = 'admin/core/accounts/reset_password.html'
context = {
'reset_token': reset_token,
'form': form,
Expand Down Expand Up @@ -379,7 +379,7 @@ def register(request):
)
return redirect(reverse('core_login'))

template = 'core/accounts/register.html'
template = 'admin/core/accounts/register.html'
context["form"] = form

return render(request, template, context)
Expand All @@ -388,7 +388,7 @@ def register(request):
def orcid_registration(request, token):
token = get_object_or_404(models.OrcidToken, token=token, expiry__gt=timezone.now())

template = 'core/accounts/orcid_registration.html'
template = 'admin/core/accounts/orcid_registration.html'
context = {
'token': token,
}
Expand Down Expand Up @@ -422,7 +422,7 @@ def activate_account(request, token):

return redirect(reverse('core_login'))

template = 'core/accounts/activate_account.html'
template = 'admin/core/accounts/activate_account.html'
context = {
'account': account,
}
Expand Down Expand Up @@ -540,7 +540,7 @@ def edit_profile(request):
elif 'export' in request.POST:
return logic.export_gdpr_user_profile(user)

template = 'core/accounts/edit_profile.html'
template = 'admin/core/accounts/edit_profile.html'
context = {
'form': form,
'staff_group_membership_form': staff_group_membership_form,
Expand Down
40 changes: 40 additions & 0 deletions src/templates/admin/core/accounts/activate_account.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{% extends "admin/core/small_form.html" %}
{% load i18n foundation %}

{% block contextual_title %}
{% trans "Activate Account" %}
{% endblock contextual_title %}

{% block breadcrumbs %}
{{ block.super }}
<li>{% trans "Activate Account" %}</li>
{% endblock breadcrumbs %}

{% block form_content %}
{% if account %}
<h2 class="text-center">
{% trans "Activate Account" %}
</h2>
{% include "admin/elements/forms/accessible_messages.html" with form=form %}
<p>{% blocktrans %}
You can complete the activation process by clicking the button
below.
{% endblocktrans %}</p>
<button class="button secondary expanded" name="activate">
{% trans "Activate Account" %}
</button>
{% else %}
<h2 class="text-center">
{% trans "No account to activate" %}
</h2>
<p>{% blocktrans %}
Sorry, we could not find an account to activate, or your account is active
already. You can check if it is active by attempting to log in.
{% endblocktrans %}</p>
<div>
<a class="button secondary expanded" href="{% url 'core_login' %}">
{% trans "Log in" %}
</a>
</div>
{% endif %}
{% endblock form_content %}
140 changes: 140 additions & 0 deletions src/templates/admin/core/accounts/edit_profile.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
{% extends "admin/core/large_form.html" %}
{% load i18n foundation static %}

{% block css %}
<link type='text/css' href="{% static " common/css/jq-ui.css" %}" rel="stylesheet">
{% endblock %}

{% block contextual_title %}
{% trans "Edit Profile" %}
{% endblock contextual_title %}

{% block body %}
<div class="grid place-content-center gap-0">
<h2 class="text-center">
{% trans "Edit Profile" %}
</h2>
{% include "admin/elements/forms/accessible_messages.html" with form=form %}
<div class="max-w-56">
<section class="card padding-block-2 padding-inline-2">
<h3>{% trans "Change Your Email Address" %}</h3>
{% blocktrans %}
<p>If you want to change your email address you may do so below,
however, you will be logged out and your
account will be marked as inactive until you follow the
instructions in the verification email. <strong>Note: </strong>
Changing your email address will also change your username as these
are one and the same.</p>
{% endblocktrans %}
<p><strong>{% trans 'Current Email Address' %}:</strong>
{{ request.user.email }}</p>
<form method="POST">
{% csrf_token %}
<div class="flex wrap column-gap-2">
<div>
<label for="email_address">
<strong>{% trans 'New Email Address' %}</strong>
<span aria-hidden="true">*</span>
</label>
<input type="email" name="email_address" required="true">
</div>
</div>
<button type="submit" name="email" class="button secondary">
{% trans "Update Email Address" %}
</button>
</form>
{% include "admin/elements/forms/denotes_required.html" %}
</section>
{% if request.journal and send_reader_notifications %}
<section class="card padding-block-2 padding-inline-2">
<h3>{% trans "Register for Article Notifications" %}</h3>
<form method="POST">
<p>{% blocktrans %}
Use the button below to register to receive notifications of new articles
published in this journal.
{% endblocktrans %}</p>
{% csrf_token %}
{% if reader %}
<button name="unsubscribe" class="button secondary">
{% trans "Unsubscribe from Article Notifications" %}
</button>
{% else %}
<button name="subscribe" class="button secondary">
{% trans "Subscribe to Article Notifications" %}
</button>
{% endif %}
</form>
</section>
{% endif %}
<section class="card padding-block-2 padding-inline-2">
<h3>{% trans "Update Password" %}</h3>
<p>{% blocktrans %}
You can update your password by entering your existing
password plus your new password.
{% endblocktrans %}</p>
<form method="POST">
{% csrf_token %}
<div class="flex wrap column-gap-2">
<div>
<label for="current_password">
<strong>{% trans "Current Password" %}</strong>
<span aria-hidden="true">*</span>
</label>
<input type="password" name="current_password" required="true">
</div>
<div>
<label for="new_password_one">
<strong>{% trans "New Password" %}</strong>
<span aria-hidden="true">*</span>
</label>
<input type="password" name="new_password_one" required="true">
</div>
<div>
<label for="new_password_two">
<strong>{% trans "Enter Password Again" %}</strong>
<span aria-hidden="true">*</span>
</label>
<input type="password" name="new_password_two" required="true">
</div>
</div>
<button
type="submit"
name="change_password"
class="button secondary">
{% trans "Update Password" %}
</button>
</form>
{% include "admin/elements/forms/denotes_required.html" %}
</section>
<section class="card padding-block-2 padding-inline-2">
<h3>{% trans 'Profile Details' %}</h3>
<form method="POST" enctype="multipart/form-data">
{% csrf_token %}
{% include "admin/elements/accounts/user_form.html" %}
<div>
<button
type="submit"
name="edit_profile"
class="button secondary">
<span class="fa fa-save"></span>
{% trans "Save" %}
</button>
</div>
{% include "admin/elements/forms/denotes_required.html" %}
</form>
</section>
</div>
</div>
{% endblock body %}

{% block js %}
<link rel="stylesheet" href="https://code.jquery.com/ui/1.11.0/themes/smoothness/jquery-ui.css">
<script type="text/javascript" src="{% static "common/js/jq-ui.min.js" %}"></script>
<script src="{% static "common/js/tagit.js" %}"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#id_interests").tagit(
{allowSpaces: true});
});
</script>
{% endblock %}
30 changes: 30 additions & 0 deletions src/templates/admin/core/accounts/get_reset_token.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{% extends "admin/core/small_form.html" %}
{% load orcid fqdn i18n foundation %}

{% block contextual_title %}
{% trans "Reset password" %}
{% endblock contextual_title %}

{% block breadcrumbs %}
{{ block.super }}
<li>{% trans "Reset password" %}</li>
{% endblock breadcrumbs %}

{% block form_content %}
<h2 class="text-center">
{% trans "Reset password" %}
</h2>
{% include "admin/elements/forms/accessible_messages.html" with form=form %}
<p>
{% blocktrans %}
Enter your email address and then follow the link sent to you by email.
{% endblocktrans %}
</p>
{% include "admin/elements/forms/field.html" with field=form.email_address %}
<div>
<button type="submit" class="button secondary expanded">
{% trans "Request link" %}
</button>
</div>
{% include "admin/elements/forms/denotes_required.html" %}
{% endblock form_content %}
Loading

0 comments on commit 042b33e

Please sign in to comment.