-
Notifications
You must be signed in to change notification settings - Fork 27
/
models.py
68 lines (54 loc) · 2.51 KB
/
models.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
from django.contrib import admin
from django.contrib.contenttypes.fields import GenericRelation
from django.core.validators import RegexValidator
from django.db import models
from core.client_configs.models import ClientConfig
from core.common.constants import NAMESPACE_REGEX, ACCESS_TYPE_VIEW, ACCESS_TYPE_EDIT
from core.common.mixins import SourceContainerMixin
from core.common.models import BaseResourceModel
from core.orgs.constants import ORG_OBJECT_TYPE
class Organization(BaseResourceModel, SourceContainerMixin):
class Meta:
db_table = 'organizations'
OBJECT_TYPE = ORG_OBJECT_TYPE
es_fields = {
'name': {'sortable': True, 'filterable': True, 'exact': True},
'mnemonic': {'sortable': True, 'filterable': True, 'exact': True},
'last_update': {'sortable': True, 'default': 'desc', 'filterable': False},
'company': {'sortable': False, 'filterable': True, 'exact': True},
'location': {'sortable': False, 'filterable': True, 'exact': True},
}
name = models.TextField()
company = models.TextField(null=True, blank=True)
website = models.TextField(null=True, blank=True)
location = models.TextField(null=True, blank=True)
mnemonic = models.CharField(
max_length=255, validators=[RegexValidator(regex=NAMESPACE_REGEX)], unique=True
)
description = models.TextField(null=True, blank=True)
client_configs = GenericRelation(ClientConfig, object_id_field='resource_id', content_type_field='resource_type')
text = models.TextField(null=True, blank=True) # for about description (markup)
@property
def org(self):
return self.mnemonic
@property
def num_members(self):
return self.members.count()
def is_member(self, user_profile):
return user_profile and self.members.filter(id=user_profile.id).exists()
@staticmethod
def get_url_kwarg():
return 'org'
@classmethod
def get_by_username(cls, username):
return cls.objects.filter(members__username=username)
@classmethod
def get_public(cls):
return cls.objects.filter(public_access__in=[ACCESS_TYPE_VIEW, ACCESS_TYPE_EDIT])
def save(self, force_insert=False, force_update=False, using=None, update_fields=None):
super().save(force_insert=force_insert, force_update=force_update, using=using, update_fields=update_fields)
if self.id:
self.members.add(self.created_by)
if self.updated_by_id:
self.members.add(self.updated_by)
admin.site.register(Organization)