From 15f5a4d281ce1e6e62406b0440f73db79fe398a6 Mon Sep 17 00:00:00 2001
From: Jannis Portmann
Date: Wed, 5 Apr 2023 23:29:30 +0200
Subject: [PATCH] User registration
---
pflaenzli/pflaenzli/forms.py | 12 +++++++++++-
pflaenzli/pflaenzli/models.py | 2 +-
pflaenzli/pflaenzli/templates/app/index.html | 4 +++-
pflaenzli/pflaenzli/urls.py | 1 +
pflaenzli/pflaenzli/views.py | 17 ++++++++++++++++-
pflaenzli/pflaenzli_django/settings.py | 7 +++++++
6 files changed, 39 insertions(+), 4 deletions(-)
diff --git a/pflaenzli/pflaenzli/forms.py b/pflaenzli/pflaenzli/forms.py
index fe2f368..8386291 100644
--- a/pflaenzli/pflaenzli/forms.py
+++ b/pflaenzli/pflaenzli/forms.py
@@ -1,9 +1,19 @@
from django import forms
+from django.contrib.auth.forms import UserCreationForm
+from friendly_captcha.fields import FrcCaptchaField
-from .models import PflaenzliUser, Offer
+from .models import Offer, PflaenzliUser
class CreateOfferForm(forms.ModelForm):
class Meta:
model = Offer
fields = ['title', 'description', 'zipcode', 'image']
+
+
+class RegistrationForm(UserCreationForm):
+ class Meta(UserCreationForm.Meta):
+ model = PflaenzliUser
+ fields = UserCreationForm.Meta.fields + ('zipcode',)
+
+ captcha = FrcCaptchaField()
diff --git a/pflaenzli/pflaenzli/models.py b/pflaenzli/pflaenzli/models.py
index 8f3f3b6..8c551ef 100644
--- a/pflaenzli/pflaenzli/models.py
+++ b/pflaenzli/pflaenzli/models.py
@@ -6,7 +6,7 @@ from django.core.files.storage import default_storage
class PflaenzliUser(User):
- zipcode = models.PositiveIntegerField()
+ zipcode = models.PositiveIntegerField(blank=True)
class Offer(models.Model):
diff --git a/pflaenzli/pflaenzli/templates/app/index.html b/pflaenzli/pflaenzli/templates/app/index.html
index a2a2f51..9cf041d 100644
--- a/pflaenzli/pflaenzli/templates/app/index.html
+++ b/pflaenzli/pflaenzli/templates/app/index.html
@@ -13,7 +13,9 @@
To offer your plants, please register first.
- Register
+ Register
{% endblock %}
diff --git a/pflaenzli/pflaenzli/urls.py b/pflaenzli/pflaenzli/urls.py
index 7e31200..3f7ea3f 100644
--- a/pflaenzli/pflaenzli/urls.py
+++ b/pflaenzli/pflaenzli/urls.py
@@ -16,5 +16,6 @@ urlpatterns = [
path("accounts/", views.user_detail, name="user_detail"),
path('accounts/login/', auth_views.LoginView.as_view(template_name='registration/login.html')),
path('accounts/profile/', auth_views.LoginView.as_view(template_name='user/detail.html'), name='user_profile'),
+ path('accounts/register/', views.register_user, name='register_user'),
path('accounts/', include('django.contrib.auth.urls')),
]
diff --git a/pflaenzli/pflaenzli/views.py b/pflaenzli/pflaenzli/views.py
index aeb09dc..ef53630 100644
--- a/pflaenzli/pflaenzli/views.py
+++ b/pflaenzli/pflaenzli/views.py
@@ -1,10 +1,12 @@
from django.shortcuts import render, get_object_or_404, redirect
from django.utils import timezone
from django.contrib import messages
+from django.contrib.auth import login
from django.contrib.auth.decorators import login_required
from django.http import HttpResponseForbidden
-from .forms import CreateOfferForm
+from .forms import CreateOfferForm, RegistrationForm
+
from .models import PflaenzliUser, Offer, Wish
from .mail import send_offer_email
from .upload import generate_unique_filename
@@ -97,3 +99,16 @@ def user_detail(request, user_id):
wishes = Wish.objects.filter(user=user_id)
return render(request, "user/public.html", {"user": user, "offers": offers, "wishes": wishes})
+
+
+def register_user(request):
+ if request.method == "POST":
+ form = RegistrationForm(request.POST)
+ if form.is_valid():
+ user = form.save()
+ login(request, user)
+ return redirect("index")
+ else:
+ form = RegistrationForm()
+
+ return render(request, "basic_form.html", {"form": form, "button_label": "Register", "title": "Registeration"})
diff --git a/pflaenzli/pflaenzli_django/settings.py b/pflaenzli/pflaenzli_django/settings.py
index b859e29..2dd84cd 100644
--- a/pflaenzli/pflaenzli_django/settings.py
+++ b/pflaenzli/pflaenzli_django/settings.py
@@ -48,6 +48,7 @@ INSTALLED_APPS = [
"fontawesomefree",
"crispy_forms",
"crispy_bootstrap5",
+ "friendly_captcha",
]
MIDDLEWARE = [
@@ -161,3 +162,9 @@ EMAIL_PORT = os.getenv('SMTP_PORT')
EMAIL_HOST_USER = os.getenv('SMTP_USER')
EMAIL_HOST_PASSWORD = os.getenv('SMTP_PASSWORD')
EMAIL_USE_SSL = True
+
+
+# Friendly Captcha setting
+FRC_CAPTCHA_SECRET = os.getenv('FRC_SECRET')
+FRC_CAPTCHA_SITE_KEY = os.getenv('FRC_SITEKEY')
+FRC_CAPTCHA_VERIFICATION_URL = 'https://api.friendlycaptcha.com/api/v1/siteverify'