Commit 4f6109e2 authored by Sli's avatar Sli

Refactor mailings email

parent c83a9901
Pipeline #1158 passed with stage
in 6 minutes and 7 seconds
......@@ -3,6 +3,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):
......@@ -16,20 +18,20 @@ class Migration(migrations.Migration):
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)),
('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(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)),
('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(
......
......@@ -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:
......
......@@ -13,7 +13,7 @@
</tr>
{% for mailing in list %}
<tr>
<td>{{ mailing.email }}</td>
<td>{{ mailing.email_full }}</td>
<td><a href="{{ url('club:mailing', club_id=mailing.club.id) }}">{{ mailing.club }}</a></td>
<td>
<a href="{{ url('com:mailing_delete', mailing_id=mailing.id) }}">{% trans %}Delete{% endtrans %}</a> - {% if not mailing.is_moderated %}<a href="{{ url('com:mailing_moderate', mailing_id=mailing.id) }}">{% trans %}Moderate{% endtrans %}</a>{% else %}{% trans user=mailing.moderator %}Moderated by {{ user }}{% endtrans %}{% endif %}
......
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('core', '0021_auto_20170822_1529'),
]
operations = [
migrations.AlterField(
model_name='notification',
name='type',
field=models.CharField(choices=[('MAILING_MODERATION', 'A new mailing list needs to be moderated'), ('NEWS_MODERATION', 'A fresh new to be moderated'), ('FILE_MODERATION', 'New files to be moderated'), ('SAS_MODERATION', 'New pictures/album to be moderated in the SAS'), ('NEW_PICTURES', "You've been identified on some pictures"), ('REFILLING', 'You just refilled of %s €'), ('SELLING', 'You just bought %s'), ('GENERIC', 'You have a notification')], default='GENERIC', max_length=32, verbose_name='type'),
),
]
This diff is collapsed.
......@@ -1340,7 +1340,7 @@ def migrate_mailings():
Mailing.objects.all().delete()
print("Migrating old database")
print("Migrating old mailing database")
cur.execute("""
SELECT * FROM mailing
......@@ -1355,7 +1355,7 @@ def migrate_mailings():
club = club.first()
if mailing['nom']:
mailing['nom'] = '.' + mailing['nom']
Mailing(id=mailing['id_mailing'], club=club, email=to_unicode(club.unix_name + mailing['nom'] + '@utbm.fr'),
Mailing(id=mailing['id_mailing'], club=club, email=to_unicode(club.unix_name + mailing['nom']),
moderator=moderator, is_moderated=(mailing['is_valid'] > 0)).save()
print("-------------------")
......
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