From a3c0a50f4ac6f8b26d258508e0ee44571e0d08f6 Mon Sep 17 00:00:00 2001 From: Gabriel Detraz Date: Wed, 31 Jan 2018 03:27:59 +0100 Subject: [PATCH] Optimisation sur assooption et divers --- cotisations/payment.py | 10 ++++------ cotisations/views.py | 31 ++++++++++++++----------------- machines/views.py | 2 +- preferences/models.py | 13 +++++++++++++ topologie/views.py | 2 +- users/models.py | 22 +++++++++------------- users/views.py | 3 +-- 7 files changed, 43 insertions(+), 40 deletions(-) diff --git a/cotisations/payment.py b/cotisations/payment.py index 07cbe5dc..f9a66bf6 100644 --- a/cotisations/payment.py +++ b/cotisations/payment.py @@ -38,7 +38,6 @@ def refuse_payment(request): @csrf_exempt def ipn(request): - option, _created = AssoOption.objects.get_or_create() p = ComnpayPayment() order = ('idTpe', 'idTransaction', 'montant', 'result', 'sec', ) try: @@ -46,7 +45,7 @@ def ipn(request): except MultiValueDictKeyError: return HttpResponseBadRequest("HTTP/1.1 400 Bad Request") - if not p.validSec(data, option.payment_pass): + if not p.validSec(data, AssoOption.get_cached_value('payment_pass')): return HttpResponseBadRequest("HTTP/1.1 400 Bad Request") result = True if (request.POST['result'] == 'OK') else False @@ -54,7 +53,7 @@ def ipn(request): idTransaction = request.POST['idTransaction'] # On vérifie que le paiement nous est destiné - if not idTpe == option.payment_id: + if not idTpe == AssoOption.get_cached_value('payment_id'): return HttpResponseBadRequest("HTTP/1.1 400 Bad Request") try: @@ -81,10 +80,9 @@ def ipn(request): def comnpay(facture, request): host = request.get_host() - option, _created = AssoOption.objects.get_or_create() p = ComnpayPayment( - str(option.payment_id), - str(option.payment_pass), + str(AssoOption.get_cached_value('payment_id')), + str(AssoOption.get_cached_value('payment_pass')), 'https://' + host + reverse( 'cotisations:accept_payment', kwargs={'factureid':facture.id} diff --git a/cotisations/views.py b/cotisations/views.py index bc0c586e..e5f929b2 100644 --- a/cotisations/views.py +++ b/cotisations/views.py @@ -180,7 +180,6 @@ def new_facture_pdf(request): Vente ou Facture correspondant en bdd""" facture_form = NewFactureFormPdf(request.POST or None) if facture_form.is_valid(): - options, _created = AssoOption.objects.get_or_create() tbl = [] article = facture_form.cleaned_data['article'] quantite = facture_form.cleaned_data['number'] @@ -199,12 +198,12 @@ def new_facture_pdf(request): 'article': tbl, 'total': prix_total, 'paid': paid, - 'asso_name': options.name, - 'line1': options.adresse1, - 'line2': options.adresse2, - 'siret': options.siret, - 'email': options.contact, - 'phone': options.telephone, + 'asso_name': AssoOption.get_cached_value('name'), + 'line1': AssoOption.get_cached_value('adresse1'), + 'line2': AssoOption.get_cached_value('adresse2'), + 'siret': AssoOption.get_cached_value('siret'), + 'email': AssoOption.get_cached_value('contact'), + 'phone': AssoOption.get_cached_value('telephone'), 'tpl_path': os.path.join(settings.BASE_DIR, LOGO_PATH) }) return form({ @@ -222,7 +221,6 @@ def facture_pdf(request, facture, factureid): ventes_objects = Vente.objects.all().filter(facture=facture) ventes = [] - options, _created = AssoOption.objects.get_or_create() for vente in ventes_objects: ventes.append([vente, vente.number, vente.prix_total]) return render_invoice(request, { @@ -232,12 +230,12 @@ def facture_pdf(request, facture, factureid): 'dest': facture.user, 'article': ventes, 'total': facture.prix_total(), - 'asso_name': options.name, - 'line1': options.adresse1, - 'line2': options.adresse2, - 'siret': options.siret, - 'email': options.contact, - 'phone': options.telephone, + 'asso_name': AssoOption.get_cached_value('name'), + 'line1': AssoOption.get_cached_value('adresse1'), + 'line2': AssoOption.get_cached_value('adresse2'), + 'siret': AssoOption.get_cached_value('siret'), + 'email': AssoOption.get_cached_value('contact'), + 'phone': AssoOption.get_cached_value('telephone'), 'tpl_path': os.path.join(settings.BASE_DIR, LOGO_PATH) }) @@ -683,8 +681,7 @@ def new_facture_solde(request, userid): @login_required def recharge(request): - options, _created = AssoOption.objects.get_or_create() - if options.payment == 'NONE': + if AssoOption.get_cached_value('payment') == 'NONE': messages.error( request, "Le paiement en ligne est désactivé." @@ -707,6 +704,6 @@ def recharge(request): number=1, ) v.save() - content = payment.PAYMENT_SYSTEM[options.payment](facture, request) + content = payment.PAYMENT_SYSTEM[AssoOption.get_cached_value('payment')](facture, request) return render(request, 'cotisations/payment.html', content) return form({'rechargeform':f}, 'cotisations/recharge.html', request) diff --git a/machines/views.py b/machines/views.py index c954766c..9208f3fb 100644 --- a/machines/views.py +++ b/machines/views.py @@ -982,7 +982,7 @@ def del_nas(request, instances): @can_view_all(Machine) def index(request): pagination_large_number = GeneralOption.get_cached_value('pagination_large_number') - machines_list = Machine.objects.select_related('user').prefetch_related('interface_set__domain__extension').prefetch_related('interface_set__ipv4__ip_type').prefetch_related('interface_set__type__ip_type__extension').prefetch_related('interface_set__domain__related_domain__extension') + machines_list = Machine.objects.select_related('user').prefetch_related('interface_set__domain__extension').prefetch_related('interface_set__ipv4__ip_type').prefetch_related('interface_set__type__ip_type__extension').prefetch_related('interface_set__domain__related_domain__extension').prefetch_related('interface_set__ipv6list') machines_list = SortTable.sort( machines_list, request.GET.get('col'), diff --git a/preferences/models.py b/preferences/models.py index 54de71b1..6bc101d1 100644 --- a/preferences/models.py +++ b/preferences/models.py @@ -571,6 +571,19 @@ class AssoOption(models.Model): blank=True, ) + @classmethod + def set_in_cache(cls, key): + machine_options, _created = cls.objects.get_or_create() + value = getattr(machine_options, key) + cache.set('assooption_' + key, value, None) + return value + + @classmethod + def get_cached_value(cls, key): + value = cache.get('assooption_' + key) + if value == None: + value = cls.set_in_cache(key) + return value class Meta: permissions = ( diff --git a/topologie/views.py b/topologie/views.py index 828488c8..cc129f42 100644 --- a/topologie/views.py +++ b/topologie/views.py @@ -190,7 +190,7 @@ def index_stack(request): @can_view_all(ConstructorSwitch) def index_model_switch(request): """ Affichage de l'ensemble des modèles de switches""" - model_switch_list = ModelSwitch.objects + model_switch_list = ModelSwitch.objects.select_related('constructor') constructor_switch_list = ConstructorSwitch.objects model_switch_list = SortTable.sort( model_switch_list, diff --git a/users/models.py b/users/models.py index 19fca2c3..c35910ab 100644 --- a/users/models.py +++ b/users/models.py @@ -530,20 +530,19 @@ class User(FieldPermissionModelMixin, AbstractBaseUser, PermissionsMixin): def notif_inscription(self): """ Prend en argument un objet user, envoie un mail de bienvenue """ template = loader.get_template('users/email_welcome') - assooptions, _created = AssoOption.objects.get_or_create() mailmessageoptions, _created = MailMessageOption\ .objects.get_or_create() context = Context({ 'nom': self.get_full_name(), - 'asso_name': assooptions.name, - 'asso_email': assooptions.contact, + 'asso_name': AssoOption.get_cached_value('name'), + 'asso_email': AssoOption.get_cached_value('contact'), 'welcome_mail_fr': mailmessageoptions.welcome_mail_fr, 'welcome_mail_en': mailmessageoptions.welcome_mail_en, 'pseudo': self.pseudo, }) send_mail( 'Bienvenue au %(name)s / Welcome to %(name)s' % { - 'name': assooptions.name + 'name': AssoOption.get_cached_value('name') }, '', GeneralOption.get_cached_value('email_from'), @@ -560,11 +559,10 @@ class User(FieldPermissionModelMixin, AbstractBaseUser, PermissionsMixin): req.user = self req.save() template = loader.get_template('users/email_passwd_request') - options, _created = AssoOption.objects.get_or_create() context = { 'name': req.user.get_full_name(), - 'asso': options.name, - 'asso_mail': options.contact, + 'asso': AssoOption.get_cached_value('name'), + 'asso_mail': AssoOption.get_cached_value('contact'), 'site_name': GeneralOption.get_cached_value('site_name'), 'url': request.build_absolute_uri( reverse('users:process', kwargs={'token': req.token})), @@ -572,7 +570,7 @@ class User(FieldPermissionModelMixin, AbstractBaseUser, PermissionsMixin): } send_mail( 'Changement de mot de passe du %(name)s / Password\ - renewal for %(name)s' % {'name': options.name}, + renewal for %(name)s' % {'name': AssoOption.get_cached_value('name')}, template.render(context), GeneralOption.get_cached_value('email_from'), [req.user.email], @@ -617,13 +615,12 @@ class User(FieldPermissionModelMixin, AbstractBaseUser, PermissionsMixin): """Notification mail lorsque une machine est automatiquement ajoutée par le radius""" template = loader.get_template('users/email_auto_newmachine') - assooptions, _created = AssoOption.objects.get_or_create() context = Context({ 'nom': self.get_full_name(), 'mac_address' : interface.mac_address, - 'asso_name': assooptions.name, + 'asso_name': AssoOption.get_cached_value('name'), 'interface_name' : interface.domain, - 'asso_email': assooptions.contact, + 'asso_email': AssoOption.get_cached_value('contact'), 'pseudo': self.pseudo, }) send_mail( @@ -1260,12 +1257,11 @@ class Ban(models.Model): def notif_ban(self): """ Prend en argument un objet ban, envoie un mail de notification """ template = loader.get_template('users/email_ban_notif') - options, _created = AssoOption.objects.get_or_create() context = Context({ 'name': self.user.get_full_name(), 'raison': self.raison, 'date_end': self.date_end, - 'asso_name': options.name, + 'asso_name': AssoOption.get_cached_value('name'), }) send_mail( 'Deconnexion disciplinaire', diff --git a/users/views.py b/users/views.py index 11da6843..9b4b818a 100644 --- a/users/views.py +++ b/users/views.py @@ -777,8 +777,7 @@ def profil(request, users, userid): SortTable.USERS_INDEX_WHITE ) user_solde = OptionalUser.get_cached_value('user_solde') - options, _created = AssoOption.objects.get_or_create() - allow_online_payment = options.payment != 'NONE' + allow_online_payment = AssoOption.get_cached_value('payment') != 'NONE' return render( request, 'users/profil.html',