-
Notifications
You must be signed in to change notification settings - Fork 28
/
models.py
131 lines (109 loc) · 4.99 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
from django.contrib.contenttypes.fields import GenericRelation
from django.core.validators import RegexValidator
from django.db import models, transaction
from pydash import get
from core.client_configs.models import ClientConfig
from core.common.checksums import ChecksumModel
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, ChecksumModel):
class Meta:
db_table = 'organizations'
indexes = [
models.Index(fields=['uri']),
] + BaseResourceModel.Meta.indexes
OBJECT_TYPE = ORG_OBJECT_TYPE
es_fields = {
'name': {'sortable': False, 'filterable': True, 'exact': True},
'_name': {'sortable': True, 'filterable': False, 'exact': False},
'mnemonic': {'sortable': False, 'filterable': True, 'exact': True},
'_mnemonic': {'sortable': True, 'filterable': False, 'exact': False},
'last_update': {'sortable': True, 'default': 'desc', 'filterable': False},
'updated_by': {'sortable': False, 'filterable': False, 'facet': True},
'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)
overview = models.JSONField(default=dict)
def get_standard_checksum_fields(self):
return self.get_standard_checksum_fields_for_resource(self)
def get_smart_checksum_fields(self):
return self.get_smart_checksum_fields_for_resource(self)
@staticmethod
def get_standard_checksum_fields_for_resource(data):
return {
'name': get(data, 'name'),
'company': get(data, 'company'),
'location': get(data, 'location'),
'website': get(data, 'website'),
'extras': get(data, 'extras'),
}
@staticmethod
def get_smart_checksum_fields_for_resource(data):
return {
'name': get(data, 'name'),
'company': get(data, 'company'),
'location': get(data, 'location'),
'website': get(data, 'website'),
'is_active': get(data, 'is_active')
}
def calculate_uri(self):
return f"/orgs/{self.mnemonic}/"
@staticmethod
def get_search_document():
from core.orgs.documents import OrganizationDocument
return OrganizationDocument
@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)
def delete(self, using=None, keep_parents=False):
with transaction.atomic():
for source in self.source_set.all():
self.batch_delete(source.concepts_set)
self.batch_delete(source.mappings_set)
source.delete(force=True)
for collection in self.collection_set.all():
self.batch_delete(collection.references)
collection.delete(force=True)
self.delete_pins()
self.delete_client_configs()
return super().delete(using=using, keep_parents=keep_parents)
def delete_client_configs(self):
ClientConfig.objects.filter(resource_type__model='organization', resource_id=self.id).delete()
def delete_pins(self):
from core.pins.models import Pin
# deletes pins where org is pinned
Pin.objects.filter(resource_type__model='organization', resource_id=self.id).delete()
# deletes pins for this org
self.pins.all().delete()