blob: 594255a03ddc91ed2bfebc9966c20cd29dda2bb6 [file] [log] [blame]
Matteo Scandolo1c049b02018-01-18 11:32:46 -08001from core.models import Service
2from xos.exceptions import XOSValidationError
3
4from models_decl import ProgranService_decl
5from models_decl import ENodeB_decl
6from models_decl import Handover_decl
7from models_decl import ProgranServiceInstance_decl
8
9
10
11
12
13
14
15class ProgranService(ProgranService_decl):
16 class Meta:
17 proxy = True
Matteo Scandolo0a207b52018-01-29 13:39:43 -080018 def save(self, *args, **kwargs):
19
20 existing_services = ProgranService.objects.all()
21
22 if len(existing_services) > 0 and not self.delete:
23 raise XOSValidationError("A ProgranService already exists, you should not have more than one")
24
25 super(ProgranService, self).save(*args, **kwargs)
Matteo Scandolo1c049b02018-01-18 11:32:46 -080026
27
28class ENodeB(ENodeB_decl):
29 class Meta:
30 proxy = True
31
32
33class Handover(Handover_decl):
34 class Meta:
35 proxy = True
36
37
38class ProgranServiceInstance(ProgranServiceInstance_decl):
39 class Meta:
40 proxy = True
41
42 def save(self, *args, **kwargs):
43 # NOTE someone is setting owner_id, so just override it for now
Matteo Scandolo1c049b02018-01-18 11:32:46 -080044 try:
Matteo Scandolo0a207b52018-01-29 13:39:43 -080045 # NOTE we allow just one ProgranService
46 progran_service = ProgranService.objects.all()[0]
Matteo Scandolo1c049b02018-01-18 11:32:46 -080047 self.owner_id = progran_service.id
48 except IndexError:
49 raise XOSValidationError("Service Progran cannot be found, please make sure that the model exists.")
50
51 # NOTE if the instance is new, check that the name is not duplicated
52 instances_with_same_name = None
Matteo Scandolo0a207b52018-01-29 13:39:43 -080053 # FIXME This may leave us vulnerable to someone changing the name at a later time and causing a conflict.
54 # If that's important to prevent, we could prevent that case when `self.pk!=None`,
55 # filter for ProgranServiceInstance with the same name but `pk!=self.pk`.
Matteo Scandolo1c049b02018-01-18 11:32:46 -080056 if self.pk is None:
57 try:
58 instances_with_same_name = ProgranServiceInstance.objects.get(name=self.name)
59 except self.DoesNotExist:
60 # it's ok not to find anything here
61 pass
62
63 if instances_with_same_name:
64 raise XOSValidationError("A ProgranServiceInstance with name '%s' already exists" % self.name)
65 super(ProgranServiceInstance, self).save(*args, **kwargs)
66
67