src/iconolab/models.py
author Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
Tue, 23 Aug 2016 18:01:08 +0200
changeset 151 797460904f77
parent 139 3e0a5286b257
child 176 ef200c865025
permissions -rw-r--r--
indexing using tag labels
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
24
6b6b183447a2 work on models + auth register/login system + adapted existing app and templates so editing annotations is working as before + created empty templates to fill
durandn
parents: 13
diff changeset
     1
from django.db import models, transaction
33
f9d4c9a63e4e Backend work on tags (needs testing) + model changes (image_guid, Tag)
durandn
parents: 30
diff changeset
     2
from django.conf import settings
6
37baf9d13f32 first commit
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
     3
from django.contrib.auth.models import User
37baf9d13f32 first commit
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
     4
from django.contrib.contenttypes.fields import GenericForeignKey
37baf9d13f32 first commit
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
     5
from django.contrib.contenttypes.models import ContentType
37
aed809b3a075 Corrected Tag methods bugs + Added comment classes and migration
durandn
parents: 35
diff changeset
     6
from django_comments_xtd.models import XtdComment
48
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
     7
from django.utils.text import slugify
90
8d7815ecd211 signals are now sent in model methods rather than views
durandn
parents: 89
diff changeset
     8
import iconolab.signals.handlers as iconolab_signals
48
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
     9
import uuid, json, re, requests, urllib
6
37baf9d13f32 first commit
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
    10
37baf9d13f32 first commit
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
    11
37baf9d13f32 first commit
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
    12
class Tag(models.Model):
104
3c4150867fe7 Added fallback tag label storing in database for external tags in case external source is down (rudimentary)
durandn
parents: 101
diff changeset
    13
    label = models.CharField(max_length=255, blank=True, null=True)
3c4150867fe7 Added fallback tag label storing in database for external tags in case external source is down (rudimentary)
durandn
parents: 101
diff changeset
    14
    label_slug = models.SlugField(blank=True, null=True)
48
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
    15
    link = models.URLField(unique=True)
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
    16
    description = models.CharField(max_length=255, blank=True, null=True)
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
    17
    collection = models.ForeignKey('Collection', blank=True, null=True)
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
    18
    
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
    19
    def is_internal(self):
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
    20
        return self.link.startswith(settings.INTERNAL_TAGS_URL)
37
aed809b3a075 Corrected Tag methods bugs + Added comment classes and migration
durandn
parents: 35
diff changeset
    21
133
088195b2a6cc iconolab profile to user relation is now one to one field + added panel for collection admin user tools (to be added) in user home + (simple) django admin for iconolab objects
durandn
parents: 132
diff changeset
    22
    def __str__(self):
088195b2a6cc iconolab profile to user relation is now one to one field + added panel for collection admin user tools (to be added) in user home + (simple) django admin for iconolab objects
durandn
parents: 132
diff changeset
    23
        return self.label_slug+":"+self.label
6
37baf9d13f32 first commit
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
    24
24
6b6b183447a2 work on models + auth register/login system + adapted existing app and templates so editing annotations is working as before + created empty templates to fill
durandn
parents: 13
diff changeset
    25
class TaggingInfo(models.Model):
48
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
    26
    revision = models.ForeignKey('AnnotationRevision', on_delete=models.CASCADE)
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
    27
    tag = models.ForeignKey('Tag', on_delete=models.CASCADE)
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
    28
    accuracy = models.IntegerField()
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
    29
    relevancy = models.IntegerField()
133
088195b2a6cc iconolab profile to user relation is now one to one field + added panel for collection admin user tools (to be added) in user home + (simple) django admin for iconolab objects
durandn
parents: 132
diff changeset
    30
    
088195b2a6cc iconolab profile to user relation is now one to one field + added panel for collection admin user tools (to be added) in user home + (simple) django admin for iconolab objects
durandn
parents: 132
diff changeset
    31
    def __str__(self):
088195b2a6cc iconolab profile to user relation is now one to one field + added panel for collection admin user tools (to be added) in user home + (simple) django admin for iconolab objects
durandn
parents: 132
diff changeset
    32
        return self.tag.label_slug+":to:"+self.revision.revision_guid
6
37baf9d13f32 first commit
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
    33
37
aed809b3a075 Corrected Tag methods bugs + Added comment classes and migration
durandn
parents: 35
diff changeset
    34
6
37baf9d13f32 first commit
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
    35
class Collection(models.Model):
64
4d1e369e85d4 Added a verbose name field for collection objects, home view now gets a list of all collections
durandn
parents: 62
diff changeset
    36
    name = models.SlugField(max_length=50, unique=True)
4d1e369e85d4 Added a verbose name field for collection objects, home view now gets a list of all collections
durandn
parents: 62
diff changeset
    37
    verbose_name = models.CharField(max_length=50, null=True, blank=True)
126
b5aa7e6f6a01 updated apps in dev.py.tmpl and deleted unused code
durandn
parents: 123
diff changeset
    38
    description = models.TextField(null=True)
122
4fab4a909915 Added image field in collection for homepage view
durandn
parents: 116
diff changeset
    39
    image = models.ImageField(upload_to='uploads/', height_field='height', width_field='width', null=True, blank=True)
4fab4a909915 Added image field in collection for homepage view
durandn
parents: 116
diff changeset
    40
    height = models.IntegerField(null=True, blank=True)
4fab4a909915 Added image field in collection for homepage view
durandn
parents: 116
diff changeset
    41
    width = models.IntegerField(null=True, blank=True)
6
37baf9d13f32 first commit
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
    42
48
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
    43
    def __str__(self):
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
    44
        return self.name
6
37baf9d13f32 first commit
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
    45
24
6b6b183447a2 work on models + auth register/login system + adapted existing app and templates so editing annotations is working as before + created empty templates to fill
durandn
parents: 13
diff changeset
    46
31
e34b70a00488 adding annotations list
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents: 30
diff changeset
    47
class Item(models.Model):
48
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
    48
    collection = models.ForeignKey(Collection, related_name="items")
65
625ed1ba472f Work on item model + item view to show items with multiple images instead of image detail + fix thumbnails in templates
durandn
parents: 64
diff changeset
    49
    item_guid = models.UUIDField(default=uuid.uuid4, editable=False)
133
088195b2a6cc iconolab profile to user relation is now one to one field + added panel for collection admin user tools (to be added) in user home + (simple) django admin for iconolab objects
durandn
parents: 132
diff changeset
    50
    
088195b2a6cc iconolab profile to user relation is now one to one field + added panel for collection admin user tools (to be added) in user home + (simple) django admin for iconolab objects
durandn
parents: 132
diff changeset
    51
    def __str__(self):
088195b2a6cc iconolab profile to user relation is now one to one field + added panel for collection admin user tools (to be added) in user home + (simple) django admin for iconolab objects
durandn
parents: 132
diff changeset
    52
        return str(self.item_guid)+":from:"+self.collection.name
48
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
    53
    
24
6b6b183447a2 work on models + auth register/login system + adapted existing app and templates so editing annotations is working as before + created empty templates to fill
durandn
parents: 13
diff changeset
    54
class ItemMetadata(models.Model):
48
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
    55
    item = models.OneToOneField('Item', related_name='metadatas')
110
ce46bbafb079 image import command + proper item metadatas
durandn
parents: 106
diff changeset
    56
    authors = models.CharField(max_length=255, default="")
ce46bbafb079 image import command + proper item metadatas
durandn
parents: 106
diff changeset
    57
    school = models.CharField(max_length=255, default="")
ce46bbafb079 image import command + proper item metadatas
durandn
parents: 106
diff changeset
    58
    designation = models.CharField(max_length=255, default="")
ce46bbafb079 image import command + proper item metadatas
durandn
parents: 106
diff changeset
    59
    datation = models.CharField(max_length=255, default="")
ce46bbafb079 image import command + proper item metadatas
durandn
parents: 106
diff changeset
    60
    technics = models.CharField(max_length=255, default="")
ce46bbafb079 image import command + proper item metadatas
durandn
parents: 106
diff changeset
    61
    measurements = models.CharField(max_length=255, default="")
ce46bbafb079 image import command + proper item metadatas
durandn
parents: 106
diff changeset
    62
    create_or_usage_location = models.CharField(max_length=255, default="")
ce46bbafb079 image import command + proper item metadatas
durandn
parents: 106
diff changeset
    63
    discovery_context = models.CharField(max_length=255, default="")
ce46bbafb079 image import command + proper item metadatas
durandn
parents: 106
diff changeset
    64
    conservation_location = models.CharField(max_length=255, default="")
ce46bbafb079 image import command + proper item metadatas
durandn
parents: 106
diff changeset
    65
    photo_credits = models.CharField(max_length=255, default="")
ce46bbafb079 image import command + proper item metadatas
durandn
parents: 106
diff changeset
    66
    inventory_number = models.CharField(max_length=255, default="")
