src/iconolab/models.py
author durandn
Fri, 16 Dec 2016 12:00:48 +0100
changeset 285 aa0f3e186d29
parent 284 f52b0f6e2cd9
child 288 9273f1f2c827
permissions -rw-r--r--
fixed metacategory str for admin
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
238
ad770589f0fe Work on admin interface and user pages + added stat metacategories count for annotations + refactored views module #41
durandn
parents: 231
diff changeset
     4
from django.contrib.contenttypes.fields import GenericRelation
6
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
228
64940145bf13 corrected problem with tags count on annotation creation + added relevant tags and accurate tags stats displayed
durandn
parents: 227
diff changeset
     9
import uuid, json, re, requests, urllib, logging
6
37baf9d13f32 first commit
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
    10
228
64940145bf13 corrected problem with tags count on annotation creation + added relevant tags and accurate tags stats displayed
durandn
parents: 227
diff changeset
    11
logger = logging.getLogger(__name__)
6
37baf9d13f32 first commit
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
    12
37
aed809b3a075 Corrected Tag methods bugs + Added comment classes and migration
durandn
parents: 35
diff changeset
    13
6
37baf9d13f32 first commit
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
    14
class Collection(models.Model):
283
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
    15
    """
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
    16
        Collection objects are the thematic item repositories in Iconolab
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
    17
        
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
    18
            name: the name displayed in the url and also used to identify the collection
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
    19
            verbose_name: the name displayed in the text of the pages
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
    20
            description: the short description of the collection that will be displayed by default in pages
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
    21
            complete_description: the complete description that will be shown with a "view more" button/link
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
    22
            image/height/width: the collection image that will be shown in the collection description
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
    23
            show_image_on_home: if True, the collection will appear by default on the homepage as one of the bigger images
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
    24
    """
64
4d1e369e85d4 Added a verbose name field for collection objects, home view now gets a list of all collections
durandn
parents: 62
diff changeset
    25
    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
    26
    verbose_name = models.CharField(max_length=50, null=True, blank=True)
212
1c7cce196665 Added complete_description attribute to Collection for homepage #12 + Vue.js components to handle display and sart to harmonize javascript
durandn
parents: 193
diff changeset
    27
    description = models.TextField(null=True, blank=True, default="")
1c7cce196665 Added complete_description attribute to Collection for homepage #12 + Vue.js components to handle display and sart to harmonize javascript
durandn
parents: 193
diff changeset
    28
    complete_description = models.TextField(null=True, blank=True, default="")
122
4fab4a909915 Added image field in collection for homepage view
durandn
parents: 116
diff changeset
    29
    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
    30
    height = models.IntegerField(null=True, blank=True)
4fab4a909915 Added image field in collection for homepage view
durandn
parents: 116
diff changeset
    31
    width = models.IntegerField(null=True, blank=True)
263
ab794183f134 Reworked home so collection images are presented when we first enter the site #54
durandn
parents: 250
diff changeset
    32
    show_image_on_home = models.BooleanField(default=False)
6
37baf9d13f32 first commit
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
    33
48
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
    34
    def __str__(self):
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
    35
        return self.name
6
37baf9d13f32 first commit
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
    36
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
    37
31
e34b70a00488 adding annotations list
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents: 30
diff changeset
    38
class Item(models.Model):
283
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
    39
    """
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
    40
        Item objects belong to a collection, are linked to a metadata item, and to one or more images
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
    41
    """
48
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
    42
    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
    43
    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
    44
    
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
    45
    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
    46
        return str(self.item_guid)+":from:"+self.collection.name
48
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
    47
    
231
b7c007c10abe images in item lists are now ordered by name to preserve naming convention logic at the very least, before implementing a better alternative #36
durandn
parents: 228
diff changeset
    48
    @property
b7c007c10abe images in item lists are now ordered by name to preserve naming convention logic at the very least, before implementing a better alternative #36
durandn
parents: 228
diff changeset
    49
    def images_sorted_by_name(self):
b7c007c10abe images in item lists are now ordered by name to preserve naming convention logic at the very least, before implementing a better alternative #36
durandn
parents: 228
diff changeset
    50
        return self.images.order_by("-name").all()
b7c007c10abe images in item lists are now ordered by name to preserve naming convention logic at the very least, before implementing a better alternative #36
durandn
parents: 228
diff changeset
    51
    
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
    52
class ItemMetadata(models.Model):
283
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
    53
    """
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
    54
        Metadata object for the item class. Each field represents what we can import from the provided .csv files 
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
    55
    """
48
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
    56
    item = models.OneToOneField('Item', related_name='metadatas')
110
ce46bbafb079 image import command + proper item metadatas
durandn
parents: 106
diff changeset
    57
    authors = models.CharField(max_length=255, default="")
ce46bbafb079 image import command + proper item metadatas
durandn
parents: 106
diff changeset
    58
    school = models.CharField(max_length=255, default="")
250
c3ff8c5b28e6 Pregeneration of thumbnail in import command + added "field" metadata for items + added updatecollection command
durandn
parents: 242
diff changeset
    59
    field = models.CharField(max_length=255, default="")
110
ce46bbafb079 image import command + proper item metadatas
durandn
parents: 106
diff changeset
    60
    designation = models.CharField(max_length=255, default="")
ce46bbafb079 image import command + proper item metadatas
durandn
parents: 106
diff changeset
    61
    datation = models.CharField(max_length=255, default="")
ce46bbafb079 image import command + proper item metadatas
durandn
parents: 106
diff changeset
    62
    technics = models.CharField(max_length=255, default="")
ce46bbafb079 image import command + proper item metadatas
durandn
parents: 106
diff changeset
    63
    measurements = models.CharField(max_length=255, default="")
ce46bbafb079 image import command + proper item metadatas
durandn
parents: 106
diff changeset
    64
    create_or_usage_location = models.CharField(max_length=255, default="")
ce46bbafb079 image import command + proper item metadatas
durandn
parents: 106
diff changeset
    65
    discovery_context = models.CharField(max_length=255, default="")
ce46bbafb079 image import command + proper item metadatas
durandn
parents: 106
diff changeset
    66
    conservation_location = models.CharField(max_length=255, default="")
ce46bbafb079 image import command + proper item metadatas
durandn
parents: 106
diff changeset
    67
    photo_credits = models.CharField(max_length=255, default="")
ce46bbafb079 image import command + proper item metadatas
durandn
parents: 106
diff changeset
    68
    inventory_number = models.CharField(max_length=255, default="")
ce46bbafb079 image import command + proper item metadatas
durandn
parents: 106
diff changeset
    69
    joconde_ref = models.CharField(max_length=255, default="")
ce46bbafb079 image import command + proper item metadatas
durandn
parents: 106
diff changeset
    70
    
ce46bbafb079 image import command + proper item metadatas
durandn
parents: 106
diff changeset
    71
    @property
ce46bbafb079 image import command + proper item metadatas
durandn
parents: 106
diff changeset
    72
    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
    73
        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
    74
    
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
    75
    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
    76
        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
    77
31
e34b70a00488 adding annotations list
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents: 30
diff changeset
    78
283
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
    79
class Image(models.Model):
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
    80
    """
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
    81
        Each image object is linked to one item, users can create annotations on images
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
    82
    """
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
    83
    
48
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
    84
    image_guid = models.UUIDField(default=uuid.uuid4, editable=False)
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
    85
    name = models.CharField(max_length=200)
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
    86
    media = models.ImageField(upload_to='uploads/', height_field='height', width_field='width')
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
    87
    item = models.ForeignKey('Item', related_name='images', null=True, blank=True)
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
    88
    height = models.IntegerField(null=False, blank=False)
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
    89
    width = models.IntegerField(null=False, blank=False)
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
    90
    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
    91
    
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
    92
    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
    93
        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
    94
    
106
233bda6f2865 iconolab search
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents: 104
diff changeset
    95
    @property
227
b67ea0d6c621 search page design and item preview for multiple images #36
durandn
parents: 225
diff changeset
    96
    def wh_ratio(self):
b67ea0d6c621 search page design and item preview for multiple images #36
durandn
parents: 225
diff changeset
    97
        return self.width / self.height
b67ea0d6c621 search page design and item preview for multiple images #36
durandn
parents: 225
diff changeset
    98
    
b67ea0d6c621 search page design and item preview for multiple images #36
durandn
parents: 225
diff changeset
    99
    @property
106
233bda6f2865 iconolab search
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents: 104
diff changeset
   100
    def collection(self):
233bda6f2865 iconolab search
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents: 104
diff changeset
   101
        return self.item.collection.name
233bda6f2865 iconolab search
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents: 104
diff changeset
   102
233bda6f2865 iconolab search
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents: 104
diff changeset
   103
    @property
233bda6f2865 iconolab search
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents: 104
diff changeset
   104
    def title(self):
116
1e2caa72bf2f updated indexes
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents: 110
diff changeset
   105
        return self.item.metadatas.designation
1e2caa72bf2f updated indexes
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents: 110
diff changeset
   106
1e2caa72bf2f updated indexes
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents: 110
diff changeset
   107
    @property
1e2caa72bf2f updated indexes
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents: 110
diff changeset
   108
    def authors(self):
1e2caa72bf2f updated indexes
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents: 110
diff changeset
   109
        return self.item.metadatas.authors
1e2caa72bf2f updated indexes
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents: 110
diff changeset
   110
