8
0
Fork 0
mirror of https://gitlab2.federez.net/re2o/re2o synced 2024-12-23 15:33:45 +00:00

Optimisation chargement des variables de conf et ipv6

This commit is contained in:
Gabriel Detraz 2018-01-30 23:07:43 +01:00 committed by root
parent 1582d286a3
commit fc85e31729
16 changed files with 142 additions and 91 deletions

View file

@ -296,9 +296,8 @@ class RechargeForm(Form):
def clean_value(self):
value = self.cleaned_data['value']
options, _created = OptionalUser.objects.get_or_create()
if value < options.min_online_payment:
raise forms.ValidationError("Montant inférieur au montant minimal de paiement en ligne (%s) €" % options.min_online_payment)
if value + self.user.solde > options.max_solde:
raise forms.ValidationError("Le solde ne peux excéder %s " % options.max_solde)
if value < OptionalUser.get_cached_value('min_online_payment'):
raise forms.ValidationError("Montant inférieur au montant minimal de paiement en ligne (%s) €" % OptionalUser.get_cached_value('min_online_payment'))
if value + self.user.solde > OptionalUser.get_cached_value('max_solde'):
raise forms.ValidationError("Le solde ne peux excéder %s " % OptionalUser.get_cached_value('max_solde'))
return value

View file