ce46bbafb079 image import command + proper item metadatas
durandn
parents: 106
diff changeset
    67
    joconde_ref = models.CharField(max_length=255, default="")
ce46bbafb079 image import command + proper item metadatas
durandn
parents: 106
diff changeset
    68
    
ce46bbafb079 image import command + proper item metadatas
durandn
parents: 106
diff changeset
    69
    @property
ce46bbafb079 image import command + proper item metadatas
durandn
parents: 106
diff changeset
    70
    def get_joconde_url(self):
136
81d82b1f431a generating joconde external url + small change on comment submit button + small change on __str__ for metacategories
durandn
parents: 134
diff changeset
    71
        return settings.JOCONDE_NOTICE_BASE_URL+self.joconde_ref.rjust(11, '0')
133
088195b2a6cc iconolab profile to user relation is now one to one field + added panel for collection admin user tools (to be added) in user home + (simple) django admin for iconolab objects
durandn
parents: 132
diff changeset
    72
    
088195b2a6cc iconolab profile to user relation is now one to one field + added panel for collection admin user tools (to be added) in user home + (simple) django admin for iconolab objects
durandn
parents: 132
diff changeset
    73
    def __str__(self):
088195b2a6cc iconolab profile to user relation is now one to one field + added panel for collection admin user tools (to be added) in user home + (simple) django admin for iconolab objects
durandn
parents: 132
diff changeset
    74
        return "metadatas:for:"+str(self.item.item_guid)
24
6b6b183447a2 work on models + auth register/login system + adapted existing app and templates so editing annotations is working as before + created empty templates to fill
durandn
parents: 13
diff changeset
    75
31
e34b70a00488 adding annotations list
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents: 30
diff changeset
    76
24
6b6b183447a2 work on models + auth register/login system + adapted existing app and templates so editing annotations is working as before + created empty templates to fill
durandn
parents: 13
diff changeset
    77
class ImageStats(models.Model):
48
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
    78
    image = models.OneToOneField('Image', related_name='stats', blank=False, null=False)
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
    79
    views_count = models.IntegerField(blank=True, null=True, default=0)
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
    80
    annotations_count = models.IntegerField(blank=True, null=True, default=0)
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
    81
    submitted_revisions_count = models.IntegerField(blank=True, null=True, default=0)
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
    82
    comments_count = models.IntegerField(blank=True, null=True, default=0)
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
    83
    folders_inclusion_count = models.IntegerField(blank=True, null=True, default=0)
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
    84
    tag_count = models.IntegerField(blank=True, null=True, default=0)
85
49b3f22948d5 work on stats: calculatestats command for manage.py, stats in item page, stats in annotations list, stats in annotation page, actions increment stats (view count, annotations count, comment count etc)
durandn
parents: 77
diff changeset
    85
    
133
088195b2a6cc iconolab profile to user relation is now one to one field + added panel for collection admin user tools (to be added) in user home + (simple) django admin for iconolab objects
durandn
parents: 132
diff changeset
    86
    def __str__(self):
088195b2a6cc iconolab profile to user relation is now one to one field + added panel for collection admin user tools (to be added) in user home + (simple) django admin for iconolab objects
durandn
parents: 132
diff changeset
    87
        return "stats:for:"+self.image.image_guid
088195b2a6cc iconolab profile to user relation is now one to one field + added panel for collection admin user tools (to be added) in user home + (simple) django admin for iconolab objects
durandn
parents: 132
diff changeset
    88
    
85
49b3f22948d5 work on stats: calculatestats command for manage.py, stats in item page, stats in annotations list, stats in annotation page, actions increment stats (view count, annotations count, comment count etc)
durandn
parents: 77
diff changeset
    89
    def set_tags_stats(self):
49b3f22948d5 work on stats: calculatestats command for manage.py, stats in item page, stats in annotations list, stats in annotation page, actions increment stats (view count, annotations count, comment count etc)
durandn
parents: 77
diff changeset
    90
        self.tag_count = Tag.objects.filter(tagginginfo__revision__annotation__image = self.image).distinct().count()
49b3f22948d5 work on stats: calculatestats command for manage.py, stats in item page, stats in annotations list, stats in annotation page, actions increment stats (view count, annotations count, comment count etc)
durandn
parents: 77
diff changeset
    91
    
93
30cd69620039 use atomic transactions on stat updates
durandn
parents: 90
diff changeset
    92
    @transaction.atomic
85
49b3f22948d5 work on stats: calculatestats command for manage.py, stats in item page, stats in annotations list, stats in annotation page, actions increment stats (view count, annotations count, comment count etc)
durandn
parents: 77
diff changeset
    93
    def update_stats(self):
49b3f22948d5 work on stats: calculatestats command for manage.py, stats in item page, stats in annotations list, stats in annotation page, actions increment stats (view count, annotations count, comment count etc)
durandn
parents: 77
diff changeset
    94
        self.annotations_count = 0
49b3f22948d5 work on stats: calculatestats command for manage.py, stats in item page, stats in annotations list, stats in annotation page, actions increment stats (view count, annotations count, comment count etc)
durandn
parents: 77
diff changeset
    95
        self.submitted_revisions_count = 0
49b3f22948d5 work on stats: calculatestats command for manage.py, stats in item page, stats in annotations list, stats in annotation page, actions increment stats (view count, annotations count, comment count etc)
durandn
parents: 77
diff changeset
    96
        self.comments_count = 0
49b3f22948d5 work on stats: calculatestats command for manage.py, stats in item page, stats in annotations list, stats in annotation page, actions increment stats (view count, annotations count, comment count etc)
durandn
parents: 77
diff changeset
    97
        image_annotations = Annotation.objects.filter(image=self.image)
49b3f22948d5 work on stats: calculatestats command for manage.py, stats in item page, stats in annotations list, stats in annotation page, actions increment stats (view count, annotations count, comment count etc)
durandn
parents: 77
diff changeset
    98
        # views_count - Can't do much about views count
49b3f22948d5 work on stats: calculatestats command for manage.py, stats in item page, stats in annotations list, stats in annotation page, actions increment stats (view count, annotations count, comment count etc)
durandn
parents: 77
diff changeset
    99
        # annotations_count
49b3f22948d5 work on stats: calculatestats command for manage.py, stats in item page, stats in annotations list, stats in annotation page, actions increment stats (view count, annotations count, comment count etc)
durandn
parents: 77
diff changeset
   100
        self.annotations_count = image_annotations.count()
49b3f22948d5 work on stats: calculatestats command for manage.py, stats in item page, stats in annotations list, stats in annotation page, actions increment stats (view count, annotations count, comment count etc)
durandn
parents: 77
diff changeset
   101
        # submitted_revisions_count & comment_count
49b3f22948d5 work on stats: calculatestats command for manage.py, stats in item page, stats in annotations list, stats in annotation page, actions increment stats (view count, annotations count, comment count etc)
durandn
parents: 77
diff changeset
   102
        for annotation in image_annotations.all():
49b3f22948d5 work on stats: calculatestats command for manage.py, stats in item page, stats in annotations list, stats in annotation page, actions increment stats (view count, annotations count, comment count etc)
durandn
parents: 77
diff changeset
   103
            annotation_revisions = annotation.revisions
49b3f22948d5 work on stats: calculatestats command for manage.py, stats in item page, stats in annotations list, stats in annotation page, actions increment stats (view count, annotations count, comment count etc)
durandn
parents: 77
diff changeset
   104
            self.submitted_revisions_count += annotation_revisions.count()
49b3f22948d5 work on stats: calculatestats command for manage.py, stats in item page, stats in annotations list, stats in annotation page, actions increment stats (view count, annotations count, comment count etc)
durandn
parents: 77
diff changeset
   105
            
49b3f22948d5 work on stats: calculatestats command for manage.py, stats in item page, stats in annotations list, stats in annotation page, actions increment stats (view count, annotations count, comment count etc)
durandn
parents: 77
diff changeset
   106
            self.comments_count += XtdComment.objects.for_app_models("iconolab.annotation").filter(
49b3f22948d5 work on stats: calculatestats command for manage.py, stats in item page, stats in annotations list, stats in annotation page, actions increment stats (view count, annotations count, comment count etc)
durandn
parents: 77
diff changeset
   107
                object_pk = annotation.pk,
49b3f22948d5 work on stats: calculatestats command for manage.py, stats in item page, stats in annotations list, stats in annotation page, actions increment stats (view count, annotations count, comment count etc)
durandn
parents: 77
diff changeset
   108
            ).count()
49b3f22948d5 work on stats: calculatestats command for manage.py, stats in item page, stats in annotations list, stats in annotation page, actions increment stats (view count, annotations count, comment count etc)
durandn
parents: 77
diff changeset
   109
        # tag_count
