mirror of
https://github.com/ae-utbm/sith.git
synced 2025-07-10 03:49:24 +00:00
Add moderation for mailing lists
This commit is contained in:
26
club/migrations/0011_auto_20170821_1702.py
Normal file
26
club/migrations/0011_auto_20170821_1702.py
Normal file
@ -0,0 +1,26 @@
|
||||
# -*- 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', '0010_auto_20170817_1537'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='mailing',
|
||||
name='is_moderated',
|
||||
field=models.BooleanField(default=False, verbose_name='is moderated'),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='mailing',
|
||||
name='moderator',
|
||||
field=models.ForeignKey(related_name='moderated_mailings', to=settings.AUTH_USER_MODEL, null=True, verbose_name='moderator'),
|
||||
),
|
||||
]
|
@ -27,12 +27,12 @@ from django.db import models
|
||||
from django.core import validators
|
||||
from django.conf import settings
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
from django.core.exceptions import ValidationError
|
||||
from django.core.exceptions import ValidationError, ObjectDoesNotExist
|
||||
from django.db import transaction
|
||||
from django.core.urlresolvers import reverse
|
||||
from django.utils import timezone
|
||||
|
||||
from core.models import User, MetaGroup, Group, SithFile
|
||||
from core.models import User, MetaGroup, Group, SithFile, RealGroup, Notification
|
||||
|
||||
|
||||
# Create your models here.
|
||||
@ -230,11 +230,18 @@ class Mailing(models.Model):
|
||||
"""
|
||||
club = models.ForeignKey(Club, verbose_name=_('Club'), related_name="mailings", null=False, blank=False)
|
||||
email = models.EmailField(_('Email address'), unique=True)
|
||||
is_moderated = models.BooleanField(_('is moderated'), default=False)
|
||||
moderator = models.ForeignKey(User, related_name="moderated_mailings", verbose_name=_("moderator"), null=True)
|
||||
|
||||
def clean(self):
|
||||
if '@' + settings.SITH_MAILING_DOMAIN not in self.email:
|
||||
raise ValidationError(_('Unothorized mailing domain'))
|
||||
super(Mailing, self).clean()
|
||||
if self.can_moderate(self.moderator):
|
||||
self.is_moderated = True
|
||||
else:
|
||||
self.moderator = None
|
||||
super(Mailing, self).clean()
|
||||
|
||||
def can_moderate(self, user):
|
||||
return user.is_root or user.is_in_group(settings.SITH_GROUP_COM_ADMIN_ID)
|
||||
|
||||
def is_owned_by(self, user):
|
||||
return user.is_in_group(self) or user.is_root or user.is_in_group(settings.SITH_GROUP_COM_ADMIN_ID)
|
||||
@ -256,6 +263,13 @@ class Mailing(models.Model):
|
||||
resp += sub.fetch_format()
|
||||
return resp
|
||||
|
||||
def save(self):
|
||||
if not self.is_moderated:
|
||||
for user in RealGroup.objects.filter(id=settings.SITH_GROUP_COM_ADMIN_ID).first().users.all():
|
||||
if not user.notifications.filter(type="MAILING_MODERATION", viewed=False).exists():
|
||||
Notification(user=user, url=reverse('com:mailing_admin'), type="MAILING_MODERATION").save()
|
||||
super(Mailing, self).save()
|
||||
|
||||
def __str__(self):
|
||||
return "%s - %s" % (self.club, self.email)
|
||||
|
||||
@ -274,10 +288,13 @@ class MailingSubscription(models.Model):
|
||||
def clean(self):
|
||||
if not self.user and not self.email:
|
||||
raise ValidationError(_("At least user or email is required"))
|
||||
if self.user and not self.email:
|
||||
self.email = self.user.email
|
||||
if MailingSubscription.objects.filter(mailing=self.mailing, email=self.email).exists():
|
||||
raise ValidationError(_("This email is already suscribed in this mailing"))
|
||||
try:
|
||||
if self.user and not self.email:
|
||||
self.email = self.user.email
|
||||
if MailingSubscription.objects.filter(mailing=self.mailing, email=self.email).exists():
|
||||
raise ValidationError(_("This email is already suscribed in this mailing"))
|
||||
except ObjectDoesNotExist:
|
||||
pass
|
||||
super(MailingSubscription, self).clean()
|
||||
|
||||
def is_owned_by(self, user):
|
||||
|
@ -7,9 +7,10 @@
|
||||
{% block content %}
|
||||
{% if has_objects %}
|
||||
|
||||
{% trans %}Remember : mailing lists need to be validated by the school to work, please inform us about any new mailing list created{% endtrans %}
|
||||
<b>{% trans %}Remember : mailing lists need to be moderated, if your new created list is not shown wait until moderation takes action{% endtrans %}</b>
|
||||
|
||||
{% for mailing in object_list %}
|
||||
{% if mailing.is_moderated %}
|
||||
<h2>{% trans %}Mailing{% endtrans %} {{ mailing.email }}
|
||||
{%- if user.is_owner(mailing) -%}
|
||||
<a href="{{ url('club:mailing_delete', mailing_id=mailing.id) }}"> - {% trans %}Delete{% endtrans %}</a>
|
||||
@ -33,6 +34,7 @@
|
||||
</tr>
|
||||
{% endfor %}
|
||||
</table>
|
||||
{% endif %}
|
||||
{% endfor %}
|
||||
|
||||
{% else %}
|
||||
|
@ -43,6 +43,7 @@ from core.views.forms import SelectDate, SelectDateTime
|
||||
from club.models import Club, Membership, Mailing, MailingSubscription
|
||||
from sith.settings import SITH_MAXIMUM_FREE_ROLE
|
||||
from counter.models import Selling, Counter
|
||||
from core.models import User
|
||||
|
||||
from django.conf import settings
|
||||
|
||||
@ -52,7 +53,7 @@ from django.conf import settings
|
||||
class MailingForm(forms.ModelForm):
|
||||
class Meta:
|
||||
model = Mailing
|
||||
fields = ('email', 'club')
|
||||
fields = ('email', 'club', 'moderator')
|
||||
|
||||
email = forms.CharField(
|
||||
label=_('Email address'),
|
||||
@ -66,11 +67,16 @@ class MailingForm(forms.ModelForm):
|
||||
|
||||
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
|
||||
super(MailingForm, self).__init__(*args, **kwargs)
|
||||
if club_id:
|
||||
self.fields['club'].queryset = Club.objects.filter(id=club_id)
|
||||
self.fields['club'].initial = club_id
|
||||
self.fields['club'].widget = forms.HiddenInput()
|
||||
if user_id >= 0:
|
||||
self.fields['moderator'].queryset = User.objects.filter(id=user_id)
|
||||
self.fields['moderator'].initial = user_id
|
||||
self.fields['moderator'].widget = forms.HiddenInput()
|
||||
|
||||
def clean(self):
|
||||
cleaned_data = super(MailingForm, self).clean()
|
||||
@ -85,11 +91,12 @@ class MailingSubscriptionForm(forms.ModelForm):
|
||||
fields = ('mailing', 'user', 'email')
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
kwargs.pop('user_id', None) # For standart interface
|
||||
club_id = kwargs.pop('club_id', None)
|
||||
super(MailingSubscriptionForm, self).__init__(*args, **kwargs)
|
||||
self.fields['email'].required = False
|
||||
if club_id:
|
||||
self.fields['mailing'].queryset = Mailing.objects.filter(club__id=club_id)
|
||||
self.fields['mailing'].queryset = Mailing.objects.filter(club__id=club_id, is_moderated=True)
|
||||
|
||||
user = AutoCompleteSelectField('users', label=_('User'), help_text=None, required=False)
|
||||
|
||||
@ -419,7 +426,7 @@ class ClubMailingView(ClubTabsMixin, ListView):
|
||||
if not self.authorized():
|
||||
raise PermissionDenied
|
||||
self.member_form = MailingSubscriptionForm(club_id=self.club.id)
|
||||
self.mailing_form = MailingForm(club_id=self.club.id)
|
||||
self.mailing_form = MailingForm(club_id=self.club.id, user_id=self.user.id)
|
||||
return super(ClubMailingView, self).dispatch(request, *args, **kwargs)
|
||||
|
||||
def post(self, request, *args, **kwargs):
|
||||
@ -464,6 +471,7 @@ class MailingGenericCreateView(CreateView, SingleObjectMixin):
|
||||
def get_form_kwargs(self):
|
||||
kwargs = super(MailingGenericCreateView, self).get_form_kwargs()
|
||||
kwargs['club_id'] = self.list_view.club.id
|
||||
kwargs['user_id'] = self.list_view.user.id
|
||||
return kwargs
|
||||
|
||||
def dispatch(self, request, *args, **kwargs):
|
||||
@ -481,13 +489,17 @@ class MailingDeleteView(CanEditMixin, DeleteView):
|
||||
model = Mailing
|
||||
template_name = 'core/delete_confirm.jinja'
|
||||
pk_url_kwarg = "mailing_id"
|
||||
redirect_page = None
|
||||
|
||||
def dispatch(self, request, *args, **kwargs):
|
||||
self.club_id = self.get_object().club.id
|
||||
return super(MailingDeleteView, self).dispatch(request, *args, **kwargs)
|
||||
|
||||
def get_success_url(self, **kwargs):
|
||||
return reverse_lazy('club:mailing', kwargs={'club_id': self.club_id})
|
||||
if self.redirect_page:
|
||||
return reverse_lazy(self.redirect_page)
|
||||
else:
|
||||
return reverse_lazy('club:mailing', kwargs={'club_id': self.club_id})
|
||||
|
||||
|
||||
class MailingSubscriptionDeleteView(CanEditMixin, DeleteView):
|
||||
|
Reference in New Issue
Block a user