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 |
| 18 | |
| 19 | |
| 20 | class ENodeB(ENodeB_decl): |
| 21 | class Meta: |
| 22 | proxy = True |
| 23 | |
| 24 | |
| 25 | class Handover(Handover_decl): |
| 26 | class Meta: |
| 27 | proxy = True |
| 28 | |
| 29 | |
| 30 | class ProgranServiceInstance(ProgranServiceInstance_decl): |
| 31 | class Meta: |
| 32 | proxy = True |
| 33 | |
| 34 | def save(self, *args, **kwargs): |
| 35 | # NOTE someone is setting owner_id, so just override it for now |
| 36 | # if not self.owner_id: |
| 37 | services = Service.objects.all() |
| 38 | services = [s for s in services if s.name.lower() == 'progran'] |
| 39 | |
| 40 | # NOTE select the correct owner |
| 41 | try: |
| 42 | progran_service = services[0] |
| 43 | self.owner_id = progran_service.id |
| 44 | except IndexError: |
| 45 | raise XOSValidationError("Service Progran cannot be found, please make sure that the model exists.") |
| 46 | |
| 47 | # NOTE if the instance is new, check that the name is not duplicated |
| 48 | instances_with_same_name = None |
| 49 | if self.pk is None: |
| 50 | try: |
| 51 | instances_with_same_name = ProgranServiceInstance.objects.get(name=self.name) |
| 52 | except self.DoesNotExist: |
| 53 | # it's ok not to find anything here |
| 54 | pass |
| 55 | |
| 56 | if instances_with_same_name: |
| 57 | raise XOSValidationError("A ProgranServiceInstance with name '%s' already exists" % self.name) |
| 58 | super(ProgranServiceInstance, self).save(*args, **kwargs) |
| 59 | |
| 60 | |