49b3f22948d5 work on stats: calculatestats command for manage.py, stats in item page, stats in annotations list, stats in annotation page, actions increment stats (view count, annotations count, comment count etc)
durandn
parents: 77
diff changeset
   110
        self.tag_count = Tag.objects.filter(tagginginfo__revision__annotation__image = self.image).distinct().count()
49b3f22948d5 work on stats: calculatestats command for manage.py, stats in item page, stats in annotations list, stats in annotation page, actions increment stats (view count, annotations count, comment count etc)
durandn
parents: 77
diff changeset
   111
        self.save()
24
6b6b183447a2 work on models + auth register/login system + adapted existing app and templates so editing annotations is working as before + created empty templates to fill
durandn
parents: 13
diff changeset
   112
101
eb856f9b518c fixed comment page calculation
durandn
parents: 100
diff changeset
   113
6
37baf9d13f32 first commit
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   114
class Image(models.Model):
48
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   115
    image_guid = models.UUIDField(default=uuid.uuid4, editable=False)
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   116
    name = models.CharField(max_length=200)
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   117
    media = models.ImageField(upload_to='uploads/', height_field='height', width_field='width')
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   118
    item = models.ForeignKey('Item', related_name='images', null=True, blank=True)
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   119
    height = models.IntegerField(null=False, blank=False)
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   120
    width = models.IntegerField(null=False, blank=False)
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   121
    created = models.DateTimeField(auto_now_add=True, null=True)
133
088195b2a6cc iconolab profile to user relation is now one to one field + added panel for collection admin user tools (to be added) in user home + (simple) django admin for iconolab objects
durandn
parents: 132
diff changeset
   122
    
088195b2a6cc iconolab profile to user relation is now one to one field + added panel for collection admin user tools (to be added) in user home + (simple) django admin for iconolab objects
durandn
parents: 132
diff changeset
   123
    def __str__(self):
088195b2a6cc iconolab profile to user relation is now one to one field + added panel for collection admin user tools (to be added) in user home + (simple) django admin for iconolab objects
durandn
parents: 132
diff changeset
   124
        return str(self.image_guid)+":"+self.name
088195b2a6cc iconolab profile to user relation is now one to one field + added panel for collection admin user tools (to be added) in user home + (simple) django admin for iconolab objects
durandn
parents: 132
diff changeset
   125
    
106
233bda6f2865 iconolab search
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents: 104
diff changeset
   126
    @property
233bda6f2865 iconolab search
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents: 104
diff changeset
   127
    def collection(self):
233bda6f2865 iconolab search
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents: 104
diff changeset
   128
        return self.item.collection.name
233bda6f2865 iconolab search
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents: 104
diff changeset
   129
233bda6f2865 iconolab search
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents: 104
diff changeset
   130
    @property
233bda6f2865 iconolab search
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents: 104
diff changeset
   131
    def title(self):
116
1e2caa72bf2f updated indexes
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents: 110
diff changeset
   132
        return self.item.metadatas.designation
1e2caa72bf2f updated indexes
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents: 110
diff changeset
   133
1e2caa72bf2f updated indexes
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents: 110
diff changeset
   134
    @property
1e2caa72bf2f updated indexes
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents: 110
diff changeset
   135
    def authors(self):
1e2caa72bf2f updated indexes
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents: 110
diff changeset
   136
        return self.item.metadatas.authors
1e2caa72bf2f updated indexes
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents: 110
diff changeset
   137
1e2caa72bf2f updated indexes
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents: 110
diff changeset
   138
    @property
1e2caa72bf2f updated indexes
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents: 110
diff changeset
   139
    def school(self):
1e2caa72bf2f updated indexes
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents: 110
diff changeset
   140
        return self.item.metadatas.school
1e2caa72bf2f updated indexes
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents: 110
diff changeset
   141
        
1e2caa72bf2f updated indexes
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents: 110
diff changeset
   142
    @property
1e2caa72bf2f updated indexes
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents: 110
diff changeset
   143
    def designation(self):
1e2caa72bf2f updated indexes
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents: 110
diff changeset
   144
        return self.item.metadatas.designation
1e2caa72bf2f updated indexes
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents: 110
diff changeset
   145
1e2caa72bf2f updated indexes
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents: 110
diff changeset
   146
    @property
1e2caa72bf2f updated indexes
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents: 110
diff changeset
   147
    def datation(self):
1e2caa72bf2f updated indexes
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents: 110
diff changeset
   148
        return self.item.metadatas.datation
106
233bda6f2865 iconolab search
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents: 104
diff changeset
   149
    
233bda6f2865 iconolab search
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents: 104
diff changeset
   150
    @property
116
1e2caa72bf2f updated indexes
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents: 110
diff changeset
   151
    def technics(self):
1e2caa72bf2f updated indexes
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents: 110
diff changeset
   152
        return self.item.metadatas.technics
1e2caa72bf2f updated indexes
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents: 110
diff changeset
   153
1e2caa72bf2f updated indexes
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents: 110
diff changeset
   154
    @property
1e2caa72bf2f updated indexes
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents: 110
diff changeset
   155
    def measurements(self):
1e2caa72bf2f updated indexes
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents: 110
diff changeset
   156
        return self.item.metadatas.measurements
1e2caa72bf2f updated indexes
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents: 110
diff changeset
   157
1e2caa72bf2f updated indexes
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents: 110
diff changeset
   158
    @property
151
797460904f77 indexing using tag labels
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents: 139
diff changeset
   159
    def tag_labels(self):
116
1e2caa72bf2f updated indexes
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents: 110
diff changeset
   160
        tag_list = []
1e2caa72bf2f updated indexes
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents: 110
diff changeset
   161
        for annotation in self.annotations.all():
151
797460904f77 indexing using tag labels
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents: 139
diff changeset
   162
            revision_tags = json.loads(annotation.current_revision.get_tags_json())
797460904f77 indexing using tag labels
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents: 139
diff changeset
   163
            tag_list += [tag_infos['tag_label'] for tag_infos in revision_tags if tag_infos.get('tag_label') is not None] #deal with
797460904f77 indexing using tag labels
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents: 139
diff changeset
   164
            print("tag_list")
797460904f77 indexing using tag labels
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents: 139
diff changeset
   165
            print(tag_list)
116
1e2caa72bf2f updated indexes
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents: 110
diff changeset
   166
        return tag_list
24
6b6b183447a2 work on models + auth register/login system + adapted existing app and templates so editing annotations is working as before + created empty templates to fill
durandn
parents: 13
diff changeset
   167
6b6b183447a2 work on models + auth register/login system + adapted existing app and templates so editing annotations is working as before + created empty templates to fill
durandn
parents: 13
diff changeset
   168
class AnnotationManager(models.Manager):
48
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   169
    
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   170
    # Call Annotation.objects.create_annotation to initialize a new Annotation with its associated AnnotationStats and initial AnnotationRevision
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   171
    @transaction.atomic
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   172
    def create_annotation(self, author, image, title='', description='', fragment='', tags_json='[]'):
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   173
        # Create annotation object
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   174
        new_annotation = Annotation(
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   175
            image=image, 
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   176
            author=author
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   177
        )
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   178
        new_annotation.save()
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   179
        
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   180
        # Create initial revision
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   181
        initial_revision = AnnotationRevision(
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   182
            annotation=new_annotation, 
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   183
            author=author,
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   184
            title=title,
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   185
            description=description,
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   186
            fragment=fragment,
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   187
            state=AnnotationRevision.ACCEPTED
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   188
        )
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   189
        initial_revision.save()
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   190
        initial_revision.set_tags(tags_json)
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   191
        
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   192
        # Create stats object
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   193
        new_annotation_stats = AnnotationStats(annotation=new_annotation)
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   194
        new_annotation_stats.save()
85
49b3f22948d5 work on stats: calculatestats command for manage.py, stats in item page, stats in annotations list, stats in annotation page, actions increment stats (view count, annotations count, comment count etc)
durandn
parents: 77
diff changeset
   195
        new_annotation_stats.set_tags_stats()
123
c8a1216fd28f removing prints
durandn
parents: 122
diff changeset
   196
        
c8a1216fd28f removing prints
durandn
parents: 122
diff changeset
   197
        # Link everything to parent
c8a1216fd28f removing prints
durandn
parents: 122
diff changeset
   198
        new_annotation.current_revision = initial_revision
48
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   199
        new_annotation.stats = new_annotation_stats
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   200
        new_annotation.save()
134
350bdfe7c289 sending revision_created signal when creating an annotation
durandn
parents: 133
diff changeset
   201
        iconolab_signals.revision_created.send(sender=AnnotationRevision, instance=initial_revision)
48
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   202
        return new_annotation
24
6b6b183447a2 work on models + auth register/login system + adapted existing app and templates so editing annotations is working as before + created empty templates to fill
durandn
parents: 13
diff changeset
   203
101
eb856f9b518c fixed comment page calculation
durandn
parents: 100
diff changeset
   204
