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 | 6b607c8 | 2018-01-30 09:12:26 -0800 | [diff] [blame] | 31 | ## TODO do not allow enbId duplicates |
| 32 | |
Matteo Scandolo | 1c049b0 | 2018-01-18 11:32:46 -0800 | [diff] [blame] | 33 | |
| 34 | class Handover(Handover_decl): |
| 35 | class Meta: |
| 36 | proxy = True |
| 37 | |
| 38 | |
| 39 | class 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 Scandolo | 1c049b0 | 2018-01-18 11:32:46 -0800 | [diff] [blame] | 45 | try: |
Matteo Scandolo | 0a207b5 | 2018-01-29 13:39:43 -0800 | [diff] [blame] | 46 | # NOTE we allow just one ProgranService |
| 47 | progran_service = ProgranService.objects.all()[0] |
Matteo Scandolo | 1c049b0 | 2018-01-18 11:32:46 -0800 | [diff] [blame] | 48 | 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 Scandolo | 0a207b5 | 2018-01-29 13:39:43 -0800 | [diff] [blame] | 54 | # 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 Scandolo | 1c049b0 | 2018-01-18 11:32:46 -0800 | [diff] [blame] | 57 | 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 | |