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(
adhesion = srv.adhesion
srv.adhesion = old_srv.adhesion
label = srv.label
srv.label = '%s (transféré à ADT%d le %s)' % (srv.label,,'%d/%m/%Y'))
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 =
ServiceAllocation.objects.create(resource=allocation.resource, service=new_srv, route=allocation.route)
super().save_model(request, srv, form, change)
def get_queryset(self, request):
qs = super().get_queryset(request)
qs = qs.prefetch_related('allocations',)
from 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(
allocations = ServiceAllocation.objects.filter(resource__pk__in=down)
#allocations = allocations.values('resource', 'route')
allocations = sorted(allocations, key=lambda a:
for route, allocs in groupby(allocations, key=lambda a:
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:'%s %s' % (route, allocs))
self.stdout.write('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