24
6b6b183447a2 work on models + auth register/login system + adapted existing app and templates so editing annotations is working as before + created empty templates to fill
durandn
parents: 13
diff changeset
   205
class AnnotationStats(models.Model):
48
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   206
    annotation = models.OneToOneField('Annotation', related_name='stats', blank=False, null=False)
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   207
    submitted_revisions_count = models.IntegerField(blank=True, null=True, default=1)
97
f747c112e8f4 Started work on notification and user homepage + method for a comment to find back its page in his annotation's comments
durandn
parents: 96
diff changeset
   208
    awaiting_revisions_count = models.IntegerField(blank=True, null=True, default=0)
48
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   209
    accepted_revisions_count = models.IntegerField(blank=True, null=True, default=1)
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   210
    contributors_count = models.IntegerField(blank=True, null=True, default=1)
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   211
    views_count = models.IntegerField(blank=True, null=True, default=0)
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   212
    comments_count = models.IntegerField(blank=True, null=True, default=0)
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   213
    tag_count = models.IntegerField(blank=True, null=True, default=0)
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   214
    
133
088195b2a6cc iconolab profile to user relation is now one to one field + added panel for collection admin user tools (to be added) in user home + (simple) django admin for iconolab objects
durandn
parents: 132
diff changeset
   215
    def __str__(self):
088195b2a6cc iconolab profile to user relation is now one to one field + added panel for collection admin user tools (to be added) in user home + (simple) django admin for iconolab objects
durandn
parents: 132
diff changeset
   216
        return "stats:for:"+str(self.annotation_guid)
088195b2a6cc iconolab profile to user relation is now one to one field + added panel for collection admin user tools (to be added) in user home + (simple) django admin for iconolab objects
durandn
parents: 132
diff changeset
   217
    
85
49b3f22948d5 work on stats: calculatestats command for manage.py, stats in item page, stats in annotations list, stats in annotation page, actions increment stats (view count, annotations count, comment count etc)
durandn
parents: 77
diff changeset
   218
    @property
48
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   219
    def contributors(self):
97
f747c112e8f4 Started work on notification and user homepage + method for a comment to find back its page in his annotation's comments
durandn
parents: 96
diff changeset
   220
        user_ids_list = self.annotation.revisions.filter(state__in=[AnnotationRevision.ACCEPTED, AnnotationRevision.STUDIED]).values_list("author__id", flat=True)
f747c112e8f4 Started work on notification and user homepage + method for a comment to find back its page in his annotation's comments
durandn
parents: 96
diff changeset
   221
        return User.objects.filter(id__in=user_ids_list).distinct()
f747c112e8f4 Started work on notification and user homepage + method for a comment to find back its page in his annotation's comments
durandn
parents: 96
diff changeset
   222
    
f747c112e8f4 Started work on notification and user homepage + method for a comment to find back its page in his annotation's comments
durandn
parents: 96
diff changeset
   223
    @property
f747c112e8f4 Started work on notification and user homepage + method for a comment to find back its page in his annotation's comments
durandn
parents: 96
diff changeset
   224
    def commenters(self):
f747c112e8f4 Started work on notification and user homepage + method for a comment to find back its page in his annotation's comments
durandn
parents: 96
diff changeset
   225
        user_ids_list = IconolabComment.objects.filter(content_type__app_label="iconolab", content_type__model="annotation", object_pk=self.annotation.id).values_list("user__id", flat=True)
f747c112e8f4 Started work on notification and user homepage + method for a comment to find back its page in his annotation's comments
durandn
parents: 96
diff changeset
   226
        return User.objects.filter(id__in=user_ids_list).distinct()
85
49b3f22948d5 work on stats: calculatestats command for manage.py, stats in item page, stats in annotations list, stats in annotation page, actions increment stats (view count, annotations count, comment count etc)
durandn
parents: 77
diff changeset
   227
    
49b3f22948d5 work on stats: calculatestats command for manage.py, stats in item page, stats in annotations list, stats in annotation page, actions increment stats (view count, annotations count, comment count etc)
durandn
parents: 77
diff changeset
   228
    def set_tags_stats(self):
49b3f22948d5 work on stats: calculatestats command for manage.py, stats in item page, stats in annotations list, stats in annotation page, actions increment stats (view count, annotations count, comment count etc)
durandn
parents: 77
diff changeset
   229
        self.tag_count = Tag.objects.filter(tagginginfo__revision__annotation = self.annotation).distinct().count()
49b3f22948d5 work on stats: calculatestats command for manage.py, stats in item page, stats in annotations list, stats in annotation page, actions increment stats (view count, annotations count, comment count etc)
durandn
parents: 77
diff changeset
   230
    
93
30cd69620039 use atomic transactions on stat updates
durandn
parents: 90
diff changeset
   231
    @transaction.atomic
85
49b3f22948d5 work on stats: calculatestats command for manage.py, stats in item page, stats in annotations list, stats in annotation page, actions increment stats (view count, annotations count, comment count etc)
durandn
parents: 77
diff changeset
   232
    def update_stats(self):
49b3f22948d5 work on stats: calculatestats command for manage.py, stats in item page, stats in annotations list, stats in annotation page, actions increment stats (view count, annotations count, comment count etc)
durandn
parents: 77
diff changeset
   233
        # views_count - Can't do much about views count
49b3f22948d5 work on stats: calculatestats command for manage.py, stats in item page, stats in annotations list, stats in annotation page, actions increment stats (view count, annotations count, comment count etc)
durandn
parents: 77
diff changeset
   234
        # submitted_revisions_count 
49b3f22948d5 work on stats: calculatestats command for manage.py, stats in item page, stats in annotations list, stats in annotation page, actions increment stats (view count, annotations count, comment count etc)
durandn
parents: 77
diff changeset
   235
        annotation_revisions = self.annotation.revisions
49b3f22948d5 work on stats: calculatestats command for manage.py, stats in item page, stats in annotations list, stats in annotation page, actions increment stats (view count, annotations count, comment count etc)
durandn
parents: 77
diff changeset
   236
        self.submitted_revisions_count = annotation_revisions.count()
97
f747c112e8f4 Started work on notification and user homepage + method for a comment to find back its page in his annotation's comments
durandn
parents: 96
diff changeset
   237
        # aawaiting_revisions_count
f747c112e8f4 Started work on notification and user homepage + method for a comment to find back its page in his annotation's comments
durandn
parents: 96
diff changeset
   238
        self.awaiting_revisions_count = annotation_revisions.filter(state=AnnotationRevision.AWAITING).count()
85
49b3f22948d5 work on stats: calculatestats command for manage.py, stats in item page, stats in annotations list, stats in annotation page, actions increment stats (view count, annotations count, comment count etc)
durandn
parents: 77
diff changeset
   239
        # accepted_revisions_count
49b3f22948d5 work on stats: calculatestats command for manage.py, stats in item page, stats in annotations list, stats in annotation page, actions increment stats (view count, annotations count, comment count etc)
durandn
parents: 77
diff changeset
   240
        self.accepted_revisions_count = annotation_revisions.filter(state=AnnotationRevision.ACCEPTED).count() + annotation_revisions.filter(state=AnnotationRevision.STUDIED).count()
49b3f22948d5 work on stats: calculatestats command for manage.py, stats in item page, stats in annotations list, stats in annotation page, actions increment stats (view count, annotations count, comment count etc)
durandn
parents: 77
diff changeset
   241
        # comment_count
49b3f22948d5 work on stats: calculatestats command for manage.py, stats in item page, stats in annotations list, stats in annotation page, actions increment stats (view count, annotations count, comment count etc)
durandn
parents: 77
diff changeset
   242
        self.comments_count = XtdComment.objects.for_app_models("iconolab.annotation").filter(
49b3f22948d5 work on stats: calculatestats command for manage.py, stats in item page, stats in annotations list, stats in annotation page, actions increment stats (view count, annotations count, comment count etc)
durandn
parents: 77
diff changeset
   243
            object_pk = self.annotation.pk,
49b3f22948d5 work on stats: calculatestats command for manage.py, stats in item page, stats in annotations list, stats in annotation page, actions increment stats (view count, annotations count, comment count etc)
durandn
parents: 77
diff changeset
   244
        ).count()
49b3f22948d5 work on stats: calculatestats command for manage.py, stats in item page, stats in annotations list, stats in annotation page, actions increment stats (view count, annotations count, comment count etc)
durandn
parents: 77
diff changeset
   245
        # contributors_count
49b3f22948d5 work on stats: calculatestats command for manage.py, stats in item page, stats in annotations list, stats in annotation page, actions increment stats (view count, annotations count, comment count etc)
durandn
parents: 77
diff changeset
   246
        self.contributors_count = len(self.contributors)
49b3f22948d5 work on stats: calculatestats command for manage.py, stats in item page, stats in annotations list, stats in annotation page, actions increment stats (view count, annotations count, comment count etc)
durandn
parents: 77
diff changeset
   247
        # tag_count