@ -106,9 +106,8 @@ def new_facture(request, user, userid):
articles = article_formset
# Si au moins un article est rempli
if any(art.cleaned_data for art in articles):
options, _created = OptionalUser.objects.get_or_create()
user_solde = options.user_solde
solde_negatif = options.solde_negatif
user_solde = OptionalUser.get_cached_value('user_solde')
solde_negatif = OptionalUser.get_cached_value('solde_negatif')
# Si on paye par solde, que l'option est activée,
# on vérifie que le négatif n'est pas atteint
if user_solde:
@ -498,8 +497,7 @@ def del_banque(request, instances):
def control(request):
"""Pour le trésorier, vue pour controler en masse les
factures.Case à cocher, pratique"""
options, _created = GeneralOption.objects.get_or_create()
pagination_number = options.pagination_number
pagination_number = GeneralOption.get_cached_value('pagination_number')
facture_list = Facture.objects.select_related('user').select_related('paiement')
facture_list = SortTable.sort(
facture_list,
@ -567,8 +565,7 @@ def index_banque(request):
@can_view_all(Facture)
def index(request):
"""Affiche l'ensemble des factures, pour les cableurs et +"""
options, _created = GeneralOption.objects.get_or_create()
pagination_number = options.pagination_number
pagination_number = GeneralOption.get_cached_value('pagination_number')
facture_list = Facture.objects.select_related('user')\
.select_related('paiement').prefetch_related('vente_set')
facture_list = SortTable.sort(
@ -617,9 +614,8 @@ def new_facture_solde(request, userid):
articles = article_formset
# Si au moins un article est rempli
if any(art.cleaned_data for art in articles):
options, _created = OptionalUser.objects.get_or_create()
user_solde = options.user_solde
solde_negatif = options.solde_negatif
user_solde = OptionalUser.get_cached_value('user_solde')
solde_negatif = OptionalUser.get_cached_value('solde_negatif')
# Si on paye par solde, que l'option est activée,
# on vérifie que le négatif n'est pas atteint
if user_solde:

View file

@ -121,8 +121,7 @@ STATS_DICT = {
def index(request):
"""Affiche les logs affinés, date reformatées, selectionne
les event importants (ajout de droits, ajout de ban/whitelist)"""
options, _created = GeneralOption.objects.get_or_create()
pagination_number = options.pagination_number
pagination_number = GeneralOption.get_cached_value('pagination_number')
# The types of content kept for display
content_type_filter = ['ban', 'whitelist', 'vente', 'interface', 'user']
# Select only wanted versions
@ -180,8 +179,7 @@ def index(request):
def stats_logs(request):
"""Affiche l'ensemble des logs et des modifications sur les objets,
classés par date croissante, en vrac"""
options, _created = GeneralOption.objects.get_or_create()
pagination_number = options.pagination_number
pagination_number = GeneralOption.get_cached_value('pagination_number')
revisions = Revision.objects.all().select_related('user')\
.prefetch_related('version_set__object')
revisions = SortTable.sort(

View file

@ -0,0 +1,21 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.10.7 on 2018-01-30 15:23
from __future__ import unicode_literals
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('machines', '0075_auto_20180130_0052'),
]
operations = [
migrations.AlterField(
model_name='ipv6list',
name='interface',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='ipv6list', to='machines.Interface'),
),
]

View file

@ -94,8 +94,7 @@ class Machine(FieldPermissionModelMixin, models.Model):
user = users.models.User.objects.get(pk=userid)
except users.models.User.DoesNotExist:
return False, u"Utilisateur inexistant"
options, created = preferences.models.OptionalMachine.objects.get_or_create()
max_lambdauser_interfaces = options.max_lambdauser_interfaces
max_lambdauser_interfaces = preferences.models.OptionalMachine.get_cached_value('max_lambdauser_interfaces')
if not user_request.has_perm('machines.add_machine'):
if user != user_request:
return False, u"Vous ne pouvez pas ajouter une machine à un\
@ -1244,7 +1243,6 @@ class Interface(FieldPermissionModelMixin,models.Model):
def sync_ipv6(self):
"""Cree et met à jour l'ensemble des ipv6 en fonction du mode choisi"""
machine_options, _created = preferences.models.OptionalMachine.objects.get_or_create()
if machine_options.ipv6_mode == 'SLAAC':
self.sync_ipv6_slaac()
elif machine_options.ipv6_mode == 'DHCPV6':
@ -1255,11 +1253,10 @@ class Interface(FieldPermissionModelMixin,models.Model):
def ipv6(self):
""" Renvoie le queryset de la liste des ipv6
On renvoie l'ipv6 slaac que si le mode slaac est activé (et non dhcpv6)"""
machine_options, _created = preferences.models.OptionalMachine.objects.get_or_create()
if machine_options.ipv6_mode == 'SLAAC':
return Ipv6List.objects.filter(interface=self)
elif machine_options.ipv6_mode == 'DHCPV6':
return Ipv6List.objects.filter(interface=self, slaac_ip=False)
if preferences.models.OptionalMachine.get_cached_value('ipv6_mode') == 'SLAAC':
return self.ipv6list.all()
elif preferences.models.OptionalMachine.get_cached_value('ipv6_mode') == 'DHCPV6':
return self.ipv6list.filter(slaac_ip=False)
else:
return None
@ -1338,8 +1335,7 @@ class Interface(FieldPermissionModelMixin,models.Model):
except Machine.DoesNotExist:
return False, u"Machine inexistante"
if not user_request.has_perm('machines.add_interface'):
options, created = preferences.models.OptionalMachine.objects.get_or_create()
max_lambdauser_interfaces = options.max_lambdauser_interfaces
max_lambdauser_interfaces = preferences.models.OptionalMachine.get_cached_value('max_lambdauser_interfaces')
if machine.user != user_request:
return False, u"Vous ne pouvez pas ajouter une interface à une\
machine d'un autre user que vous sans droit"
@ -1432,7 +1428,7 @@ class Ipv6List(FieldPermissionModelMixin, models.Model):
protocol='IPv6',
unique=True
)
interface = models.ForeignKey('Interface', on_delete=models.CASCADE)
interface = models.ForeignKey('Interface', on_delete=models.CASCADE, related_name='ipv6list')
slaac_ip = models.BooleanField(default=False)
class Meta:
@ -1655,8 +1651,7 @@ class Domain(models.Model):
except Interface.DoesNotExist:
return False, u"Interface inexistante"
if not user_request.has_perm('machines.add_domain'):
options, created = preferences.models.OptionalMachine.objects.get_or_create()
max_lambdauser_aliases = options.max_lambdauser_aliases
max_lambdauser_aliases = preferences.models.OptionalMachine.get_cached_value('max_lambdauser_aliases')
if interface.machine.user != user_request:
return False, u"Vous ne pouvez pas ajouter un alias à une\
machine d'un autre user que vous sans droit"

View file

@ -981,8 +981,7 @@ def del_nas(request, instances):
@login_required
@can_view_all(Machine)
def index(request):
options, created = GeneralOption.objects.get_or_create()
pagination_large_number = options.pagination_large_number
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 = SortTable.sort(
machines_list,
@ -1166,7 +1165,7 @@ def mac_ip_list(request):
@login_required
@permission_required('machines.serveur')
def full_mac_ip_list(request):
interfaces = all_active_assigned_interfaces()
interfaces = all_active_assigned_interfaces(full=True)
seria = FullInterfaceSerializer(interfaces, many=True)
return seria.data

View file

@ -31,6 +31,7 @@ import cotisations.models
import machines.models
from django.db.models.signals import post_save, post_delete
from django.dispatch import receiver
from django.core.cache import cache
from .aes_field import AESEncryptedField
@ -67,6 +68,20 @@ class OptionalUser(models.Model):
help_text="Un nouvel utilisateur peut se créer son compte sur re2o"
)
@classmethod
def set_in_cache(cls, key):
machine_options, _created = cls.objects.get_or_create()
value = getattr(machine_options, key)
cache.set('optionaluser_' + key, value, None)
return value
@classmethod
def get_cached_value(cls, key):
value = cache.get('optionaluser_' + key)
if value == None:
value = cls.set_in_cache(key)
return value
class Meta:
permissions = (
("view_optionaluser", "Peut voir les options de l'user"),
@ -158,7 +173,21 @@ class OptionalMachine(models.Model):
@cached_property
def ipv6(self):
return not self.ipv6_mode == 'DISABLED'
return not self.get_cached_value('ipv6_mode') == 'DISABLED'
@classmethod
def set_in_cache(cls, key):
machine_options, _created = cls.objects.get_or_create()
value = getattr(machine_options, key)
cache.set('optionalmachine_' + key, value, None)
return value
@classmethod
def get_cached_value(cls, key):
value = cache.get('optionalmachine_' + key)
if value == None:
value = cls.set_in_cache(key)
return value
class Meta:
permissions = (
@ -220,9 +249,10 @@ class OptionalMachine(models.Model):
@receiver(post_save, sender=OptionalMachine)
def interface_post_save(sender, **kwargs):
def optionalmachine_post_save(sender, **kwargs):
"""Synchronisation ipv6"""
machine_pref = kwargs['instance']
if machine_pref.ipv6_mode != "DISABLED":
for interface in machines.models.Interface.objects.all():
interface.sync_ipv6()
@ -260,6 +290,20 @@ class OptionalTopologie(models.Model):
null=True
)
@classmethod
def set_in_cache(cls, key):
machine_options, _created = cls.objects.get_or_create()
value = getattr(machine_options, key)
cache.set('optionaltopologie_' + key, value, None)
return value
@classmethod
def get_cached_value(cls, key):
value = cache.get('optionaltopologie_' + key)
if value == None:
value = cls.set_in_cache(key)
return value
class Meta:
permissions = (
("view_optionaltopologie", "Peut voir les options de topologie"),
@ -345,6 +389,20 @@ class GeneralOption(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('generaloption_' + key, value, None)
return value
@classmethod
def get_cached_value(cls, key):
value = cache.get('generaloption_' + key)
if value == None:
value = cls.set_in_cache(key)
return value
class Meta:
permissions = (
("view_generaloption", "Peut voir les options générales"),

View file

@ -45,7 +45,6 @@ def can_create(model):
def decorator(view):
def wrapper(request, *args, **kwargs):
can, msg = model.can_create(request.user, *args, **kwargs)
#options, _created = OptionalUser.objects.get_or_create()
if not can:
messages.error(request, msg or "Vous ne pouvez pas accéder à ce menu")
return redirect(reverse('index'))

View file

@ -31,10 +31,8 @@ from preferences.models import GeneralOption, OptionalMachine
def context_user(request):
"""Fonction de context lorsqu'un user est logué (ou non),
renvoie les infos sur l'user, la liste de ses droits, ses machines"""
general_options, _created = GeneralOption.objects.get_or_create()
machine_options, _created = OptionalMachine.objects.get_or_create()
user = request.user
global_message = general_options.general_message
global_message = GeneralOption.get_cached_value('general_message')
if global_message:
messages.warning(request, global_message)
if user.is_authenticated():
@ -44,6 +42,6 @@ def context_user(request):
return {
'request_user': user,
'interfaces': interfaces,
'site_name': general_options.site_name,
'ipv6_enabled': machine_options.ipv6,
'site_name': GeneralOption.get_cached_value('site_name'),
'ipv6_enabled': OptionalMachine.get_cached_value('ipv6'),
}

View file

@ -121,15 +121,23 @@ def filter_active_interfaces(interface_set):
.distinct()
def all_active_interfaces():
def filter_complete_interfaces(interface_set):
"""Appel la fonction précédente avec un prefetch_related ipv6 en plus"""
return filter_active_interfaces(interface_set).prefetch_related('ipv6list')
def all_active_interfaces(full=False):
"""Renvoie l'ensemble des machines autorisées à sortir sur internet """
if full:
return filter_complete_interfaces(Interface.objects)
else:
return filter_active_interfaces(Interface.objects)
def all_active_assigned_interfaces():
def all_active_assigned_interfaces(full=False):
""" Renvoie l'ensemble des machines qui ont une ipv4 assignées et
disposant de l'accès internet"""
return all_active_interfaces().filter(ipv4__isnull=False)
return all_active_interfaces(full=full).filter(ipv4__isnull=False)
def all_active_interfaces_count():

View file

@ -136,8 +136,7 @@ def history(request, application, object_name, object_id):
'users:profil',
kwargs={'userid':str(request.user.id)}
))
options, _created = GeneralOption.objects.get_or_create()
pagination_number = options.pagination_number
pagination_number = GeneralOption.get_cached_value('pagination_number')
reversions = Version.objects.get_for_object(instance)
paginator = Paginator(reversions, pagination_number)
page = request.GET.get('page')

View file

@ -111,8 +111,7 @@ def finish_results(results, col, order):
SortTable.TOPOLOGIE_INDEX
)
options, _ = GeneralOption.objects.get_or_create()
max_result = options.search_display_page
max_result = GeneralOption.get_cached_value('search_display_page')
for name, val in results.items():
results[name] = val.distinct()[:max_result]
results.update({'max_result': max_result})

View file

@ -99,8 +99,7 @@ def index(request):
request.GET.get('order'),
SortTable.TOPOLOGIE_INDEX
)
options, _created = GeneralOption.objects.get_or_create()
pagination_number = options.pagination_number
pagination_number = GeneralOption.get_cached_value('pagination_number')
paginator = Paginator(switch_list, pagination_number)
page = request.GET.get('page')
try:
@ -153,8 +152,7 @@ def index_room(request):
request.GET.get('order'),
SortTable.TOPOLOGIE_INDEX_ROOM
)
options, _created = GeneralOption.objects.get_or_create()
pagination_number = options.pagination_number
pagination_number = GeneralOption.get_cached_value('pagination_number')
paginator = Paginator(room_list, pagination_number)
page = request.GET.get('page')
try:

