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