49b3f22948d5 work on stats: calculatestats command for manage.py, stats in item page, stats in annotations list, stats in annotation page, actions increment stats (view count, annotations count, comment count etc)
durandn
parents: 77
diff changeset
   248
        self.tag_count = Tag.objects.filter(tagginginfo__revision__annotation = self.annotation).distinct().count()
49b3f22948d5 work on stats: calculatestats command for manage.py, stats in item page, stats in annotations list, stats in annotation page, actions increment stats (view count, annotations count, comment count etc)
durandn
parents: 77
diff changeset
   249
        
49b3f22948d5 work on stats: calculatestats command for manage.py, stats in item page, stats in annotations list, stats in annotation page, actions increment stats (view count, annotations count, comment count etc)
durandn
parents: 77
diff changeset
   250
        self.save()
6
37baf9d13f32 first commit
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   251
101
eb856f9b518c fixed comment page calculation
durandn
parents: 100
diff changeset
   252
24
6b6b183447a2 work on models + auth register/login system + adapted existing app and templates so editing annotations is working as before + created empty templates to fill
durandn
parents: 13
diff changeset
   253
class Annotation(models.Model):
48
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   254
    annotation_guid = models.UUIDField(default=uuid.uuid4, editable=False)
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   255
    image = models.ForeignKey('Image', related_name='annotations', on_delete=models.CASCADE)
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   256
    source_revision = models.ForeignKey('AnnotationRevision', related_name='source_related_annotation', blank=True, null=True)
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   257
    current_revision = models.OneToOneField('AnnotationRevision', related_name='current_for_annotation', blank=True, null=True)
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   258
    author = models.ForeignKey(User, null=True)
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   259
    created = models.DateTimeField(auto_now_add=True, null=True)
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   260
    
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   261
    objects = AnnotationManager()
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   262
    
133
088195b2a6cc iconolab profile to user relation is now one to one field + added panel for collection admin user tools (to be added) in user home + (simple) django admin for iconolab objects
durandn
parents: 132
diff changeset
   263
    def __str__(self):
088195b2a6cc iconolab profile to user relation is now one to one field + added panel for collection admin user tools (to be added) in user home + (simple) django admin for iconolab objects
durandn
parents: 132
diff changeset
   264
        return str(self.annotation_guid)+":"+self.current_revision.title
088195b2a6cc iconolab profile to user relation is now one to one field + added panel for collection admin user tools (to be added) in user home + (simple) django admin for iconolab objects
durandn
parents: 132
diff changeset
   265
        
97
f747c112e8f4 Started work on notification and user homepage + method for a comment to find back its page in his annotation's comments
durandn
parents: 96
diff changeset
   266
    @property
f747c112e8f4 Started work on notification and user homepage + method for a comment to find back its page in his annotation's comments
durandn
parents: 96
diff changeset
   267
    def awaiting_revisions_count(self):
f747c112e8f4 Started work on notification and user homepage + method for a comment to find back its page in his annotation's comments
durandn
parents: 96
diff changeset
   268
        return self.revisions.filter(state=AnnotationRevision.AWAITING).distinct().count()
f747c112e8f4 Started work on notification and user homepage + method for a comment to find back its page in his annotation's comments
durandn
parents: 96
diff changeset
   269
    
106
233bda6f2865 iconolab search
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents: 104
diff changeset
   270
    @property
139
3e0a5286b257 collection home work (list and tabs) + adjusted image list template+ various design fixes
durandn
parents: 137
diff changeset
   271
    def accepted_revisions_count(self):
3e0a5286b257 collection home work (list and tabs) + adjusted image list template+ various design fixes
durandn
parents: 137
diff changeset
   272
        return self.revisions.filter(state=AnnotationRevision.ACCEPTED).distinct().count()
3e0a5286b257 collection home work (list and tabs) + adjusted image list template+ various design fixes
durandn
parents: 137
diff changeset
   273
    
3e0a5286b257 collection home work (list and tabs) + adjusted image list template+ various design fixes
durandn
parents: 137
diff changeset
   274
    @property
3e0a5286b257 collection home work (list and tabs) + adjusted image list template+ various design fixes
durandn
parents: 137
diff changeset
   275
    def rejected_revisions_count(self):
3e0a5286b257 collection home work (list and tabs) + adjusted image list template+ various design fixes
durandn
parents: 137
diff changeset
   276
        return self.revisions.filter(state=AnnotationRevision.REJECTED).distinct().count()
3e0a5286b257 collection home work (list and tabs) + adjusted image list template+ various design fixes
durandn
parents: 137
diff changeset
   277
    
3e0a5286b257 collection home work (list and tabs) + adjusted image list template+ various design fixes
durandn
parents: 137
diff changeset
   278
    @property
3e0a5286b257 collection home work (list and tabs) + adjusted image list template+ various design fixes
durandn
parents: 137
diff changeset
   279
    def studied_revisions_count(self):
3e0a5286b257 collection home work (list and tabs) + adjusted image list template+ various design fixes
durandn
parents: 137
diff changeset
   280
        return self.revisions.filter(state=AnnotationRevision.STUDIED).distinct().count()
3e0a5286b257 collection home work (list and tabs) + adjusted image list template+ various design fixes
durandn
parents: 137
diff changeset
   281
      
3e0a5286b257 collection home work (list and tabs) + adjusted image list template+ various design fixes
durandn
parents: 137
diff changeset
   282
    @property
3e0a5286b257 collection home work (list and tabs) + adjusted image list template+ various design fixes
durandn
parents: 137
diff changeset
   283
    def total_revisions_count(self):
3e0a5286b257 collection home work (list and tabs) + adjusted image list template+ various design fixes
durandn
parents: 137
diff changeset
   284
        return self.revisions.distinct().count()
3e0a5286b257 collection home work (list and tabs) + adjusted image list template+ various design fixes
durandn
parents: 137
diff changeset
   285
   
3e0a5286b257 collection home work (list and tabs) + adjusted image list template+ various design fixes
durandn
parents: 137
diff changeset
   286
    @property
124
e5267573edd8 search engine
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents: 116
diff changeset
   287
    def collection(self):
e5267573edd8 search engine
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents: 116
diff changeset
   288
        return self.image.collection
e5267573edd8 search engine
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents: 116
diff changeset
   289
e5267573edd8 search engine
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents: 116
diff changeset
   290
    @property
151
797460904f77 indexing using tag labels
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents: 139
diff changeset
   291
    def tag_labels(self):
797460904f77 indexing using tag labels
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents: 139
diff changeset
   292
        current_revision_tags = json.loads(self.current_revision.get_tags_json())
797460904f77 indexing using tag labels
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents: 139
diff changeset
   293
        print("tagss")
797460904f77 indexing using tag labels
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents: 139
diff changeset
   294
        print(current_revision_tags)
797460904f77 indexing using tag labels
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents: 139
diff changeset
   295
        return [tag_infos['tag_label'] for tag_infos in current_revision_tags if tag_infos.get('tag_label') is not None ]
106
233bda6f2865 iconolab search
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents: 104
diff changeset
   296
    
97
f747c112e8f4 Started work on notification and user homepage + method for a comment to find back its page in his annotation's comments
durandn
parents: 96
diff changeset
   297
    
48
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   298
    # Call to create a new revision, possibly from a merge
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   299
    @transaction.atomic
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   300
    def make_new_revision(self, author, title, description, fragment, tags_json):
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   301
        if author == self.author:
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   302
            # We're creating an automatically accepted revision
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   303
            new_revision_state = AnnotationRevision.ACCEPTED
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   304
        else:
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   305
            # Revision will require validation
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   306
            new_revision_state = AnnotationRevision.AWAITING
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   307
        new_revision = AnnotationRevision(
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   308
            annotation = self,
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   309
            parent_revision=self.current_revision,
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   310
            title=title,
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   311
            description=description,
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   312
            author=author,
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   313
            fragment=fragment,
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   314
            state=new_revision_state
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   315
        )
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   316
        new_revision.save()
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   317
        new_revision.set_tags(tags_json)
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   318
        if new_revision.state == AnnotationRevision.ACCEPTED:
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   319
            self.current_revision = new_revision
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   320
            self.save()
90
8d7815ecd211 signals are now sent in model methods rather than views
durandn
parents: 89
diff changeset
   321
        iconolab_signals.revision_created.send(sender=AnnotationRevision, instance=new_revision)
48
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   322
        return new_revision
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   323
    
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   324
    # Call when we're validating an awaiting revision whose parent is the current revision AS IT WAS CREATED
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   325
    @transaction.atomic
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   326
    def validate_existing_revision(self, revision_to_validate):
62
8702ab13783e design work on templates, redirect to home on /, implemented accept and reject revisions shortcuts when applicable
durandn
parents: 58
diff changeset
   327
        if revision_to_validate.parent_revision == self.current_revision and revision_to_validate.state == AnnotationRevision.AWAITING:
48
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   328
            self.current_revision = revision_to_validate
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   329
            revision_to_validate.state = AnnotationRevision.ACCEPTED
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   330
            revision_to_validate.save()
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   331
            self.save()
90
8d7815ecd211 signals are now sent in model methods rather than views
durandn
parents: 89
diff changeset
   332
            iconolab_signals.revision_accepted.send(sender=AnnotationRevision, instance=revision_to_validate)
62
8702ab13783e design work on templates, redirect to home on /, implemented accept and reject revisions shortcuts when applicable
durandn
parents: 58
diff changeset
   333
    
8702ab13783e design work on templates, redirect to home on /, implemented accept and reject revisions shortcuts when applicable
durandn
parents: 58
diff changeset
   334
    # Call to reject a 
8702ab13783e design work on templates, redirect to home on /, implemented accept and reject revisions shortcuts when applicable
durandn
parents: 58
diff changeset
   335
    @transaction.atomic
8702ab13783e design work on templates, redirect to home on /, implemented accept and reject revisions shortcuts when applicable
durandn
parents: 58
diff changeset
   336
    def reject_existing_revision(self, revision_to_reject):
8702ab13783e design work on templates, redirect to home on /, implemented accept and reject revisions shortcuts when applicable
durandn
parents: 58
diff changeset
   337
        if revision_to_reject.state == AnnotationRevision.AWAITING:
8702ab13783e design work on templates, redirect to home on /, implemented accept and reject revisions shortcuts when applicable
durandn
parents: 58
diff changeset
   338
            revision_to_reject.state = AnnotationRevision.REJECTED
8702ab13783e design work on templates, redirect to home on /, implemented accept and reject revisions shortcuts when applicable
durandn
parents: 58
diff changeset
   339
            revision_to_reject.save()
98
2b738b88d483 workon on notifications: user home, user notifications page, triggers in signals
durandn
parents: 97
diff changeset
   340
            iconolab_signals.revision_rejected.send(sender=AnnotationRevision, instance=revision_to_reject)
2b738b88d483 workon on notifications: user home, user notifications page, triggers in signals
durandn
parents: 97
diff changeset
   341
    
48
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   342
    # Call when we're validating an awaiting revision whose parent isn't the current revision OR IF IT WAS CHANGED BY THE ANNOTATION AUTHOR
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   343
    @transaction.atomic
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   344
    def merge_existing_revision(self, title, description, fragment, tags, revision_to_merge):
77
89bfc5499455 first pass on merge system
durandn
parents: 68
diff changeset
   345
        merged_revision = self.make_new_revision(author=self.author, title=title, description=description, fragment=fragment, tags_json=tags)
48
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   346
        merged_revision.merge_parent_revision = revision_to_merge
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   347
        merged_revision.save()
62
8702ab13783e design work on templates, redirect to home on /, implemented accept and reject revisions shortcuts when applicable
durandn
parents: 58
diff changeset
   348
        revision_to_merge.state = AnnotationRevision.STUDIED
8702ab13783e design work on templates, redirect to home on /, implemented accept and reject revisions shortcuts when applicable
durandn
parents: 58
diff changeset
   349
        revision_to_merge.save()
90
8d7815ecd211 signals are now sent in model methods rather than views
durandn
parents: 89
diff changeset
   350
        iconolab_signals.revision_accepted.send(sender=AnnotationRevision, instance=revision_to_merge)
48
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   351
        self.current_revision=merged_revision
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   352
        self.save()
77
89bfc5499455 first pass on merge system
durandn
parents: 68
diff changeset
   353
        return merged_revision
6
37baf9d13f32 first commit
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   354
37
aed809b3a075 Corrected Tag methods bugs + Added comment classes and migration
durandn
parents: 35
diff changeset
   355
24
6b6b183447a2 work on models + auth register/login system + adapted existing app and templates so editing annotations is working as before + created empty templates to fill
durandn
parents: 13
diff changeset
   356
class AnnotationRevision(models.Model):
48
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   357
    
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   358
    AWAITING = 0
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   359
    ACCEPTED = 1
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   360
    REJECTED = 2
62
8702ab13783e design work on templates, redirect to home on /, implemented accept and reject revisions shortcuts when applicable
durandn
parents: 58
diff changeset
   361
    STUDIED = 3
48
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   362
    
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   363
    REVISION_STATES = (
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   364
        (AWAITING, 'awaiting'),
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   365
        (ACCEPTED, 'accepted'),
62
8702ab13783e design work on templates, redirect to home on /, implemented accept and reject revisions shortcuts when applicable
durandn
parents: 58
diff changeset
   366
        (REJECTED, 'rejected'),
8702ab13783e design work on templates, redirect to home on /, implemented accept and reject revisions shortcuts when applicable
durandn
parents: 58
diff changeset
   367
        (STUDIED, 'studied'),
48
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   368
    )
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   369
    
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   370
    revision_guid = models.UUIDField(default=uuid.uuid4)
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   371
    annotation = models.ForeignKey('Annotation', related_name='revisions', null=False, blank=False)
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   372
    parent_revision = models.ForeignKey('AnnotationRevision', related_name='child_revisions', blank=True, null=True)
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   373
    merge_parent_revision = models.ForeignKey('AnnotationRevision', related_name='child_revisions_merge', blank=True, null=True)
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   374
    author = models.ForeignKey(User, null=True)
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   375
    title = models.CharField(max_length=255)
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   376
    description = models.TextField(null=True)
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   377
    fragment = models.TextField()
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   378
    tags = models.ManyToManyField('Tag', through='TaggingInfo', through_fields=('revision', 'tag'))
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   379
    state = models.IntegerField(choices=REVISION_STATES, default=AWAITING)
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   380
    created = models.DateTimeField(auto_now_add=True, null=True)
6
37baf9d13f32 first commit
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   381
133
088195b2a6cc iconolab profile to user relation is now one to one field + added panel for collection admin user tools (to be added) in user home + (simple) django admin for iconolab objects
durandn
parents: 132
diff changeset
   382
    def __str__(self):
088195b2a6cc iconolab profile to user relation is now one to one field + added panel for collection admin user tools (to be added) in user home + (simple) django admin for iconolab objects
durandn
parents: 132
diff changeset
   383
        return str(self.revision_guid)+":"+self.title
088195b2a6cc iconolab profile to user relation is now one to one field + added panel for collection admin user tools (to be added) in user home + (simple) django admin for iconolab objects
durandn
parents: 132
diff changeset
   384
48
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   385
    def set_tags(self, tags_json_string):
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   386
        try:
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   387
            tags_dict = json.loads(tags_json_string)
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   388
        except ValueError:
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   389
            pass
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   390
        for tag_data in tags_dict:
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   391
            tag_string = tag_data.get("tag_input")
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   392
            tag_accuracy = tag_data.get("accuracy", 0)
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   393
            tag_relevancy = tag_data.get("relevancy", 0)
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   394
            
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   395
            if tag_string.startswith("http://") or tag_string.startswith("https://"): #check if url
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   396
                if Tag.objects.filter(link=tag_string).exists(): #check if tag already exists
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   397
                    tag_obj = Tag.objects.get(link=tag_string)
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   398
                else:
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   399
                    tag_obj = Tag.objects.create(
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   400
                        link = tag_string,
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   401
                    )
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   402
            else:
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   403
                new_tag_link = settings.BASE_URL+'/'+slugify(tag_string)
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   404
                if Tag.objects.filter(link=new_tag_link).exists():
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   405
                    # Somehow we received a label for an existing tag
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   406
                    tag_obj = Tag.objects.get(link=new_tag_link)
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   407
                else:
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   408
                    tag_obj = Tag.objects.create(
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   409
                        label = tag_string,
104
3c4150867fe7 Added fallback tag label storing in database for external tags in case external source is down (rudimentary)
durandn
parents: 101
diff changeset
   410
                        label_slug = slugify(tag_string),
48
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   411
                        description = "",
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   412
                        link = settings.INTERNAL_TAGS_URL+'/'+slugify(tag_string),
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   413
                        collection = self.annotation.image.item.collection
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   414
                    )
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   415
            tag_info = TaggingInfo.objects.create(
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   416
                tag=tag_obj, 
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   417
                revision=self,
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   418
                accuracy = tag_accuracy,
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   419
                relevancy = tag_relevancy
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   420
            )
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   421
        
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   422
    def get_tags_json(self):
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   423
        
54
147c8a8b66b6 streamlined templates for easy navigation and missing info + handled fr.dbpedia tags
durandn
parents: 53
diff changeset
   424
        def fetch_from_dbpedia(uri, lang, source):
48
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   425
            sparql_template = 'select distinct * where { <<%uri%>> rdfs:label ?l FILTER( langMatches( lang(?l), "<%lang%>" ) ) }' 
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   426
            sparql_query = re.sub("<%uri%>", uri, re.sub("<%lang%>", lang, sparql_template))
54
147c8a8b66b6 streamlined templates for easy navigation and missing info + handled fr.dbpedia tags
durandn
parents: 53
diff changeset
   427
            sparql_query_url = source+'sparql'
98
2b738b88d483 workon on notifications: user home, user notifications page, triggers in signals
durandn
parents: 97
diff changeset
   428
            try:
2b738b88d483 workon on notifications: user home, user notifications page, triggers in signals
durandn
parents: 97
diff changeset
   429
                dbpedia_resp = requests.get(
2b738b88d483 workon on notifications: user home, user notifications page, triggers in signals
durandn
parents: 97
diff changeset
   430
                    sparql_query_url, 
2b738b88d483 workon on notifications: user home, user notifications page, triggers in signals
durandn
parents: 97
diff changeset
   431
                    params={
2b738b88d483 workon on notifications: user home, user notifications page, triggers in signals
durandn
parents: 97
diff changeset
   432
                            "query": sparql_query,
2b738b88d483 workon on notifications: user home, user notifications page, triggers in signals
durandn
parents: 97
diff changeset
   433
                            "format": "json"
2b738b88d483 workon on notifications: user home, user notifications page, triggers in signals
durandn
parents: 97
diff changeset
   434
                    }
2b738b88d483 workon on notifications: user home, user notifications page, triggers in signals
durandn
parents: 97
diff changeset
   435
                )
2b738b88d483 workon on notifications: user home, user notifications page, triggers in signals
durandn
parents: 97
diff changeset
   436
            except:
104
3c4150867fe7 Added fallback tag label storing in database for external tags in case external source is down (rudimentary)
durandn
parents: 101
diff changeset
   437
                # dbpedia is down, will be handled with database label
98
2b738b88d483 workon on notifications: user home, user notifications page, triggers in signals
durandn
parents: 97
diff changeset
   438
                pass
96
09b2da30cd93 try except block missing around a json.loads call
durandn
parents: 93
diff changeset
   439
            try:
09b2da30cd93 try except block missing around a json.loads call
durandn
parents: 93
diff changeset
   440
                results = json.loads(dbpedia_resp.text).get("results", {})
09b2da30cd93 try except block missing around a json.loads call
durandn
parents: 93
diff changeset
   441
            except:
09b2da30cd93 try except block missing around a json.loads call
durandn
parents: 93
diff changeset
   442
                # if error with json, results is empty
09b2da30cd93 try except block missing around a json.loads call
durandn
parents: 93
diff changeset
   443
                results = {}
09b2da30cd93 try except block missing around a json.loads call
durandn
parents: 93
diff changeset
   444
            variable_bindings = results.get("bindings", None)
104
3c4150867fe7 Added fallback tag label storing in database for external tags in case external source is down (rudimentary)
durandn
parents: 101
diff changeset
   445
            label_data = {}
58
9f7e484baf73 Error handling in models + templates fixes
durandn
parents: 54
diff changeset
   446
            if variable_bindings:
104
3c4150867fe7 Added fallback tag label storing in database for external tags in case external source is down (rudimentary)
durandn
parents: 101
diff changeset
   447
                label_data = variable_bindings.pop()
3c4150867fe7 Added fallback tag label storing in database for external tags in case external source is down (rudimentary)
durandn
parents: 101
diff changeset
   448
            return label_data.get("l", {"value": False}).get("value")
48
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   449
        
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   450
        final_list = []
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   451
        for tagging_info in self.tagginginfo_set.select_related("tag").all():
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   452
            if tagging_info.tag.is_internal():
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   453
                final_list.append({
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   454
                    "tag_label": tagging_info.tag.label,
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   455
                    "tag_link": tagging_info.tag.link,
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   456
                    "accuracy": tagging_info.accuracy,
89
23679a6def77 fixed error on stats (comments count not updating correctly for images)
durandn
parents: 85
diff changeset
   457
                    "relevancy": tagging_info.relevancy,
23679a6def77 fixed error on stats (comments count not updating correctly for images)
durandn
parents: 85
diff changeset
   458
                    "is_internal": tagging_info.tag.is_internal()
48
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   459
                })
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   460
            else:
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   461
                tag_link = tagging_info.tag.link
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   462
                #import label from external
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   463
                externaL_repos_fetch_dict = {
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   464
                    "http://dbpedia.org/": fetch_from_dbpedia,
53
ed9acfa5dd6e collection home page + tags in revision_detail view + optimization in views + fixture for demo
durandn
parents: 49
diff changeset
   465
                    "http://fr.dbpedia.org/": fetch_from_dbpedia
48
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   466
                }
53
ed9acfa5dd6e collection home page + tags in revision_detail view + optimization in views + fixture for demo
durandn
parents: 49
diff changeset
   467
                try:
54
147c8a8b66b6 streamlined templates for easy navigation and missing info + handled fr.dbpedia tags
durandn
parents: 53
diff changeset
   468
                    (source, fetch_label) = next(item for item in externaL_repos_fetch_dict.items() if tag_link.startswith(item[0]))
147c8a8b66b6 streamlined templates for easy navigation and missing info + handled fr.dbpedia tags
durandn
parents: 53
diff changeset
   469
                    tag_label = fetch_label(tag_link, "fr", source)
104
3c4150867fe7 Added fallback tag label storing in database for external tags in case external source is down (rudimentary)
durandn
parents: 101
diff changeset
   470
                    if not tag_label: # Error happened and we got False as a fetch return
3c4150867fe7 Added fallback tag label storing in database for external tags in case external source is down (rudimentary)
durandn
parents: 101
diff changeset
   471
                        tag_label = tagging_info.tag.label 
3c4150867fe7 Added fallback tag label storing in database for external tags in case external source is down (rudimentary)
durandn
parents: 101
diff changeset
   472
                    else:
3c4150867fe7 Added fallback tag label storing in database for external tags in case external source is down (rudimentary)
durandn
parents: 101
diff changeset
   473
                        tagging_info.tag.label = tag_label
3c4150867fe7 Added fallback tag label storing in database for external tags in case external source is down (rudimentary)
durandn
parents: 101
diff changeset
   474
                        tagging_info.tag.save()
53
ed9acfa5dd6e collection home page + tags in revision_detail view + optimization in views + fixture for demo
durandn
parents: 49
diff changeset
   475
                    final_list.append({
ed9acfa5dd6e collection home page + tags in revision_detail view + optimization in views + fixture for demo
durandn
parents: 49
diff changeset
   476
                        "tag_label": tag_label,
ed9acfa5dd6e collection home page + tags in revision_detail view + optimization in views + fixture for demo
durandn
parents: 49
diff changeset
   477
                        "tag_link": tag_link,
ed9acfa5dd6e collection home page + tags in revision_detail view + optimization in views + fixture for demo
durandn
parents: 49
diff changeset
   478
                        "accuracy": tagging_info.accuracy,
89
23679a6def77 fixed error on stats (comments count not updating correctly for images)
durandn
parents: 85
diff changeset
   479
                        "relevancy": tagging_info.relevancy,
23679a6def77 fixed error on stats (comments count not updating correctly for images)
durandn
parents: 85
diff changeset
   480
                        "is_internal": tagging_info.tag.is_internal()
53
ed9acfa5dd6e collection home page + tags in revision_detail view + optimization in views + fixture for demo
durandn
parents: 49
diff changeset
   481
                    })
ed9acfa5dd6e collection home page + tags in revision_detail view + optimization in views + fixture for demo
durandn
parents: 49
diff changeset
   482
                except StopIteration:
ed9acfa5dd6e collection home page + tags in revision_detail view + optimization in views + fixture for demo
durandn
parents: 49
diff changeset
   483
                    pass
48
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   484
        return json.dumps(final_list) 
101
eb856f9b518c fixed comment page calculation
durandn
parents: 100
diff changeset
   485
48
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   486
    
37
aed809b3a075 Corrected Tag methods bugs + Added comment classes and migration
durandn
parents: 35
diff changeset
   487
class IconolabComment(XtdComment):
48
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   488
    revision = models.ForeignKey('AnnotationRevision', related_name='creation_comment', null=True, blank=True)
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   489
    metacategories = models.ManyToManyField('MetaCategory', through='MetaCategoryInfo', through_fields=('comment', 'metacategory'))
68
55214839b541 Comments threading, pagination and date format
durandn
parents: 65
diff changeset
   490
    
55214839b541 Comments threading, pagination and date format
durandn
parents: 65
diff changeset
   491
    objects = XtdComment.objects
55214839b541 Comments threading, pagination and date format
durandn
parents: 65
diff changeset
   492
    
133
088195b2a6cc iconolab profile to user relation is now one to one field + added panel for collection admin user tools (to be added) in user home + (simple) django admin for iconolab objects
durandn
parents: 132
diff changeset
   493
    def __str__(self):
088195b2a6cc iconolab profile to user relation is now one to one field + added panel for collection admin user tools (to be added) in user home + (simple) django admin for iconolab objects
durandn
parents: 132
diff changeset
   494
        return self.id
088195b2a6cc iconolab profile to user relation is now one to one field + added panel for collection admin user tools (to be added) in user home + (simple) django admin for iconolab objects
durandn
parents: 132
diff changeset
   495
    
68
55214839b541 Comments threading, pagination and date format
durandn
parents: 65
diff changeset
   496
    class Meta:
55214839b541 Comments threading, pagination and date format
durandn
parents: 65
diff changeset
   497
        ordering = ["thread_id", "id"]
48
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   498
    
97
f747c112e8f4 Started work on notification and user homepage + method for a comment to find back its page in his annotation's comments
durandn
parents: 96
diff changeset
   499
    # Get page for considered comment, with COMMENTS_PER_PAGE_DEFAULT comments per page
f747c112e8f4 Started work on notification and user homepage + method for a comment to find back its page in his annotation's comments
durandn
parents: 96
diff changeset
   500
    def get_comment_page(self):
100
8ec8aced3f68 small fixes on comment page calculation + added functionality to clear unread notification from button in user home + quick css fixes
durandn
parents: 98
diff changeset
   501
        return (self._default_manager.for_app_models("iconolab.annotation").filter(
8ec8aced3f68 small fixes on comment page calculation + added functionality to clear unread notification from button in user home + quick css fixes
durandn
parents: 98
diff changeset
   502
            object_pk=self.object_pk,
101
eb856f9b518c fixed comment page calculation
durandn
parents: 100
diff changeset
   503
        ).filter(thread_id__gte=self.thread_id).filter(order__lte=self.order).count() +1) // settings.COMMENTS_PER_PAGE_DEFAULT + 1
eb856f9b518c fixed comment page calculation
durandn
parents: 100
diff changeset
   504
97
f747c112e8f4 Started work on notification and user homepage + method for a comment to find back its page in his annotation's comments
durandn
parents: 96
diff changeset
   505
    
37
aed809b3a075 Corrected Tag methods bugs + Added comment classes and migration
durandn
parents: 35
diff changeset
   506
class MetaCategory(models.Model):
97
f747c112e8f4 Started work on notification and user homepage + method for a comment to find back its page in his annotation's comments
durandn
parents: 96
diff changeset
   507
    NONE = 0 # Notifies nobody
f747c112e8f4 Started work on notification and user homepage + method for a comment to find back its page in his annotation's comments
durandn
parents: 96
diff changeset
   508
    CONTRIBUTORS = 1 # Notifies contributors (revision owners) on target annotation
f747c112e8f4 Started work on notification and user homepage + method for a comment to find back its page in his annotation's comments
durandn
parents: 96
diff changeset
   509
    COMMENTERS = 2 # Notifies commenters (contributors + comment owners) on target annotation
f747c112e8f4 Started work on notification and user homepage + method for a comment to find back its page in his annotation's comments
durandn
parents: 96
diff changeset
   510
    COLLECTION_ADMINS = 3 # Notifies collection admins
f747c112e8f4 Started work on notification and user homepage + method for a comment to find back its page in his annotation's comments
durandn
parents: 96
diff changeset
   511
    
f747c112e8f4 Started work on notification and user homepage + method for a comment to find back its page in his annotation's comments
durandn
parents: 96
diff changeset
   512
    NOTIFIED_USERS = (
f747c112e8f4 Started work on notification and user homepage + method for a comment to find back its page in his annotation's comments
durandn
parents: 96
diff changeset
   513
        (NONE, 'none'),
f747c112e8f4 Started work on notification and user homepage + method for a comment to find back its page in his annotation's comments
durandn
parents: 96
diff changeset
   514
        (CONTRIBUTORS, 'contributors'),
f747c112e8f4 Started work on notification and user homepage + method for a comment to find back its page in his annotation's comments
durandn
parents: 96
diff changeset
   515
        (COMMENTERS, 'commenters'),
f747c112e8f4 Started work on notification and user homepage + method for a comment to find back its page in his annotation's comments
durandn
parents: 96
diff changeset
   516
        (COLLECTION_ADMINS, 'collection admins'),
f747c112e8f4 Started work on notification and user homepage + method for a comment to find back its page in his annotation's comments
durandn
parents: 96
diff changeset
   517
    )
f747c112e8f4 Started work on notification and user homepage + method for a comment to find back its page in his annotation's comments
durandn
parents: 96
diff changeset
   518
    
139
3e0a5286b257 collection home work (list and tabs) + adjusted image list template+ various design fixes
durandn
parents: 137
diff changeset
   519
    collection = models.ForeignKey(Collection, related_name="metacategories")
48
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   520
    label = models.CharField(max_length=255)
97
f747c112e8f4 Started work on notification and user homepage + method for a comment to find back its page in his annotation's comments
durandn
parents: 96
diff changeset
   521
    triggers_notifications = models.IntegerField(choices=NOTIFIED_USERS, default=NONE)
f747c112e8f4 Started work on notification and user homepage + method for a comment to find back its page in his annotation's comments
durandn
parents: 96
diff changeset
   522
    
48
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   523
    def __str__(self):
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   524
        return self.label
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   525
    
37
aed809b3a075 Corrected Tag methods bugs + Added comment classes and migration
durandn
parents: 35
diff changeset
   526
aed809b3a075 Corrected Tag methods bugs + Added comment classes and migration
durandn
parents: 35
diff changeset
   527
class MetaCategoryInfo(models.Model):
48
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   528
    comment = models.ForeignKey('IconolabComment', on_delete=models.CASCADE)
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   529
    metacategory = models.ForeignKey('MetaCategory', on_delete=models.CASCADE)
133
088195b2a6cc iconolab profile to user relation is now one to one field + added panel for collection admin user tools (to be added) in user home + (simple) django admin for iconolab objects
durandn
parents: 132
diff changeset
   530
    
088195b2a6cc iconolab profile to user relation is now one to one field + added panel for collection admin user tools (to be added) in user home + (simple) django admin for iconolab objects
durandn
parents: 132
diff changeset
   531
    def __str__(self):
088195b2a6cc iconolab profile to user relation is now one to one field + added panel for collection admin user tools (to be added) in user home + (simple) django admin for iconolab objects
durandn
parents: 132
diff changeset
   532
        return "metacategory:"+self.metacategory.label+":on:"+self.comment.id
6
37baf9d13f32 first commit
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   533
37
aed809b3a075 Corrected Tag methods bugs + Added comment classes and migration
durandn
parents: 35
diff changeset
   534
class CommentAttachement(models.Model):
48
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   535
     
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   536
    LINK = 0
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   537
    IMAGE = 1
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   538
    PDF = 2
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   539
    COMMENT_CHOICES = (
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   540
        (LINK, 'link'),
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   541
        (IMAGE, 'image'),
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   542
        (PDF, 'pdf')
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   543
    )
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   544
    
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   545
    comment = models.ForeignKey('IconolabComment', related_name='attachments', on_delete=models.CASCADE)
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   546
    attachment_type = models.IntegerField(choices=COMMENT_CHOICES, default=0)
132
4728f6c0102e Added user profile to identify collection admins and extend user model + notification on collection_admins metacategories
durandn
parents: 126
diff changeset
   547
    data = models.TextField(blank=False)
4728f6c0102e Added user profile to identify collection admins and extend user model + notification on collection_admins metacategories
durandn
parents: 126
diff changeset
   548
    
4728f6c0102e Added user profile to identify collection admins and extend user model + notification on collection_admins metacategories
durandn
parents: 126
diff changeset
   549
class UserProfile(models.Model):
133
088195b2a6cc iconolab profile to user relation is now one to one field + added panel for collection admin user tools (to be added) in user home + (simple) django admin for iconolab objects
durandn
parents: 132
diff changeset
   550
    user = models.OneToOneField(User, related_name='profile', on_delete=models.CASCADE)
088195b2a6cc iconolab profile to user relation is now one to one field + added panel for collection admin user tools (to be added) in user home + (simple) django admin for iconolab objects
durandn
parents: 132
diff changeset
   551
    administers_collection = models.ForeignKey('Collection', related_name='collection', blank=True, null=True)
088195b2a6cc iconolab profile to user relation is now one to one field + added panel for collection admin user tools (to be added) in user home + (simple) django admin for iconolab objects
durandn
parents: 132
diff changeset
   552
    
088195b2a6cc iconolab profile to user relation is now one to one field + added panel for collection admin user tools (to be added) in user home + (simple) django admin for iconolab objects
durandn
parents: 132
diff changeset
   553
    def __str__(self):
088195b2a6cc iconolab profile to user relation is now one to one field + added panel for collection admin user tools (to be added) in user home + (simple) django admin for iconolab objects
durandn
parents: 132
diff changeset
   554
        return "profile:"+self.user.username