1
|
|
|
from django.contrib.auth.models import User |
2
|
|
|
from django.db import models |
3
|
|
|
|
4
|
|
|
from wye.base.constants import OrganisationType |
5
|
|
|
from wye.base.models import AuditModel |
6
|
|
|
from wye.regions.models import Location |
7
|
|
|
|
8
|
|
|
|
9
|
|
|
class Organisation(AuditModel): |
10
|
|
|
organisation_type = models.PositiveSmallIntegerField( |
11
|
|
|
choices=OrganisationType.CHOICES, verbose_name="organisation type") |
12
|
|
|
name = models.CharField(max_length=300, unique=True) |
13
|
|
|
description = models.TextField() |
14
|
|
|
full_address = models.TextField(blank=True, null=True) |
15
|
|
|
location = models.ForeignKey(Location) |
16
|
|
|
pincode = models.CharField(max_length=6, blank=True, null=True) |
17
|
|
|
address_map_url = models.URLField(blank=True, null=True) |
18
|
|
|
organisation_role = models.CharField( |
19
|
|
|
max_length=300, verbose_name="Your position in organisation") |
20
|
|
|
user = models.ManyToManyField(User, related_name='organisation_users') |
21
|
|
|
active = models.BooleanField(default=True) |
22
|
|
|
|
23
|
|
|
@classmethod |
24
|
|
|
def list_user_organisations(cls, user): |
25
|
|
|
return cls.objects.filter(user=user, active=True) |
26
|
|
|
|
27
|
|
|
@property |
28
|
|
|
def get_organisation_user_list(self): |
29
|
|
|
return self.user.all() |
30
|
|
|
|
31
|
|
|
def toggle_active(self, logged_user, **kwargs): |
32
|
|
|
""" |
33
|
|
|
Helper method to toggle active flag for the model. |
34
|
|
|
""" |
35
|
|
|
|
36
|
|
|
action_map = {'active': True, 'deactive': False} |
37
|
|
|
action = kwargs.get('action') |
38
|
|
|
# check if user is only poc for the organisation |
39
|
|
|
self.user.remove(logged_user) |
40
|
|
|
if not self.user.count(): |
41
|
|
|
# if there are no more poc for this organisation disable it |
42
|
|
|
# else Organisation will be active |
43
|
|
|
self.active = action_map.get(action) |
44
|
|
|
self.save() |
45
|
|
|
|
46
|
|
|
return { |
47
|
|
|
'status': True, |
48
|
|
|
'msg': 'Organisation disabled'} |
49
|
|
|
|
50
|
|
|
class Meta: |
51
|
|
|
db_table = 'organisations' |
52
|
|
|
|
53
|
|
|
def __str__(self): |
54
|
|
|
return '{} , {}'.format(self.name, self.location) |
55
|
|
|
|