8
0
Fork 0
mirror of https://gitlab2.federez.net/re2o/re2o synced 2024-11-26 06:32:26 +00:00

Proof of concept can_delete_all

This commit is contained in:
Gabriel Detraz 2017-12-12 04:33:50 +01:00 committed by root
parent b066f66316
commit d648e7d8b4
4 changed files with 63 additions and 20 deletions

View file

@ -123,6 +123,28 @@ def can_delete(model):
return decorator 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): def can_view(model):
"""Decorator to check if an user can view a model. """Decorator to check if an user can view a model.
It tries to get an instance of the model, using It tries to get an instance of the model, using

View file

@ -493,6 +493,11 @@ class DelSchoolForm(Form):
widget=forms.CheckboxSelectMultiple 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): class RightForm(ModelForm):
"""Assignation d'un droit à un user""" """Assignation d'un droit à un user"""
@ -531,14 +536,6 @@ class BanForm(ModelForm):
model = Ban model = Ban
exclude = ['user'] 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): class WhitelistForm(ModelForm):
"""Creation, edition d'un objet whitelist""" """Creation, edition d'un objet whitelist"""
@ -550,11 +547,3 @@ class WhitelistForm(ModelForm):
class Meta: class Meta:
model = Whitelist model = Whitelist
exclude = ['user'] 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

View file

@ -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

View file

@ -93,7 +93,7 @@ from preferences.models import OptionalUser, GeneralOption
from re2o.views import form from re2o.views import form
from re2o.utils import ( 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): def password_change_action(u_form, user, request, req=False):
@ -500,13 +500,13 @@ def edit_school(request, school_instance, schoolid):
@login_required @login_required
@permission_required('cableur') @all_can_delete(School)
def del_school(request): def del_school(request, instances):
""" Supprimer un établissement d'enseignement à la base de donnée, """ Supprimer un établissement d'enseignement à la base de donnée,
need cableur need cableur
Objet protégé, possible seulement si aucun user n'est affecté à Objet protégé, possible seulement si aucun user n'est affecté à
l'établissement """ l'établissement """
school = DelSchoolForm(request.POST or None) school = DelSchoolForm(request.POST or None, instances=instances)
if school.is_valid(): if school.is_valid():
school_dels = school.cleaned_data['schools'] school_dels = school.cleaned_data['schools']
for school_del in school_dels: for school_del in school_dels: