Scott Baker | 619de67 | 2016-06-20 12:49:38 -0700 | [diff] [blame] | 1 | # models.py - ExampleService Models |
| 2 | |
| 3 | from core.models import Service, TenantWithContainer |
| 4 | from django.db import models, transaction |
| 5 | |
| 6 | SERVICE_NAME = 'exampleservice' |
| 7 | SERVICE_NAME_VERBOSE = 'Example Service' |
| 8 | SERVICE_NAME_VERBOSE_PLURAL = 'Example Services' |
| 9 | TENANT_NAME_VERBOSE = 'Example Tenant' |
| 10 | TENANT_NAME_VERBOSE_PLURAL = 'Example Tenants' |
| 11 | |
| 12 | class ExampleService(Service): |
| 13 | |
| 14 | KIND = SERVICE_NAME |
| 15 | |
| 16 | class Meta: |
| 17 | app_label = SERVICE_NAME |
| 18 | verbose_name = SERVICE_NAME_VERBOSE |
| 19 | |
| 20 | service_message = models.CharField(max_length=254, help_text="Service Message to Display") |
| 21 | |
| 22 | class ExampleTenant(TenantWithContainer): |
| 23 | |
| 24 | KIND = SERVICE_NAME |
| 25 | |
| 26 | class Meta: |
| 27 | verbose_name = TENANT_NAME_VERBOSE |
| 28 | |
| 29 | tenant_message = models.CharField(max_length=254, help_text="Tenant Message to Display") |
| 30 | |
| 31 | def __init__(self, *args, **kwargs): |
| 32 | exampleservice = ExampleService.get_service_objects().all() |
| 33 | if exampleservice: |
| 34 | self._meta.get_field('provider_service').default = exampleservice[0].id |
| 35 | super(ExampleTenant, self).__init__(*args, **kwargs) |
| 36 | |
| 37 | def save(self, *args, **kwargs): |
| 38 | super(ExampleTenant, self).save(*args, **kwargs) |
| 39 | model_policy_exampletenant(self.pk) |
| 40 | |
| 41 | def delete(self, *args, **kwargs): |
| 42 | self.cleanup_container() |
| 43 | super(ExampleTenant, self).delete(*args, **kwargs) |
| 44 | |
| 45 | |
| 46 | def model_policy_exampletenant(pk): |
| 47 | with transaction.atomic(): |
| 48 | tenant = ExampleTenant.objects.select_for_update().filter(pk=pk) |
| 49 | if not tenant: |
| 50 | return |
| 51 | tenant = tenant[0] |
| 52 | tenant.manage_container() |
| 53 | |