1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- from django.db import models
- from django.core.validators import MaxValueValidator
- from django.utils import timezone
- from django.contrib.auth.models import Group
- from django.contrib.contenttypes.fields import GenericRelation
- from adhesions.models import Adherent
- from banking.models import Payment
- class IPResource(models.Model):
- ip = models.GenericIPAddressField(verbose_name='IP')
- mask = models.PositiveIntegerField(validators=[MaxValueValidator(128)],
- default=0, verbose_name='Masque')
- @property
- def in_use(self):
- if Service.objects.filter(ip_resources=self, start__lte=timezone.now(),
- end__isnull=True).exists():
- return True
- else:
- return False
- class Meta:
- verbose_name = 'Ressource IP'
- verbose_name_plural = 'Ressources IP'
- def __str__(self):
- r = str(self.ip)
- if self.mask:
- r += '/%d' % self.mask
- return r
- class ServiceType(models.Model):
- name = models.CharField(max_length=64, verbose_name='Nom')
- group = models.ForeignKey(Group, null=True, blank=True,
- verbose_name='Groupe de gestion',
- related_name='service_types')
- class Meta:
- verbose_name = 'Type de service'
- verbose_name_plural = 'Types de service'
- def __str__(self):
- return self.name
- class Service(models.Model):
- adherent = models.ForeignKey(Adherent, verbose_name='Adhérent', null=True, blank=True)
- service_type = models.ForeignKey(ServiceType, related_name='services',
- verbose_name='Type de service')
- notes = models.TextField(blank=True, default='')
- contribution = GenericRelation(Payment,
- content_type_field='reason_type',
- object_id_field='reason_id',
- related_query_name='service')
- ip_resources = models.ManyToManyField(IPResource, blank=True,
- verbose_name='Ressources IP')
- start = models.DateTimeField(verbose_name='Début du service')
- end = models.DateTimeField(null=True, blank=True, verbose_name='Fin du service')
- @property
- def status(self):
- if self.end:
- return 'Terminé le %s' % self.end
- else:
- return 'En cours'
- def __str__(self):
- return str(self.service_type) + ' ' + str(self.adherent)
|