1e2caa72bf2f updated indexes
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents: 110
diff changeset
   111
    @property
1e2caa72bf2f updated indexes
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents: 110
diff changeset
   112
    def school(self):
1e2caa72bf2f updated indexes
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents: 110
diff changeset
   113
        return self.item.metadatas.school
1e2caa72bf2f updated indexes
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents: 110
diff changeset
   114
        
1e2caa72bf2f updated indexes
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents: 110
diff changeset
   115
    @property
1e2caa72bf2f updated indexes
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents: 110
diff changeset
   116
    def designation(self):
1e2caa72bf2f updated indexes
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents: 110
diff changeset
   117
        return self.item.metadatas.designation
1e2caa72bf2f updated indexes
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents: 110
diff changeset
   118
1e2caa72bf2f updated indexes
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents: 110
diff changeset
   119
    @property
1e2caa72bf2f updated indexes
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents: 110
diff changeset
   120
    def datation(self):
1e2caa72bf2f updated indexes
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents: 110
diff changeset
   121
        return self.item.metadatas.datation
106
233bda6f2865 iconolab search
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents: 104
diff changeset
   122
    
233bda6f2865 iconolab search
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents: 104
diff changeset
   123
    @property
116
1e2caa72bf2f updated indexes
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents: 110
diff changeset
   124
    def technics(self):
1e2caa72bf2f updated indexes
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents: 110
diff changeset
   125
        return self.item.metadatas.technics
1e2caa72bf2f updated indexes
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents: 110
diff changeset
   126
1e2caa72bf2f updated indexes
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents: 110
diff changeset
   127
    @property
1e2caa72bf2f updated indexes
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents: 110
diff changeset
   128
    def measurements(self):
1e2caa72bf2f updated indexes
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents: 110
diff changeset
   129
        return self.item.metadatas.measurements
1e2caa72bf2f updated indexes
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents: 110
diff changeset
   130
1e2caa72bf2f updated indexes
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents: 110
diff changeset
   131
    @property
151
797460904f77 indexing using tag labels
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents: 139
diff changeset
   132
    def tag_labels(self):
116
1e2caa72bf2f updated indexes
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents: 110
diff changeset
   133
        tag_list = []
1e2caa72bf2f updated indexes
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents: 110
diff changeset
   134
        for annotation in self.annotations.all():
151
797460904f77 indexing using tag labels
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents: 139
diff changeset
   135
            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
   136
            tag_list += [tag_infos['tag_label'] for tag_infos in revision_tags if tag_infos.get('tag_label') is not None] #deal with
116
1e2caa72bf2f updated indexes
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents: 110
diff changeset
   137
        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
   138
283
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   139
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   140
class ImageStats(models.Model):
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   141
    """
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   142
        Stats objects for a given image, keep count of several values to be displayed in image and item pages
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   143
    """
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   144
    image = models.OneToOneField('Image', related_name='stats', blank=False, null=False)
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   145
    views_count = models.IntegerField(blank=True, null=True, default=0)
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   146
    annotations_count = models.IntegerField(blank=True, null=True, default=0)
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   147
    submitted_revisions_count = models.IntegerField(blank=True, null=True, default=0)
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   148
    comments_count = models.IntegerField(blank=True, null=True, default=0)
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   149
    folders_inclusion_count = models.IntegerField(blank=True, null=True, default=0)
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   150
    tag_count = models.IntegerField(blank=True, null=True, default=0)
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   151
    
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   152
    def __str__(self):
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   153
        return "stats:for:"+str(self.image.image_guid)
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   154
    
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   155
    def set_tags_stats(self):
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   156
        self.tag_count = Tag.objects.filter(tagginginfo__revision__annotation__image = self.image).distinct().count()
48
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   157
    
283
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   158
    @transaction.atomic
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   159
    def update_stats(self):
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   160
        self.annotations_count = 0
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   161
        self.submitted_revisions_count = 0
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   162
        self.comments_count = 0
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   163
        image_annotations = Annotation.objects.filter(image=self.image)
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   164
        # views_count - Can't do much about views count
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   165
        # annotations_count
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   166
        self.annotations_count = image_annotations.count()
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   167
        # submitted_revisions_count & comment_count
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   168
        for annotation in image_annotations.all():
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   169
            annotation_revisions = annotation.revisions
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   170
            self.submitted_revisions_count += annotation_revisions.count()
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   171
            
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   172
            self.comments_count += XtdComment.objects.for_app_models("iconolab.annotation").filter(
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   173
                object_pk = annotation.pk,
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   174
            ).count()
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   175
        # tag_count
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   176
        self.tag_count = Tag.objects.filter(tagginginfo__revision__annotation__image = self.image).distinct().count()
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   177
        self.save()
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   178
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   179
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   180
class AnnotationManager(models.Manager):
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   181
    """
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   182
        Manager class for annotation, it handles annotation creation (with initial revision creation, and 
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   183
        has methods to get a list of annotation commented for a given user, and a list of annotations contributed for a
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   184
        given user
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   185
    """
48
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   186
    @transaction.atomic
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   187
    def create_annotation(self, author, image, title='', description='', fragment='', tags_json='[]'):
283
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   188
        """
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   189
            Creates a new Annotation with its associated AnnotationStats and initial AnnotationRevision
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   190
        """
48
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   191
        # Create annotation object
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   192
        new_annotation = Annotation(
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   193
            image=image, 
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   194
            author=author
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   195
        )
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   196
        new_annotation.save()
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   197
        
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   198
        # Create initial revision
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   199
        initial_revision = AnnotationRevision(
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   200
            annotation=new_annotation, 
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   201
            author=author,
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   202
            title=title,
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   203
            description=description,
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   204
            fragment=fragment,
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   205
            state=AnnotationRevision.ACCEPTED
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   206
        )
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   207
        initial_revision.save()
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   208
        initial_revision.set_tags(tags_json)
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   209
        
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   210
        # Create stats object
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   211
        new_annotation_stats = AnnotationStats(annotation=new_annotation)
228
64940145bf13 corrected problem with tags count on annotation creation + added relevant tags and accurate tags stats displayed
durandn
parents: 227
diff changeset
   212
        new_annotation_stats.set_tags_stats()
48
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   213
        new_annotation_stats.save()
123
c8a1216fd28f removing prints
durandn
parents: 122
diff changeset
   214
        
c8a1216fd28f removing prints
durandn
parents: 122
diff changeset
   215
        # Link everything to parent
c8a1216fd28f removing prints
durandn
parents: 122
diff changeset
   216
        new_annotation.current_revision = initial_revision
48
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   217
        new_annotation.stats = new_annotation_stats
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   218
        new_annotation.save()
134
350bdfe7c289 sending revision_created signal when creating an annotation
durandn
parents: 133
diff changeset
   219
        iconolab_signals.revision_created.send(sender=AnnotationRevision, instance=initial_revision)
48
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   220
        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
   221
238
ad770589f0fe Work on admin interface and user pages + added stat metacategories count for annotations + refactored views module #41
durandn
parents: 231
diff changeset
   222
    @transaction.atomic
ad770589f0fe Work on admin interface and user pages + added stat metacategories count for annotations + refactored views module #41
durandn
parents: 231
diff changeset
   223
    def get_annotations_contributed_for_user(self, user):
ad770589f0fe Work on admin interface and user pages + added stat metacategories count for annotations + refactored views module #41
durandn
parents: 231
diff changeset
   224
        """
ad770589f0fe Work on admin interface and user pages + added stat metacategories count for annotations + refactored views module #41
durandn
parents: 231
diff changeset
   225
            user is the user whom we want to get the contributed annotations
ad770589f0fe Work on admin interface and user pages + added stat metacategories count for annotations + refactored views module #41
durandn
parents: 231
diff changeset
   226
            
ad770589f0fe Work on admin interface and user pages + added stat metacategories count for annotations + refactored views module #41
durandn
parents: 231
diff changeset
   227
            Returns the list of all the annotations on which the user submitted a revision but did not create the annotation
ad770589f0fe Work on admin interface and user pages + added stat metacategories count for annotations + refactored views module #41
durandn
parents: 231
diff changeset
   228
            List of dict in the format:
ad770589f0fe Work on admin interface and user pages + added stat metacategories count for annotations + refactored views module #41
durandn
parents: 231
diff changeset
   229
            
ad770589f0fe Work on admin interface and user pages + added stat metacategories count for annotations + refactored views module #41
durandn
parents: 231
diff changeset
   230
            {
ad770589f0fe Work on admin interface and user pages + added stat metacategories count for annotations + refactored views module #41
durandn
parents: 231
diff changeset
   231
                "annotation_obj": annotation object,
ad770589f0fe Work on admin interface and user pages + added stat metacategories count for annotations + refactored views module #41
durandn
parents: 231
diff changeset
   232
                "revisions_count": revisions count for user
ad770589f0fe Work on admin interface and user pages + added stat metacategories count for annotations + refactored views module #41
durandn
parents: 231
diff changeset
   233
                "awaiting_count": awaiting revisions for user on this annotation
ad770589f0fe Work on admin interface and user pages + added stat metacategories count for annotations + refactored views module #41
durandn
parents: 231
diff changeset
   234
                "accepted_count": accepted revisions for user
ad770589f0fe Work on admin interface and user pages + added stat metacategories count for annotations + refactored views module #41
durandn
parents: 231
diff changeset
   235
                "latest_submitted_revision": date of the latest submitted revision from user on annotation
ad770589f0fe Work on admin interface and user pages + added stat metacategories count for annotations + refactored views module #41
durandn
parents: 231
diff changeset
   236
            }
ad770589f0fe Work on admin interface and user pages + added stat metacategories count for annotations + refactored views module #41
durandn
parents: 231
diff changeset
   237
        """
