Omar Abdelkader | a1f06be | 2017-08-03 14:52:18 -0700 | [diff] [blame^] | 1 | from core.models.plcorebase import * |
| 2 | from models_decl import VHSSService_decl |
| 3 | from models_decl import VHSSTenant_decl |
| 4 | |
| 5 | class VHSSService(VHSSService_decl): |
| 6 | class Meta: |
| 7 | proxy = True |
| 8 | |
| 9 | class VHSSTenant(VHSSTenant_decl): |
| 10 | class Meta: |
| 11 | proxy = True |
| 12 | |
| 13 | def __init__(self, *args, **kwargs): |
| 14 | vhssservice = VHSSService.get_service_objects().all() |
| 15 | if vhssservice: |
| 16 | self._meta.get_field( |
| 17 | "provider_service").default = vhssservice[0].id |
| 18 | super(VHSSTenant, self).__init__(*args, **kwargs) |
| 19 | |
| 20 | def save(self, *args, **kwargs): |
| 21 | super(VHSSTenant, self).save(*args, **kwargs) |
| 22 | # This call needs to happen so that an instance is created for this |
| 23 | # tenant is created in the slice. One instance is created per tenant. |
| 24 | model_policy_vhsstenant(self.pk) |
| 25 | |
| 26 | def delete(self, *args, **kwargs): |
| 27 | # Delete the instance that was created for this tenant |
| 28 | self.cleanup_container() |
| 29 | super(VHSSTenant, self).delete(*args, **kwargs) |
| 30 | |
| 31 | def model_policy_vhsstenant(pk): |
| 32 | # This section of code is atomic to prevent race conditions |
| 33 | with transaction.atomic(): |
| 34 | # We find all of the tenants that are waiting to update |
| 35 | tenant = VHSSTenant.objects.select_for_update().filter(pk=pk) |
| 36 | if not tenant: |
| 37 | return |
| 38 | # Since this code is atomic it is safe to always use the first tenant |
| 39 | tenant = tenant[0] |
| 40 | tenant.manage_container() |