Skip to content

Commit

Permalink
Merge pull request #73 from geislern/feature-signup
Browse files Browse the repository at this point in the history
Signup
  • Loading branch information
geislern authored Apr 20, 2024
2 parents fcad48b + 555452f commit 7862b4b
Show file tree
Hide file tree
Showing 18 changed files with 264 additions and 49 deletions.
8 changes: 7 additions & 1 deletion dachor/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,4 +158,10 @@
KEEP_COMMENTS_ON_MINIFYING = True

LOGIN_URL = "/accounts/login/"
LOGIN_REDIRECT_URL = "/"
LOGIN_REDIRECT_URL = "/intern/"

# Emails
SEND_MAILS = True
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
ADMIN_MAILS = ['[email protected]']
DEFAULT_FROM_EMAIL = '[email protected]'
18 changes: 18 additions & 0 deletions dachor/settings_production.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,21 @@
}

# TODO: caching

### EMAILS ###
SEND_MAILS = True
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'

# Addresses of all admins of this instance that will receive mail notifications
ADMIN_MAILS = secrets.MAIL_ADMINS

# Address to send from
SERVER_EMAIL = secrets.MAIL_ADDRESS
DEFAULT_FROM_EMAIL = SERVER_EMAIL

# SMTP Server Details
EMAIL_HOST = secrets.MAIL_HOST
EMAIL_PORT = secrets.MAIL_PORT
EMAIL_USE_TLS = secrets.MAIL_USE_TLS
EMAIL_HOST_USER = secrets.MAIL_USER
EMAIL_HOST_PASSWORD = secrets.MAIL_PASSWORD
11 changes: 11 additions & 0 deletions dachor/settings_secrets.py.sample
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,14 @@ HOSTS = []
DB_NAME = ''
DB_USER = ''
DB_PASSWORD = ''

# Addresses of all admins of this instance that will receive mail notifications
MAIL_ADMINS = []
# Address to send from
MAIL_SERVER_ADDRESS = ''
# SMTP Server Details
MAIL_HOST = ''
MAIL_PORT = 587
MAIL_USE_TLS = True
MAIL_HOST_USER = ''
MAIL_HOST_PASSWORD = ''
2 changes: 1 addition & 1 deletion dachor/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@
path('admin/', admin.site.urls),
path('accounts/', include('django.contrib.auth.urls')),
path('', include('dachor_website.urls', namespace='website')),
path('', include('dachor_internal.urls', namespace='internal')),
path('intern/', include('dachor_internal.urls', namespace='internal')),
]
49 changes: 49 additions & 0 deletions dachor_internal/forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
from betterforms.multiform import MultiModelForm
from django.contrib.auth.forms import UserCreationForm
from django.forms import ModelForm, TextInput

from dachor_internal.models import Profile


class DateInput(TextInput):
input_type = 'date'


class UserForm(UserCreationForm):
class Meta(UserCreationForm.Meta):
fields = ['email', 'first_name', 'last_name', 'username']

def __init__(self, *args, **kwargs):
super(UserForm, self).__init__(*args, **kwargs)
self.fields['email'].required = True
self.fields['first_name'].required = True


class ProfileForm(ModelForm):
class Meta:
model = Profile
exclude = ['user']

def __init__(self, *args, **kwargs):
super(ProfileForm, self).__init__(*args, **kwargs)
self.fields['birthday'].widget = DateInput()


class SignupForm(MultiModelForm):
form_classes = {
'user': UserForm,
'profile': ProfileForm
}

def save(self, commit=True):
objects = super(SignupForm, self).save(commit=False)

if commit:
user = objects['user']
user.is_active = False
user.save()
profile = objects['profile']
profile.user = user
profile.save()

return objects
21 changes: 21 additions & 0 deletions dachor_internal/models.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import datetime

from django.conf import settings
from django.core.exceptions import ValidationError
from django.core.mail import EmailMessage
from django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.dispatch import receiver
from django.urls import reverse_lazy


def validate_birthday(value):
Expand Down Expand Up @@ -77,3 +80,21 @@ def save_user_profile(sender, instance, **kwargs):
profile.save()
except Profile.DoesNotExist:
pass