ad770589f0fe Work on admin interface and user pages + added stat metacategories count for annotations + refactored views module #41
durandn
parents: 231
diff changeset
   238
        latest_revision_on_annotations = []
ad770589f0fe Work on admin interface and user pages + added stat metacategories count for annotations + refactored views module #41
durandn
parents: 231
diff changeset
   239
        user_contributed_annotations = Annotation.objects.filter(revisions__author=user).exclude(author=user).prefetch_related(
ad770589f0fe Work on admin interface and user pages + added stat metacategories count for annotations + refactored views module #41
durandn
parents: 231
diff changeset
   240
            'current_revision',
ad770589f0fe Work on admin interface and user pages + added stat metacategories count for annotations + refactored views module #41
durandn
parents: 231
diff changeset
   241
            'revisions',
ad770589f0fe Work on admin interface and user pages + added stat metacategories count for annotations + refactored views module #41
durandn
parents: 231
diff changeset
   242
            'image',
ad770589f0fe Work on admin interface and user pages + added stat metacategories count for annotations + refactored views module #41
durandn
parents: 231
diff changeset
   243
            'image__item',
ad770589f0fe Work on admin interface and user pages + added stat metacategories count for annotations + refactored views module #41
durandn
parents: 231
diff changeset
   244
            'image__item__collection').distinct()
ad770589f0fe Work on admin interface and user pages + added stat metacategories count for annotations + refactored views module #41
durandn
parents: 231
diff changeset
   245
        for annotation in user_contributed_annotations.all():
ad770589f0fe Work on admin interface and user pages + added stat metacategories count for annotations + refactored views module #41
durandn
parents: 231
diff changeset
   246
            latest_revision_on_annotations.append(annotation.revisions.filter(author=user).latest(field_name="created"))
ad770589f0fe Work on admin interface and user pages + added stat metacategories count for annotations + refactored views module #41
durandn
parents: 231
diff changeset
   247
        contributed_list = []
ad770589f0fe Work on admin interface and user pages + added stat metacategories count for annotations + refactored views module #41
durandn
parents: 231
diff changeset
   248
        if latest_revision_on_annotations:
ad770589f0fe Work on admin interface and user pages + added stat metacategories count for annotations + refactored views module #41
durandn
parents: 231
diff changeset
   249
            latest_revision_on_annotations.sort(key=lambda item:item.created, reverse=True)
ad770589f0fe Work on admin interface and user pages + added stat metacategories count for annotations + refactored views module #41
durandn
parents: 231
diff changeset
   250
            contributed_list= [
ad770589f0fe Work on admin interface and user pages + added stat metacategories count for annotations + refactored views module #41
durandn
parents: 231
diff changeset
   251
                {
ad770589f0fe Work on admin interface and user pages + added stat metacategories count for annotations + refactored views module #41
durandn
parents: 231
diff changeset
   252
                    "annotation_obj": revision.annotation, 
ad770589f0fe Work on admin interface and user pages + added stat metacategories count for annotations + refactored views module #41
durandn
parents: 231
diff changeset
   253
                    "revisions_count": revision.annotation.revisions.filter(author=user).count(),
ad770589f0fe Work on admin interface and user pages + added stat metacategories count for annotations + refactored views module #41
durandn
parents: 231
diff changeset
   254
                    "awaiting_count": revision.annotation.revisions.filter(author=user, state=AnnotationRevision.AWAITING).count(),
ad770589f0fe Work on admin interface and user pages + added stat metacategories count for annotations + refactored views module #41
durandn
parents: 231
diff changeset
   255
                    "accepted_count": revision.annotation.revisions.filter(author=user, state=AnnotationRevision.ACCEPTED).count(),
ad770589f0fe Work on admin interface and user pages + added stat metacategories count for annotations + refactored views module #41
durandn
parents: 231
diff changeset
   256
                    "latest_submitted_revision": revision.created
ad770589f0fe Work on admin interface and user pages + added stat metacategories count for annotations + refactored views module #41
durandn
parents: 231
diff changeset
   257
                }
ad770589f0fe Work on admin interface and user pages + added stat metacategories count for annotations + refactored views module #41
durandn
parents: 231
diff changeset
   258
                for revision in latest_revision_on_annotations
ad770589f0fe Work on admin interface and user pages + added stat metacategories count for annotations + refactored views module #41
durandn
parents: 231
diff changeset
   259
            ]
ad770589f0fe Work on admin interface and user pages + added stat metacategories count for annotations + refactored views module #41
durandn
parents: 231
diff changeset
   260
        logger.debug(contributed_list)
ad770589f0fe Work on admin interface and user pages + added stat metacategories count for annotations + refactored views module #41
durandn
parents: 231
diff changeset
   261
        return contributed_list
ad770589f0fe Work on admin interface and user pages + added stat metacategories count for annotations + refactored views module #41
durandn
parents: 231
diff changeset
   262
    
ad770589f0fe Work on admin interface and user pages + added stat metacategories count for annotations + refactored views module #41
durandn
parents: 231
diff changeset
   263
    @transaction.atomic
ad770589f0fe Work on admin interface and user pages + added stat metacategories count for annotations + refactored views module #41
durandn
parents: 231
diff changeset
   264
    def get_annotations_commented_for_user(self, user, ignore_revisions_comments=True):
ad770589f0fe Work on admin interface and user pages + added stat metacategories count for annotations + refactored views module #41
durandn
parents: 231
diff changeset
   265
        """
ad770589f0fe Work on admin interface and user pages + added stat metacategories count for annotations + refactored views module #41
durandn
parents: 231
diff changeset
   266
            user is the user for which we want to get the commented annotations
ad770589f0fe Work on admin interface and user pages + added stat metacategories count for annotations + refactored views module #41
durandn
parents: 231
diff changeset
   267
            ignore_revisions_comment allows to filter comments that are associated with a revision
ad770589f0fe Work on admin interface and user pages + added stat metacategories count for annotations + refactored views module #41
durandn
parents: 231
diff changeset
   268
        
ad770589f0fe Work on admin interface and user pages + added stat metacategories count for annotations + refactored views module #41
durandn
parents: 231
diff changeset
   269
        
ad770589f0fe Work on admin interface and user pages + added stat metacategories count for annotations + refactored views module #41
durandn
parents: 231
diff changeset
   270
            Returns a list of all annotations on which a given user commented with user-comments-related data
ad770589f0fe Work on admin interface and user pages + added stat metacategories count for annotations + refactored views module #41
durandn
parents: 231
diff changeset
   271
            List of dict in the format:
ad770589f0fe Work on admin interface and user pages + added stat metacategories count for annotations + refactored views module #41
durandn
parents: 231
diff changeset
   272
            
ad770589f0fe Work on admin interface and user pages + added stat metacategories count for annotations + refactored views module #41
durandn
parents: 231
diff changeset
   273
            {
ad770589f0fe Work on admin interface and user pages + added stat metacategories count for annotations + refactored views module #41
durandn
parents: 231
diff changeset
   274
                "annotation_obj": annotation object,
ad770589f0fe Work on admin interface and user pages + added stat metacategories count for annotations + refactored views module #41
durandn
parents: 231
diff changeset
   275
                "comment_count": comment count for user
ad770589f0fe Work on admin interface and user pages + added stat metacategories count for annotations + refactored views module #41
durandn
parents: 231
diff changeset
   276
                "latest_comment_date": date of the latest comment from user on annotation
ad770589f0fe Work on admin interface and user pages + added stat metacategories count for annotations + refactored views module #41
durandn
parents: 231
diff changeset
   277
            }
ad770589f0fe Work on admin interface and user pages + added stat metacategories count for annotations + refactored views module #41
durandn
parents: 231
diff changeset
   278
        """
ad770589f0fe Work on admin interface and user pages + added stat metacategories count for annotations + refactored views module #41
durandn
parents: 231
diff changeset
   279
        user_comments = IconolabComment.objects.filter(user=user, content_type__app_label='iconolab', content_type__model='annotation').order_by('-submit_date')
ad770589f0fe Work on admin interface and user pages + added stat metacategories count for annotations + refactored views module #41
durandn
parents: 231
diff changeset
   280
        if ignore_revisions_comments:
ad770589f0fe Work on admin interface and user pages + added stat metacategories count for annotations + refactored views module #41
durandn
parents: 231
diff changeset
   281
            logger.debug(user_comments.count())
ad770589f0fe Work on admin interface and user pages + added stat metacategories count for annotations + refactored views module #41
durandn
parents: 231
diff changeset
   282
            user_comments = user_comments.filter(revision__isnull=True)
ad770589f0fe Work on admin interface and user pages + added stat metacategories count for annotations + refactored views module #41
durandn
parents: 231
diff changeset
   283
            logger.debug(user_comments.count())
ad770589f0fe Work on admin interface and user pages + added stat metacategories count for annotations + refactored views module #41
durandn
parents: 231
diff changeset
   284
        all_user_comments_data = [(comment.object_pk, comment.submit_date) for comment in user_comments]
ad770589f0fe Work on admin interface and user pages + added stat metacategories count for annotations + refactored views module #41
durandn
parents: 231
diff changeset
   285
        unique_ordered_comments_data = []