View file

@ -289,8 +289,7 @@ class AdherentForm(FieldPermissionFormMixin, ModelForm):
"""Verifie que le tel est présent si 'option est validée
dans preferences"""
telephone = self.cleaned_data['telephone']
preferences, _created = OptionalUser.objects.get_or_create()
if not telephone and preferences.is_tel_mandatory:
if not telephone and OptionalUser.get_cached_value('is_tel_mandatory'):
raise forms.ValidationError(
"Un numéro de téléphone valide est requis"
)
@ -341,8 +340,7 @@ class ClubForm(FieldPermissionFormMixin, ModelForm):
"""Verifie que le tel est présent si 'option est validée
dans preferences"""
telephone = self.cleaned_data['telephone']
preferences, _created = OptionalUser.objects.get_or_create()
if not telephone and preferences.is_tel_mandatory:
if not telephone and OptionalUser.get_cached_value('is_tel_mandatory'):
raise forms.ValidationError(
"Un numéro de téléphone valide est requis"
)

View file

@ -406,8 +406,7 @@ class User(FieldPermissionModelMixin, AbstractBaseUser, PermissionsMixin):
""" Renvoie le solde d'un user. Vérifie que l'option solde est
activé, retourne 0 sinon.
Somme les crédits de solde et retire les débit payés par solde"""
options, _created = OptionalUser.objects.get_or_create()
user_solde = options.user_solde
user_solde = OptionalUser.get_cached_value('user_solde')
if user_solde:
solde_objects = Paiement.objects.filter(moyen='Solde')
somme_debit = Vente.objects.filter(
@ -534,7 +533,6 @@ class User(FieldPermissionModelMixin, AbstractBaseUser, PermissionsMixin):
assooptions, _created = AssoOption.objects.get_or_create()
mailmessageoptions, _created = MailMessageOption\
.objects.get_or_create()
general_options, _created = GeneralOption.objects.get_or_create()
context = Context({
'nom': self.get_full_name(),
'asso_name': assooptions.name,
@ -548,7 +546,7 @@ class User(FieldPermissionModelMixin, AbstractBaseUser, PermissionsMixin):
'name': assooptions.name
},
'',
general_options.email_from,
GeneralOption.get_cached_value('email_from'),
[self.email],
html_message=template.render(context)
)
@ -563,21 +561,20 @@ class User(FieldPermissionModelMixin, AbstractBaseUser, PermissionsMixin):
req.save()
template = loader.get_template('users/email_passwd_request')
options, _created = AssoOption.objects.get_or_create()
general_options, _created = GeneralOption.objects.get_or_create()
context = {
'name': req.user.get_full_name(),
'asso': options.name,
'asso_mail': options.contact,
'site_name': general_options.site_name,
'site_name': GeneralOption.get_cached_value('site_name'),
'url': request.build_absolute_uri(
reverse('users:process', kwargs={'token': req.token})),
'expire_in': str(general_options.req_expire_hrs) + ' heures',
'expire_in': str(GeneralOption.get_cached_value('req_expire_hrs')) + ' heures',
}
send_mail(
'Changement de mot de passe du %(name)s / Password\
renewal for %(name)s' % {'name': options.name},
template.render(context),
general_options.email_from,
GeneralOption.get_cached_value('email_from'),
[req.user.email],
fail_silently=False
)
@ -587,8 +584,7 @@ class User(FieldPermissionModelMixin, AbstractBaseUser, PermissionsMixin):
""" Fonction appellée par freeradius. Enregistre la mac pour
une machine inconnue sur le compte de l'user"""
all_interfaces = self.user_interfaces(active=False)
options, _created = OptionalMachine.objects.get_or_create()
if all_interfaces.count() > options.max_lambdauser_interfaces:
if all_interfaces.count() > OptionalMachine.get_cached_value('max_lambdauser_interfaces'):
return False, "Maximum de machines enregistrees atteinte"
if not nas_type:
return False, "Re2o ne sait pas à quel machinetype affecter cette\
@ -622,7 +618,6 @@ class User(FieldPermissionModelMixin, AbstractBaseUser, PermissionsMixin):
ajoutée par le radius"""
template = loader.get_template('users/email_auto_newmachine')
assooptions, _created = AssoOption.objects.get_or_create()
general_options, _created = GeneralOption.objects.get_or_create()
context = Context({
'nom': self.get_full_name(),
'mac_address' : interface.mac_address,
@ -634,7 +629,7 @@ class User(FieldPermissionModelMixin, AbstractBaseUser, PermissionsMixin):
send_mail(
"Ajout automatique d'une machine / New machine autoregistered",
'',
general_options.email_from,
GeneralOption.get_cached_value('email_from'),
[self.email],
html_message=template.render(context)
)
@ -683,11 +678,10 @@ class User(FieldPermissionModelMixin, AbstractBaseUser, PermissionsMixin):
:return: a message and a boolean which is True if the user can create
an user or if the `options.all_can_create` is set.
"""
options, _created = OptionalUser.objects.get_or_create()
if(not user_request.is_authenticated and not options.self_adhesion):
if(not user_request.is_authenticated and not OptionalUser.get_cached_value('self_adhesion')):
return False, None
else:
if(options.all_can_create or options.self_adhesion):
if(OptionalUser.get_cached_value('all_can_create') or OptionalUser.get_cached_value('self_adhesion')):
return True, None
else:
return user_request.has_perm('users.add_user'), u"Vous n'avez pas le\
@ -1265,7 +1259,6 @@ class Ban(models.Model):
def notif_ban(self):
""" Prend en argument un objet ban, envoie un mail de notification """
general_options, _created = GeneralOption.objects.get_or_create()
template = loader.get_template('users/email_ban_notif')
options, _created = AssoOption.objects.get_or_create()
context = Context({
@ -1277,7 +1270,7 @@ class Ban(models.Model):
send_mail(
'Deconnexion disciplinaire',
template.render(context),
general_options.email_from,
GeneralOption.get_cached_value('email_from'),
[self.user.email],
fail_silently=False
)
@ -1499,9 +1492,8 @@ class Request(models.Model):
def save(self):
if not self.expires_at:
options, _created = GeneralOption.objects.get_or_create()
self.expires_at = timezone.now() \
+ datetime.timedelta(hours=options.req_expire_hrs)
+ datetime.timedelta(hours=GeneralOption.get_cached_value('req_expire_hrs'))
if not self.token:
self.token = str(uuid.uuid4()).replace('-', '') # remove hyphens
super(Request, self).save()

View file

@ -122,9 +122,8 @@ def new_user(request):
""" Vue de création d'un nouvel utilisateur,
envoie un mail pour le mot de passe"""
user = AdherentForm(request.POST or None, user=request.user)
options, _created = GeneralOption.objects.get_or_create()
GTU_sum_up = options.GTU_sum_up
GTU = options.GTU
GTU_sum_up = GeneralOption.get_cached_value('GTU_sum_up')
GTU = GeneralOption.get_cached_value('GTU')
if user.is_valid():
user = user.save(commit=False)
with transaction.atomic(), reversion.create_revision():
@ -595,8 +594,7 @@ def mass_archive(request):
@can_view_all(Adherent)
def index(request):
""" Affiche l'ensemble des adherents, need droit cableur """
options, _created = GeneralOption.objects.get_or_create()
pagination_number = options.pagination_number
pagination_number = GeneralOption.get_cached_value('pagination_number')
users_list = Adherent.objects.select_related('room')
users_list = SortTable.sort(
users_list,
@ -621,8 +619,7 @@ def index(request):
@can_view_all(Club)
def index_clubs(request):
""" Affiche l'ensemble des clubs, need droit cableur """
options, _created = GeneralOption.objects.get_or_create()
pagination_number = options.pagination_number
pagination_number = GeneralOption.get_cached_value('pagination_number')
clubs_list = Club.objects.select_related('room')
clubs_list = SortTable.sort(
clubs_list,
@ -647,8 +644,7 @@ def index_clubs(request):
@can_view_all(Ban)
def index_ban(request):
""" Affiche l'ensemble des ban, need droit cableur """
options, _created = GeneralOption.objects.get_or_create()
pagination_number = options.pagination_number
pagination_number = GeneralOption.get_cached_value('pagination_number')
ban_list = Ban.objects.select_related('user')
ban_list = SortTable.sort(
ban_list,
@ -673,8 +669,7 @@ def index_ban(request):
@can_view_all(Whitelist)
def index_white(request):
""" Affiche l'ensemble des whitelist, need droit cableur """
options, _created = GeneralOption.objects.get_or_create()
pagination_number = options.pagination_number
pagination_number = GeneralOption.get_cached_value('pagination_number')
white_list = Whitelist.objects.select_related('user')
white_list = SortTable.sort(
white_list,
@ -780,8 +775,7 @@ def profil(request, users, userid):
request.GET.get('order'),
SortTable.USERS_INDEX_WHITE
)
options, _created = OptionalUser.objects.get_or_create()
user_solde = options.user_solde
user_solde = OptionalUser.get_cached_value('user_solde')
options, _created = AssoOption.objects.get_or_create()
allow_online_payment = options.payment != 'NONE'
return render(