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