ad770589f0fe Work on admin interface and user pages + added stat metacategories count for annotations + refactored views module #41
durandn
parents: 231
diff changeset
   286
        for (id, submit_date) in all_user_comments_data: 
ad770589f0fe Work on admin interface and user pages + added stat metacategories count for annotations + refactored views module #41
durandn
parents: 231
diff changeset
   287
            if id not in [item["annotation_id"] for item in unique_ordered_comments_data]:
ad770589f0fe Work on admin interface and user pages + added stat metacategories count for annotations + refactored views module #41
durandn
parents: 231
diff changeset
   288
                unique_ordered_comments_data.append({"annotation_id": id, "latest_comment_date": submit_date})
ad770589f0fe Work on admin interface and user pages + added stat metacategories count for annotations + refactored views module #41
durandn
parents: 231
diff changeset
   289
        commented_annotations = Annotation.objects.filter(id__in=[item["annotation_id"] for item in unique_ordered_comments_data]).prefetch_related(
ad770589f0fe Work on admin interface and user pages + added stat metacategories count for annotations + refactored views module #41
durandn
parents: 231
diff changeset
   290
            'current_revision',
ad770589f0fe Work on admin interface and user pages + added stat metacategories count for annotations + refactored views module #41
durandn
parents: 231
diff changeset
   291
            'revisions',
ad770589f0fe Work on admin interface and user pages + added stat metacategories count for annotations + refactored views module #41
durandn
parents: 231
diff changeset
   292
            'image',
ad770589f0fe Work on admin interface and user pages + added stat metacategories count for annotations + refactored views module #41
durandn
parents: 231
diff changeset
   293
            'image__item',
ad770589f0fe Work on admin interface and user pages + added stat metacategories count for annotations + refactored views module #41
durandn
parents: 231
diff changeset
   294
            'image__item__collection'
ad770589f0fe Work on admin interface and user pages + added stat metacategories count for annotations + refactored views module #41
durandn
parents: 231
diff changeset
   295
        ).distinct()
ad770589f0fe Work on admin interface and user pages + added stat metacategories count for annotations + refactored views module #41
durandn
parents: 231
diff changeset
   296
        sorted_annotations_list = []
ad770589f0fe Work on admin interface and user pages + added stat metacategories count for annotations + refactored views module #41
durandn
parents: 231
diff changeset
   297
        logger.debug(unique_ordered_comments_data)
ad770589f0fe Work on admin interface and user pages + added stat metacategories count for annotations + refactored views module #41
durandn
parents: 231
diff changeset
   298
        for comment_data in unique_ordered_comments_data:
ad770589f0fe Work on admin interface and user pages + added stat metacategories count for annotations + refactored views module #41
durandn
parents: 231
diff changeset
   299
            annotation_obj = commented_annotations.get(id=comment_data["annotation_id"])
ad770589f0fe Work on admin interface and user pages + added stat metacategories count for annotations + refactored views module #41
durandn
parents: 231
diff changeset
   300
            sorted_annotations_list.append(
ad770589f0fe Work on admin interface and user pages + added stat metacategories count for annotations + refactored views module #41
durandn
parents: 231
diff changeset
   301
                {
ad770589f0fe Work on admin interface and user pages + added stat metacategories count for annotations + refactored views module #41
durandn
parents: 231
diff changeset
   302
                    "annotation_obj": annotation_obj, 
ad770589f0fe Work on admin interface and user pages + added stat metacategories count for annotations + refactored views module #41
durandn
parents: 231
diff changeset
   303
                    "comment_count_for_user": user_comments.filter(object_pk=annotation_obj.id).count(), 
ad770589f0fe Work on admin interface and user pages + added stat metacategories count for annotations + refactored views module #41
durandn
parents: 231
diff changeset
   304
                    "latest_comment_date": comment_data["latest_comment_date"]
ad770589f0fe Work on admin interface and user pages + added stat metacategories count for annotations + refactored views module #41
durandn
parents: 231
diff changeset
   305
                }
ad770589f0fe Work on admin interface and user pages + added stat metacategories count for annotations + refactored views module #41
durandn
parents: 231
diff changeset
   306
            )
ad770589f0fe Work on admin interface and user pages + added stat metacategories count for annotations + refactored views module #41
durandn
parents: 231
diff changeset
   307
        return sorted_annotations_list
ad770589f0fe Work on admin interface and user pages + added stat metacategories count for annotations + refactored views module #41
durandn
parents: 231
diff changeset
   308
101
eb856f9b518c fixed comment page calculation
durandn
parents: 100
diff changeset
   309
283
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   310
class Annotation(models.Model):
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   311
    """
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   312
        Annotation objects are created on a given image, each annotation have a list of revisions to keep track of its history, the latest revision is the 'current revision' 
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   313
        that will be displayed by default in most pages. 
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   314
        
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   315
        Annotation data (title, description, fragment) is thus stored in the revision.
284
f52b0f6e2cd9 added fields on metacategories, annotations to track the validation state of the annotation, with example handler in signals that check and update the state when a relevant metacategory is posted.
durandn
parents: 283
diff changeset
   316
        
f52b0f6e2cd9 added fields on metacategories, annotations to track the validation state of the annotation, with example handler in signals that check and update the state when a relevant metacategory is posted.
durandn
parents: 283
diff changeset
   317
        Annotations can be considered validated or not depending on the metacategories posted in their comments through the attribute validation_state. Their validation state
f52b0f6e2cd9 added fields on metacategories, annotations to track the validation state of the annotation, with example handler in signals that check and update the state when a relevant metacategory is posted.
durandn
parents: 283
diff changeset
   318
        can also be overriden and in such case we can use validation_state_overriden attribute to remember it in the model (so for instance if an admin un-validates an annotation
f52b0f6e2cd9 added fields on metacategories, annotations to track the validation state of the annotation, with example handler in signals that check and update the state when a relevant metacategory is posted.
durandn
parents: 283
diff changeset
   319
        we could block it from being validated again)
283
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   320
    """
284
f52b0f6e2cd9 added fields on metacategories, annotations to track the validation state of the annotation, with example handler in signals that check and update the state when a relevant metacategory is posted.
durandn
parents: 283
diff changeset
   321
    UNVALIDATED = 0
f52b0f6e2cd9 added fields on metacategories, annotations to track the validation state of the annotation, with example handler in signals that check and update the state when a relevant metacategory is posted.
durandn
parents: 283
diff changeset
   322
    VALIDATED = 1
f52b0f6e2cd9 added fields on metacategories, annotations to track the validation state of the annotation, with example handler in signals that check and update the state when a relevant metacategory is posted.
durandn
parents: 283
diff changeset
   323
    VALIDATION_STATES = (
f52b0f6e2cd9 added fields on metacategories, annotations to track the validation state of the annotation, with example handler in signals that check and update the state when a relevant metacategory is posted.
durandn
parents: 283
diff changeset
   324
        (UNVALIDATED, 'unvalidated'),
f52b0f6e2cd9 added fields on metacategories, annotations to track the validation state of the annotation, with example handler in signals that check and update the state when a relevant metacategory is posted.
durandn
parents: 283
diff changeset
   325
        (VALIDATED, 'validated'),
f52b0f6e2cd9 added fields on metacategories, annotations to track the validation state of the annotation, with example handler in signals that check and update the state when a relevant metacategory is posted.
durandn
parents: 283
diff changeset
   326
    )
283
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   327
    annotation_guid = models.UUIDField(default=uuid.uuid4, editable=False)
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   328
    image = models.ForeignKey('Image', related_name='annotations', on_delete=models.CASCADE)
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   329
    source_revision = models.ForeignKey('AnnotationRevision', related_name='source_related_annotation', blank=True, null=True)
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   330
    current_revision = models.OneToOneField('AnnotationRevision', related_name='current_for_annotation', blank=True, null=True)
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   331
    author = models.ForeignKey(User, null=True)
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   332
    created = models.DateTimeField(auto_now_add=True, null=True)
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   333
    comments = GenericRelation('IconolabComment', content_type_field='content_type_id', object_id_field='object_pk')
284
f52b0f6e2cd9 added fields on metacategories, annotations to track the validation state of the annotation, with example handler in signals that check and update the state when a relevant metacategory is posted.
durandn
parents: 283
diff changeset
   334
    validation_state = models.IntegerField(choices=VALIDATION_STATES, default=UNVALIDATED)
f52b0f6e2cd9 added fields on metacategories, annotations to track the validation state of the annotation, with example handler in signals that check and update the state when a relevant metacategory is posted.
durandn
parents: 283
diff changeset
   335
    validation_state_overriden = models.BooleanField(default=False)
