blob: 295c1726888a4c4f639813bc8fd84611914eab10 [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 Scandolo6b607c82018-01-30 09:12:26 -080031 ## TODO do not allow enbId duplicates
32
Matteo Scandolo1c049b02018-01-18 11:32:46 -080033
34class Handover(Handover_decl):
35 class Meta:
36 proxy = True
37
38
39class ProgranServiceInstance(ProgranServiceInstance_decl):
40 class Meta:
41 proxy = True
42
43 def save(self, *args, **kwargs):
44 # NOTE someone is setting owner_id, so just override it for now
Matteo Scandolo1c049b02018-01-18 11:32:46 -080045 try:
Matteo Scandolo0a207b52018-01-29 13:39:43 -080046 # NOTE we allow just one ProgranService
47 progran_service = ProgranService.objects.all()[0]
Matteo Scandolo1c049b02018-01-18 11:32:46 -080048 self.owner_id = progran_service.id
49 except IndexError:
50 raise XOSValidationError("Service Progran cannot be found, please make sure that the model exists.")
51
52 # NOTE if the instance is new, check that the name is not duplicated
53 instances_with_same_name = None
Matteo Scandolo0a207b52018-01-29 13:39:43 -080054 # FIXME This may leave us vulnerable to someone changing the name at a later time and causing a conflict.
55 # If that's important to prevent, we could prevent that case when `self.pk!=None`,
56 # filter for ProgranServiceInstance with the same name but `pk!=self.pk`.
Matteo Scandolo1c049b02018-01-18 11:32:46 -080057 if self.pk is None:
58 try:
59 instances_with_same_name = ProgranServiceInstance.objects.get(name=self.name)
60 except self.DoesNotExist:
61 # it's ok not to find anything here
62 pass
63
64 if instances_with_same_name:
65 raise XOSValidationError("A ProgranServiceInstance with name '%s' already exists" % self.name)
66 super(ProgranServiceInstance, self).save(*args, **kwargs)
67
68