Self hosted FLOSS fitness/workout, nutrition and weight tracker
Add django app to verify user's emails
Roland Geider62a5d4c1294d
diff --git a/wger/core/forms.py b/wger/core/forms.pyindex 1c2538605..602ba4c9a 100644--- a/wger/core/forms.py+++ b/wger/core/forms.py@@ -102,15 +102,12 @@ class UserPreferencesForm(forms.ModelForm):Row(Column('first_name', css_class='form-group col-6 mb-0'),Column('last_name', css_class='form-group col-6 mb-0'),css_class='form-row'),- HTML("<hr>"),- ),-+ HTML("<hr>")),Fieldset(_("Workout reminders"),'workout_reminder_active','workout_reminder','workout_duration',- HTML("<hr>")- ),+ HTML("<hr>")),Fieldset(_("Other settings"),"ro_access","notification_language",@@ -118,8 +115,7 @@ class UserPreferencesForm(forms.ModelForm):"show_comments","show_english_ingredients","num_days_weight_reminder",- "birthdate",- ),+ "birthdate"),ButtonHolder(Submit('submit', _("Save"), css_class='btn-success btn-block')))diff --git a/wger/core/models.py b/wger/core/models.pyindex b10a99908..c615f686f 100644--- a/wger/core/models.py+++ b/wger/core/models.py@@ -19,14 +19,11 @@ import datetimeimport decimal# Django-from django.contrib.auth.models import (- Permission,- User,-)+from django.contrib.auth.models import Userfrom django.core.exceptions import ValidationErrorfrom django.core.validators import (MaxValueValidator,- MinValueValidator,+ MinValueValidator)from django.db import modelsfrom django.db.models import IntegerField@@ -139,6 +136,9 @@ class UserProfile(models.Model):The gym this user belongs to, if any"""+ email_verified = models.BooleanField(default=False)+ """Flag indicating whether the user's email has been verified"""+is_temporary = models.BooleanField(default=False,editable=False)"""@@ -205,16 +205,19 @@ by the US Department of Agriculture. It is extremely complete, with aroundon_delete=models.CASCADE)@property- def is_trustworthy(self):+ def is_trustworthy(self) -> bool:"""- Returns true for trustworthy if they've had an account for more than three weeks.- More trustworthiness criteria could be added in future.+ Flag indicating whether the user "is trustworthy" and can submit or edit exercises++ At the moment the criteria are:+ - the account has existed for 3 weeks+ - the email address has been verified"""days_since_joined = datetime.date.today() - self.user.date_joined.date()- days_in_three_weeks = 21+ minimum_account_age = 21- return days_since_joined.days > days_in_three_weeks+ return days_since_joined.days > minimum_account_age and self.email_verified@propertydef has_exercise_permission(self):diff --git a/wger/core/templates/email_verification/confirm_template.html b/wger/core/templates/email_verification/confirm_template.htmlnew file mode 100644index 000000000..bb2125090--- /dev/null+++ b/wger/core/templates/email_verification/confirm_template.html@@ -0,0 +1,25 @@+{% extends "base.html" %}+{% load i18n %}++{% block title %}{% translate "Email verified" %}{% endblock %}++{# #}+{# Header #}+{# #}+{% block header %}+{% endblock %}+++{# #}+{# Content #}+{# #}+{% block content %}+{% if success %}+ {% blocktranslate trimmed %}+ {{ user.username }}, your email was verified+ {% endblocktranslate %}++{% else %}+ {% translate "Error, invalid verification token" %}+{% endif %}+{% endblock %}diff --git a/wger/core/templates/email_verification/email_body.html b/wger/core/templates/email_verification/email_body.htmlnew file mode 100644index 000000000..13836cf5b--- /dev/null+++ b/wger/core/templates/email_verification/email_body.html@@ -0,0 +1,12 @@+<!DOCTYPE html>+<html lang="en">+<head>+ <meta charset="UTF-8">+ <title>Email Confirmation</title>+</head>+<body>+<h1>You are almost there, {{ user.username }}!</h1><br>+<h2>Please click <a href="{{ link }}">here</a> to confirm your account</h2>+<h2>The token expires on {{ expiry|time:"TIME_FORMAT" }}</h2>+</body>+</html>diff --git a/wger/core/templates/email_verification/email_body.txt b/wger/core/templates/email_verification/email_body.txtnew file mode 100644index 000000000..deb6b475f--- /dev/null+++ b/wger/core/templates/email_verification/email_body.txt@@ -0,0 +1,3 @@+You are almost there, {{ user.username }}!+Please click the following link to confirm your account: {{ link }}+The token expires on {{ expiry|time:"TIME_FORMAT" }}diff --git a/wger/core/urls.py b/wger/core/urls.pyindex 951911d58..298d6c5b8 100644--- a/wger/core/urls.py+++ b/wger/core/urls.py@@ -67,6 +67,9 @@ patterns_user = [path('delete',user.delete,name='delete'),+ path('confirm-email',+ user.confirm_email,+ name='confirm-email'),path('<int:user_pk>/delete',user.delete,name='delete'),diff --git a/wger/core/views/user.py b/wger/core/views/user.pyindex f1361ed6f..2afc549da 100644--- a/wger/core/views/user.py+++ b/wger/core/views/user.py@@ -72,6 +72,7 @@ from crispy_forms.layout import (Row,Submit)+from django_email_verification import send_emailfrom rest_framework.authtoken.models import Token# wger@@ -610,3 +611,11 @@ class WgerPasswordResetConfirmView(PasswordResetConfirmView):form.helper.form_class = 'wger-form'form.helper.add_input(Submit('submit', _("Save"), css_class='btn-success btn-block'))return form+++@login_required+def confirm_email(request):+ if not request.user.userprofile.email_verified:+ send_email(request.user)++ return HttpResponseRedirect(reverse('software:features'))diff --git a/wger/exercises/api/permissions.py b/wger/exercises/api/permissions.pyindex 9b570679c..c270ff210 100644--- a/wger/exercises/api/permissions.py+++ b/wger/exercises/api/permissions.py@@ -29,5 +29,5 @@ class CanEditExercises(BasePermission):if request.user.is_anonymous:return False- return request.user.userprofile.is_trustworthy or\- request.user.has_perm('exercises.add_exercise')+ return (request.user.userprofile.is_trustworthy+ or request.user.has_perm('exercises.add_exercise'))diff --git a/wger/exercises/api/views.py b/wger/exercises/api/views.pyindex 5315d6e64..dbf6ba240 100644--- a/wger/exercises/api/views.py+++ b/wger/exercises/api/views.py@@ -29,15 +29,12 @@ from rest_framework.decorators import (action,api_view)-from rest_framework.permissions import (- AllowAny,- IsAuthenticatedOrReadOnly-)+from rest_framework.permissions import IsAuthenticatedOrReadOnlyfrom rest_framework.response import Response# wger-from wger.core.api.viewsets import CreateUpdateModelViewSetfrom wger.config.models import LanguageConfig+from wger.core.api.viewsets import CreateUpdateModelViewSetfrom wger.exercises.api.permissions import CanEditExercisesfrom wger.exercises.api.serializers import (EquipmentSerializer,