283
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   336
    
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   337
    objects = AnnotationManager()
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   338
    
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   339
    def __str__(self):
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   340
        return str(self.annotation_guid)+":"+self.current_revision.title
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   341
        
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   342
    @property
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   343
    def awaiting_revisions_count(self):
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   344
        return self.revisions.filter(state=AnnotationRevision.AWAITING).distinct().count()
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   345
    
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   346
    @property
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   347
    def accepted_revisions_count(self):
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   348
        return self.revisions.filter(state=AnnotationRevision.ACCEPTED).distinct().count()
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   349
    
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   350
    @property
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   351
    def rejected_revisions_count(self):
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   352
        return self.revisions.filter(state=AnnotationRevision.REJECTED).distinct().count()
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   353
    
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   354
    @property
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   355
    def studied_revisions_count(self):
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   356
        return self.revisions.filter(state=AnnotationRevision.STUDIED).distinct().count()
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   357
      
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   358
    @property
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   359
    def total_revisions_count(self):
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   360
        return self.revisions.distinct().count()
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   361
   
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   362
    @property
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   363
    def collection(self):
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   364
        return self.image.collection
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   365
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   366
    @property
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   367
    def tag_labels(self):
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   368
        current_revision_tags = json.loads(self.current_revision.get_tags_json())
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   369
        return [tag_infos['tag_label'] for tag_infos in current_revision_tags if tag_infos.get('tag_label') is not None ]
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   370
    
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   371
    def latest_revision_for_user(self, user):
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   372
        user_revisions = self.revisions.filter(creator=user)
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   373
        if user_revisions.exists():
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   374
            return user_revisions.filter(creator=author).order_by("-created").first()
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   375
        return None
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   376
    
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   377
    @transaction.atomic
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   378
    def make_new_revision(self, author, title, description, fragment, tags_json):
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   379
        """
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   380
            Called to create a new revision, potentially from a merge
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   381
        """
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   382
        if author == self.author:
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   383
            # We're creating an automatically accepted revision
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   384
            new_revision_state = AnnotationRevision.ACCEPTED
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   385
        else:
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   386
            # Revision will require validation
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   387
            new_revision_state = AnnotationRevision.AWAITING
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   388
        new_revision = AnnotationRevision(
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   389
            annotation = self,
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   390
            parent_revision=self.current_revision,
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   391
            title=title,
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   392
            description=description,
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   393
            author=author,
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   394
            fragment=fragment,
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   395
            state=new_revision_state
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   396
        )
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   397
        new_revision.save()
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   398
        new_revision.set_tags(tags_json)
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   399
        if new_revision.state == AnnotationRevision.ACCEPTED:
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   400
            self.current_revision = new_revision
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   401
            self.save()
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   402
        iconolab_signals.revision_created.send(sender=AnnotationRevision, instance=new_revision)
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   403
        return new_revision
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   404
    
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   405
    @transaction.atomic
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   406
    def validate_existing_revision(self, revision_to_validate):
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   407
        """
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   408
            Called when we're validating an awaiting revision whose parent is the current revision AS IT WAS CREATED
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   409
        """
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   410
        if revision_to_validate.parent_revision == self.current_revision and revision_to_validate.state == AnnotationRevision.AWAITING:
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   411
            self.current_revision = revision_to_validate
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   412
            revision_to_validate.state = AnnotationRevision.ACCEPTED
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   413
            revision_to_validate.save()
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   414
            self.save()
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   415
            iconolab_signals.revision_accepted.send(sender=AnnotationRevision, instance=revision_to_validate)
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   416
    
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   417
    @transaction.atomic
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   418
    def reject_existing_revision(self, revision_to_reject):
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   419
        """
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   420
             Called when we reject a revision
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   421
        """
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   422
        if revision_to_reject.state == AnnotationRevision.AWAITING:
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   423
            revision_to_reject.state = AnnotationRevision.REJECTED
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   424
            revision_to_reject.save()
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   425
            iconolab_signals.revision_rejected.send(sender=AnnotationRevision, instance=revision_to_reject)
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   426
    
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   427
    @transaction.atomic
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   428
    def merge_existing_revision(self, title, description, fragment, tags, revision_to_merge):
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   429
        """
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   430
            Called when we're validating an awaiting revision whose parent isn't the current revision or if the awaiting revision was modified by the annotation author
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   431
        """
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   432
        merged_revision = self.make_new_revision(author=self.author, title=title, description=description, fragment=fragment, tags_json=tags)
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   433
        merged_revision.merge_parent_revision = revision_to_merge
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   434
        merged_revision.save()
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   435
        revision_to_merge.state = AnnotationRevision.STUDIED
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   436
        revision_to_merge.save()
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   437
        iconolab_signals.revision_accepted.send(sender=AnnotationRevision, instance=revision_to_merge)
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   438
        self.current_revision=merged_revision
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   439
        self.save()
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   440
        return merged_revision
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   441
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   442
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
   443
class AnnotationStats(models.Model):
283
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   444
    """
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   445
        Stats objects for a given annotation, keep count of several values to be displayed in annotation pages
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   446
    """
48
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   447
    annotation = models.OneToOneField('Annotation', related_name='stats', blank=False, null=False)
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   448
    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
   449
    awaiting_revisions_count = models.IntegerField(blank=True, null=True, default=0)
48
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   450
    accepted_revisions_count = models.IntegerField(blank=True, null=True, default=1)
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   451
    contributors_count = models.IntegerField(blank=True, null=True, default=1)
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   452
    views_count = models.IntegerField(blank=True, null=True, default=0)
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   453
    comments_count = models.IntegerField(blank=True, null=True, default=0)
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   454
    tag_count = models.IntegerField(blank=True, null=True, default=0)
238
ad770589f0fe Work on admin interface and user pages + added stat metacategories count for annotations + refactored views module #41
durandn
parents: 231
diff changeset
   455
    metacategories = models.ManyToManyField('MetaCategory', through='MetaCategoriesCountInfo', through_fields=('annotation_stats_obj', 'metacategory'))
48
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   456
    
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
   457
    def __str__(self):
219
ce1e12435c44 cleaned up __str__ methods
durandn
parents: 216
diff changeset
   458
        return "stats:for:"+str(self.annotation.annotation_guid)
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
   459
    
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
   460
    @property
48
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   461
    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
   462
        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
   463
        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
   464
    
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
   465
    @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
   466
    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
   467
        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
   468
        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
   469
    
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
   470
    def set_tags_stats(self):
228
64940145bf13 corrected problem with tags count on annotation creation + added relevant tags and accurate tags stats displayed
durandn
parents: 227
diff changeset
   471
        self.tag_count = Tag.objects.filter(tagginginfo__revision = self.annotation.current_revision).distinct().count()
64940145bf13 corrected problem with tags count on annotation creation + added relevant tags and accurate tags stats displayed
durandn
parents: 227
diff changeset
   472
    
64940145bf13 corrected problem with tags count on annotation creation + added relevant tags and accurate tags stats displayed
durandn
parents: 227
diff changeset
   473
    @property
64940145bf13 corrected problem with tags count on annotation creation + added relevant tags and accurate tags stats displayed
durandn
parents: 227
diff changeset
   474
    def relevant_tags_count(self, score=settings.RELEVANT_TAGS_MIN_SCORE):
64940145bf13 corrected problem with tags count on annotation creation + added relevant tags and accurate tags stats displayed
durandn
parents: 227
diff changeset
   475
        return TaggingInfo.objects.filter(revision=self.annotation.current_revision, relevancy__gte=score).distinct().count()
64940145bf13 corrected problem with tags count on annotation creation + added relevant tags and accurate tags stats displayed
durandn
parents: 227
diff changeset
   476
    
64940145bf13 corrected problem with tags count on annotation creation + added relevant tags and accurate tags stats displayed
durandn
parents: 227
diff changeset
   477
    @property
64940145bf13 corrected problem with tags count on annotation creation + added relevant tags and accurate tags stats displayed
durandn
parents: 227
diff changeset
   478
    def accurate_tags_count(self, score=settings.ACCURATE_TAGS_MIN_SCORE):
64940145bf13 corrected problem with tags count on annotation creation + added relevant tags and accurate tags stats displayed
durandn
parents: 227
diff changeset
   479
        return TaggingInfo.objects.filter(revision=self.annotation.current_revision, accuracy__gte=score).distinct().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
   480
    
93
30cd69620039 use atomic transactions on stat updates
durandn
parents: 90
diff changeset
   481
    @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
   482
    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
   483
        # 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
   484
        # 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
   485
        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
   486
        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
   487
        # 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
   488
        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
   489
        # 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
   490
        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
   491
        # 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
   492
        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
   493
            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
   494
        ).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
   495
        # 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
   496
        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
   497
        # tag_count
238
ad770589f0fe Work on admin interface and user pages + added stat metacategories count for annotations + refactored views module #41
durandn
parents: 231
diff changeset
   498
        
ad770589f0fe Work on admin interface and user pages + added stat metacategories count for annotations + refactored views module #41
durandn
parents: 231
diff changeset
   499
        annotation_comments_with_metacategories = IconolabComment.objects.filter(
ad770589f0fe Work on admin interface and user pages + added stat metacategories count for annotations + refactored views module #41
durandn
parents: 231
diff changeset
   500
            content_type__app_label="iconolab", 
ad770589f0fe Work on admin interface and user pages + added stat metacategories count for annotations + refactored views module #41
durandn
parents: 231
diff changeset
   501
            content_type__model="annotation", 
ad770589f0fe Work on admin interface and user pages + added stat metacategories count for annotations + refactored views module #41
durandn
parents: 231
diff changeset
   502
            object_pk=self.annotation.id, 
ad770589f0fe Work on admin interface and user pages + added stat metacategories count for annotations + refactored views module #41
durandn
parents: 231
diff changeset
   503
            metacategories__collection=self.annotation.image.item.collection
ad770589f0fe Work on admin interface and user pages + added stat metacategories count for annotations + refactored views module #41
durandn
parents: 231
diff changeset
   504
        )
