Matteo Scandolo | 1c049b0 | 2018-01-18 11:32:46 -0800 | [diff] [blame] | 1 | from core.models import Service |
| 2 | from xos.exceptions import XOSValidationError |
| 3 | |
| 4 | from models_decl import ProgranService_decl |
| 5 | from models_decl import ENodeB_decl |
| 6 | from models_decl import Handover_decl |
| 7 | from models_decl import ProgranServiceInstance_decl |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | class ProgranService(ProgranService_decl): |
| 16 | class Meta: |
| 17 | proxy = True |
Matteo Scandolo | 0a207b5 | 2018-01-29 13:39:43 -0800 | [diff] [blame] | 18 | 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 Scandolo | 1c049b0 | 2018-01-18 11:32:46 -0800 | [diff] [blame] | 26 | |
| 27 | |
| 28 | class ENodeB(ENodeB_decl): |
| 29 | class Meta: |
| 30 | proxy = True |
| 31 | |
Matteo Scandolo | 6b607c8 | 2018-01-30 09:12:26 -0800 | [diff] [blame^] | 32 | ## TODO do not allow enbId duplicates |
| 33 | |
Matteo Scandolo | 1c049b0 | 2018-01-18 11:32:46 -0800 | [diff] [blame] | 34 | |
| 35 | class Handover(Handover_decl): |
| 36 | class Meta: |
| 37 | proxy = True |
| 38 | |
| 39 | |
| 40 | class 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 Scandolo | 1c049b0 | 2018-01-18 11:32:46 -0800 | [diff] [blame] | 46 | try: |
Matteo Scandolo | 0a207b5 | 2018-01-29 13:39:43 -0800 | [diff] [blame] | 47 | # NOTE we allow just one ProgranService |
| 48 | progran_service = ProgranService.objects.all()[0] |
Matteo Scandolo | 1c049b0 | 2018-01-18 11:32:46 -0800 | [diff] [blame] | 49 | 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 Scandolo | 0a207b5 | 2018-01-29 13:39:43 -0800 | [diff] [blame] | 55 | # 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 Scandolo | 1c049b0 | 2018-01-18 11:32:46 -0800 | [diff] [blame] | 58 | 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 | |