mirror of
https://gitlab2.federez.net/re2o/re2o
synced 2024-11-27 07:02:26 +00:00
Fonctions mass_achivage pour objects multiples
This commit is contained in:
parent
706903f8c9
commit
e0194ba4e3
4 changed files with 95 additions and 35 deletions
|
@ -41,6 +41,8 @@ from django.db.models.signals import post_save, post_delete
|
|||
from django.dispatch import receiver
|
||||
from django.forms import ValidationError
|
||||
from django.utils import timezone
|
||||
from django.db import transaction
|
||||
from reversion import revisions as reversion
|
||||
from django.utils.functional import cached_property
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
from macaddress.fields import MACAddressField, default_dialect
|
||||
|
@ -1134,6 +1136,21 @@ class Interface(RevMixin, AclMixin, FieldPermissionModelMixin, models.Model):
|
|||
""" Sans commentaire, désassigne une ipv4"""
|
||||
self.ipv4 = None
|
||||
|
||||
@classmethod
|
||||
def mass_unassign_ipv4(cls, interface_list):
|
||||
"""Unassign ipv4 to multiple interfaces"""
|
||||
with transaction.atomic(), reversion.create_revision():
|
||||
interface_list.update(ipv4=None)
|
||||
reversion.set_comment(_("IPv4 unassigning"))
|
||||
|
||||
@classmethod
|
||||
def mass_assign_ipv4(cls, interface_list):
|
||||
for interface in interface_list:
|
||||
with transaction.atomic(), reversion.create_revision():
|
||||
interface.assign_ipv4()
|
||||
interface.save()
|
||||
reversion.set_comment(_("IPv4 assigning"))
|
||||
|
||||
def update_type(self):
|
||||
""" Lorsque le machinetype est changé de type d'ip, on réassigne"""
|
||||
self.clean()
|
||||
|
|
|
@ -297,6 +297,11 @@ class MassArchiveForm(forms.Form):
|
|||
du formulaire la date de depart avant laquelle archiver les
|
||||
users"""
|
||||
date = forms.DateTimeField(help_text='%d/%m/%y')
|
||||
full_archive = forms.BooleanField(
|
||||
label=_("Make a full archive operation ? (WARNING : CRITICAL OPERATION IF TRUE)"),
|
||||
initial=False,
|
||||
required=False
|
||||
)
|
||||
|
||||
def clean(self):
|
||||
cleaned_data = super(MassArchiveForm, self).clean()
|
||||
|
|
|
@ -527,36 +527,57 @@ class User(RevMixin, FieldPermissionModelMixin, AbstractBaseUser,
|
|||
)['total'] or 0
|
||||
return somme_credit - somme_debit
|
||||
|
||||
def user_interfaces(self, active=True, all_interfaces=False):
|
||||
@classmethod
|
||||
def users_interfaces(cls, users, active=True, all_interfaces=False):
|
||||
""" Renvoie toutes les interfaces dont les machines appartiennent à
|
||||
self. Par defaut ne prend que les interfaces actives"""
|
||||
if all_interfaces:
|
||||
return Interface.objects.filter(
|
||||
machine__in=Machine.objects.filter(user=self)
|
||||
machine__in=Machine.objects.filter(user__in=users)
|
||||
).select_related('domain__extension')
|
||||
else:
|
||||
return Interface.objects.filter(
|
||||
machine__in=Machine.objects.filter(user=self, active=active)
|
||||
machine__in=Machine.objects.filter(user__in=users, active=active)
|
||||
).select_related('domain__extension')
|
||||
|
||||
def user_interfaces(self, active=True, all_interfaces=False):
|
||||
""" Renvoie toutes les interfaces dont les machines appartiennent à
|
||||
self. Par defaut ne prend que les interfaces actives"""
|
||||
return self.users_interfaces([self], active=active, all_interfaces=all_interfaces)
|
||||
|
||||
def assign_ips(self):
|
||||
""" Assign une ipv4 aux machines d'un user """
|
||||
interfaces = self.user_interfaces()
|
||||
for interface in interfaces:
|
||||
if not interface.ipv4:
|
||||
with transaction.atomic(), reversion.create_revision():
|
||||
interface.assign_ipv4()
|
||||
Interface.mass_assign_ipv4(interfaces)
|
||||
reversion.set_comment(_("IPv4 assigning"))
|
||||
interface.save()
|
||||
|
||||
def unassign_ips(self):
|
||||
""" Désassigne les ipv4 aux machines de l'user"""
|
||||
interfaces = self.user_interfaces()
|
||||
for interface in interfaces:
|
||||
with transaction.atomic(), reversion.create_revision():
|
||||
interface.unassign_ipv4()
|
||||
Interface.mass_unassign_ipv4(interfaces)
|
||||
reversion.set_comment(_("IPv4 unassigning"))
|
||||
interface.save()
|
||||
|
||||
@classmethod
|
||||
def mass_unassign_ips(cls, users_list):
|
||||
interfaces = cls.users_interfaces(users_list)
|
||||
with transaction.atomic(), reversion.create_revision():
|
||||
Interface.mass_unassign_ipv4(interfaces)
|
||||
reversion.set_comment(_("IPv4 assigning"))
|
||||
|
||||
@classmethod
|
||||
def mass_disable_email(cls, queryset_users):
|
||||
"""Disable email account and redirection"""
|
||||
queryset_users.update(local_email_enabled=False)
|
||||
queryset_users.update(local_email_redirect=False)
|
||||
|
||||
@classmethod
|
||||
def mass_delete_data(cls, queryset_users):
|
||||
"""This users will be completely archived, so only keep mandatory data"""
|
||||
cls.mass_disable_email(queryset_users)
|
||||
Machine.objects.filter(user__in=queryset_users).delete()
|
||||
cls.ldap_delete_users(queryset_users)
|
||||
|
||||
def disable_email(self):
|
||||
"""Disable email account and redirection"""
|
||||
|
@ -565,9 +586,19 @@ class User(RevMixin, FieldPermissionModelMixin, AbstractBaseUser,
|
|||
|
||||
def delete_data(self):
|
||||
"""This user will be completely archived, so only keep mandatory data"""
|
||||
self.disabled_email()
|
||||
self.user_interfaces(all_interfaces=True).delete()
|
||||
self.ldap_del()
|
||||
self.disable_email()
|
||||
self.machine_set.all().delete()
|
||||
|
||||
@classmethod
|
||||
def mass_archive(cls, users_list):
|
||||
cls.mass_unassign_ips(users_list)
|
||||
users_list.update(state=User.STATE_ARCHIVE)
|
||||
|
||||
@classmethod
|
||||
def mass_full_archive(cls, users_list):
|
||||
cls.mass_unassign_ips(users_list)
|
||||
cls.mass_delete_data(users_list)
|
||||
users_list.update(state=User.STATE_FULL_ARCHIVE)
|
||||
|
||||
def archive(self):
|
||||
""" Filling the user; no more active"""
|
||||
|
@ -660,6 +691,11 @@ class User(RevMixin, FieldPermissionModelMixin, AbstractBaseUser,
|
|||
except LdapUser.DoesNotExist:
|
||||
pass
|
||||
|
||||
@classmethod
|
||||
def ldap_delete_users(cls, queryset_users):
|
||||
"""Delete multiple users in ldap"""
|
||||
LdapUser.objects.filter(name__in=list(queryset_users.values_list('pseudo', flat=True)))
|
||||
|
||||
def notif_inscription(self):
|
||||
""" Prend en argument un objet user, envoie un mail de bienvenue """
|
||||
template = loader.get_template('users/email_welcome')
|
||||
|
|
|
@ -776,26 +776,28 @@ def del_listright(request, instances):
|
|||
@can_change(User, 'state')
|
||||
def mass_archive(request):
|
||||
""" Permet l'archivage massif"""
|
||||
to_archive_date = MassArchiveForm(request.POST or None)
|
||||
pagination_number = GeneralOption.get_cached_value('pagination_number')
|
||||
to_archive_form = MassArchiveForm(request.POST or None)
|
||||
to_archive_list = []
|
||||
if to_archive_date.is_valid():
|
||||
date = to_archive_date.cleaned_data['date']
|
||||
to_archive_list = [user for user in
|
||||
User.objects.exclude(state=User.STATE_ARCHIVE)
|
||||
if not user.end_access()
|
||||
or user.end_access() < date]
|
||||
if to_archive_form.is_valid():
|
||||
date = to_archive_form.cleaned_data['date']
|
||||
full_archive = to_archive_form.cleaned_data['full_archive']
|
||||
to_archive_list = User.objects.exclude(id__in=all_has_access()).exclude(id__in=all_has_access(search_time=date)).exclude(state=User.STATE_NOT_YET_ACTIVE).exclude(state=User.STATE_FULL_ARCHIVE)
|
||||
if not full_archive:
|
||||
to_archive_list = to_archive_list.exclude(state=User.STATE_ARCHIVE)
|
||||
number_to_archive = to_archive_list.count()
|
||||
if "valider" in request.POST:
|
||||
for user in to_archive_list:
|
||||
with transaction.atomic(), reversion.create_revision():
|
||||
user.archive()
|
||||
user.save()
|
||||
reversion.set_comment(_("Archiving"))
|
||||
messages.success(request, _("%s users were archived.") % len(
|
||||
to_archive_list
|
||||
))
|
||||
if full_archive:
|
||||
User.mass_full_archive(to_archive_list)
|
||||
else:
|
||||
User.mass_archive(to_archive_list)
|
||||
messages.success(request, _("%s users were archived.") %
|
||||
number_to_archive
|
||||
)
|
||||
return redirect(reverse('users:index'))
|
||||
to_archive_list = re2o_paginator(request, to_archive_list, pagination_number)
|
||||
return form(
|
||||
{'userform': to_archive_date, 'to_archive_list': to_archive_list},
|
||||
{'userform': to_archive_form, 'to_archive_list': to_archive_list},
|
||||
'users/mass_archive.html',
|
||||
request
|
||||
)
|
||||
|
|
Loading…
Reference in a new issue