ad770589f0fe Work on admin interface and user pages + added stat metacategories count for annotations + refactored views module #41
durandn
parents: 231
diff changeset
   505
        m2m_objects = MetaCategoriesCountInfo.objects.filter(annotation_stats_obj=self)
ad770589f0fe Work on admin interface and user pages + added stat metacategories count for annotations + refactored views module #41
durandn
parents: 231
diff changeset
   506
        for obj in m2m_objects.all():
ad770589f0fe Work on admin interface and user pages + added stat metacategories count for annotations + refactored views module #41
durandn
parents: 231
diff changeset
   507
            obj.count = 0
ad770589f0fe Work on admin interface and user pages + added stat metacategories count for annotations + refactored views module #41
durandn
parents: 231
diff changeset
   508
            obj.save()
ad770589f0fe Work on admin interface and user pages + added stat metacategories count for annotations + refactored views module #41
durandn
parents: 231
diff changeset
   509
        for comment in annotation_comments_with_metacategories.all():
ad770589f0fe Work on admin interface and user pages + added stat metacategories count for annotations + refactored views module #41
durandn
parents: 231
diff changeset
   510
            for metacategory in comment.metacategories.all():
ad770589f0fe Work on admin interface and user pages + added stat metacategories count for annotations + refactored views module #41
durandn
parents: 231
diff changeset
   511
                if metacategory not in self.metacategories.all():
ad770589f0fe Work on admin interface and user pages + added stat metacategories count for annotations + refactored views module #41
durandn
parents: 231
diff changeset
   512
                    MetaCategoriesCountInfo.objects.create(annotation_stats_obj=self, metacategory=metacategory, count=1)
ad770589f0fe Work on admin interface and user pages + added stat metacategories count for annotations + refactored views module #41
durandn
parents: 231
diff changeset
   513
                else:
ad770589f0fe Work on admin interface and user pages + added stat metacategories count for annotations + refactored views module #41
durandn
parents: 231
diff changeset
   514
                    m2m_object = MetaCategoriesCountInfo.objects.filter(annotation_stats_obj=self, metacategory=metacategory).first()
ad770589f0fe Work on admin interface and user pages + added stat metacategories count for annotations + refactored views module #41
durandn
parents: 231
diff changeset
   515
                    m2m_object.count += 1
ad770589f0fe Work on admin interface and user pages + added stat metacategories count for annotations + refactored views module #41
durandn
parents: 231
diff changeset
   516
                    m2m_object.save()
228
64940145bf13 corrected problem with tags count on annotation creation + added relevant tags and accurate tags stats displayed
durandn
parents: 227
diff changeset
   517
        self.set_tags_stats()
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
   518
        self.save()
283
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   519
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   520
 
238
ad770589f0fe Work on admin interface and user pages + added stat metacategories count for annotations + refactored views module #41
durandn
parents: 231
diff changeset
   521
class MetaCategoriesCountInfo(models.Model):
283
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   522
    """
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   523
        M2M class to keep a count of a given metacategory on a given annotation. metacategories are linked to comments, themselve linked to an annotation
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   524
    """
238
ad770589f0fe Work on admin interface and user pages + added stat metacategories count for annotations + refactored views module #41
durandn
parents: 231
diff changeset
   525
    annotation_stats_obj = models.ForeignKey('AnnotationStats', on_delete=models.CASCADE)
ad770589f0fe Work on admin interface and user pages + added stat metacategories count for annotations + refactored views module #41
durandn
parents: 231
diff changeset
   526
    metacategory = models.ForeignKey('MetaCategory', on_delete=models.CASCADE)
ad770589f0fe Work on admin interface and user pages + added stat metacategories count for annotations + refactored views module #41
durandn
parents: 231
diff changeset
   527
    count = models.IntegerField(default=1, blank=False, null=False)
ad770589f0fe Work on admin interface and user pages + added stat metacategories count for annotations + refactored views module #41
durandn
parents: 231
diff changeset
   528
    
ad770589f0fe Work on admin interface and user pages + added stat metacategories count for annotations + refactored views module #41
durandn
parents: 231
diff changeset
   529
    def __str__(self):
ad770589f0fe Work on admin interface and user pages + added stat metacategories count for annotations + refactored views module #41
durandn
parents: 231
diff changeset
   530
        return "metacategory_count_for:"+self.metacategory.label+":on:"+str(self.annotation_stats_obj.annotation.annotation_guid)
6
37baf9d13f32 first commit
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   531
101
eb856f9b518c fixed comment page calculation
durandn
parents: 100
diff changeset
   532
283
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   533
class AnnotationRevision(models.Model):
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   534
    """
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   535
        AnnotationRevisions objects are linked to an annotation and store the data of the annotation at a given time
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   536
        
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   537
        A revision is always in one out of multiple states:
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
   538
        
283
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   539
        - Awaiting: the revision has been submitted but must be validated by the original author of the related annotation
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   540
        - Accepted: the revision has been accepted *as-is* by the author of the related annotation (this happens automatically 
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   541
        if the revision is created by the author of the annotation)
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   542
        - Rejected: the revision has been rejected by the author of the related annotation
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   543
        - Studied: the revision has been studied by the author of the related annotation and was either modified or at the very least compared with the current state
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   544
        through the merge interface, thus creating a new revision merging the current state with the proposal. At this point the proposal is flagged as "studied" to show
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   545
        that the author of the original annotation has considered it
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   546
    """
48
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   547
    AWAITING = 0
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   548
    ACCEPTED = 1
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   549
    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
   550
    STUDIED = 3
