guest users

This commit is contained in:
Tom Irgang 2023-04-23 13:56:52 +02:00
parent 017410fa6b
commit 70c36a44f5
11 changed files with 114 additions and 43 deletions

View file

@ -155,3 +155,5 @@ EMAIL_USE_TLS = os.getenv("DJANGO_EMAIL_USE_TLS", 'true') in ('true', '1', 't')
AUTH_USER_MODEL = "repapp_users.CustomUser" AUTH_USER_MODEL = "repapp_users.CustomUser"
AUTHENTICATION_BACKENDS = [ AUTHENTICATION_BACKENDS = [
"repapp_users.backends.EmailBackend", "django.contrib.auth.backends.ModelBackend"] "repapp_users.backends.EmailBackend", "django.contrib.auth.backends.ModelBackend"]
LOGIN_REDIRECT_URL = '/guest/profile/'

View file

@ -22,7 +22,6 @@ from django.conf.urls.static import static
urlpatterns = [ urlpatterns = [
path("", include("repapp.urls")), path("", include("repapp.urls")),
path("", include("repapp_users.urls")), path('accounts/', include('django.contrib.auth.urls')),
path('users/', include('django.contrib.auth.urls')),
path('admin/', admin.site.urls), path('admin/', admin.site.urls),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) ] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

View file

@ -0,0 +1,28 @@
{% extends "repapp/base.html" %}
{% block title %}
- Gast Daten
{% endblock title %}
{% block content %}
<h1 class="mt-2">Ihre Daten</h1>
<hr class="mt-0 mb-4">
<table class="table">
<tbody>
<tr>
<th scope="row">eMail Adresse</th>
<td>{{ guest.mail }}</td>
</tr>
<tr>
<th scope="row">Name</th>
<td>{{ guest.name }}</td>
</tr>
<tr>
<th scope="row">Wohnort</th>
<td>{{ guest.residence }}</td>
</tr>
<tr>
<th scope="row">Telefonnummer</th>
<td>{{ guest.phone }}</td>
</tr>
</tbody>
</table>
{% endblock content %}

View file

@ -21,4 +21,6 @@ urlpatterns = [
views.register_device_final, name="register_device_final"), views.register_device_final, name="register_device_final"),
path("device/<str:device_identifier>/", path("device/<str:device_identifier>/",
views.device_view, name="view_device"), views.device_view, name="view_device"),
path("guest/profile/",
views.profile, name="guest_profile"),
] ]

View file

@ -3,14 +3,19 @@ Views of RepApp.
""" """
import datetime import datetime
import os import os
import random
import string
from hashlib import sha256 from hashlib import sha256
from django.views import generic from django.views import generic
from django.urls import reverse_lazy from django.urls import reverse_lazy
from django.shortcuts import get_object_or_404, render from django.shortcuts import get_object_or_404, render
from django.http import HttpResponseRedirect from django.http import HttpResponseRedirect
from django.core.exceptions import PermissionDenied
from django.contrib import messages from django.contrib import messages
from django.core.mail import send_mail from django.core.mail import send_mail
from django.template.loader import render_to_string from django.template.loader import render_to_string
from django.contrib.auth.decorators import login_required
from repapp_users.models import CustomUser
from .models import Cafe, Device, Guest, Organisator from .models import Cafe, Device, Guest, Organisator
from .forms import RegisterDevice, RegisterGuest from .forms import RegisterDevice, RegisterGuest
@ -156,6 +161,7 @@ class RegisterGuestFormView(generic.edit.FormView):
device = get_object_or_404(Device, identifier=device_identifier) device = get_object_or_404(Device, identifier=device_identifier)
name = form.cleaned_data['name'] name = form.cleaned_data['name']
mail = self.kwargs['mail']
residence = form.cleaned_data['residence'] residence = form.cleaned_data['residence']
identifier = sha256( identifier = sha256(
f'{name}{residence}{datetime.datetime.now()}'.encode('utf-8') f'{name}{residence}{datetime.datetime.now()}'.encode('utf-8')
@ -165,13 +171,25 @@ class RegisterGuestFormView(generic.edit.FormView):
name=name, name=name,
phone=form.cleaned_data['phone'], phone=form.cleaned_data['phone'],
residence=residence, residence=residence,
mail=self.kwargs['mail'], mail=mail,
) )
guest.save() guest.save()
device.guest = guest device.guest = guest
device.save() device.save()
if not CustomUser.objects.filter(email=mail).exists():
password = ''.join(random.choice(string.ascii_letters)
for i in range(10))
user = CustomUser.objects.create_user(
username=name,
email=mail,
password=password)
user.save()
print(f'{mail} {password}')
# TODO: send account creation email
send_confirmation_mails(device, guest, cafe, self.request) send_confirmation_mails(device, guest, cafe, self.request)
return HttpResponseRedirect( return HttpResponseRedirect(
@ -208,11 +226,38 @@ def register_device_final(request, cafe, device_identifier):
) )
@login_required
def device_view(request, device_identifier): def device_view(request, device_identifier):
user = request.user
if not user:
raise PermissionDenied()
device = get_object_or_404(Device, identifier=device_identifier) device = get_object_or_404(Device, identifier=device_identifier)
if not device.guest:
raise PermissionDenied()
# TODO: Guests can only view their devices, Reparateur and Orga can view all devices.
return render( return render(
request, request,
"repapp/device_view.html", "repapp/device_view.html",
{"device": device}, {"device": device},
) )
@login_required
def profile(request):
user = request.user
if not user:
raise PermissionDenied()
guest = get_object_or_404(Guest, mail=user.email)
return render(
request,
"repapp/guest_profile.html",
{
'user': user,
'guest': guest
}
)

View file

@ -0,0 +1,22 @@
# Generated by Django 4.2 on 2023-04-23 11:56
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('repapp_users', '0001_initial'),
]
operations = [
migrations.AlterModelOptions(
name='customuser',
options={'verbose_name': 'Benutzer', 'verbose_name_plural': 'Benutzer'},
),
migrations.AlterField(
model_name='customuser',
name='email',
field=models.EmailField(max_length=254, unique=True, verbose_name='eMail Adresse'),
),
]

View file

@ -1,33 +0,0 @@
{% extends "repapp/base.html" %}
{% block content %}
{% if form.errors %}<p>Your username and password didn't match. Please try again.</p>{% endif %}
{% if next %}
{% if user.is_authenticated %}
<p>
Your account doesn't have access to this page. To proceed,
please login with an account that has access.
</p>
{% else %}
<p>Please login to see this page.</p>
{% endif %}
{% endif %}
<form method="post" action="{% url 'login' %}">
{% csrf_token %}
<table>
<tr>
<td>{{ form.username.label_tag }}</td>
<td>{{ form.username }}</td>
</tr>
<tr>
<td>{{ form.password.label_tag }}</td>
<td>{{ form.password }}</td>
</tr>
</table>
<input type="submit" value="login">
<input type="hidden" name="next" value="{{ next }}">
</form>
{# Assumes you setup the password_reset view in your URLconf #}
<p>
<a href="{% url 'password_reset' %}">Lost password?</a>
</p>
{% endblock %}

View file

@ -0,0 +1,13 @@
{% extends "repapp/base.html" %}
{% load crispy_forms_tags %}
{% block title %}
- Gast Login
{% endblock title %}
{% block content %}
<h2>Gast Login</h2>
<form method="post">
{% csrf_token %}
{{ form|crispy }}
<button type="submit" class="btn btn-primary">Anmelden</button>
</form>
{% endblock content %}

View file

@ -1,4 +0,0 @@
from django.urls import path, include
urlpatterns = [
]

View file

@ -1,3 +0,0 @@
from django.shortcuts import render
# Create your views here.