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