48
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   551
    
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   552
    REVISION_STATES = (
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   553
        (AWAITING, 'awaiting'),
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   554
        (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
   555
        (REJECTED, 'rejected'),
8702ab13783e design work on templates, redirect to home on /, implemented accept and reject revisions shortcuts when applicable
durandn
parents: 58
diff changeset
   556
        (STUDIED, 'studied'),
48
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   557
    )
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   558
    
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   559
    revision_guid = models.UUIDField(default=uuid.uuid4)
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   560
    annotation = models.ForeignKey('Annotation', related_name='revisions', null=False, blank=False)
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   561
    parent_revision = models.ForeignKey('AnnotationRevision', related_name='child_revisions', blank=True, null=True)
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   562
    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
   563
    author = models.ForeignKey(User, null=True)
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   564
    title = models.CharField(max_length=255)
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   565
    description = models.TextField(null=True)
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   566
    fragment = models.TextField()
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   567
    tags = models.ManyToManyField('Tag', through='TaggingInfo', through_fields=('revision', 'tag'))
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   568
    state = models.IntegerField(choices=REVISION_STATES, default=AWAITING)
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   569
    created = models.DateTimeField(auto_now_add=True, null=True)
6
37baf9d13f32 first commit
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   570
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
   571
    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
   572
        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
   573
48
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   574
    def set_tags(self, tags_json_string):
283
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   575
        """
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   576
            This method creates tags object and links them to the revision, from a given json that has the following format:
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   577
            
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   578
            [
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   579
                {
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   580
                    "tag_input": the tag string that has been provided. If it is an http(s?):// pattern, it means the tag is external, else it means it is a custom tag
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   581
                    "accuracy": the accuracy value provided by the user
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   582
                    "relevancy": the relevancy value provided by the user
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   583
                },
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   584
                {
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   585
                   ...
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   586
                }
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   587
            ]
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   588
        """
48
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   589
        try:
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   590
            tags_dict = json.loads(tags_json_string)
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   591
        except ValueError:
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   592
            pass
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   593
        for tag_data in tags_dict:
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   594
            tag_string = tag_data.get("tag_input")
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   595
            tag_accuracy = tag_data.get("accuracy", 0)
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   596
            tag_relevancy = tag_data.get("relevancy", 0)
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   597
            
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   598
            if tag_string.startswith("http://") or tag_string.startswith("https://"): #check if url
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   599
                if Tag.objects.filter(link=tag_string).exists(): #check if tag already exists
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   600
                    tag_obj = Tag.objects.get(link=tag_string)
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   601
                else:
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   602
                    tag_obj = Tag.objects.create(
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   603
                        link = tag_string,
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   604
                    )
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   605
            else:
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   606
                new_tag_link = settings.BASE_URL+'/'+slugify(tag_string)
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   607
                if Tag.objects.filter(link=new_tag_link).exists():
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   608
                    # Somehow we received a label for an existing tag
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   609
                    tag_obj = Tag.objects.get(link=new_tag_link)
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   610
                else:
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   611
                    tag_obj = Tag.objects.create(
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   612
                        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
   613
                        label_slug = slugify(tag_string),
48
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   614
                        description = "",
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   615
                        link = settings.INTERNAL_TAGS_URL+'/'+slugify(tag_string),
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   616
                        collection = self.annotation.image.item.collection
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   617
                    )
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   618
            tag_info = TaggingInfo.objects.create(
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   619
                tag=tag_obj, 
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   620
                revision=self,
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   621
                accuracy = tag_accuracy,
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   622
                relevancy = tag_relevancy
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   623
            )
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   624
        
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   625
    def get_tags_json(self):
283
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   626
        """
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   627
            This method returns the json data that will be sent to the js to display tags for the revision.
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   628
            
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   629
            The json data returned will be of the following format:
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   630
            
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   631
            [
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   632
                {
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   633
                        "tag_label": the tag label for display purposes,
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   634
                        "tag_link": the link of the tag, for instance for dbpedia links,
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   635
                        "accuracy": the accuracy value of the tag,
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   636
                        "relevancy": the relevancy value of the tag,
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   637
                        "is_internal": will be True if the tag is 'internal', meaning specific to Iconolab and 
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   638
                        not an external tag like a dbpedia reference for instance
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   639
                },
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   640
                {
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   641
                    ...
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   642
                }
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   643
            ]
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   644
        """
54
147c8a8b66b6 streamlined templates for easy navigation and missing info + handled fr.dbpedia tags
durandn
parents: 53
diff changeset
   645
        def fetch_from_dbpedia(uri, lang, source):
48
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   646
            sparql_template = 'select distinct * where { <<%uri%>> rdfs:label ?l FILTER( langMatches( lang(?l), "<%lang%>" ) ) }' 
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   647
            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
   648
            sparql_query_url = source+'sparql'
98
2b738b88d483 workon on notifications: user home, user notifications page, triggers in signals
durandn
parents: 97
diff changeset
   649
            try:
2b738b88d483 workon on notifications: user home, user notifications page, triggers in signals
durandn
parents: 97
diff changeset
   650
                dbpedia_resp = requests.get(
2b738b88d483 workon on notifications: user home, user notifications page, triggers in signals
durandn
parents: 97
diff changeset
   651
                    sparql_query_url, 
2b738b88d483 workon on notifications: user home, user notifications page, triggers in signals
durandn
parents: 97
diff changeset
   652
                    params={
2b738b88d483 workon on notifications: user home, user notifications page, triggers in signals
durandn
parents: 97
diff changeset
   653
                            "query": sparql_query,
2b738b88d483 workon on notifications: user home, user notifications page, triggers in signals
durandn
parents: 97
diff changeset
   654
                            "format": "json"
2b738b88d483 workon on notifications: user home, user notifications page, triggers in signals
durandn
parents: 97
diff changeset
   655
                    }
2b738b88d483 workon on notifications: user home, user notifications page, triggers in signals
durandn
parents: 97
diff changeset
   656
                )
2b738b88d483 workon on notifications: user home, user notifications page, triggers in signals
durandn
parents: 97
diff changeset
   657
            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
   658
                # 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
   659
                pass
96
09b2da30cd93 try except block missing around a json.loads call
durandn
parents: 93
diff changeset
   660
            try:
09b2da30cd93 try except block missing around a json.loads call
durandn
parents: 93
diff changeset
   661
                results = json.loads(dbpedia_resp.text).get("results", {})
09b2da30cd93 try except block missing around a json.loads call
durandn
parents: 93
diff changeset
   662
            except:
09b2da30cd93 try except block missing around a json.loads call
durandn
parents: 93
diff changeset
   663
                # if error with json, results is empty
09b2da30cd93 try except block missing around a json.loads call
durandn
parents: 93
diff changeset
   664
                results = {}
09b2da30cd93 try except block missing around a json.loads call
durandn
parents: 93
diff changeset
   665
            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
   666
            label_data = {}
58
9f7e484baf73 Error handling in models + templates fixes
durandn
parents: 54
diff changeset
   667
            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
   668
                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
   669
            return label_data.get("l", {"value": False}).get("value")
48
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   670
        
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   671
        final_list = []
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   672
        for tagging_info in self.tagginginfo_set.select_related("tag").all():
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   673
            if tagging_info.tag.is_internal():
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   674
                final_list.append({
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   675
                    "tag_label": tagging_info.tag.label,
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   676
                    "tag_link": tagging_info.tag.link,
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   677
                    "accuracy": tagging_info.accuracy,
89
23679a6def77 fixed error on stats (comments count not updating correctly for images)
durandn
parents: 85
diff changeset
   678
                    "relevancy": tagging_info.relevancy,
23679a6def77 fixed error on stats (comments count not updating correctly for images)
durandn
parents: 85
diff changeset
   679
                    "is_internal": tagging_info.tag.is_internal()
48
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   680
                })
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   681
            else:
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   682
                tag_link = tagging_info.tag.link
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   683
                #import label from external
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   684
                externaL_repos_fetch_dict = {
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   685
                    "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
   686
                    "http://fr.dbpedia.org/": fetch_from_dbpedia
48
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   687
                }
53
ed9acfa5dd6e collection home page + tags in revision_detail view + optimization in views + fixture for demo
durandn
parents: 49
diff changeset
   688
                try:
54
147c8a8b66b6 streamlined templates for easy navigation and missing info + handled fr.dbpedia tags
durandn
parents: 53
diff changeset
   689
                    (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
   690
                    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
   691
                    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
   692
                        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
   693
                    else:
3c4150867fe7 Added fallback tag label storing in database for external tags in case external source is down (rudimentary)
durandn
parents: 101
diff changeset
   694
                        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
   695
                        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
   696
                    final_list.append({
ed9acfa5dd6e collection home page + tags in revision_detail view + optimization in views + fixture for demo
durandn
parents: 49
diff changeset
   697
                        "tag_label": tag_label,
ed9acfa5dd6e collection home page + tags in revision_detail view + optimization in views + fixture for demo
durandn
parents: 49
diff changeset
   698
                        "tag_link": tag_link,
ed9acfa5dd6e collection home page + tags in revision_detail view + optimization in views + fixture for demo
durandn
parents: 49
diff changeset
   699
                        "accuracy": tagging_info.accuracy,
89
23679a6def77 fixed error on stats (comments count not updating correctly for images)
durandn
parents: 85
diff changeset
   700
                        "relevancy": tagging_info.relevancy,
23679a6def77 fixed error on stats (comments count not updating correctly for images)
durandn
parents: 85
diff changeset
   701
                        "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
   702
                    })
ed9acfa5dd6e collection home page + tags in revision_detail view + optimization in views + fixture for demo
durandn
parents: 49
diff changeset
   703
                except StopIteration:
ed9acfa5dd6e collection home page + tags in revision_detail view + optimization in views + fixture for demo
durandn
parents: 49
diff changeset
   704
                    pass
48
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   705
        return json.dumps(final_list) 
101
eb856f9b518c fixed comment page calculation
durandn
parents: 100
diff changeset
   706
283
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   707
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   708
class Tag(models.Model):
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   709
    """
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   710
        Tag objects that are linked to revisions.
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   711
        
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   712
        Each tag is linked to a specific collection, this is important for internal tags
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   713
        so each collection can build its own vocabulary
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   714
    """
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   715
    label = models.CharField(max_length=255, blank=True, null=True)
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   716
    label_slug = models.SlugField(blank=True, null=True)
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   717
    link = models.URLField(unique=True)
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   718
    description = models.CharField(max_length=255, blank=True, null=True)
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   719
    collection = models.ForeignKey('Collection', blank=True, null=True)
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   720
    
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   721
    def is_internal(self):
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   722
        return self.link.startswith(settings.INTERNAL_TAGS_URL)
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   723
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   724
    def __str__(self):
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   725
        return self.label_slug+":"+self.label
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   726
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   727
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   728
class TaggingInfo(models.Model):
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   729
    """
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   730
        M2M object for managing tag relation to a revision with its associated relevancy and accuracy
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   731
    """
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   732
    revision = models.ForeignKey('AnnotationRevision', on_delete=models.CASCADE)
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   733
    tag = models.ForeignKey('Tag', on_delete=models.CASCADE)
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   734
    accuracy = models.IntegerField()
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   735
    relevancy = models.IntegerField()
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   736
    
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   737
    def __str__(self):
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   738
        return str(str(self.tag.label_slug)+":to:"+str(self.revision.revision_guid))
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   739
48
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   740
    
37
aed809b3a075 Corrected Tag methods bugs + Added comment classes and migration
durandn
parents: 35
diff changeset
   741
class IconolabComment(XtdComment):
283
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   742
    """
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   743
        Comment objects that extends XtdComment model, itself extending the django-contrib-comments model.
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   744
        
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   745
        Each comment can have 0 or 1 revision, if it is a comment created alongside a revision
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   746
        Each comment can have a set of metacategories
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   747
    """
48
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   748
    revision = models.ForeignKey('AnnotationRevision', related_name='creation_comment', null=True, blank=True)
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   749
    metacategories = models.ManyToManyField('MetaCategory', through='MetaCategoryInfo', through_fields=('comment', 'metacategory'))
68
55214839b541 Comments threading, pagination and date format
durandn
parents: 65
diff changeset
   750
    
55214839b541 Comments threading, pagination and date format
durandn
parents: 65
diff changeset
   751
    objects = XtdComment.objects
55214839b541 Comments threading, pagination and date format
durandn
parents: 65
diff changeset
   752
    
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
   753
    def __str__(self):
219
ce1e12435c44 cleaned up __str__ methods
durandn
parents: 216
diff changeset
   754
        return str(self.id)
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
   755
    
68
55214839b541 Comments threading, pagination and date format
durandn
parents: 65
diff changeset
   756
    class Meta:
55214839b541 Comments threading, pagination and date format
durandn
parents: 65
diff changeset
   757
        ordering = ["thread_id", "id"]
48
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   758
    
238
ad770589f0fe Work on admin interface and user pages + added stat metacategories count for annotations + refactored views module #41
durandn
parents: 231
diff changeset
   759
    @property
ad770589f0fe Work on admin interface and user pages + added stat metacategories count for annotations + refactored views module #41
durandn
parents: 231
diff changeset
   760
    def annotation(self):
ad770589f0fe Work on admin interface and user pages + added stat metacategories count for annotations + refactored views module #41
durandn
parents: 231
diff changeset
   761
        if self.content_type.app_label == "iconolab" and self.content_type.model == "annotation":
ad770589f0fe Work on admin interface and user pages + added stat metacategories count for annotations + refactored views module #41
durandn
parents: 231
diff changeset
   762
            return Annotation.objects.get(pk=self.object_pk)
ad770589f0fe Work on admin interface and user pages + added stat metacategories count for annotations + refactored views module #41
durandn
parents: 231
diff changeset
   763
        return None
ad770589f0fe Work on admin interface and user pages + added stat metacategories count for annotations + refactored views module #41
durandn
parents: 231
diff changeset
   764
            
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
   765
    def get_comment_page(self):
283
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   766
        """
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   767
            Shortcut function to get page for considered comment, with COMMENTS_PER_PAGE_DEFAULT comments per page, used for notifications links generation
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   768
        """
176
ef200c865025 Corrected error in comments model
durandn
parents: 151
diff changeset
   769
        return (IconolabComment.objects.for_app_models("iconolab.annotation").filter(
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
   770
            object_pk=self.object_pk,
101
eb856f9b518c fixed comment page calculation
durandn
parents: 100
diff changeset
   771
        ).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
   772
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
   773
    
37
aed809b3a075 Corrected Tag methods bugs + Added comment classes and migration
durandn
parents: 35
diff changeset
   774
class MetaCategory(models.Model):
283
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   775
    """
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   776
        Metacategories are objects that can be linked to a comment to augment it with meaning (depending on the metacategories defined for a given collection)
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   777
        
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   778
        Metacategories can trigger notifications when they are linked to a given coment depending on their trigger_notifications property:
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   779
        
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   780
            - NONE : Notifies nobody
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   781
            - CONTRIBUTORS : Notifies contributors (revision owners) on target annotation
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   782
            - COMMENTERS : Notifies commenters (contributors + comment owners) on target annotation
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   783
            - COLLECTION_ADMINS : Notifies collection admins
284
f52b0f6e2cd9 added fields on metacategories, annotations to track the validation state of the annotation, with example handler in signals that check and update the state when a relevant metacategory is posted.
durandn
parents: 283
diff changeset
   784
            
f52b0f6e2cd9 added fields on metacategories, annotations to track the validation state of the annotation, with example handler in signals that check and update the state when a relevant metacategory is posted.
durandn
parents: 283
diff changeset
   785
        Metacategories can be used to consider an annotation as "validated" if a certain agreement threshold is reached using their validation_value property
f52b0f6e2cd9 added fields on metacategories, annotations to track the validation state of the annotation, with example handler in signals that check and update the state when a relevant metacategory is posted.
durandn
parents: 283
diff changeset
   786
        
f52b0f6e2cd9 added fields on metacategories, annotations to track the validation state of the annotation, with example handler in signals that check and update the state when a relevant metacategory is posted.
durandn
parents: 283
diff changeset
   787
            - NEUTRAL : The metacategory doesn't affect the validation state
f52b0f6e2cd9 added fields on metacategories, annotations to track the validation state of the annotation, with example handler in signals that check and update the state when a relevant metacategory is posted.
durandn
parents: 283
diff changeset
   788
            - AGREEMENT : The metacategory can be used to validate the annotation when linked to a comment on said annotation
f52b0f6e2cd9 added fields on metacategories, annotations to track the validation state of the annotation, with example handler in signals that check and update the state when a relevant metacategory is posted.
durandn
parents: 283
diff changeset
   789
            - DISAGREEMENT : The metacategory can be used to unvalidate the annotation when linked to a comment on said annotation
f52b0f6e2cd9 added fields on metacategories, annotations to track the validation state of the annotation, with example handler in signals that check and update the state when a relevant metacategory is posted.
durandn
parents: 283
diff changeset
   790
   
283
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   791
    """
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   792
    NONE = 0
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   793
    CONTRIBUTORS = 1
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   794
    COMMENTERS = 2
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   795
    COLLECTION_ADMINS = 3
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
   796
    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
   797
        (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
   798
        (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
   799
        (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
   800
        (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
   801
    )
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
   802
    
284
f52b0f6e2cd9 added fields on metacategories, annotations to track the validation state of the annotation, with example handler in signals that check and update the state when a relevant metacategory is posted.
durandn
parents: 283
diff changeset
   803
    NEUTRAL = 0
f52b0f6e2cd9 added fields on metacategories, annotations to track the validation state of the annotation, with example handler in signals that check and update the state when a relevant metacategory is posted.
durandn
parents: 283
diff changeset
   804
    AGREEMENT = 1
f52b0f6e2cd9 added fields on metacategories, annotations to track the validation state of the annotation, with example handler in signals that check and update the state when a relevant metacategory is posted.
durandn
parents: 283
diff changeset
   805
    DISAGREEMENT = 2
f52b0f6e2cd9 added fields on metacategories, annotations to track the validation state of the annotation, with example handler in signals that check and update the state when a relevant metacategory is posted.
durandn
parents: 283
diff changeset
   806
    VALIDATION_VALUES = (
f52b0f6e2cd9 added fields on metacategories, annotations to track the validation state of the annotation, with example handler in signals that check and update the state when a relevant metacategory is posted.
durandn
parents: 283
diff changeset
   807
        (NEUTRAL, 'neutral'),
f52b0f6e2cd9 added fields on metacategories, annotations to track the validation state of the annotation, with example handler in signals that check and update the state when a relevant metacategory is posted.
durandn
parents: 283
diff changeset
   808
        (AGREEMENT, 'agreement'),
f52b0f6e2cd9 added fields on metacategories, annotations to track the validation state of the annotation, with example handler in signals that check and update the state when a relevant metacategory is posted.
durandn
parents: 283
diff changeset
   809
        (DISAGREEMENT, 'disagreement'),
f52b0f6e2cd9 added fields on metacategories, annotations to track the validation state of the annotation, with example handler in signals that check and update the state when a relevant metacategory is posted.
durandn
parents: 283
diff changeset
   810
    )
f52b0f6e2cd9 added fields on metacategories, annotations to track the validation state of the annotation, with example handler in signals that check and update the state when a relevant metacategory is posted.
durandn
parents: 283
diff changeset
   811
    
139
3e0a5286b257 collection home work (list and tabs) + adjusted image list template+ various design fixes
durandn
parents: 137
diff changeset
   812
    collection = models.ForeignKey(Collection, related_name="metacategories")
48
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   813
    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
   814
    triggers_notifications = models.IntegerField(choices=NOTIFIED_USERS, default=NONE)
284
f52b0f6e2cd9 added fields on metacategories, annotations to track the validation state of the annotation, with example handler in signals that check and update the state when a relevant metacategory is posted.
durandn
parents: 283
diff changeset
   815
    validation_value = models.IntegerField(choices=VALIDATION_VALUES, default=NEUTRAL)
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
   816
    
48
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   817
    def __str__(self):
285
aa0f3e186d29 fixed metacategory str for admin
durandn
parents: 284
diff changeset
   818
        return self.label+":"+self.collection.name
48
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   819
    
37
aed809b3a075 Corrected Tag methods bugs + Added comment classes and migration
durandn
parents: 35
diff changeset
   820
aed809b3a075 Corrected Tag methods bugs + Added comment classes and migration
durandn
parents: 35
diff changeset
   821
class MetaCategoryInfo(models.Model):
283
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   822
    """
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   823
        M2M class linking comments and metacategories
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   824
    """
48
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   825
    comment = models.ForeignKey('IconolabComment', on_delete=models.CASCADE)
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   826
    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
   827
    
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
   828
    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
   829
        return "metacategory:"+self.metacategory.label+":on:"+self.comment.id
6
37baf9d13f32 first commit
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   830
283
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   831
37
aed809b3a075 Corrected Tag methods bugs + Added comment classes and migration
durandn
parents: 35
diff changeset
   832
class CommentAttachement(models.Model):
283
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   833
    """
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   834
        This class is supposed to represent added resources to a given comment
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   835
        Not implemented as of v0.0.19
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   836
    """
48
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   837
    LINK = 0
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   838
    IMAGE = 1
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   839
    PDF = 2
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   840
    COMMENT_CHOICES = (
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   841
        (LINK, 'link'),
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   842
        (IMAGE, 'image'),
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   843
        (PDF, 'pdf')
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   844
    )
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   845
    
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   846
    comment = models.ForeignKey('IconolabComment', related_name='attachments', on_delete=models.CASCADE)
8125ce36415c start on dbpedia requests
durandn
parents: 47
diff changeset
   847
    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
   848
    data = models.TextField(blank=False)
283
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   849
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   850
  
132
4728f6c0102e Added user profile to identify collection admins and extend user model + notification on collection_admins metacategories
durandn
parents: 126
diff changeset
   851
class UserProfile(models.Model):
283
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   852
    """
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   853
        UserProfile objects are extensions of user model
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   854
        
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   855
        As of v0.0.19 they are used to define collection admins. Each user can thus managed 0-N collections.
d8fcfac848ed added/corrected method strings on models, views and signal handlers
durandn
parents: 263
diff changeset
   856
    """
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
   857
    user = models.OneToOneField(User, related_name='profile', on_delete=models.CASCADE)
242
430188380ba8 Added option to assign multiple managed collections on one account + small correction on stats template #41
durandn
parents: 238
diff changeset
   858
    managed_collections = models.ManyToManyField('Collection', related_name='admins', blank=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
   859
    
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
   860
    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
   861
        return "profile:"+self.user.username