Omar Abdelkader | d76c89d | 2017-08-02 19:39:44 -0500 | [diff] [blame^] | 1 | from core.models.plcorebase import * |
| 2 | from models_decl import VPGWCService_decl |
| 3 | from models_decl import VPGWCTenant_decl |
| 4 | |
| 5 | class VPGWCService(VPGWCService_decl): |
| 6 | class Meta: |
| 7 | proxy = True |
| 8 | |
| 9 | class VPGWCTenant(VPGWCTenant_decl): |
| 10 | class Meta: |
| 11 | proxy = True |
| 12 | |
| 13 | def __init__(self, *args, **kwargs): |
| 14 | vpgwcservice = VPGWCService.get_service_objects().all() |
| 15 | # When the tenant is created the default service in the form is set |
| 16 | # to be the first created HelloWorldServiceComplete |
| 17 | if vpgwcservice: |
| 18 | self._meta.get_field( |
| 19 | "provider_service").default = vpgwcservice[0].id |
| 20 | super(VPGWCTenant, self).__init__(*args, **kwargs) |
| 21 | |
| 22 | def save(self, *args, **kwargs): |
| 23 | # Update the instance that was created for this tenant |
| 24 | super(VPGWCTenant, self).save(*args, **kwargs) |
| 25 | model_policy_vpgwctenant(self.pk) |
| 26 | |
| 27 | def delete(self, *args, **kwargs): |
| 28 | # Delete the instance that was created for this tenant |
| 29 | self.cleanup_container() |
| 30 | super(VPGWCTenant, self).delete(*args, **kwargs) |
| 31 | |
| 32 | def model_policy_vpgwctenant(pk): |
| 33 | # This section of code is atomic to prevent race conditions |
| 34 | with transaction.atomic(): |
| 35 | # We find all of the tenants that are waiting to update |
| 36 | tenant = VPGWCTenant.objects.select_for_update().filter(pk=pk) |
| 37 | if not tenant: |
| 38 | return |
| 39 | # Since this code is atomic it is safe to always use the first tenant |
| 40 | tenant = tenant[0] |
| 41 | tenant.manage_container() |