From b5513db8ae3afd972aae4bf32004e0bb7c822518 Mon Sep 17 00:00:00 2001 From: Hugo LEVY-FALK Date: Thu, 26 Jul 2018 20:11:13 +0200 Subject: [PATCH] Suppression du dictionnaire de mort dans re2o/templatetags/acl.py --- re2o/templatetags/acl.py | 90 +++++++++++----------------------------- 1 file changed, 25 insertions(+), 65 deletions(-) diff --git a/re2o/templatetags/acl.py b/re2o/templatetags/acl.py index fccbea1d..269ea051 100644 --- a/re2o/templatetags/acl.py +++ b/re2o/templatetags/acl.py @@ -74,82 +74,42 @@ import sys from django import template from django.template.base import Node, NodeList +from django.contrib.contenttypes.models import ContentType -import cotisations -import machines -import preferences -import topologie -import users register = template.Library() -MODEL_NAME = { - # cotisations - 'Facture': cotisations.models.Facture, - 'Vente': cotisations.models.Vente, - 'Article': cotisations.models.Article, - 'Banque': cotisations.models.Banque, - 'Paiement': cotisations.models.Paiement, - 'Cotisation': cotisations.models.Cotisation, - # machines - 'Machine': machines.models.Machine, - 'MachineType': machines.models.MachineType, - 'IpType': machines.models.IpType, - 'Vlan': machines.models.Vlan, - 'Nas': machines.models.Nas, - 'SOA': machines.models.SOA, - 'Extension': machines.models.Extension, - 'Mx': machines.models.Mx, - 'Ns': machines.models.Ns, - 'Txt': machines.models.Txt, - 'Srv': machines.models.Srv, - 'Interface': machines.models.Interface, - 'Domain': machines.models.Domain, - 'IpList': machines.models.IpList, - 'Ipv6List': machines.models.Ipv6List, - 'machines.Service': machines.models.Service, - 'Service_link': machines.models.Service_link, - 'OuverturePortList': machines.models.OuverturePortList, - 'OuverturePort': machines.models.OuverturePort, - # preferences - 'OptionalUser': preferences.models.OptionalUser, - 'OptionalMachine': preferences.models.OptionalMachine, - 'OptionalTopologie': preferences.models.OptionalTopologie, - 'GeneralOption': preferences.models.GeneralOption, - 'preferences.Service': preferences.models.Service, - 'AssoOption': preferences.models.AssoOption, - 'MailMessageOption': preferences.models.MailMessageOption, - # topologie - 'Stack': topologie.models.Stack, - 'Switch': topologie.models.Switch, - 'AccessPoint': topologie.models.AccessPoint, - 'ModelSwitch': topologie.models.ModelSwitch, - 'ConstructorSwitch': topologie.models.ConstructorSwitch, - 'Port': topologie.models.Port, - 'Room': topologie.models.Room, - 'Building': topologie.models.Building, - 'SwitchBay': topologie.models.SwitchBay, - # users - 'User': users.models.User, - 'Adherent': users.models.Adherent, - 'Club': users.models.Club, - 'ServiceUser': users.models.ServiceUser, - 'School': users.models.School, - 'ListRight': users.models.ListRight, - 'ListShell': users.models.ListShell, - 'Ban': users.models.Ban, - 'Whitelist': users.models.Whitelist, -} - def get_model(model_name): """Retrieve the model object from its name""" + splitted = model_name.split('.') + if len(splitted) > 1: + try: + app_label, name = splitted + except ValueError: + raise template.TemplateSyntaxError( + "%r is an inconsistent model name" % model_name + ) + else: + app_label, name = None, splitted[0] try: - return MODEL_NAME[model_name] - except KeyError: + if app_label is not None: + content_type = ContentType.objects.get( + model=name, + app_label=app_label + ) + else: + content_type = ContentType.objects.get(model=name) + except ContentType.DoesNotExist: raise template.TemplateSyntaxError( "%r is not a valid model for an acl tag" % model_name ) + except ContentType.MultipleObjectsReturned: + raise template.TemplateSyntaxError( + "More than one model found for %r. Try with `app.model`." + % model_name + ) + return content_type.model_class() def get_callback(tag_name, obj=None):