Commit 6c3cc3a8 authored by Élie Bouttier's avatar Élie Bouttier

copie d’un service en cas de changement d’adhérent

parent 7f4155e0
......@@ -11,7 +11,7 @@ from django.core.serializers import serialize
from django.http import HttpResponse
from django.db.models.functions import Cast
from django.contrib.postgres.aggregates import StringAgg
from django.db import connection
from django.db import connection, transaction
from django.core.cache import cache
from django.contrib.humanize.templatetags.humanize import naturaltime
from django.contrib.contenttypes.models import ContentType
......@@ -347,6 +347,24 @@ class ServiceAdmin(admin.ModelAdmin):
readonly_fields = ('get_contribution_link', 'is_active',)
raw_id_fields = ('adhesion',)
def save_model(self, request, srv, form, change):
if 'adhesion' in form.changed_data:
with transaction.atomic():
old_srv = Service.objects.get(pk=srv.pk)
adhesion = srv.adhesion
srv.adhesion = old_srv.adhesion
label = srv.label
srv.label = '%s (transféré à ADT%d le %s)' % (srv.label, adhesion.pk, timezone.now().strftime('%d/%m/%Y'))
srv.save()
new_srv = Service.objects.create(adhesion=adhesion, service_type=srv.service_type, label=label,
notes=srv.notes, loan_equipment=srv.loan_equipment)
for allocation in srv.active_allocations:
allocation.end = timezone.now()
allocation.save()
ServiceAllocation.objects.create(resource=allocation.resource, service=new_srv, route=allocation.route)
else:
super().save_model(request, srv, form, change)
def get_queryset(self, request):
qs = super().get_queryset(request)
qs = qs.prefetch_related('allocations',)
......
from django.core.management.base import BaseCommand
from django.utils import timezone
from datetime import timedelta
from itertools import groupby
from services.models import ServiceAllocation, IPResource, IPResourceState
class Command(BaseCommand):
help = 'Détection des pannes'
def handle(self, *args, **options):
down = IPResource.objects.filter(
category=IPResource.CATEGORY_PUBLIC,
last_state__state=IPResourceState.STATE_DOWN,
last_time_up__gt=timezone.now()-timedelta(minutes=5))
allocations = ServiceAllocation.objects.filter(resource__pk__in=down)
#allocations = allocations.values('resource', 'route')
allocations = sorted(allocations, key=lambda a: a.route.pk)
for route, allocs in groupby(allocations, key=lambda a: a.route.pk):
allocs = list(allocs)
route = allocs[0].route
print("%s: %d" % (route, len(allocs)))
for alloc in allocs:
print("\t%s" % alloc.resource)
if len(allocs) >= 4:
logger.info('%s %s' % (route, allocs))
self.stdout.write(self.style.SUCCESS('Aucune panne détectée.'))
......@@ -166,6 +166,9 @@ class ServiceType(models.Model):
class Service(models.Model):
"""
En cas d’ajout de champs, penser à mettre à jour la méthode save_model de ServiceAdmin.
"""
adhesion = models.ForeignKey(Adhesion, verbose_name='Adhérent·e', related_name='services', on_delete=models.CASCADE)
service_type = models.ForeignKey(ServiceType, related_name='services',
verbose_name='Type de service', on_delete=models.PROTECT)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment