Completed
Push — master ( 1b8e23...81a01d )
by Vijay
9s
created

Organisation.__str__()   A

Complexity

Conditions 1

Size

Total Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
dl 0
loc 2
rs 10
c 0
b 0
f 0
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
    location = models.ForeignKey(Location)
15
    organisation_role = models.CharField(
16
        max_length=300, verbose_name="Your position in organisation")
17
    user = models.ManyToManyField(User, related_name='organisation_users')
18
    active = models.BooleanField(default=True)
19
20
    @classmethod
21
    def list_user_organisations(cls, user):
22
        return cls.objects.filter(user=user, active=True)
23
24
    @property
25
    def get_organisation_user_list(self):
26
        return self.user.all()
27
28
    def toggle_active(self, logged_user, **kwargs):
29
        """
30
        Helper method to toggle active flag for the model.
31
        """
32
33
        action_map = {'active': True, 'deactive': False}
34
        action = kwargs.get('action')
35
        # check if user is only poc for the organisation
36
        self.user.remove(logged_user)
37
        if not self.user.count():
38
            # if there are no more poc for this organisation disable it
39
            # else Organisation will be active
40
            self.active = action_map.get(action)
41
            self.save()
42
43
        return {
44
            'status': True,
45
            'msg': 'Organisation disabled'}
46
47
    class Meta:
48
        db_table = 'organisations'
49
50
    def __str__(self):
51
        return '{} , {}'.format(self.name, self.location)
52