From e29dcbd5e3a2e3eae6ffbe9638bad2180ee02eee Mon Sep 17 00:00:00 2001 From: Gabriel Detraz Date: Sat, 19 Aug 2017 00:01:06 +0200 Subject: [PATCH] Nettoyage du code (modelform et update) phase1 --- cotisations/forms.py | 18 +++--------------- machines/forms.py | 12 ++---------- machines/models.py | 8 +++----- users/forms.py | 21 +++------------------ users/models.py | 18 ++++++++++-------- 5 files changed, 21 insertions(+), 56 deletions(-) diff --git a/cotisations/forms.py b/cotisations/forms.py index 9bee0d2a..e3c38f7a 100644 --- a/cotisations/forms.py +++ b/cotisations/forms.py @@ -102,13 +102,9 @@ class ArticleForm(ModelForm): super(ArticleForm, self).__init__(*args, **kwargs) self.fields['name'].label = "Désignation de l'article" -class DelArticleForm(ModelForm): +class DelArticleForm(Form): articles = forms.ModelMultipleChoiceField(queryset=Article.objects.all(), label="Articles actuels", widget=forms.CheckboxSelectMultiple) - class Meta: - fields = ['articles'] - model = Article - class PaiementForm(ModelForm): class Meta: model = Paiement @@ -119,13 +115,9 @@ class PaiementForm(ModelForm): self.fields['moyen'].label = 'Moyen de paiement à ajouter' self.fields['type_paiement'].label = 'Type de paiement à ajouter' -class DelPaiementForm(ModelForm): +class DelPaiementForm(Form): paiements = forms.ModelMultipleChoiceField(queryset=Paiement.objects.all(), label="Moyens de paiement actuels", widget=forms.CheckboxSelectMultiple) - class Meta: - exclude = ['moyen'] - model = Paiement - class BanqueForm(ModelForm): class Meta: model = Banque @@ -135,9 +127,5 @@ class BanqueForm(ModelForm): super(BanqueForm, self).__init__(*args, **kwargs) self.fields['name'].label = 'Banque à ajouter' -class DelBanqueForm(ModelForm): +class DelBanqueForm(Form): banques = forms.ModelMultipleChoiceField(queryset=Banque.objects.all(), label="Banques actuelles", widget=forms.CheckboxSelectMultiple) - - class Meta: - exclude = ['name'] - model = Banque diff --git a/machines/forms.py b/machines/forms.py index 668f8ac3..64f4f3af 100644 --- a/machines/forms.py +++ b/machines/forms.py @@ -119,7 +119,7 @@ class DomainForm(AliasForm): kwargs['initial'] = initial super(DomainForm, self).__init__(*args, **kwargs) -class DelAliasForm(ModelForm): +class DelAliasForm(Form): alias = forms.ModelMultipleChoiceField(queryset=Domain.objects.all(), label="Alias actuels", widget=forms.CheckboxSelectMultiple) def __init__(self, *args, **kwargs): @@ -127,10 +127,6 @@ class DelAliasForm(ModelForm): super(DelAliasForm, self).__init__(*args, **kwargs) self.fields['alias'].queryset = Domain.objects.filter(cname__in=Domain.objects.filter(interface_parent=interface)) - class Meta: - exclude = ['interface_parent', 'name', 'extension', 'cname'] - model = Domain - class MachineTypeForm(ModelForm): class Meta: model = MachineType @@ -170,13 +166,9 @@ class ExtensionForm(ModelForm): self.fields['name'].label = 'Extension à ajouter' self.fields['origin'].label = 'Enregistrement A origin' -class DelExtensionForm(ModelForm): +class DelExtensionForm(Form): extensions = forms.ModelMultipleChoiceField(queryset=Extension.objects.all(), label="Extensions actuelles", widget=forms.CheckboxSelectMultiple) - class Meta: - exclude = ['name', 'need_infra', 'origin'] - model = Extension - class MxForm(ModelForm): class Meta: model = Mx diff --git a/machines/models.py b/machines/models.py index bc89e3c6..2207d1b6 100644 --- a/machines/models.py +++ b/machines/models.py @@ -281,17 +281,15 @@ class Service(models.Model): def ask_regen(self): """ Marque à True la demande de régénération pour un service x """ - for serv in Service_link.objects.filter(service=self).exclude(asked_regen=True): - serv.asked_regen = True - serv.save() + Service_link.objects.filter(service=self).exclude(asked_regen=True).update(asked_regen=True) + return def process_link(self, servers): """ Django ne peut créer lui meme les relations manytomany avec table intermediaire explicite""" for serv in servers.exclude(pk__in=Interface.objects.filter(service=self)): link = Service_link(service=self, server=serv) link.save() - for serv in Service_link.objects.filter(service=self).exclude(server__in=servers): - serv.delete() + Service_link.objects.filter(service=self).exclude(server__in=servers).delete() return def save(self, *args, **kwargs): diff --git a/users/forms.py b/users/forms.py index 335c5786..3bcde523 100644 --- a/users/forms.py +++ b/users/forms.py @@ -257,21 +257,12 @@ class NewListRightForm(ListRightForm): super(NewListRightForm, self).__init__(*args, **kwargs) self.fields['gid'].label = 'Gid, attention, cet attribut ne doit pas être modifié après création' -class DelListRightForm(ModelForm): +class DelListRightForm(Form): listrights = forms.ModelMultipleChoiceField(queryset=ListRight.objects.all(), label="Droits actuels", widget=forms.CheckboxSelectMultiple) - class Meta: - exclude = ['listright','gid','details'] - model = ListRight - -class DelSchoolForm(ModelForm): +class DelSchoolForm(Form): schools = forms.ModelMultipleChoiceField(queryset=School.objects.all(), label="Etablissements actuels", widget=forms.CheckboxSelectMultiple) - class Meta: - exclude = ['name'] - model = School - - class RightForm(ModelForm): def __init__(self, *args, **kwargs): super(RightForm, self).__init__(*args, **kwargs) @@ -283,19 +274,13 @@ class RightForm(ModelForm): fields = ['right'] -class DelRightForm(ModelForm): +class DelRightForm(Form): rights = forms.ModelMultipleChoiceField(queryset=Right.objects.all(), widget=forms.CheckboxSelectMultiple) def __init__(self, right, *args, **kwargs): super(DelRightForm, self).__init__(*args, **kwargs) self.fields['rights'].queryset = Right.objects.filter(right=right) - - class Meta: - model = Right - exclude = ['user', 'right'] - - class BanForm(ModelForm): def __init__(self, *args, **kwargs): super(BanForm, self).__init__(*args, **kwargs) diff --git a/users/models.py b/users/models.py index 41eb969a..e2f2c818 100644 --- a/users/models.py +++ b/users/models.py @@ -46,6 +46,8 @@ from cotisations.models import Cotisation, Facture, Paiement, Vente from machines.models import Interface, Machine from preferences.models import OptionalUser +now = timezone.now() + def remove_user_room(room): """ Déménage de force l'ancien locataire de la chambre """ try: @@ -93,17 +95,17 @@ def get_admin_right(): admin_right.save() return admin_right -def all_adherent(): - return User.objects.filter(facture__in=Facture.objects.filter(vente__in=Vente.objects.filter(cotisation__in=Cotisation.objects.filter(vente__in=Vente.objects.filter(facture__in=Facture.objects.all().exclude(valid=False))).filter(date_end__gt=timezone.now())))).distinct() +def all_adherent(search_time=now): + return User.objects.filter(facture__in=Facture.objects.filter(vente__in=Vente.objects.filter(cotisation__in=Cotisation.objects.filter(vente__in=Vente.objects.filter(facture__in=Facture.objects.all().exclude(valid=False))).filter(date_end__gt=search_time)))).distinct() -def all_baned(): - return User.objects.filter(ban__in=Ban.objects.filter(date_end__gt=timezone.now())).distinct() +def all_baned(search_time=now): + return User.objects.filter(ban__in=Ban.objects.filter(date_end__gt=search_time)).distinct() -def all_whitelisted(): - return User.objects.filter(whitelist__in=Whitelist.objects.filter(date_end__gt=timezone.now())).distinct() +def all_whitelisted(search_time=now): + return User.objects.filter(whitelist__in=Whitelist.objects.filter(date_end__gt=search_time)).distinct() -def all_has_access(): - return User.objects.filter(Q(state=User.STATE_ACTIVE) & ~Q(ban__in=Ban.objects.filter(date_end__gt=timezone.now())) & (Q(whitelist__in=Whitelist.objects.filter(date_end__gt=timezone.now())) | Q(facture__in=Facture.objects.filter(vente__in=Vente.objects.filter(cotisation__in=Cotisation.objects.filter(vente__in=Vente.objects.filter(facture__in=Facture.objects.all().exclude(valid=False))).filter(date_end__gt=timezone.now())))))).distinct() +def all_has_access(search_time=now): + return User.objects.filter(Q(state=User.STATE_ACTIVE) & ~Q(ban__in=Ban.objects.filter(date_end__gt=timezone.now())) & (Q(whitelist__in=Whitelist.objects.filter(date_end__gt=timezone.now())) | Q(facture__in=Facture.objects.filter(vente__in=Vente.objects.filter(cotisation__in=Cotisation.objects.filter(vente__in=Vente.objects.filter(facture__in=Facture.objects.all().exclude(valid=False))).filter(date_end__gt=search_time)))))).distinct() class UserManager(BaseUserManager): def _create_user(self, pseudo, name, surname, email, password=None, su=False):