Matteo Scandolo | 1c049b0 | 2018-01-18 11:32:46 -0800 | [diff] [blame] | 1 | from xos.exceptions import XOSValidationError |
| 2 | |
| 3 | from models_decl import ProgranService_decl |
| 4 | from models_decl import ENodeB_decl |
| 5 | from models_decl import Handover_decl |
| 6 | from models_decl import ProgranServiceInstance_decl |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | class ProgranService(ProgranService_decl): |
| 15 | class Meta: |
| 16 | proxy = True |
Matteo Scandolo | 0a207b5 | 2018-01-29 13:39:43 -0800 | [diff] [blame] | 17 | 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 Scandolo | 1c049b0 | 2018-01-18 11:32:46 -0800 | [diff] [blame] | 25 | |
| 26 | |
| 27 | class ENodeB(ENodeB_decl): |
| 28 | class Meta: |
| 29 | proxy = True |
| 30 | |
Matteo Scandolo | cfa9e8f | 2018-01-31 18:29:06 -0800 | [diff] [blame] | 31 | 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 Scandolo | 6b607c8 | 2018-01-30 09:12:26 -0800 | [diff] [blame] | 47 | |
Matteo Scandolo | 1c049b0 | 2018-01-18 11:32:46 -0800 | [diff] [blame] | 48 | |
| 49 | class Handover(Handover_decl): |
| 50 | class Meta: |
| 51 | proxy = True |
| 52 | |
| 53 | |
| 54 | class 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 Scandolo | 1c049b0 | 2018-01-18 11:32:46 -0800 | [diff] [blame] | 60 | try: |
Matteo Scandolo | 0a207b5 | 2018-01-29 13:39:43 -0800 | [diff] [blame] | 61 | # NOTE we allow just one ProgranService |
| 62 | progran_service = ProgranService.objects.all()[0] |
Matteo Scandolo | 1c049b0 | 2018-01-18 11:32:46 -0800 | [diff] [blame] | 63 | 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 Scandolo | e463b06 | 2018-02-01 10:14:03 -0800 | [diff] [blame^] | 67 | # prevent name duplicates |
| 68 | try: |
| 69 | instances_with_same_name = ProgranServiceInstance.objects.get(name=self.name) |
Matteo Scandolo | 1c049b0 | 2018-01-18 11:32:46 -0800 | [diff] [blame] | 70 | |
Matteo Scandolo | e463b06 | 2018-02-01 10:14:03 -0800 | [diff] [blame^] | 71 | 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 Scandolo | d52da97 | 2018-01-31 14:37:43 -0800 | [diff] [blame] | 75 | |
| 76 | # TODO when saving set status to "in progress" |
Matteo Scandolo | 1c049b0 | 2018-01-18 11:32:46 -0800 | [diff] [blame] | 77 | super(ProgranServiceInstance, self).save(*args, **kwargs) |
| 78 | |
| 79 | |