Commit 2f45e4c8 authored by Robert's avatar Robert

Add unsubscription request form

parent e1f48d9c
# DEPRECATED CODE - DO NOT USE!
from django import forms
from features.gestalten import models as entities_models
import functools
......@@ -81,7 +83,7 @@ class EmailGestalt(Email):
has_data = True
def get_data(self, form_data):
return entities_models.Gestalt.get_or_create(form_data)
return entities_models.Gestalt.objects.get_or_create_by_email(form_data)
email_gestalt = functools.partial(fieldclass_factory, EmailGestalt)
......@@ -108,7 +110,7 @@ class CurrentGestalt(Field):
if self.view.request.user.is_authenticated:
return self.view.request.user.gestalt
elif not self.null:
return entities_models.Gestalt.get_or_create(form_data)
return entities_models.Gestalt.objects.get_or_create_by_email(form_data)
else:
return None
......
......@@ -51,7 +51,7 @@ class Create(forms.ModelForm):
self.contribution.contribution = contributions_models.Text.objects.create(
text=self.cleaned_data['text'])
if 'author' in self.cleaned_data:
self.contribution.author = gestalten.Gestalt.get_or_create(
self.contribution.author = gestalten.Gestalt.objects.get_or_create_by_email(
self.cleaned_data['author'])
self.contribution.save()
......
......@@ -25,7 +25,7 @@ class GestaltByEmailField(forms.EmailField):
def clean(self, value):
value = super().clean(value)
gestalt = models.Gestalt.get_or_create(value)
gestalt = models.Gestalt.objects.get_or_create_by_email(value)
if gestalt.can_login():
raise ValidationError(self.error_messages['login'], code='login')
return gestalt
......
......@@ -12,6 +12,21 @@ import core
from core import colors
class GestaltQuerySet(models.QuerySet):
def get_or_create_by_email(self, email):
try:
created = False
user = auth.get_user_model().objects.get(emailaddress__email=email)
except auth.get_user_model().DoesNotExist:
user, created = auth.get_user_model().objects.get_or_create(
email=email)
if created:
allauth_adapter.get_adapter().populate_username(None, user)
user.set_unusable_password()
user.save()
return user.gestalt
class Gestalt(core.models.Model):
is_group = False
......@@ -36,6 +51,8 @@ class Gestalt(core.models.Model):
'associations.Association', content_type_field='entity_type',
object_id_field='entity_id', related_query_name='gestalt')
objects = models.Manager.from_queryset(GestaltQuerySet)()
@property
def name(self):
return ' '.join(filter(None, [self.user.first_name, self.user.last_name]))
......@@ -44,20 +61,6 @@ class Gestalt(core.models.Model):
def slug(self):
return self.user.username
@staticmethod
def get_or_create(email):
try:
created = False
user = auth.get_user_model().objects.get(emailaddress__email=email)
except auth.get_user_model().DoesNotExist:
user, created = auth.get_user_model().objects.get_or_create(
email=email)
if created:
allauth_adapter.get_adapter().populate_username(None, user)
user.set_unusable_password()
user.save()
return user.gestalt
def __str__(self):
return self.name or self.slug
......
......@@ -22,7 +22,7 @@ class Command(django.core.management.base.BaseCommand):
creator = Gestalt.objects.get(user__username=options['creator_username'])
emails = re.findall(EMAIL_RE, (open(options['emails_filename']).read()))
for email in emails:
gestalt = Gestalt.get_or_create(email)
gestalt = Gestalt.objects.get_or_create_by_email(email)
try:
group.memberships.create(member=gestalt, created_by=creator)
num_new_members += 1
......
......@@ -15,3 +15,7 @@ class Subscribe(forms.ModelForm):
super().__init__(*args, **kwargs)
if hasattr(self.instance, 'subscriber'):
del self.fields['subscriber']
class UnsubscribeRequest(forms.Form):
subscriber = forms.EmailField(label='E-Mail-Adresse')
......@@ -11,3 +11,5 @@ def is_subscribed(user, model):
add_perm('subscriptions.create', ~is_authenticated | (is_authenticated & ~is_subscribed))
add_perm('subscriptions.delete', is_authenticated & is_subscribed)
add_perm('subscriptions.delete_request', ~is_authenticated)
{% extends 'stadt/stadt.html' %}
{% block title %}Abonnement - {{ block.super }}{% endblock %}
{% block menu %}{% menu 'group' view.group %}{% endblock %}
{% block breadcrumb %}{% breadcrumb view.group 'Abonnement' %}{% endblock %}
{% block heading_title_text %}Abonnenment{% endblock %}
{% block sidebar %}
{% include 'associations/_sidebar_entity.html' with entity=view.group %}
{% endblock %}
{% block content %}
<form method="post">
{% csrf_token %}
{% include 'core/_field.html' with field=form.subscriber %}
<button class="btn btn-primary">Abonnement kündigen</button>
</form>
{% endblock %}
......@@ -13,4 +13,9 @@ urlpatterns = [
views.GroupUnsubscribe.as_view(),
name='group-unsubscribe',
),
url(
r'^stadt/groups/(?P<group_pk>[0-9]+)/unsubscribe/request/$',
views.GroupUnsubscribeRequest.as_view(),
name='group-unsubscribe-request',
),
]
......@@ -3,7 +3,7 @@ from django.contrib.messages import info
from django.contrib.messages.views import SuccessMessageMixin
from django.http import HttpResponseRedirect
from django.shortcuts import get_object_or_404
from django.views.generic import CreateView, DeleteView
from django.views.generic import CreateView, DeleteView, FormView
from core.views import PermissionMixin
from features.groups.models import Group
......@@ -68,3 +68,21 @@ class GroupUnsubscribe(PermissionMixin, DeleteView):
def get_success_url(self):
return self.group.get_absolute_url()
class GroupUnsubscribeRequest(PermissionMixin, FormView):
permission_required = 'subscriptions.delete_request'
form_class = forms.UnsubscribeRequest
template_name = 'subscriptions/delete_request.html'
def form_valid(self, form):
info(self.request, 'Es wurde eine E-Mail an die angebene Adresse versendet.')
return super().form_valid(form)
def get_permission_object(self):
self.group = get_object_or_404(Group, pk=self.kwargs.get('group_pk'))
return self.group
def get_success_url(self):
return self.group.get_absolute_url()
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment