diff --git a/re2o/utils.py b/re2o/utils.py index f77c03a5..0deacc63 100644 --- a/re2o/utils.py +++ b/re2o/utils.py @@ -123,6 +123,28 @@ def can_delete(model): return decorator +def all_can_delete(model): + """Decorator which returns a list of detable models by request user. + If none of them, return an error""" + def decorator(view): + def wrapper(request, *args, **kwargs): + all_objects = model.objects.all() + instances_id = [] + for instance in all_objects: + can, msg = instance.can_delete(request.user) + if can: + instances_id.append(instance.id) + instances = model.objects.filter(id__in=instances_id) + if not instances: + messages.error(request, "Vous ne pouvez pas accéder à ce menu") + return redirect(reverse('users:profil', + kwargs={'userid':str(request.user.id)} + )) + return view(request, instances, *args, **kwargs) + return wrapper + return decorator + + def can_view(model): """Decorator to check if an user can view a model. It tries to get an instance of the model, using diff --git a/users/forms.py b/users/forms.py index a47c1436..a65255d5 100644 --- a/users/forms.py +++ b/users/forms.py @@ -493,6 +493,11 @@ class DelSchoolForm(Form): widget=forms.CheckboxSelectMultiple ) + def __init__(self, *args, **kwargs): + instances = kwargs.pop('instances') + super(DelSchoolForm, self).__init__(*args, **kwargs) + self.fields['schools'].queryset = instances + class RightForm(ModelForm): """Assignation d'un droit à un user""" @@ -531,14 +536,6 @@ class BanForm(ModelForm): model = Ban exclude = ['user'] - def clean_date_end(self): - """Verification que date_end est après now""" - date_end = self.cleaned_data['date_end'] - if date_end < NOW: - raise forms.ValidationError("Triple buse, la date de fin ne peut\ - pas être avant maintenant... Re2o ne voyage pas dans le temps") - return date_end - class WhitelistForm(ModelForm): """Creation, edition d'un objet whitelist""" @@ -550,11 +547,3 @@ class WhitelistForm(ModelForm): class Meta: model = Whitelist exclude = ['user'] - - def clean_date_end(self): - """Verification que la date_end est posterieur à now""" - date_end = self.cleaned_data['date_end'] - if date_end < NOW: - raise forms.ValidationError("Triple buse, la date de fin ne peut pas\ - être avant maintenant... Re2o ne voyage pas dans le temps") - return date_end diff --git a/users/management/commands/email.py b/users/management/commands/email.py new file mode 100644 index 00000000..a7518b9f --- /dev/null +++ b/users/management/commands/email.py @@ -0,0 +1,32 @@ +from django.core.management.base import BaseCommand, CommandError + +from datetime import datetime, timedelta +from pytz + +from users.models import User + +UTC = pytz.timezone('UTC') + +class Command(BaseCommand): + commands = ['email_remainder',] + args = '[command]' + help = 'Send email remainders' + + def handle(self, *args, **options): + ''' + Sends an email before the end of a user's subscription + ''' + users = User.objects.filter(state="STATE_ACTIVE") + + for user in users: + remaining = user.end_adhesion() - datetime.today(tz=UTC) + if (timedelta(weeks=4) - remaining).days == 1: + 4_weeks_reminder() + elif (timedelta(weeks=1) - remaining).days == 1: + week_reminder() + elif remaining.days == 1: + last_day_reminder() + +def month_reminder(): + pass + diff --git a/users/views.py b/users/views.py index a506e160..1a662ae0 100644 --- a/users/views.py +++ b/users/views.py @@ -93,7 +93,7 @@ from preferences.models import OptionalUser, GeneralOption from re2o.views import form from re2o.utils import ( - all_has_access, SortTable, can_create, can_edit, can_delete, can_view + all_has_access, SortTable, can_create, can_edit, all_can_delete, can_delete, can_view ) def password_change_action(u_form, user, request, req=False): @@ -500,13 +500,13 @@ def edit_school(request, school_instance, schoolid): @login_required -@permission_required('cableur') -def del_school(request): +@all_can_delete(School) +def del_school(request, instances): """ Supprimer un établissement d'enseignement à la base de donnée, need cableur Objet protégé, possible seulement si aucun user n'est affecté à l'établissement """ - school = DelSchoolForm(request.POST or None) + school = DelSchoolForm(request.POST or None, instances=instances) if school.is_valid(): school_dels = school.cleaned_data['schools'] for school_del in school_dels: