-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathmodels.py
More file actions
241 lines (202 loc) · 8.4 KB
/
models.py
File metadata and controls
241 lines (202 loc) · 8.4 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
import json
from collections import Counter
from django.conf import settings
from django.contrib.postgres.fields import ArrayField
from django.core.exceptions import ValidationError
from django.db import models, IntegrityError
from pydash import get
from core.common.constants import PERSIST_NEW_ERROR_MESSAGE
from core.common.models import BaseModel
from core.common.utils import get_export_service, generate_temp_version
def default_score_configuration():
return {'recommended': 100, 'available': 70}
class MapProject(BaseModel):
OBJECT_TYPE = 'MapProject'
mnemonic_attr = 'id'
BATCH_SIZE = 50
name = models.TextField()
description = models.TextField(null=True, blank=True)
organization = models.ForeignKey(
'orgs.Organization', on_delete=models.CASCADE, null=True, blank=True, related_name='map_projects')
user = models.ForeignKey(
'users.UserProfile', on_delete=models.CASCADE, null=True, blank=True, related_name='map_projects')
input_file_name = models.TextField()
matches = ArrayField(models.JSONField(), default=list, null=True, blank=True)
columns = ArrayField(models.JSONField(), default=list)
target_repo_url = models.TextField(null=True, blank=True)
algorithms = ArrayField(models.JSONField(), default=list, null=True, blank=True)
include_retired = models.BooleanField(default=False)
logs = models.JSONField(default=dict, null=True, blank=True)
score_configuration = models.JSONField(default=default_score_configuration, null=True, blank=True)
filters = models.JSONField(default=dict, null=True, blank=True)
candidates = models.JSONField(default=dict, null=True, blank=True)
lookup_config = models.JSONField(default=dict, null=True, blank=True)
analysis = models.JSONField(default=dict, null=True, blank=True)
encoder_model = models.TextField(null=True, blank=True, default=settings.ENCODER_MODEL_NAME)
class Meta:
db_table = 'map_projects'
@property
def mnemonic(self):
return self.id
@property
def parent(self):
return self.organization if self.organization_id else self.user
@property
def parent_id(self):
return self.organization_id or self.user_id
@property
def matches_summary(self):
if self.matches:
counter = Counter()
counter.update(match.get('state') for match in self.matches if 'state' in match)
return dict(counter)
return None
@property
def visible_columns(self):
if self.columns:
return [col for col in self.columns if 'hidden' not in col or col['hidden'] is False and col.get('label')] # pylint: disable=not-an-iterable,line-too-long
return []
@property
def summary(self):
return {
'matches': self.matches_summary,
'columns': [col.get('label') for col in self.visible_columns],
}
def calculate_uri(self):
return self.parent.uri + "map-projects/" + str((self.id or generate_temp_version())) + "/"
@property
def _upload_dir_path(self):
return f"map_projects/{self.id}/"
@property
def file_path(self):
return self._upload_dir_path + self.input_file_name
@property
def file_url(self):
if self.input_file_name:
service = get_export_service()
return service.url_for(self.file_path)
return None
def update_input_file(self, input_file):
if input_file:
service = get_export_service()
file_name = input_file.name
key = self._upload_dir_path + file_name
result = service.upload(key=key, file_content=input_file)
if result == 204:
self.input_file_name = file_name
self.save()
@classmethod
def persist_new(cls, instance, user, **kwargs):
errors = {}
persisted = False
instance.created_by = instance.updated_by = user
try:
input_file = kwargs.pop('input_file', None)
instance.input_file_name = input_file.name if input_file else None
instance.full_clean()
instance.save(**kwargs)
if instance.id:
instance.save()
persisted = True
instance.update_input_file(input_file)
except IntegrityError as ex:
errors.update({'__all__': ex.args})
except ValidationError as ex:
errors = get(ex, 'message_dict', {}) or get(ex, 'error_dict', {})
finally:
if not persisted and not errors:
errors['non_field_errors'] = PERSIST_NEW_ERROR_MESSAGE.format(cls.__name__)
return errors
@classmethod
def persist_changes(cls, instance, user, **kwargs):
errors = {}
try:
input_file = kwargs.pop('input_file', None)
instance.input_file_name = input_file.name if input_file else None
instance.updated_by = user
instance.full_clean()
instance.save(**kwargs)
if input_file:
instance.update_input_file(input_file)
except ValidationError as ex:
errors.update(get(ex, 'message_dict', {}) or get(ex, 'error_dict', {}))
except IntegrityError as ex:
errors.update({'__all__': ex.args})
return errors
def delete(self, using=None, keep_parents=False):
file_path = self.file_path
result = super().delete(using=using, keep_parents=keep_parents)
self._delete_uploaded_file(file_path)
return result
@staticmethod
def _delete_uploaded_file(file_path):
from core.common.tasks import delete_s3_objects
delete_s3_objects.apply_async((file_path,), queue='default', permanent=False)
@classmethod
def format_request_data(cls, data, parent_resource=None):
new_data = {
key: val[0] if isinstance(val, list) and len(val) == 1 and key not in [
'candidates', 'analysis'] else val for key, val in data.items()
}
cls.format_json(new_data, 'matches')
cls.format_json(new_data, 'columns')
cls.format_json(new_data, 'score_configuration')
cls.format_json(new_data, 'filters')
cls.format_json(new_data, 'candidates')
cls.format_json(new_data, 'analysis')
cls.format_json(new_data, 'algorithms')
cls.format_json(new_data, 'lookup_config')
if parent_resource:
new_data[parent_resource.resource_type.lower() + '_id'] = parent_resource.id
file = data.get('file')
if file:
new_data['input_file_name'] = file.name
return new_data
@staticmethod
def format_json(new_data, field):
if field in new_data and isinstance(new_data[field], str):
try:
new_data[field] = json.loads(new_data[field])
except json.JSONDecodeError:
pass
def soft_delete(self):
self.delete()
def clean(self):
self.clean_filters()
self.clean_include_retired()
self.clean_encoder_model()
self.clean_matches()
def clean_matches(self):
if self.matches:
try:
self.matches = json.loads(self.matches)
except (json.JSONDecodeError, TypeError):
pass
def clean_include_retired(self):
if not self.include_retired:
self.include_retired = False
def clean_encoder_model(self):
self.encoder_model = self.encoder_model.strip() if self.encoder_model else settings.ENCODER_MODEL_NAME
def clean_filters(self):
if not self.filters:
self.filters = {}
for key, value in self.filters.copy().items():
if not value:
self.filters.pop(key)
@property
def target_repo(self):
if not self.target_repo_url:
return None
from core.sources.models import Source
repo, _ = Source.resolve_reference_expression(self.target_repo_url)
return repo if repo and repo.id else None
@property
def fields_mapped(self):
return [
col.get('label') for col in self.visible_columns if (
col['label'].lower() in [
'id', 'description', 'mapping: list', 'mapping: code',
'concept_class', 'class', 'datatype', 'name', 'synonyms'
] or col['label'].lower().startswith('property:')
)
] if self.columns else []