blob: 692b1aa51e17c409ab4a4604e3c2f0d3d7d2aab8 [file] [log] [blame]
Matteo Scandolo1c049b02018-01-18 11:32:46 -08001from xos.exceptions import XOSValidationError
2
3from models_decl import ProgranService_decl
4from models_decl import ENodeB_decl
5from models_decl import Handover_decl
6from models_decl import ProgranServiceInstance_decl
7
8
9
10
11
12
13
14class ProgranService(ProgranService_decl):
15 class Meta:
16 proxy = True
Matteo Scandolo0a207b52018-01-29 13:39:43 -080017 def save(self, *args, **kwargs):
18
19 existing_services = ProgranService.objects.all()
20
21 if len(existing_services) > 0 and not self.delete:
22 raise XOSValidationError("A ProgranService already exists, you should not have more than one")
23
24 super(ProgranService, self).save(*args, **kwargs)
Matteo Scandolo1c049b02018-01-18 11:32:46 -080025
26
27class ENodeB(ENodeB_decl):
28 class Meta:
29 proxy = True
30
Matteo Scandolocfa9e8f2018-01-31 18:29:06 -080031 def save(self, *args, **kwargs):
32
33 # remove all the profiles related to this enodeb (clearing the relation, not the models)
34 if self.deleted:
35 self.profiles.clear()
36
37 # prevent enbId duplicates
38 try:
39 instance_with_same_id = ENodeB.objects.get(enbId=self.enbId)
40
41 if (not self.pk and instance_with_same_id) or (self.pk and self.pk != instance_with_same_id.pk):
42 raise XOSValidationError("A ENodeB with enbId '%s' already exists" % self.enbId)
43 except self.DoesNotExist:
44 pass
45
46 super(ENodeB, self).save(*args, **kwargs)
Matteo Scandolo6b607c82018-01-30 09:12:26 -080047
Matteo Scandolo1c049b02018-01-18 11:32:46 -080048
49class Handover(Handover_decl):
50 class Meta:
51 proxy = True
52
53
54class ProgranServiceInstance(ProgranServiceInstance_decl):
55 class Meta:
56 proxy = True
57
58 def save(self, *args, **kwargs):
59 # NOTE someone is setting owner_id, so just override it for now
Matteo Scandolo1c049b02018-01-18 11:32:46 -080060 try:
Matteo Scandolo0a207b52018-01-29 13:39:43 -080061 # NOTE we allow just one ProgranService
62 progran_service = ProgranService.objects.all()[0]
Matteo Scandolo1c049b02018-01-18 11:32:46 -080063 self.owner_id = progran_service.id
64 except IndexError:
65 raise XOSValidationError("Service Progran cannot be found, please make sure that the model exists.")
66
Matteo Scandoloe463b062018-02-01 10:14:03 -080067 # prevent name duplicates
68 try:
69 instances_with_same_name = ProgranServiceInstance.objects.get(name=self.name)
Matteo Scandolo1c049b02018-01-18 11:32:46 -080070
Matteo Scandoloe463b062018-02-01 10:14:03 -080071 if (not self.pk and instances_with_same_name) or (self.pk and self.pk != instances_with_same_name.pk):
72 raise XOSValidationError("A ProgranServiceInstance with name '%s' already exists" % self.name)
73 except self.DoesNotExist:
74 pass
Matteo Scandolod52da972018-01-31 14:37:43 -080075
76 # TODO when saving set status to "in progress"
Matteo Scandolo1c049b02018-01-18 11:32:46 -080077 super(ProgranServiceInstance, self).save(*args, **kwargs)
78
79