mirror of
https://github.com/ae-utbm/sith.git
synced 2025-07-10 03:49:24 +00:00
Refactor mailings email
This commit is contained in:
@ -1,39 +0,0 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.db import migrations, models
|
||||
from django.conf import settings
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
|
||||
('club', '0008_auto_20170515_2214'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='Mailing',
|
||||
fields=[
|
||||
('id', models.AutoField(verbose_name='ID', serialize=False, primary_key=True, auto_created=True)),
|
||||
('email', models.EmailField(verbose_name='Email address', max_length=254, unique=True)),
|
||||
('is_moderated', models.BooleanField(verbose_name='is moderated', default=False)),
|
||||
('club', models.ForeignKey(related_name='mailings', to='club.Club', verbose_name='Club')),
|
||||
('moderator', models.ForeignKey(related_name='moderated_mailings', to=settings.AUTH_USER_MODEL, verbose_name='moderator', null=True)),
|
||||
],
|
||||
),
|
||||
migrations.CreateModel(
|
||||
name='MailingSubscription',
|
||||
fields=[
|
||||
('id', models.AutoField(verbose_name='ID', serialize=False, primary_key=True, auto_created=True)),
|
||||
('email', models.EmailField(verbose_name='Email address', max_length=254)),
|
||||
('mailing', models.ForeignKey(related_name='subscriptions', to='club.Mailing', verbose_name='Mailing')),
|
||||
('user', models.ForeignKey(null=True, related_name='mailing_subscriptions', to=settings.AUTH_USER_MODEL, verbose_name='User', blank=True)),
|
||||
],
|
||||
),
|
||||
migrations.AlterUniqueTogether(
|
||||
name='mailingsubscription',
|
||||
unique_together=set([('user', 'email', 'mailing')]),
|
||||
),
|
||||
]
|
41
club/migrations/0009_auto_20170822_2232.py
Normal file
41
club/migrations/0009_auto_20170822_2232.py
Normal file
@ -0,0 +1,41 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.db import migrations, models
|
||||
from django.conf import settings
|
||||
import re
|
||||
import django.core.validators
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
|
||||
('club', '0008_auto_20170515_2214'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='Mailing',
|
||||
fields=[
|
||||
('id', models.AutoField(auto_created=True, verbose_name='ID', serialize=False, primary_key=True)),
|
||||
('email', models.CharField(max_length=256, unique=True, validators=[django.core.validators.RegexValidator(re.compile('(^[-!#$%&\'*+/=?^_`{}|~0-9A-Z]+(\\.[-!#$%&\'*+/=?^_`{}|~0-9A-Z]+)*\\Z|^"([\\001-\\010\\013\\014\\016-\\037!#-\\[\\]-\\177]|\\\\[\\001-\\011\\013\\014\\016-\\177])*"\\Z)', 34), 'Enter a valid address. Only the root of the address is needed.')], verbose_name='Email address')),
|
||||
('is_moderated', models.BooleanField(default=False, verbose_name='is moderated')),
|
||||
('club', models.ForeignKey(verbose_name='Club', related_name='mailings', to='club.Club')),
|
||||
('moderator', models.ForeignKey(null=True, verbose_name='moderator', related_name='moderated_mailings', to=settings.AUTH_USER_MODEL)),
|
||||
],
|
||||
),
|
||||
migrations.CreateModel(
|
||||
name='MailingSubscription',
|
||||
fields=[
|
||||
('id', models.AutoField(auto_created=True, verbose_name='ID', serialize=False, primary_key=True)),
|
||||
('email', models.EmailField(max_length=254, verbose_name='Email address')),
|
||||
('mailing', models.ForeignKey(verbose_name='Mailing', related_name='subscriptions', to='club.Mailing')),
|
||||
('user', models.ForeignKey(null=True, verbose_name='User', related_name='mailing_subscriptions', blank=True, to=settings.AUTH_USER_MODEL)),
|
||||
],
|
||||
),
|
||||
migrations.AlterUniqueTogether(
|
||||
name='mailingsubscription',
|
||||
unique_together=set([('user', 'email', 'mailing')]),
|
||||
),
|
||||
]
|
@ -31,6 +31,7 @@ from django.core.exceptions import ValidationError, ObjectDoesNotExist
|
||||
from django.db import transaction
|
||||
from django.core.urlresolvers import reverse
|
||||
from django.utils import timezone
|
||||
from django.core.validators import RegexValidator, validate_email
|
||||
|
||||
from core.models import User, MetaGroup, Group, SithFile, RealGroup, Notification
|
||||
|
||||
@ -229,7 +230,11 @@ class Mailing(models.Model):
|
||||
Remember that mailing lists should be validated by UTBM
|
||||
"""
|
||||
club = models.ForeignKey(Club, verbose_name=_('Club'), related_name="mailings", null=False, blank=False)
|
||||
email = models.EmailField(_('Email address'), unique=True)
|
||||
email = models.CharField(_('Email address'), unique=True, null=False, blank=False, max_length=256,
|
||||
validators=[
|
||||
RegexValidator(validate_email.user_regex,
|
||||
_('Enter a valid address. Only the root of the address is needed.'))
|
||||
])
|
||||
is_moderated = models.BooleanField(_('is moderated'), default=False)
|
||||
moderator = models.ForeignKey(User, related_name="moderated_mailings", verbose_name=_("moderator"), null=True)
|
||||
|
||||
@ -240,6 +245,10 @@ class Mailing(models.Model):
|
||||
self.moderator = None
|
||||
super(Mailing, self).clean()
|
||||
|
||||
@property
|
||||
def email_full(self):
|
||||
return self.email + '@' + settings.SITH_MAILING_DOMAIN
|
||||
|
||||
def can_moderate(self, user):
|
||||
return user.is_root or user.is_in_group(settings.SITH_GROUP_COM_ADMIN_ID)
|
||||
|
||||
@ -254,11 +263,8 @@ class Mailing(models.Model):
|
||||
sub.delete()
|
||||
super(Mailing, self).delete()
|
||||
|
||||
def base_mail(self):
|
||||
return self.email.split('@')[0]
|
||||
|
||||
def fetch_format(self):
|
||||
resp = self.base_mail() + ': '
|
||||
resp = self.email + ': '
|
||||
for sub in self.subscriptions.all():
|
||||
resp += sub.fetch_format()
|
||||
return resp
|
||||
@ -271,7 +277,7 @@ class Mailing(models.Model):
|
||||
super(Mailing, self).save()
|
||||
|
||||
def __str__(self):
|
||||
return "%s - %s" % (self.club, self.email)
|
||||
return "%s - %s" % (self.club, self.email_full)
|
||||
|
||||
|
||||
class MailingSubscription(models.Model):
|
||||
|
@ -11,7 +11,7 @@
|
||||
|
||||
{% for mailing in object_list %}
|
||||
{% if mailing.is_moderated %}
|
||||
<h2>{% trans %}Mailing{% endtrans %} {{ mailing.email }}
|
||||
<h2>{% trans %}Mailing{% endtrans %} {{ mailing.email_full }}
|
||||
{%- if user.is_owner(mailing) -%}
|
||||
<a href="{{ url('club:mailing_delete', mailing_id=mailing.id) }}"> - {% trans %}Delete{% endtrans %}</a>
|
||||
{%- endif -%}
|
||||
|
@ -36,7 +36,6 @@ from django.utils.translation import ugettext as _t
|
||||
from ajax_select.fields import AutoCompleteSelectField
|
||||
from django.core.exceptions import PermissionDenied
|
||||
from django.shortcuts import get_object_or_404
|
||||
from django.core.validators import RegexValidator, validate_email
|
||||
|
||||
from core.views import CanViewMixin, CanEditMixin, CanEditPropMixin, TabedViewMixin
|
||||
from core.views.forms import SelectDate, SelectDateTime
|
||||
@ -55,16 +54,6 @@ class MailingForm(forms.ModelForm):
|
||||
model = Mailing
|
||||
fields = ('email', 'club', 'moderator')
|
||||
|
||||
email = forms.CharField(
|
||||
label=_('Email address'),
|
||||
validators=[
|
||||
RegexValidator(
|
||||
validate_email.user_regex,
|
||||
_('Enter a valid address. Only the root of the address is needed.')
|
||||
)
|
||||
],
|
||||
required=True)
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
club_id = kwargs.pop('club_id', None)
|
||||
user_id = kwargs.pop('user_id', -1) # Remember 0 is treated as None
|
||||
@ -78,12 +67,6 @@ class MailingForm(forms.ModelForm):
|
||||
self.fields['moderator'].initial = user_id
|
||||
self.fields['moderator'].widget = forms.HiddenInput()
|
||||
|
||||
def clean(self):
|
||||
cleaned_data = super(MailingForm, self).clean()
|
||||
if self.is_valid():
|
||||
cleaned_data['email'] += '@' + settings.SITH_MAILING_DOMAIN
|
||||
return cleaned_data
|
||||
|
||||
|
||||
class MailingSubscriptionForm(forms.ModelForm):
|
||||
class Meta:
|
||||
|
Reference in New Issue
Block a user