@receiver(post_save, sender=User)
def user_saved_handler(sender, instance: User, created, **kwargs):
"""
React to user creation by sending a notification mail to the instance admins
"""
if created and settings.SEND_MAILS:
host = 'https://' + settings.ALLOWED_HOSTS[0] if len(settings.ALLOWED_HOSTS) > 0 else 'http://127.0.0.1:8000'
url = f"{host}{reverse_lazy('admin:auth_user_change', kwargs={'object_id': instance.pk})}"

mail = EmailMessage(
f"[DA!CHOR Webseite] Neuer Benutzer '{instance.username}' angelegt",
f"Ein neuer Nutzer {instance.first_name} {instance.last_name} ({instance.username}) hat sich registriert.\n\nFreigeben: {url}",
settings.DEFAULT_FROM_EMAIL,
settings.ADMIN_MAILS
)
mail.send(fail_silently=True)
12 changes: 12 additions & 0 deletions dachor_internal/templates/dachor_internal/base_internal.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{% extends 'dachor_website/base.html' %}

{% load bootstrap4 %}

{% block content %}
<div class="mt-2">
{% bootstrap_messages %}
</div>

{% block content_internal %}
{% endblock %}
{% endblock %}
13 changes: 13 additions & 0 deletions dachor_internal/templates/dachor_internal/edit_profile.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{% extends 'dachor_internal/base_internal.html' %}

{% load bootstrap4 %}

{% block content_internal %}
<h1>Profil bearbeiten</h1>

<form method="post" class="post-form">
{% csrf_token %}
{% bootstrap_form form %}
<input type="submit" class="btn btn-outline-custom float-right" value="Aktualisieren">
</form>
{% endblock %}
9 changes: 9 additions & 0 deletions dachor_internal/templates/dachor_internal/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{% extends 'dachor_internal/base_internal.html' %}

{% load bootstrap4 %}

{% block content_internal %}
<h1>Interner Bereich</h1>

<p class="text-center">Hallo {{ user.first_name }}</p>
{% endblock %}
17 changes: 17 additions & 0 deletions dachor_internal/templates/dachor_internal/not_confirmed.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{% extends 'dachor_internal/base_internal.html' %}

{% load bootstrap4 %}

{% block content_internal %}
<h1>Interner Bereich</h1>

<div class="alert alert-warning">
<h4 class="alert-heading">Zugang noch nicht bestätigt</h4>
<p class="mb-0">
Dein Zugang zum internen Bereich muss noch überprüft und freigeschaltet werden.
Hab bitte noch ein bisschen Geduld.
</p>
</div>


{% endblock %}
14 changes: 14 additions & 0 deletions dachor_internal/templates/dachor_internal/signup.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{% extends 'dachor_internal/base_internal.html' %}

{% load bootstrap4 %}

{% block content_internal %}
<h1>Freischaltung für internen Bereich beantragen</h1>

<form method="post" class="post-form">
{% csrf_token %}
{% bootstrap_form form.user %}
{% bootstrap_form form.profile %}
<input type="submit" class="btn btn-outline-custom float-right" value="Registrieren">
</form>
{% endblock %}
18 changes: 12 additions & 6 deletions dachor_internal/templates/registration/login.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,17 @@
{% block content %}
<h1>Login</h1>

<form method="post" class="post-form">
{% csrf_token %}
{% bootstrap_form form %}
<input type="submit" class="btn btn-outline-custom float-right" value="Login!">
</form>
<div class="alert alert-dismissible alert-light">
Wenn du deinen Account erst kürzlich erstellt hast, kannst du dich vielleicht noch nicht einloggen,
weil alle Accounts erst überprüft und freigeschaltet werden müssen.
</div>

<a href="{% url "password_reset" %}" class="text-white">Passwort vergessen?</a>
<form method="post" class="post-form">
{% csrf_token %}
{% bootstrap_form form %}
<input type="submit" class="btn btn-outline-custom float-right" value="Login!">
</form>

<a href="{% url "password_reset" %}" class="text-white">Passwort vergessen?</a> &middot;
<a href="{% url "internal:signup" %}" class="text-white">Neu hier?</a>
{% endblock %}
6 changes: 5 additions & 1 deletion dachor_internal/urls.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
from django.urls import path, include
from django.urls import path

from . import views

app_name = 'internal'

