From 6f9932add4ed003c6fc62cc120cf31eaf6242072 Mon Sep 17 00:00:00 2001 From: chibrac Date: Mon, 26 Jun 2017 01:29:34 +0200 Subject: [PATCH] =?UTF-8?q?Les=20limites=20de=20pagination=20et=20de=20cr?= =?UTF-8?q?=C3=A9ation=20machines=20pour=20les=20users=20sont=20g=C3=A9r?= =?UTF-8?q?=C3=A9es=20dans=20pr=C3=A9f=C3=A9rences?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cotisations/views.py | 15 +++++++++++---- logs/views.py | 7 ++++--- machines/views.py | 28 +++++++++++++++++++--------- re2o/settings.py | 4 ---- search/views.py | 9 +++++---- topologie/views.py | 7 +++++-- users/models.py | 8 ++++++++ users/views.py | 15 +++++++++++---- 8 files changed, 63 insertions(+), 30 deletions(-) diff --git a/cotisations/views.py b/cotisations/views.py index 1437bcab..95ceff86 100644 --- a/cotisations/views.py +++ b/cotisations/views.py @@ -41,8 +41,9 @@ from .models import Facture, Article, Vente, Cotisation, Paiement, Banque from .forms import NewFactureForm, TrezEditFactureForm, EditFactureForm, ArticleForm, DelArticleForm, PaiementForm, DelPaiementForm, BanqueForm, DelBanqueForm, NewFactureFormPdf, SelectArticleForm from users.models import User from .tex import render_tex -from re2o.settings import ASSO_NAME, ASSO_ADDRESS_LINE1, ASSO_ADDRESS_LINE2, ASSO_SIRET, ASSO_EMAIL, ASSO_PHONE, LOGO_PATH, PAGINATION_NUMBER +from re2o.settings import ASSO_NAME, ASSO_ADDRESS_LINE1, ASSO_ADDRESS_LINE2, ASSO_SIRET, ASSO_EMAIL, ASSO_PHONE, LOGO_PATH from re2o import settings +from preferences.models import GeneralOption from dateutil.relativedelta import relativedelta from django.utils import timezone @@ -339,9 +340,11 @@ def del_banque(request): @login_required @permission_required('trésorier') def control(request): + options, created = GeneralOption.objects.get_or_create() + pagination_number = options.pagination_number facture_list = Facture.objects.order_by('date').reverse() controlform_set = modelformset_factory(Facture, fields=('control','valid'), extra=0) - paginator = Paginator(facture_list, PAGINATION_NUMBER) + paginator = Paginator(facture_list, pagination_number) page = request.GET.get('page') try: facture_list = paginator.page(page) @@ -380,8 +383,10 @@ def index_banque(request): @login_required @permission_required('cableur') def index(request): + options, created = GeneralOption.objects.get_or_create() + pagination_number = options.pagination_number facture_list = Facture.objects.order_by('date').select_related('user').select_related('paiement').prefetch_related('vente_set').reverse() - paginator = Paginator(facture_list, PAGINATION_NUMBER) + paginator = Paginator(facture_list, pagination_number) page = request.GET.get('page') try: facture_list = paginator.page(page) @@ -425,8 +430,10 @@ def history(request, object, id): else: messages.error(request, "Objet inconnu") return redirect("/cotisations/") + options, created = GeneralOption.objects.get_or_create() + pagination_number = options.pagination_number reversions = Version.objects.get_for_object(object_instance) - paginator = Paginator(reversions, PAGINATION_NUMBER) + paginator = Paginator(reversions, pagination_number) page = request.GET.get('page') try: reversions = paginator.page(page) diff --git a/logs/views.py b/logs/views.py index f16e9807..b20f1b5f 100644 --- a/logs/views.py +++ b/logs/views.py @@ -45,8 +45,7 @@ from cotisations.models import Facture, Vente, Article, Banque, Paiement, Cotisa from machines.models import Machine, MachineType, IpType, Extension, Interface, Domain, IpList from machines.views import all_active_assigned_interfaces_count, all_active_interfaces_count from topologie.models import Switch, Port, Room - -from re2o.settings import PAGINATION_NUMBER, PAGINATION_LARGE_NUMBER +from preferences.models import GeneralOption from django.utils import timezone from dateutil.relativedelta import relativedelta @@ -68,8 +67,10 @@ def form(ctx, template, request): @login_required @permission_required('cableur') def index(request): + options, created = GeneralOption.objects.get_or_create() + pagination_number = options.pagination_number revisions = Revision.objects.all().order_by('date_created').reverse().select_related('user').prefetch_related('version_set__object') - paginator = Paginator(revisions, PAGINATION_NUMBER) + paginator = Paginator(revisions, pagination_number) page = request.GET.get('page') try: revisions = paginator.page(page) diff --git a/machines/views.py b/machines/views.py index c8ed1155..c3778a21 100644 --- a/machines/views.py +++ b/machines/views.py @@ -48,7 +48,7 @@ from .forms import IpTypeForm, DelIpTypeForm, AliasForm, DelAliasForm, NsForm, D from .models import IpType, Machine, Interface, IpList, MachineType, Extension, Mx, Ns, Domain from users.models import User from users.models import all_has_access -from re2o.settings import PAGINATION_NUMBER, PAGINATION_LARGE_NUMBER, MAX_INTERFACES, MAX_ALIAS +from preferences.models import GeneralOption, OptionalMachine def full_domain_validator(request, domain): """ Validation du nom de domaine, extensions dans type de machine, prefixe pas plus long que 63 caractères """ @@ -129,12 +129,14 @@ def new_machine(request, userid): except User.DoesNotExist: messages.error(request, u"Utilisateur inexistant" ) return redirect("/machines/") + options, created = OptionalMachine.objects.get_or_create() + max_lambdauser_interfaces = options.max_lambdauser_interfaces if not request.user.has_perms(('cableur',)): if user != request.user: messages.error(request, "Vous ne pouvez pas ajouter une machine à un autre user que vous sans droit") return redirect("/users/profil/" + str(request.user.id)) - if user.user_interfaces().count() >= MAX_INTERFACES: - messages.error(request, "Vous avez atteint le maximum d'interfaces autorisées que vous pouvez créer vous même (%s) " % MAX_INTERFACES) + if user.user_interfaces().count() >= max_lambdauser_interfaces: + messages.error(request, "Vous avez atteint le maximum d'interfaces autorisées que vous pouvez créer vous même (%s) " % max_lambdauser_interfaces) return redirect("/users/profil/" + str(request.user.id)) machine = NewMachineForm(request.POST or None) interface = AddInterfaceForm(request.POST or None, infra=request.user.has_perms(('infra',))) @@ -241,11 +243,13 @@ def new_interface(request, machineid): messages.error(request, u"Machine inexistante" ) return redirect("/machines") if not request.user.has_perms(('cableur',)): + options, created = OptionalMachine.objects.get_or_create() + max_lambdauser_interfaces = options.max_lambdauser_interfaces if machine.user != request.user: messages.error(request, "Vous ne pouvez pas ajouter une interface à une machine d'un autre user que vous sans droit") return redirect("/users/profil/" + str(request.user.id)) - if machine.user.user_interfaces().count() >= MAX_INTERFACES: - messages.error(request, "Vous avez atteint le maximum d'interfaces autorisées que vous pouvez créer vous même (%s) " % MAX_INTERFACES) + if machine.user.user_interfaces().count() >= max_lambdauser_interfaces: + messages.error(request, "Vous avez atteint le maximum d'interfaces autorisées que vous pouvez créer vous même (%s) " % max_lambdauser_interfaces) return redirect("/users/profil/" + str(request.user.id)) interface_form = AddInterfaceForm(request.POST or None, infra=request.user.has_perms(('infra',))) domain_form = AliasForm(request.POST or None, infra=request.user.has_perms(('infra',))) @@ -544,11 +548,13 @@ def add_alias(request, interfaceid): messages.error(request, u"Interface inexistante" ) return redirect("/machines") if not request.user.has_perms(('cableur',)): + options, created = OptionalMachine.objects.get_or_create() + max_lambdauser_aliases = options.max_lambdauser_aliases if interface.machine.user != request.user: messages.error(request, "Vous ne pouvez pas ajouter un alias à une machine d'un autre user que vous sans droit") return redirect("/users/profil/" + str(request.user.id)) - if Domain.objects.filter(cname__in=Domain.objects.filter(interface_parent__in=interface.machine.user.user_interfaces())).count() >= MAX_ALIAS: - messages.error(request, "Vous avez atteint le maximum d'alias autorisées que vous pouvez créer vous même (%s) " % MAX_ALIAS) + if Domain.objects.filter(cname__in=Domain.objects.filter(interface_parent__in=interface.machine.user.user_interfaces())).count() >= max_lambdauser_aliases: + messages.error(request, "Vous avez atteint le maximum d'alias autorisées que vous pouvez créer vous même (%s) " % max_lambdauser_aliases) return redirect("/users/profil/" + str(request.user.id)) alias = AliasForm(request.POST or None, infra=request.user.has_perms(('infra',))) if alias.is_valid(): @@ -609,8 +615,10 @@ def del_alias(request, interfaceid): @login_required @permission_required('cableur') def index(request): + options, created = GeneralOption.objects.get_or_create() + pagination_large_number = options.pagination_large_number machines_list = Machine.objects.select_related('user').prefetch_related('interface_set__domain__extension').prefetch_related('interface_set__ipv4__ip_type__extension').prefetch_related('interface_set__type').prefetch_related('interface_set__domain__related_domain__extension').order_by('pk') - paginator = Paginator(machines_list, PAGINATION_LARGE_NUMBER) + paginator = Paginator(machines_list, pagination_large_number) page = request.GET.get('page') try: machines_list = paginator.page(page) @@ -717,8 +725,10 @@ def history(request, object, id): else: messages.error(request, "Objet inconnu") return redirect("/machines/") + options, created = GeneralOption.objects.get_or_create() + pagination_number = options.pagination_number reversions = Version.objects.get_for_object(object_instance) - paginator = Paginator(reversions, PAGINATION_NUMBER) + paginator = Paginator(reversions, pagination_number) page = request.GET.get('page') try: reversions = paginator.page(page) diff --git a/re2o/settings.py b/re2o/settings.py index 09d0278e..dd601baa 100644 --- a/re2o/settings.py +++ b/re2o/settings.py @@ -156,10 +156,6 @@ RIGHTS_LINK = { 'bofh' : ['bureau','trésorier'], } -PAGINATION_NUMBER = 25 - -PAGINATION_LARGE_NUMBER = 8 - GRAPH_MODELS = { 'all_applications': True, 'group_models': True, diff --git a/search/views.py b/search/views.py index 1642db38..fffd5a82 100644 --- a/search/views.py +++ b/search/views.py @@ -35,8 +35,7 @@ from machines.models import Machine, Interface from topologie.models import Port, Switch from cotisations.models import Facture from search.models import SearchForm, SearchFormPlus - -from re2o.settings import SEARCH_RESULT +from preferences.models import GeneralOption def form(ctx, template, request): c = ctx @@ -106,12 +105,14 @@ def search_result(search, type, request): recherche['switch_list'] = Switch.objects.filter(details__icontains = search) if not request.user.has_perms(('cableur',)): recherche['switch_list'] = None + options, created = GeneralOption.objects.get_or_create() + search_display_page = options.search_display_page for r in recherche: if recherche[r] != None: - recherche[r] = recherche[r][:SEARCH_RESULT] + recherche[r] = recherche[r][:search_display_page] - recherche.update({'max_result': SEARCH_RESULT}) + recherche.update({'max_result': search_display_page}) return recherche diff --git a/topologie/views.py b/topologie/views.py index 5ee5f003..d7b4a1b8 100644 --- a/topologie/views.py +++ b/topologie/views.py @@ -36,8 +36,9 @@ from users.models import User from machines.forms import AliasForm, NewMachineForm, EditMachineForm, EditInterfaceForm, AddInterfaceForm from machines.views import free_ip, full_domain_validator, assign_ipv4 +from preferences.models import GeneralOption -from re2o.settings import ASSO_PSEUDO, PAGINATION_NUMBER +from re2o.settings import ASSO_PSEUDO @login_required @permission_required('cableur') @@ -69,8 +70,10 @@ def history(request, object, id): else: messages.error(request, "Objet inconnu") return redirect("/topologie/") + options, created = GeneralOption.objects.get_or_create() + pagination_number = options.pagination_number reversions = Version.objects.get_for_object(object_instance) - paginator = Paginator(reversions, PAGINATION_NUMBER) + paginator = Paginator(reversions, pagination_number) page = request.GET.get('page') try: reversions = paginator.page(page) diff --git a/users/models.py b/users/models.py index 4fd4aa98..fc617e65 100644 --- a/users/models.py +++ b/users/models.py @@ -42,6 +42,7 @@ from django.core.validators import MinLengthValidator from topologie.models import Room from cotisations.models import Cotisation, Facture, Vente from machines.models import Interface, Machine +from preferences.models import OptionalUser def remove_user_room(room): """ Déménage de force l'ancien locataire de la chambre """ @@ -687,6 +688,13 @@ class BaseInfoForm(ModelForm): 'telephone', ] + def clean_telephone(self): + telephone = self.cleaned_data['telephone'] + preferences, created = OptionalUser.objects.get_or_create() + if not telephone and preferences.is_tel_mandatory: + raise forms.ValidationError("Un numéro de téléphone valide est requis") + return telephone + class EditInfoForm(BaseInfoForm): class Meta(BaseInfoForm.Meta): fields = [ diff --git a/users/views.py b/users/views.py index 708b7f8e..aaf83d42 100644 --- a/users/views.py +++ b/users/views.py @@ -45,9 +45,10 @@ from cotisations.models import Facture from machines.models import Machine, Interface from users.forms import MassArchiveForm, PassForm, ResetPasswordForm from machines.views import unassign_ips, assign_ips +from preferences.models import GeneralOption from re2o.login import hashNT -from re2o.settings import REQ_EXPIRE_STR, EMAIL_FROM, ASSO_NAME, ASSO_EMAIL, SITE_NAME, PAGINATION_NUMBER +from re2o.settings import REQ_EXPIRE_STR, EMAIL_FROM, ASSO_NAME, ASSO_EMAIL, SITE_NAME def archive(user): """ Archive un utilisateur """ @@ -534,8 +535,10 @@ def mass_archive(request): @permission_required('cableur') def index(request): """ Affiche l'ensemble des users, need droit cableur """ + options, created = GeneralOption.objects.get_or_create() + pagination_number = options.pagination_number users_list = User.objects.select_related('room').order_by('state', 'name') - paginator = Paginator(users_list, PAGINATION_NUMBER) + paginator = Paginator(users_list, pagination_number) page = request.GET.get('page') try: users_list = paginator.page(page) @@ -551,8 +554,10 @@ def index(request): @permission_required('cableur') def index_ban(request): """ Affiche l'ensemble des ban, need droit cableur """ + options, created = GeneralOption.objects.get_or_create() + pagination_number = options.pagination_number ban_list = Ban.objects.order_by('date_start').select_related('user').reverse() - paginator = Paginator(ban_list, PAGINATION_NUMBER) + paginator = Paginator(ban_list, pagination_number) page = request.GET.get('page') try: ban_list = paginator.page(page) @@ -652,8 +657,10 @@ def history(request, object, id): else: messages.error(request, "Objet inconnu") return redirect("/users/") + options, created = GeneralOption.objects.get_or_create() + pagination_number = options.pagination_number reversions = Version.objects.get_for_object(object_instance) - paginator = Paginator(reversions, PAGINATION_NUMBER) + paginator = Paginator(reversions, pagination_number) page = request.GET.get('page') try: reversions = paginator.page(page)