| author | Alexandre Segura <mex.zktk@gmail.com> |
| Wed, 19 Apr 2017 15:10:06 +0200 | |
| changeset 471 | d80f62757142 |
| parent 464 | 655136b09cf3 |
| child 484 | 6d46a199d5db |
| permissions | -rw-r--r-- |
| 299 | 1 |
import json |
2 |
import logging |
|
3 |
import re |
|
4 |
import urllib |
|
5 |
import uuid |
|
6 |
||
7 |
import requests |
|
| 298 | 8 |
from django.conf import settings |
9 |
from django.contrib.auth.models import User |
|
10 |
from django.contrib.contenttypes.fields import GenericRelation |
|
11 |
from django.contrib.contenttypes.models import ContentType |
|
| 299 | 12 |
from django.db import models, transaction |
13 |
from django.utils.text import slugify |
|
| 298 | 14 |
from django_comments_xtd.models import XtdComment |
| 299 | 15 |
|
| 298 | 16 |
import iconolab.signals.handlers as iconolab_signals |
17 |
||
18 |
logger = logging.getLogger(__name__) |
|
19 |
||
20 |
||
21 |
class Collection(models.Model): |
|
22 |
""" |
|
23 |
Collection objects are the thematic item repositories in Iconolab |
|
24 |
||
25 |
name: the name displayed in the url and also used to identify the collection |
|
26 |
verbose_name: the name displayed in the text of the pages |
|
27 |
description: the short description of the collection that will be |
|
28 |
displayed by default in pages |
|
29 |
complete_description: the complete description that will be shown |
|
30 |
with a "view more" button/link |
|
31 |
image/height/width: the collection image that will be shown in the |
|
32 |
collection description |
|
33 |
show_image_on_home: if True, the collection will appear by default |
|
34 |
on the homepage as one of the bigger images |
|
35 |
""" |
|
36 |
name = models.SlugField(max_length=50, unique=True) |
|
37 |
verbose_name = models.CharField(max_length=50, null=True, blank=True) |
|
38 |
description = models.TextField(null=True, blank=True, default="") |
|
39 |
complete_description = models.TextField(null=True, blank=True, default="") |
|
40 |
image = models.ImageField( |
|
41 |
upload_to='uploads/', height_field='height', width_field='width', null=True, blank=True) |
|
42 |
height = models.IntegerField(null=True, blank=True) |
|
43 |
width = models.IntegerField(null=True, blank=True) |
|
44 |
show_image_on_home = models.BooleanField(default=False) |
|
45 |
||
46 |
def __str__(self): |
|
47 |
return self.name |
|
48 |
||
| 416 | 49 |
class Folder(models.Model): |
50 |
""" |
|
51 |
Some items may belong to a "folder". This is actually a physical folder |
|
52 |
""" |
|
|
464
655136b09cf3
Introduce more fields in Folder model.
Alexandre Segura <mex.zktk@gmail.com>
parents:
416
diff
changeset
|
53 |
folder_guid = models.UUIDField(default=uuid.uuid4, editable=False) |
|
655136b09cf3
Introduce more fields in Folder model.
Alexandre Segura <mex.zktk@gmail.com>
parents:
416
diff
changeset
|
54 |
collection = models.ForeignKey(Collection, related_name="folders") |
| 416 | 55 |
name = models.TextField(null=False, blank=False) |
|
464
655136b09cf3
Introduce more fields in Folder model.
Alexandre Segura <mex.zktk@gmail.com>
parents:
416
diff
changeset
|
56 |
description = models.TextField(null=True, blank=True) |
|
655136b09cf3
Introduce more fields in Folder model.
Alexandre Segura <mex.zktk@gmail.com>
parents:
416
diff
changeset
|
57 |
original_id = models.CharField(max_length=256, null=True, blank=True) |
| 416 | 58 |
|
|
471
d80f62757142
Display image & item count for folders.
Alexandre Segura <mex.zktk@gmail.com>
parents:
464
diff
changeset
|
59 |
@property |
|
d80f62757142
Display image & item count for folders.
Alexandre Segura <mex.zktk@gmail.com>
parents:
464
diff
changeset
|
60 |
def items(self): |
|
d80f62757142
Display image & item count for folders.
Alexandre Segura <mex.zktk@gmail.com>
parents:
464
diff
changeset
|
61 |
return Item.objects.filter(folders=self) |
|
d80f62757142
Display image & item count for folders.
Alexandre Segura <mex.zktk@gmail.com>
parents:
464
diff
changeset
|
62 |
|
|
d80f62757142
Display image & item count for folders.
Alexandre Segura <mex.zktk@gmail.com>
parents:
464
diff
changeset
|
63 |
@property |
|
d80f62757142
Display image & item count for folders.
Alexandre Segura <mex.zktk@gmail.com>
parents:
464
diff
changeset
|
64 |
def items_count(self): |
|
d80f62757142
Display image & item count for folders.
Alexandre Segura <mex.zktk@gmail.com>
parents:
464
diff
changeset
|
65 |
return self.items.count() |
|
d80f62757142
Display image & item count for folders.
Alexandre Segura <mex.zktk@gmail.com>
parents:
464
diff
changeset
|
66 |
|
|
d80f62757142
Display image & item count for folders.
Alexandre Segura <mex.zktk@gmail.com>
parents:
464
diff
changeset
|
67 |
@property |
|
d80f62757142
Display image & item count for folders.
Alexandre Segura <mex.zktk@gmail.com>
parents:
464
diff
changeset
|
68 |
def image(self): |
|
d80f62757142
Display image & item count for folders.
Alexandre Segura <mex.zktk@gmail.com>
parents:
464
diff
changeset
|
69 |
first_item = self.items.first() |
|
d80f62757142
Display image & item count for folders.
Alexandre Segura <mex.zktk@gmail.com>
parents:
464
diff
changeset
|
70 |
images = Image.objects.filter(item=first_item) |
|
d80f62757142
Display image & item count for folders.
Alexandre Segura <mex.zktk@gmail.com>
parents:
464
diff
changeset
|
71 |
return images.first() |
|
d80f62757142
Display image & item count for folders.
Alexandre Segura <mex.zktk@gmail.com>
parents:
464
diff
changeset
|
72 |
|
| 416 | 73 |
def __str__(self): |
74 |
return 'Folder ' + self.name |
|
| 298 | 75 |
|
76 |
class Item(models.Model): |
|
77 |
""" |
|
78 |
Item objects belong to a collection, are linked to a metadata item, and |
|
79 |
to one or more images |
|
80 |
""" |
|
81 |
collection = models.ForeignKey(Collection, related_name="items") |
|
82 |
item_guid = models.UUIDField(default=uuid.uuid4, editable=False) |
|
| 416 | 83 |
folders = models.ManyToManyField('Folder') |
| 298 | 84 |
|
85 |
def __str__(self): |
|
86 |
return str(self.item_guid) + ":from:" + self.collection.name |
|
87 |
||
88 |
@property |
|
89 |
def images_sorted_by_name(self): |
|
90 |
return self.images.order_by("-name").all() |
|
91 |
||
92 |
||
93 |
class ItemMetadata(models.Model): |
|
94 |
""" |
|
| 299 | 95 |
Metadata object for the item class. Each field represents what we can |
96 |
import from the provided .csv files |
|
| 298 | 97 |
""" |
98 |
item = models.OneToOneField('Item', related_name='metadatas') |
|
99 |
authors = models.CharField(max_length=255, default="") |
|
100 |
school = models.CharField(max_length=255, default="") |
|
101 |
field = models.CharField(max_length=255, default="") |
|
102 |
designation = models.CharField(max_length=255, default="") |
|
103 |
datation = models.CharField(max_length=255, default="") |
|
104 |
technics = models.CharField(max_length=255, default="") |
|
105 |
measurements = models.CharField(max_length=255, default="") |
|
106 |
create_or_usage_location = models.CharField(max_length=255, default="") |
|
107 |
discovery_context = models.CharField(max_length=255, default="") |
|
108 |
conservation_location = models.CharField(max_length=255, default="") |
|
109 |
photo_credits = models.CharField(max_length=255, default="") |
|
110 |
inventory_number = models.CharField(max_length=255, default="") |
|
111 |
joconde_ref = models.CharField(max_length=255, default="") |
|
112 |
||
113 |
@property |
|
114 |
def get_joconde_url(self): |
|
115 |
return settings.JOCONDE_NOTICE_BASE_URL + self.joconde_ref.rjust(11, '0') |
|
116 |
||
117 |
def __str__(self): |
|
118 |
return "metadatas:for:" + str(self.item.item_guid) |
|
119 |
||
120 |
||
121 |
class Image(models.Model): |
|
122 |
""" |
|
123 |
Each image object is linked to one item, users can create annotations on images |
|
124 |
""" |
|
125 |
||
126 |
image_guid = models.UUIDField(default=uuid.uuid4, editable=False) |
|
127 |
name = models.CharField(max_length=200) |
|
128 |
media = models.ImageField(upload_to='uploads/', |
|
129 |
height_field='height', width_field='width') |
|
130 |
item = models.ForeignKey( |
|
131 |
'Item', related_name='images', null=True, blank=True) |
|
132 |
height = models.IntegerField(null=False, blank=False) |
|
133 |
width = models.IntegerField(null=False, blank=False) |
|
134 |
created = models.DateTimeField(auto_now_add=True, null=True) |
|
135 |
||
136 |
def __str__(self): |
|
137 |
return str(self.image_guid) + ":" + self.name |
|
138 |
||
139 |
@property |
|
140 |
def wh_ratio(self): |
|
141 |
return self.width / self.height |
|
142 |
||
143 |
@property |
|
144 |
def collection(self): |
|
145 |
return self.item.collection.name |
|
146 |
||
147 |
@property |
|
148 |
def title(self): |
|
149 |
return self.item.metadatas.designation |
|
150 |
||
151 |
@property |
|
152 |
def authors(self): |
|
153 |
return self.item.metadatas.authors |
|
154 |
||
155 |
@property |
|
156 |
def school(self): |
|
157 |
return self.item.metadatas.school |
|
158 |
||
159 |
@property |
|
160 |
def designation(self): |
|
161 |
return self.item.metadatas.designation |
|
162 |
||
163 |
@property |
|
164 |
def datation(self): |
|
165 |
return self.item.metadatas.datation |
|
166 |
||
167 |
@property |
|
168 |
def technics(self): |
|
169 |
return self.item.metadatas.technics |
|
170 |
||
171 |
@property |
|
172 |
def measurements(self): |
|
173 |
return self.item.metadatas.measurements |
|
174 |
||
175 |
@property |
|
|
323
55c024fc7c60
Roughly implement annotation navigator.
Alexandre Segura <mex.zktk@gmail.com>
parents:
299
diff
changeset
|
176 |
def latest_annotations(self): |
|
55c024fc7c60
Roughly implement annotation navigator.
Alexandre Segura <mex.zktk@gmail.com>
parents:
299
diff
changeset
|
177 |
return self.annotations.all().order_by('-created') |
|
55c024fc7c60
Roughly implement annotation navigator.
Alexandre Segura <mex.zktk@gmail.com>
parents:
299
diff
changeset
|
178 |
|
|
55c024fc7c60
Roughly implement annotation navigator.
Alexandre Segura <mex.zktk@gmail.com>
parents:
299
diff
changeset
|
179 |
@property |
| 298 | 180 |
def tag_labels(self): |
181 |
tag_list = [] |
|
182 |
for annotation in self.annotations.all(): |
|
183 |
revision_tags = json.loads( |
|
184 |
annotation.current_revision.get_tags_json()) |
|
185 |
tag_list += [tag_infos['tag_label'] |
|
186 |
for tag_infos in revision_tags if tag_infos.get('tag_label') is not None] # deal with |
|
187 |
return tag_list |
|
188 |
||
189 |
||
190 |
class ImageStats(models.Model): |
|
191 |
""" |
|
| 299 | 192 |
Stats objects for a given image, keep count of several values to be |
193 |
displayed in image and item pages |
|
| 298 | 194 |
""" |
195 |
image = models.OneToOneField( |
|
196 |
'Image', related_name='stats', blank=False, null=False) |
|
197 |
views_count = models.IntegerField(blank=True, null=True, default=0) |
|
198 |
annotations_count = models.IntegerField(blank=True, null=True, default=0) |
|
199 |
submitted_revisions_count = models.IntegerField( |
|
200 |
blank=True, null=True, default=0) |
|
201 |
comments_count = models.IntegerField(blank=True, null=True, default=0) |
|
202 |
folders_inclusion_count = models.IntegerField( |
|
203 |
blank=True, null=True, default=0) |
|
204 |
tag_count = models.IntegerField(blank=True, null=True, default=0) |
|
205 |
||
206 |
def __str__(self): |
|
207 |
return "stats:for:" + str(self.image.image_guid) |
|
208 |
||
209 |
def set_tags_stats(self): |
|
210 |
self.tag_count = Tag.objects.filter( |
|
211 |
tagginginfo__revision__annotation__image=self.image).distinct().count() |
|
212 |
||
213 |
@transaction.atomic |
|
214 |
def update_stats(self): |
|
215 |
self.annotations_count = 0 |
|
216 |
self.submitted_revisions_count = 0 |
|
217 |
self.comments_count = 0 |
|
218 |
image_annotations = Annotation.objects.filter(image=self.image) |
|
219 |
# views_count - Can't do much about views count |
|
220 |
# annotations_count |
|
221 |
self.annotations_count = image_annotations.count() |
|
222 |
# submitted_revisions_count & comment_count |
|
223 |
for annotation in image_annotations.all(): |
|
224 |
annotation_revisions = annotation.revisions |
|
225 |
self.submitted_revisions_count += annotation_revisions.count() |
|
226 |
||
227 |
self.comments_count += XtdComment.objects.for_app_models("iconolab.annotation").filter( |
|
228 |
object_pk=annotation.pk, |
|
229 |
).count() |
|
230 |
# tag_count |
|
231 |
self.tag_count = Tag.objects.filter( |
|
232 |
tagginginfo__revision__annotation__image=self.image).distinct().count() |
|
233 |
self.save() |
|
234 |
||
235 |
||
236 |
class AnnotationManager(models.Manager): |
|
237 |
""" |
|
238 |
Manager class for annotation, it handles annotation creation (with initial revision creation, and |
|
239 |
has methods to get a list of annotation commented for a given user, and a list of annotations contributed for a |
|
240 |
given user |
|
241 |
""" |
|
242 |
@transaction.atomic |
|
243 |
def create_annotation(self, author, image, title='', description='', fragment='', tags_json='[]'): |
|
244 |
""" |
|
245 |
Creates a new Annotation with its associated AnnotationStats and initial AnnotationRevision |
|
246 |
""" |
|
247 |
# Create annotation object |
|
248 |
new_annotation = Annotation( |
|
249 |
image=image, |
|
250 |
author=author |
|
251 |
) |
|
252 |
new_annotation.save() |
|
253 |
||
254 |
# Create initial revision |
|
255 |
initial_revision = AnnotationRevision( |
|
256 |
annotation=new_annotation, |
|
257 |
author=author, |
|
258 |
title=title, |
|
259 |
description=description, |
|
260 |
fragment=fragment, |
|
261 |
state=AnnotationRevision.ACCEPTED |
|
262 |
) |
|
263 |
initial_revision.save() |
|
264 |
initial_revision.set_tags(tags_json) |
|
265 |
||
266 |
# Create stats object |
|
267 |
new_annotation_stats = AnnotationStats(annotation=new_annotation) |
|
268 |
new_annotation_stats.set_tags_stats() |
|
269 |
new_annotation_stats.save() |
|
270 |
||
271 |
# Link everything to parent |
|
272 |
new_annotation.current_revision = initial_revision |
|
273 |
new_annotation.stats = new_annotation_stats |
|
274 |
new_annotation.save() |
|
275 |
iconolab_signals.revision_created.send( |
|
276 |
sender=AnnotationRevision, instance=initial_revision) |
|
277 |
return new_annotation |
|
278 |
||
279 |
@transaction.atomic |
|
280 |
def get_annotations_contributed_for_user(self, user): |
|
281 |
""" |
|
282 |
user is the user whom we want to get the contributed annotations |
|
283 |
||
284 |
Returns the list of all the annotations on which the user submitted |
|
285 |
a revision but did not create the annotation |
|
286 |
List of dict in the format: |
|
287 |
||
288 |
{ |
|
289 |
"annotation_obj": annotation object, |
|
290 |
"revisions_count": revisions count for user |
|
291 |
"awaiting_count": awaiting revisions for user on this annotation |
|
292 |
"accepted_count": accepted revisions for user |
|
293 |
"latest_submitted_revision": date of the latest submitted revision |
|
294 |
from user on annotation |
|
295 |
} |
|
296 |
""" |
|
297 |
latest_revision_on_annotations = [] |
|
298 |
user_contributed_annotations = Annotation.objects.filter(revisions__author=user).exclude(author=user).prefetch_related( |
|
299 |
'current_revision', |
|
300 |
'revisions', |
|
301 |
'image', |
|
302 |
'image__item', |
|
303 |
'image__item__collection').distinct() |
|
304 |
for annotation in user_contributed_annotations.all(): |
|
305 |
latest_revision_on_annotations.append( |
|
306 |
annotation.revisions.filter(author=user).latest(field_name="created")) |
|
307 |
contributed_list = [] |
|
308 |
if latest_revision_on_annotations: |
|
309 |
latest_revision_on_annotations.sort( |
|
310 |
key=lambda item: item.created, reverse=True) |
|
311 |
contributed_list = [ |
|
312 |
{ |
|
313 |
"annotation_obj": revision.annotation, |
|
314 |
"revisions_count": revision.annotation.revisions.filter(author=user).count(), |
|
315 |
"awaiting_count": revision.annotation.revisions.filter(author=user, state=AnnotationRevision.AWAITING).count(), |
|
316 |
"accepted_count": revision.annotation.revisions.filter(author=user, state=AnnotationRevision.ACCEPTED).count(), |
|
317 |
"latest_submitted_revision": revision.created |
|
318 |
} |
|
319 |
for revision in latest_revision_on_annotations |
|
320 |
] |
|
321 |
return contributed_list |
|
322 |
||
323 |
@transaction.atomic |
|
324 |
def get_annotations_commented_for_user(self, user, ignore_revisions_comments=True): |
|
325 |
""" |
|
326 |
user is the user for which we want to get the commented annotations |
|
327 |
ignore_revisions_comment allows to filter comments that are associated with a revision |
|
328 |
||
329 |
||
| 299 | 330 |
Returns a list of all annotations on which a given user commented |
331 |
with user-comments-related data |
|
| 298 | 332 |
List of dict in the format: |
333 |
||
334 |
{ |
|
335 |
"annotation_obj": annotation object, |
|
336 |
"comment_count": comment count for user |
|
337 |
"latest_comment_date": date of the latest comment from user on annotation |
|
338 |
} |
|
339 |
""" |
|
340 |
user_comments = IconolabComment.objects.filter( |
|
341 |
user=user, content_type__app_label='iconolab', content_type__model='annotation').order_by('-submit_date') |
|
342 |
if ignore_revisions_comments: |
|
343 |
user_comments = user_comments.filter(revision__isnull=True) |
|
344 |
all_user_comments_data = [ |
|
345 |
(comment.object_pk, comment.submit_date) for comment in user_comments] |
|
346 |
unique_ordered_comments_data = [] |
|
347 |
for (id, submit_date) in all_user_comments_data: |
|
348 |
if id not in [item["annotation_id"] for item in unique_ordered_comments_data]: |
|
349 |
unique_ordered_comments_data.append( |
|
350 |
{"annotation_id": id, "latest_comment_date": submit_date}) |
|
351 |
commented_annotations = Annotation.objects.filter(id__in=[item["annotation_id"] for item in unique_ordered_comments_data]).prefetch_related( |
|
352 |
'current_revision', |
|
353 |
'revisions', |
|
354 |
'image', |
|
355 |
'image__item', |
|
356 |
'image__item__collection' |
|
357 |
).distinct() |
|
358 |
sorted_annotations_list = [] |
|
359 |
for comment_data in unique_ordered_comments_data: |
|
360 |
annotation_obj = commented_annotations.get( |
|
361 |
id=comment_data["annotation_id"]) |
|
362 |
sorted_annotations_list.append( |
|
363 |
{ |
|
364 |
"annotation_obj": annotation_obj, |
|
365 |
"comment_count_for_user": user_comments.filter(object_pk=annotation_obj.id).count(), |
|
366 |
"latest_comment_date": comment_data["latest_comment_date"] |
|
367 |
} |
|
368 |
) |
|
369 |
return sorted_annotations_list |
|
370 |
||
371 |
||
372 |
class Annotation(models.Model): |
|
373 |
""" |
|
| 299 | 374 |
Annotation objects are created on a given image, each annotation have a |
375 |
list of revisions to keep track of its history, the latest revision is |
|
376 |
the 'current revision' that will be displayed by default in most pages. |
|
| 298 | 377 |
|
378 |
Annotation data (title, description, fragment) is thus stored in the revision. |
|
379 |
||
| 299 | 380 |
Annotations can be considered validated or not depending on the metacategories |
381 |
posted in their comments through the attribute validation_state. Their |
|
382 |
validation state can also be overriden and in such case we can use |
|
383 |
validation_state_overriden attribute to remember it in the model (so for |
|
384 |
instance if an admin un-validates an annotation we could block it from |
|
385 |
being validated again) |
|
| 298 | 386 |
""" |
387 |
UNVALIDATED = 0 |
|
388 |
VALIDATED = 1 |
|
389 |
VALIDATION_STATES = ( |
|
390 |
(UNVALIDATED, 'unvalidated'), |
|
391 |
(VALIDATED, 'validated'), |
|
392 |
) |
|
393 |
annotation_guid = models.UUIDField(default=uuid.uuid4, editable=False) |
|
394 |
image = models.ForeignKey( |
|
395 |
'Image', related_name='annotations', on_delete=models.CASCADE) |
|
396 |
source_revision = models.ForeignKey( |
|
397 |
'AnnotationRevision', related_name='source_related_annotation', blank=True, null=True) |
|
398 |
current_revision = models.OneToOneField( |
|
399 |
'AnnotationRevision', related_name='current_for_annotation', blank=True, null=True) |
|
400 |
author = models.ForeignKey(User, null=True) |
|
401 |
created = models.DateTimeField(auto_now_add=True, null=True) |
|
402 |
comments = GenericRelation( |
|
403 |
'IconolabComment', content_type_field='content_type_id', object_id_field='object_pk') |
|
404 |
validation_state = models.IntegerField( |
|
405 |
choices=VALIDATION_STATES, default=UNVALIDATED) |
|
406 |
validation_state_overriden = models.BooleanField(default=False) |
|
407 |
||
408 |
objects = AnnotationManager() |
|
409 |
||
410 |
def __str__(self): |
|
411 |
return str(self.annotation_guid) + ":" + self.current_revision.title |
|
412 |
||
413 |
@property |
|
414 |
def awaiting_revisions_count(self): |
|
415 |
return self.revisions.filter(state=AnnotationRevision.AWAITING).distinct().count() |
|
416 |
||
417 |
@property |
|
418 |
def accepted_revisions_count(self): |
|
419 |
return self.revisions.filter(state=AnnotationRevision.ACCEPTED).distinct().count() |
|
420 |
||
421 |
@property |
|
422 |
def rejected_revisions_count(self): |
|
423 |
return self.revisions.filter(state=AnnotationRevision.REJECTED).distinct().count() |
|
424 |
||
425 |
@property |
|
426 |
def studied_revisions_count(self): |
|
427 |
return self.revisions.filter(state=AnnotationRevision.STUDIED).distinct().count() |
|
428 |
||
429 |
@property |
|
430 |
def total_revisions_count(self): |
|
431 |
return self.revisions.distinct().count() |
|
432 |
||
433 |
@property |
|
434 |
def collection(self): |
|
435 |
return self.image.collection |
|
436 |
||
437 |
@property |
|
438 |
def tag_labels(self): |
|
439 |
current_revision_tags = json.loads( |
|
440 |
self.current_revision.get_tags_json()) |
|
441 |
return [tag_infos['tag_label'] for tag_infos in current_revision_tags if tag_infos.get('tag_label') is not None] |
|
442 |
||
443 |
def latest_revision_for_user(self, user): |
|
444 |
user_revisions = self.revisions.filter(creator=user) |
|
445 |
if user_revisions.exists(): |
|
446 |
return user_revisions.filter(creator=author).order_by("-created").first() |
|
447 |
return None |
|
448 |
||
449 |
@transaction.atomic |
|
450 |
def make_new_revision(self, author, title, description, fragment, tags_json): |
|
451 |
""" |
|
452 |
Called to create a new revision, potentially from a merge |
|
453 |
""" |
|
454 |
if author == self.author: |
|
455 |
# We're creating an automatically accepted revision |
|
456 |
new_revision_state = AnnotationRevision.ACCEPTED |
|
457 |
else: |
|
458 |
# Revision will require validation |
|
459 |
new_revision_state = AnnotationRevision.AWAITING |
|
460 |
new_revision = AnnotationRevision( |
|
461 |
annotation=self, |
|
462 |
parent_revision=self.current_revision, |
|
463 |
title=title, |
|
464 |
description=description, |
|
465 |
author=author, |
|
466 |
fragment=fragment, |
|
467 |
state=new_revision_state |
|
468 |
) |
|
469 |
new_revision.save() |
|
470 |
new_revision.set_tags(tags_json) |
|
471 |
if new_revision.state == AnnotationRevision.ACCEPTED: |
|
472 |
self.current_revision = new_revision |
|
473 |
self.save() |
|
474 |
iconolab_signals.revision_created.send( |
|
475 |
sender=AnnotationRevision, instance=new_revision) |
|
476 |
return new_revision |
|
477 |
||
478 |
@transaction.atomic |
|
479 |
def validate_existing_revision(self, revision_to_validate): |
|
480 |
""" |
|
| 299 | 481 |
Called when we're validating an awaiting revision whose parent is |
482 |
the current revision AS IT WAS CREATED |
|
| 298 | 483 |
""" |
484 |
if revision_to_validate.parent_revision == self.current_revision and revision_to_validate.state == AnnotationRevision.AWAITING: |
|
485 |
self.current_revision = revision_to_validate |
|
486 |
revision_to_validate.state = AnnotationRevision.ACCEPTED |
|
487 |
revision_to_validate.save() |
|
488 |
self.save() |
|
489 |
iconolab_signals.revision_accepted.send( |
|
490 |
sender=AnnotationRevision, instance=revision_to_validate) |
|
491 |
||
492 |
@transaction.atomic |
|
493 |
def reject_existing_revision(self, revision_to_reject): |
|
494 |
""" |
|
495 |
Called when we reject a revision |
|
496 |
""" |
|
497 |
if revision_to_reject.state == AnnotationRevision.AWAITING: |
|
498 |
revision_to_reject.state = AnnotationRevision.REJECTED |
|
499 |
revision_to_reject.save() |
|
500 |
iconolab_signals.revision_rejected.send( |
|
501 |
sender=AnnotationRevision, instance=revision_to_reject) |
|
502 |
||
503 |
@transaction.atomic |
|
504 |
def merge_existing_revision(self, title, description, fragment, tags, revision_to_merge): |
|
505 |
""" |
|
| 299 | 506 |
Called when we're validating an awaiting revision whose parent isn't |
507 |
the current revision or if the awaiting revision was modified by the annotation author |
|
| 298 | 508 |
""" |
509 |
merged_revision = self.make_new_revision( |
|
510 |
author=self.author, title=title, description=description, fragment=fragment, tags_json=tags) |
|
511 |
merged_revision.merge_parent_revision = revision_to_merge |
|
512 |
merged_revision.save() |
|
513 |
revision_to_merge.state = AnnotationRevision.STUDIED |
|
514 |
revision_to_merge.save() |
|
515 |
iconolab_signals.revision_accepted.send( |
|
516 |
sender=AnnotationRevision, instance=revision_to_merge) |
|
517 |
self.current_revision = merged_revision |
|
518 |
self.save() |
|
519 |
return merged_revision |
|
520 |
||
521 |
||
522 |
class AnnotationStats(models.Model): |
|
523 |
""" |
|
| 299 | 524 |
Stats objects for a given annotation, keep count of several values to be |
525 |
displayed in annotation pages |
|
| 298 | 526 |
""" |
527 |
annotation = models.OneToOneField( |
|
528 |
'Annotation', related_name='stats', blank=False, null=False) |
|
529 |
submitted_revisions_count = models.IntegerField( |
|
530 |
blank=True, null=True, default=1) |
|
531 |
awaiting_revisions_count = models.IntegerField( |
|
532 |
blank=True, null=True, default=0) |
|
533 |
accepted_revisions_count = models.IntegerField( |
|
534 |
blank=True, null=True, default=1) |
|
535 |
contributors_count = models.IntegerField(blank=True, null=True, default=1) |
|
536 |
views_count = models.IntegerField(blank=True, null=True, default=0) |
|
537 |
comments_count = models.IntegerField(blank=True, null=True, default=0) |
|
538 |
tag_count = models.IntegerField(blank=True, null=True, default=0) |
|
539 |
metacategories = models.ManyToManyField( |
|
| 299 | 540 |
'MetaCategory', |
541 |
through='MetaCategoriesCountInfo', |
|
542 |
through_fields=('annotation_stats_obj', 'metacategory') |
|
543 |
) |
|
| 298 | 544 |
|
545 |
def __str__(self): |
|
546 |
return "stats:for:" + str(self.annotation.annotation_guid) |
|
547 |
||
548 |
@property |
|
549 |
def contributors(self): |
|
| 299 | 550 |
user_ids_list = self.annotation.revisions.filter( |
551 |
state__in=[AnnotationRevision.ACCEPTED, AnnotationRevision.STUDIED] |
|
552 |
).values_list("author__id", flat=True) |
|
| 298 | 553 |
return User.objects.filter(id__in=user_ids_list).distinct() |
554 |
||
555 |
@property |
|
556 |
def commenters(self): |
|
557 |
user_ids_list = IconolabComment.objects.filter( |
|
| 299 | 558 |
content_type__app_label="iconolab", |
559 |
content_type__model="annotation", |
|
560 |
object_pk=self.annotation.id |
|
561 |
).values_list("user__id", flat=True) |
|
| 298 | 562 |
return User.objects.filter(id__in=user_ids_list).distinct() |
563 |
||
564 |
def set_tags_stats(self): |
|
565 |
self.tag_count = Tag.objects.filter( |
|
566 |
tagginginfo__revision=self.annotation.current_revision).distinct().count() |
|
567 |
||
568 |
@property |
|
569 |
def relevant_tags_count(self, score=settings.RELEVANT_TAGS_MIN_SCORE): |
|
570 |
return TaggingInfo.objects.filter(revision=self.annotation.current_revision, relevancy__gte=score).distinct().count() |
|
571 |
||
572 |
@property |
|
573 |
def accurate_tags_count(self, score=settings.ACCURATE_TAGS_MIN_SCORE): |
|
574 |
return TaggingInfo.objects.filter(revision=self.annotation.current_revision, accuracy__gte=score).distinct().count() |
|
575 |
||
576 |
@transaction.atomic |
|
577 |
def update_stats(self): |
|
578 |
# views_count - Can't do much about views count |
|
579 |
# submitted_revisions_count |
|
580 |
annotation_revisions = self.annotation.revisions |
|
581 |
self.submitted_revisions_count = annotation_revisions.count() |
|
582 |
# aawaiting_revisions_count |
|
583 |
self.awaiting_revisions_count = annotation_revisions.filter( |
|
584 |
state=AnnotationRevision.AWAITING).count() |
|
585 |
# accepted_revisions_count |
|
586 |
self.accepted_revisions_count = annotation_revisions.filter(state=AnnotationRevision.ACCEPTED).count( |
|
587 |
) + annotation_revisions.filter(state=AnnotationRevision.STUDIED).count() |
|
588 |
# comment_count |
|
589 |
self.comments_count = XtdComment.objects.for_app_models("iconolab.annotation").filter( |
|
590 |
object_pk=self.annotation.pk, |
|
591 |
).count() |
|
592 |
# contributors_count |
|
593 |
self.contributors_count = len(self.contributors) |
|
594 |
# tag_count |
|
595 |
||
596 |
annotation_comments_with_metacategories = IconolabComment.objects.filter( |
|
597 |
content_type__app_label="iconolab", |
|
598 |
content_type__model="annotation", |
|
599 |
object_pk=self.annotation.id, |
|
600 |
metacategories__collection=self.annotation.image.item.collection |
|
601 |
) |
|
602 |
m2m_objects = MetaCategoriesCountInfo.objects.filter( |
|
603 |
annotation_stats_obj=self) |
|
604 |
for obj in m2m_objects.all(): |
|
605 |
obj.count = 0 |
|
606 |
obj.save() |
|
607 |
for comment in annotation_comments_with_metacategories.all(): |
|
608 |
for metacategory in comment.metacategories.all(): |
|
609 |
if metacategory not in self.metacategories.all(): |
|
610 |
MetaCategoriesCountInfo.objects.create( |
|
611 |
annotation_stats_obj=self, metacategory=metacategory, count=1) |
|
612 |
else: |
|
613 |
m2m_object = MetaCategoriesCountInfo.objects.filter( |
|
614 |
annotation_stats_obj=self, metacategory=metacategory).first() |
|
615 |
m2m_object.count += 1 |
|
616 |
m2m_object.save() |
|
617 |
self.set_tags_stats() |
|
618 |
self.save() |
|
619 |
||
620 |
||
621 |
class MetaCategoriesCountInfo(models.Model): |
|
622 |
""" |
|
| 299 | 623 |
M2M class to keep a count of a given metacategory on a given annotation. |
624 |
Metacategories are linked to comments, themselve linked to an annotation |
|
| 298 | 625 |
""" |
626 |
annotation_stats_obj = models.ForeignKey( |
|
627 |
'AnnotationStats', on_delete=models.CASCADE) |
|
628 |
metacategory = models.ForeignKey('MetaCategory', on_delete=models.CASCADE) |
|
629 |
count = models.IntegerField(default=1, blank=False, null=False) |
|
630 |
||
631 |
def __str__(self): |
|
632 |
return "metacategory_count_for:" + self.metacategory.label + ":on:" + str(self.annotation_stats_obj.annotation.annotation_guid) |
|
633 |
||
634 |
||
635 |
class AnnotationRevision(models.Model): |
|
636 |
""" |
|
637 |
AnnotationRevisions objects are linked to an annotation and store the data of the annotation at a given time |
|
638 |
||
639 |
A revision is always in one out of multiple states: |
|
640 |
||
| 299 | 641 |
- Awaiting: the revision has been submitted but must be validated by the |
642 |
original author of the related annotation |
|
643 |
- Accepted: the revision has been accepted *as-is* by the author of the |
|
644 |
related annotation (this happens automatically if the revision is created |
|
645 |
by the author of the annotation) |
|
| 298 | 646 |
- Rejected: the revision has been rejected by the author of the related annotation |
| 299 | 647 |
- Studied: the revision has been studied by the author of the related |
648 |
annotation and was either modified or at the very least compared with the current state |
|
649 |
through the merge interface, thus creating a new revision merging the |
|
650 |
current state with the proposal. At this point the proposal is flagged |
|
651 |
as "studied" to show that the author of the original annotation has considered it |
|
| 298 | 652 |
""" |
653 |
AWAITING = 0 |
|
654 |
ACCEPTED = 1 |
|
655 |
REJECTED = 2 |
|
656 |
STUDIED = 3 |
|
657 |
||
658 |
REVISION_STATES = ( |
|
659 |
(AWAITING, 'awaiting'), |
|
660 |
(ACCEPTED, 'accepted'), |
|
661 |
(REJECTED, 'rejected'), |
|
662 |
(STUDIED, 'studied'), |
|
663 |
) |
|
664 |
||
665 |
revision_guid = models.UUIDField(default=uuid.uuid4) |
|
666 |
annotation = models.ForeignKey( |
|
667 |
'Annotation', related_name='revisions', null=False, blank=False) |
|
668 |
parent_revision = models.ForeignKey( |
|
669 |
'AnnotationRevision', related_name='child_revisions', blank=True, null=True) |
|
670 |
merge_parent_revision = models.ForeignKey( |
|
671 |
'AnnotationRevision', related_name='child_revisions_merge', blank=True, null=True) |
|
672 |
author = models.ForeignKey(User, null=True) |
|
673 |
title = models.CharField(max_length=255) |
|
674 |
description = models.TextField(null=True) |
|
675 |
fragment = models.TextField() |
|
676 |
tags = models.ManyToManyField( |
|
677 |
'Tag', through='TaggingInfo', through_fields=('revision', 'tag')) |
|
678 |
state = models.IntegerField(choices=REVISION_STATES, default=AWAITING) |
|
679 |
created = models.DateTimeField(auto_now_add=True, null=True) |
|
680 |
||
681 |
def __str__(self): |
|
682 |
return str(self.revision_guid) + ":" + self.title |
|
683 |
||
684 |
def set_tags(self, tags_json_string): |
|
685 |
""" |
|
686 |
This method creates tags object and links them to the revision, from a given json that has the following format: |
|
687 |
||
688 |
[ |
|
689 |
{ |
|
| 299 | 690 |
"tag_input": the tag string that has been provided. If it is |
691 |
an http(s?):// pattern, it means the tag is external, else |
|
692 |
it means it is a custom tag |
|
| 298 | 693 |
"accuracy": the accuracy value provided by the user |
694 |
"relevancy": the relevancy value provided by the user |
|
695 |
}, |
|
696 |
{ |
|
697 |
... |
|
698 |
} |
|
699 |
] |
|
700 |
""" |
|
701 |
try: |
|
702 |
tags_dict = json.loads(tags_json_string) |
|
703 |
except ValueError: |
|
704 |
pass |
|
705 |
for tag_data in tags_dict: |
|
706 |
tag_string = tag_data.get("tag_input") |
|
|
323
55c024fc7c60
Roughly implement annotation navigator.
Alexandre Segura <mex.zktk@gmail.com>
parents:
299
diff
changeset
|
707 |
tag_label = tag_data.get("tag_label") |
| 298 | 708 |
tag_accuracy = tag_data.get("accuracy", 0) |
709 |
tag_relevancy = tag_data.get("relevancy", 0) |
|
710 |
||
711 |
# check if url |
|
712 |
if tag_string.startswith("http://") or tag_string.startswith("https://"): |
|
713 |
# check if tag already exists |
|
714 |
if Tag.objects.filter(link=tag_string).exists(): |
|
715 |
tag_obj = Tag.objects.get(link=tag_string) |
|
716 |
else: |
|
717 |
tag_obj = Tag.objects.create( |
|
718 |
link=tag_string, |
|
|
323
55c024fc7c60
Roughly implement annotation navigator.
Alexandre Segura <mex.zktk@gmail.com>
parents:
299
diff
changeset
|
719 |
label=tag_label |
| 298 | 720 |
) |
721 |
else: |
|
722 |
new_tag_link = settings.BASE_URL + '/' + slugify(tag_string) |
|
723 |
if Tag.objects.filter(link=new_tag_link).exists(): |
|
724 |
# Somehow we received a label for an existing tag |
|
725 |
tag_obj = Tag.objects.get(link=new_tag_link) |
|
726 |
else: |
|
727 |
tag_obj = Tag.objects.create( |
|
728 |
label=tag_string, |
|
729 |
label_slug=slugify(tag_string), |
|
730 |
description="", |
|
731 |
link=settings.INTERNAL_TAGS_URL + |
|
732 |
'/' + slugify(tag_string), |
|
733 |
collection=self.annotation.image.item.collection |
|
734 |
) |
|
735 |
tag_info = TaggingInfo.objects.create( |
|
736 |
tag=tag_obj, |
|
737 |
revision=self, |
|
738 |
accuracy=tag_accuracy, |
|
739 |
relevancy=tag_relevancy |
|
740 |
) |
|
741 |
||
|
323
55c024fc7c60
Roughly implement annotation navigator.
Alexandre Segura <mex.zktk@gmail.com>
parents:
299
diff
changeset
|
742 |
# FIXME Avoid calling DBPedia all the time |
| 298 | 743 |
def get_tags_json(self): |
744 |
""" |
|
| 299 | 745 |
This method returns the json data that will be sent to the js to display |
746 |
tags for the revision. |
|
| 298 | 747 |
|
748 |
The json data returned will be of the following format: |
|
749 |
||
750 |
[ |
|
751 |
{ |
|
752 |
"tag_label": the tag label for display purposes, |
|
753 |
"tag_link": the link of the tag, for instance for dbpedia links, |
|
754 |
"accuracy": the accuracy value of the tag, |
|
755 |
"relevancy": the relevancy value of the tag, |
|
| 299 | 756 |
"is_internal": will be True if the tag is 'internal', |
757 |
meaning specific to Iconolab and |
|
| 298 | 758 |
not an external tag like a dbpedia reference for instance |
759 |
}, |
|
760 |
{ |
|
761 |
... |
|
762 |
} |
|
763 |
] |
|
764 |
""" |
|
765 |
def fetch_from_dbpedia(uri, lang, source): |
|
766 |
sparql_template = 'select distinct * where { <<%uri%>> rdfs:label ?l FILTER( langMatches( lang(?l), "<%lang%>" ) ) }' |
|
767 |
sparql_query = re.sub("<%uri%>", uri, re.sub( |
|
768 |
"<%lang%>", lang, sparql_template)) |
|
769 |
sparql_query_url = source + 'sparql' |
|
770 |
try: |
|
771 |
dbpedia_resp = requests.get( |
|
772 |
sparql_query_url, |
|
773 |
params={ |
|
774 |
"query": sparql_query, |
|
775 |
"format": "json" |
|
776 |
} |
|
777 |
) |
|
778 |
except: |
|
779 |
# dbpedia is down, will be handled with database label |
|
780 |
pass |
|
781 |
try: |
|
782 |
results = json.loads(dbpedia_resp.text).get("results", {}) |
|
783 |
except: |
|
784 |
# if error with json, results is empty |
|
785 |
results = {} |
|
786 |
variable_bindings = results.get("bindings", None) |
|
787 |
label_data = {} |
|
788 |
if variable_bindings: |
|
789 |
label_data = variable_bindings.pop() |
|
790 |
return label_data.get("l", {"value": False}).get("value") |
|
791 |
||
792 |
final_list = [] |
|
793 |
for tagging_info in self.tagginginfo_set.select_related("tag").all(): |
|
794 |
if tagging_info.tag.is_internal(): |
|
795 |
final_list.append({ |
|
796 |
"tag_label": tagging_info.tag.label, |
|
797 |
"tag_link": tagging_info.tag.link, |
|
798 |
"accuracy": tagging_info.accuracy, |
|
799 |
"relevancy": tagging_info.relevancy, |
|
800 |
"is_internal": tagging_info.tag.is_internal() |
|
801 |
}) |
|
802 |
else: |
|
803 |
tag_link = tagging_info.tag.link |
|
804 |
# import label from external |
|
805 |
externaL_repos_fetch_dict = { |
|
806 |
"http://dbpedia.org/": fetch_from_dbpedia, |
|
807 |
"http://fr.dbpedia.org/": fetch_from_dbpedia |
|
808 |
} |
|
809 |
try: |
|
810 |
(source, fetch_label) = next( |
|
811 |
item for item in externaL_repos_fetch_dict.items() if tag_link.startswith(item[0])) |
|
812 |
tag_label = fetch_label(tag_link, "fr", source) |
|
813 |
if not tag_label: # Error happened and we got False as a fetch return |
|
814 |
tag_label = tagging_info.tag.label |
|
815 |
else: |
|
816 |
tagging_info.tag.label = tag_label |
|
817 |
tagging_info.tag.save() |
|
818 |
final_list.append({ |
|
819 |
"tag_label": tag_label, |
|
820 |
"tag_link": tag_link, |
|
821 |
"accuracy": tagging_info.accuracy, |
|
822 |
"relevancy": tagging_info.relevancy, |
|
823 |
"is_internal": tagging_info.tag.is_internal() |
|
824 |
}) |
|
825 |
except StopIteration: |
|
826 |
pass |
|
827 |
return json.dumps(final_list) |
|
828 |
||
829 |
||
830 |
class Tag(models.Model): |
|
831 |
""" |
|
832 |
Tag objects that are linked to revisions. |
|
833 |
||
834 |
Each tag is linked to a specific collection, this is important for internal tags |
|
835 |
so each collection can build its own vocabulary |
|
836 |
""" |
|
837 |
label = models.CharField(max_length=255, blank=True, null=True) |
|
838 |
label_slug = models.SlugField(blank=True, null=True) |
|
839 |
link = models.URLField(unique=True) |
|
840 |
description = models.CharField(max_length=255, blank=True, null=True) |
|
841 |
collection = models.ForeignKey('Collection', blank=True, null=True) |
|
842 |
||
843 |
def is_internal(self): |
|
844 |
return self.link.startswith(settings.INTERNAL_TAGS_URL) |
|
845 |
||
846 |
def __str__(self): |
|
|
323
55c024fc7c60
Roughly implement annotation navigator.
Alexandre Segura <mex.zktk@gmail.com>
parents:
299
diff
changeset
|
847 |
return "Tag:" + self.label |
| 298 | 848 |
|
849 |
||
850 |
class TaggingInfo(models.Model): |
|
851 |
""" |
|
| 299 | 852 |
M2M object for managing tag relation to a revision with its associated |
853 |
relevancy and accuracy |
|
| 298 | 854 |
""" |
855 |
revision = models.ForeignKey( |
|
856 |
'AnnotationRevision', on_delete=models.CASCADE) |
|
857 |
tag = models.ForeignKey('Tag', on_delete=models.CASCADE) |
|
858 |
accuracy = models.IntegerField() |
|
859 |
relevancy = models.IntegerField() |
|
860 |
||
861 |
def __str__(self): |
|
|
323
55c024fc7c60
Roughly implement annotation navigator.
Alexandre Segura <mex.zktk@gmail.com>
parents:
299
diff
changeset
|
862 |
return str(str(self.tag.label) + ":to:" + str(self.revision.revision_guid)) |
| 298 | 863 |
|
864 |
||
865 |
class IconolabComment(XtdComment): |
|
866 |
""" |
|
| 299 | 867 |
Comment objects that extends XtdComment model, itself extending the |
868 |
django-contrib-comments model. |
|
| 298 | 869 |
|
870 |
Each comment can have 0 or 1 revision, if it is a comment created alongside a revision |
|
871 |
Each comment can have a set of metacategories |
|
872 |
""" |
|
873 |
revision = models.ForeignKey( |
|
874 |
'AnnotationRevision', related_name='creation_comment', null=True, blank=True) |
|
875 |
metacategories = models.ManyToManyField( |
|
876 |
'MetaCategory', through='MetaCategoryInfo', through_fields=('comment', 'metacategory')) |
|
877 |
||
878 |
objects = XtdComment.objects |
|
879 |
||
880 |
def __str__(self): |
|
881 |
return str(self.id) |
|
882 |
||
883 |
class Meta: |
|
884 |
ordering = ["thread_id", "id"] |
|
885 |
||
886 |
@property |
|
887 |
def annotation(self): |
|
888 |
if self.content_type.app_label == "iconolab" and self.content_type.model == "annotation": |
|
889 |
return Annotation.objects.get(pk=self.object_pk) |
|
890 |
return None |
|
891 |
||
892 |
def get_comment_page(self): |
|
893 |
""" |
|
| 299 | 894 |
Shortcut function to get page for considered comment, with |
895 |
COMMENTS_PER_PAGE_DEFAULT comments per page, used for notifications links generation |
|
| 298 | 896 |
""" |
897 |
return (IconolabComment.objects.for_app_models("iconolab.annotation").filter( |
|
898 |
object_pk=self.object_pk, |
|
899 |
).filter(thread_id__gte=self.thread_id).filter(order__lte=self.order).count() + 1) // settings.COMMENTS_PER_PAGE_DEFAULT + 1 |
|
900 |
||
901 |
||
902 |
class MetaCategory(models.Model): |
|
903 |
""" |
|
| 299 | 904 |
Metacategories are objects that can be linked to a comment to augment it |
905 |
with meaning (depending on the metacategories defined for a given collection) |
|
| 298 | 906 |
|
| 299 | 907 |
Metacategories can trigger notifications when they are linked to a given |
908 |
coment depending on their trigger_notifications property: |
|
| 298 | 909 |
|
910 |
- NONE : Notifies nobody |
|
911 |
- CONTRIBUTORS : Notifies contributors (revision owners) on target annotation |
|
912 |
- COMMENTERS : Notifies commenters (contributors + comment owners) on target annotation |
|
913 |
- COLLECTION_ADMINS : Notifies collection admins |
|
914 |
||
| 299 | 915 |
Metacategories can be used to consider an annotation as "validated" if a |
916 |
certain agreement threshold is reached using their validation_value property |
|
| 298 | 917 |
|
918 |
- NEUTRAL : The metacategory doesn't affect the validation state |
|
| 299 | 919 |
- AGREEMENT : The metacategory can be used to validate the annotation |
920 |
when linked to a comment on said annotation |
|
921 |
- DISAGREEMENT : The metacategory can be used to unvalidate the annotation |
|
922 |
when linked to a comment on said annotation |
|
| 298 | 923 |
|
924 |
""" |
|
925 |
NONE = 0 |
|
926 |
CONTRIBUTORS = 1 |
|
927 |
COMMENTERS = 2 |
|
928 |
COLLECTION_ADMINS = 3 |
|
929 |
NOTIFIED_USERS = ( |
|
930 |
(NONE, 'none'), |
|
931 |
(CONTRIBUTORS, 'contributors'), |
|
932 |
(COMMENTERS, 'commenters'), |
|
933 |
(COLLECTION_ADMINS, 'collection admins'), |
|
934 |
) |
|
935 |
||
936 |
NEUTRAL = 0 |
|
937 |
AGREEMENT = 1 |
|
938 |
DISAGREEMENT = 2 |
|
939 |
VALIDATION_VALUES = ( |
|
940 |
(NEUTRAL, 'neutral'), |
|
941 |
(AGREEMENT, 'agreement'), |
|
942 |
(DISAGREEMENT, 'disagreement'), |
|
943 |
) |
|
944 |
||
945 |
collection = models.ForeignKey(Collection, related_name="metacategories") |
|
946 |
label = models.CharField(max_length=255) |
|
947 |
triggers_notifications = models.IntegerField( |
|
948 |
choices=NOTIFIED_USERS, default=NONE) |
|
949 |
validation_value = models.IntegerField( |
|
950 |
choices=VALIDATION_VALUES, default=NEUTRAL) |
|
951 |
||
952 |
def __str__(self): |
|
953 |
return self.label + ":" + self.collection.name |
|
954 |
||
955 |
||
956 |
class MetaCategoryInfo(models.Model): |
|
957 |
""" |
|
958 |
M2M class linking comments and metacategories |
|
959 |
""" |
|
960 |
comment = models.ForeignKey('IconolabComment', on_delete=models.CASCADE) |
|
961 |
metacategory = models.ForeignKey('MetaCategory', on_delete=models.CASCADE) |
|
962 |
||
963 |
def __str__(self): |
|
964 |
return "metacategory:" + self.metacategory.label + ":on:" + self.comment.id |
|
965 |
||
966 |
||
967 |
class CommentAttachement(models.Model): |
|
968 |
""" |
|
969 |
This class is supposed to represent added resources to a given comment |
|
970 |
Not implemented as of v0.0.19 |
|
971 |
""" |
|
972 |
LINK = 0 |
|
973 |
IMAGE = 1 |
|
974 |
PDF = 2 |
|
975 |
COMMENT_CHOICES = ( |
|
976 |
(LINK, 'link'), |
|
977 |
(IMAGE, 'image'), |
|
978 |
(PDF, 'pdf') |
|
979 |
) |
|
980 |
||
981 |
comment = models.ForeignKey( |
|
982 |
'IconolabComment', related_name='attachments', on_delete=models.CASCADE) |
|
983 |
attachment_type = models.IntegerField(choices=COMMENT_CHOICES, default=0) |
|
984 |
data = models.TextField(blank=False) |
|
985 |
||
986 |
||
987 |
class UserProfile(models.Model): |
|
988 |
""" |
|
989 |
UserProfile objects are extensions of user model |
|
990 |
||
| 299 | 991 |
As of v0.0.19 they are used to define collection admins. Each user can |
992 |
thus managed 0-N collections. |
|
| 298 | 993 |
""" |
994 |
user = models.OneToOneField( |
|
995 |
User, related_name='profile', on_delete=models.CASCADE) |
|
996 |
managed_collections = models.ManyToManyField( |
|
997 |
'Collection', related_name='admins', blank=True) |
|
998 |
||
999 |
def __str__(self): |
|
1000 |
return "profile:" + self.user.username |