urlpatterns = [
path('', views.StartView.as_view(), name='start'),
path('signup/', views.SignupView.as_view(), name='signup'),
path('not-confirmed/', views.NotConfirmedView.as_view(), name='not-confirmed'),
path('profile/edit/', views.ProfileEditView.as_view(), name='profile-edit'),
]
42 changes: 40 additions & 2 deletions dachor_internal/views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,41 @@
from django.shortcuts import render
from django.contrib import messages
from django.contrib.auth.mixins import LoginRequiredMixin
from django.urls import reverse_lazy
from django.views.generic import TemplateView, CreateView, FormView

# Create your views here.
from dachor_internal.forms import SignupForm, ProfileForm
from dachor_website.views import NavbarHighlightMixin


class StartView(NavbarHighlightMixin, LoginRequiredMixin, TemplateView):
template_name = 'dachor_internal/index.html'
navbar_active = 'internal'


class NotConfirmedView(NavbarHighlightMixin, TemplateView):
template_name = 'dachor_internal/not_confirmed.html'
navbar_active = 'internal'


class SignupView(NavbarHighlightMixin, CreateView):
form_class = SignupForm
template_name = 'dachor_internal/signup.html'
success_url = reverse_lazy('internal:not-confirmed')
navbar_active = 'internal'


class ProfileEditView(NavbarHighlightMixin, FormView):
form_class = ProfileForm
template_name = 'dachor_internal/edit_profile.html'
success_url = reverse_lazy('internal:profile-edit')
navbar_active = 'internal'

def get_form_kwargs(self):
kwargs = super().get_form_kwargs()
kwargs.update(instance=self.request.user.profile)
return kwargs

def form_valid(self, form):
form.save()
messages.add_message(self.request, messages.SUCCESS, "Informationen aktualisiert")
return super().form_valid(form)
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ $gray-800: #343a40 !default;
$gray-900: #212529 !default;
$black: #000 !default;

$blue: #df691a !default;
$blue: #00528c !default;
$indigo: #6610f2 !default;
$purple: #6f42c1 !default;
$pink: #e83e8c !default;
Expand All @@ -41,7 +41,7 @@ $yiq-contrasted-threshold: 185 !default;

// Body

$body-bg: #00528c !default;
$body-bg: $blue !default;
$body-color: $white !default;

// Components
Expand Down Expand Up @@ -77,11 +77,12 @@ $custom-file-border-color: $gray-200 !default;

// Dropdowns

$dropdown-bg: $gray-200 !default;
$dropdown-divider-bg: rgba($black, .15) !default;
$dropdown-bg: $blue !default;
$dropdown-divider-bg: $gray-100 !default;
$dropdown-link-color: $body-color !default;
$dropdown-link-hover-color: $dropdown-link-color !default;
$dropdown-link-hover-bg: $table-hover-bg !default;
$dropdown-link-hover-color: $blue !default;
$dropdown-link-hover-bg: $white !default;
$dropdown-border-color: $white !default;

// Navs

Expand Down
13 changes: 11 additions & 2 deletions dachor_website/templates/dachor_website/navbar.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@
</a>
<ul class="navbar-nav ml-auto flex-row">
{% if user.is_authenticated %}
<div class="nav-item dropdown">
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" role="button" id="dropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
{{ user }}
</a>
<div class="dropdown-menu" aria-labelledby="dropdownMenuLink">
<a class="dropdown-item" href="{% url "password_change" %}">Passwort ändern</a>
<a class="dropdown-item" href="{% url "logout" %}?next=/">Logout</a>
</div>
</div>
</li>
{% else %}
<li class="nav-item nav-button">
<div class="d-none d-sm-block">
Expand Down Expand Up @@ -55,6 +55,15 @@
<li class="nav-item">
<a class="nav-link {{ contact }}" href="{% url "website:contact" %}">KONTAKT</a>
</li>
{% if user.is_authenticated %}
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle {{ internal }}" data-toggle="dropdown" href="#" role="button" aria-haspopup="true" aria-expanded="true">INTERN</a>
<div class="dropdown-menu">
<a class="dropdown-item" href="{% url "internal:start" %}">Übersicht</a>
<a class="dropdown-item" href="{% url "internal:profile-edit" %}">Profil bearbeiten</a>
</div>
</li>
{% endif %}
</ul>
<div class="d-block d-md-none nav-collapse-lines">
<div class="hline"></div>
Expand Down
Loading

0 comments on commit 7862b4b

Please sign in to comment.