src/iconolab/views/objects.py
author Alexandre Segura <mex.zktk@gmail.com>
Mon, 22 May 2017 11:39:26 +0200
changeset 515 c1077e8f595d
parent 514 accd1fded1a5
child 521 63a7f61554fe
permissions -rw-r--r--
Add search in tags only.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
298
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
     1
from django.shortcuts import HttpResponse, get_object_or_404, render, redirect
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
     2
from django.http import Http404
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
     3
from django.db.models import Count
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
     4
from django.contrib.auth.decorators import login_required
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
     5
from django.contrib.auth.models import User
461
4701ee4556e3 Display message when revision needs approval.
Alexandre Segura <mex.zktk@gmail.com>
parents: 416
diff changeset
     6
from django.contrib import messages
298
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
     7
from django.views.generic import View, DetailView, RedirectView, TemplateView
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
     8
from django.views.generic.base import ContextMixin
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
     9
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
    10
from django.core.urlresolvers import reverse
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
    11
from django.core.exceptions import ObjectDoesNotExist
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
    12
from django.contrib.contenttypes.models import ContentType
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
    13
from django.contrib.sites.models import Site
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
    14
from django.conf import settings
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
    15
from notifications.models import Notification
514
accd1fded1a5 Improve homepage.
Alexandre Segura <mex.zktk@gmail.com>
parents: 506
diff changeset
    16
from iconolab.models import Annotation, AnnotationRevision, Collection, Folder, Item, Image, IconolabComment, MetaCategory, MetaCategoryInfo, BookmarkCategory, Bookmark, Tag, TaggingInfo
298
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
    17
from iconolab.forms.annotations import AnnotationRevisionForm
506
4e18e1f69db9 Introduce bookmarks feature.
Alexandre Segura <mex.zktk@gmail.com>
parents: 491
diff changeset
    18
from iconolab.forms.bookmarks import BookmarkForm
337
db6c41f04e79 Move serializers to dedicated file, do not create comment on revision creation.
Alexandre Segura <mex.zktk@gmail.com>
parents: 323
diff changeset
    19
from iconolab.serializers import AnnotationRevisionSerializer
298
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
    20
import logging
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
    21
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
    22
logger = logging.getLogger(__name__)
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
    23
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
    24
class GlobalHomepageView(View):
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
    25
    """
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
    26
        View for the opening page of Iconolab.
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
    27
    """
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
    28
    def get(self, request, *args, **kwargs):
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
    29
        """
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
    30
            Template is iconolab/home.html
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
    31
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
    32
            Context variables provided to the template are:
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
    33
                collections_primary: list of collections to display as big images
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
    34
                collections_secondary: list of collections to display as small links at the bottom
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
    35
                homepage = True: used to pass checks in the partials/header.html
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
    36
                    template to adjust the navbar to the homepage
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
    37
        """
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
    38
        context = {}
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
    39
        context['collections_primary'] = Collection.objects.filter(show_image_on_home=True).all()
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
    40
        context['collections_secondary'] = Collection.objects.filter(show_image_on_home=False).all()
514
accd1fded1a5 Improve homepage.
Alexandre Segura <mex.zktk@gmail.com>
parents: 506
diff changeset
    41
        context['latest_annotations'] = Annotation.objects.order_by("-created").all()[:5]
accd1fded1a5 Improve homepage.
Alexandre Segura <mex.zktk@gmail.com>
parents: 506
diff changeset
    42
accd1fded1a5 Improve homepage.
Alexandre Segura <mex.zktk@gmail.com>
parents: 506
diff changeset
    43
        # Best contributors
accd1fded1a5 Improve homepage.
Alexandre Segura <mex.zktk@gmail.com>
parents: 506
diff changeset
    44
        count_contributions = Annotation.objects.all()\
accd1fded1a5 Improve homepage.
Alexandre Segura <mex.zktk@gmail.com>
parents: 506
diff changeset
    45
            .values('author').annotate(contributions=Count('author')).order_by('-contributions')[:10]
accd1fded1a5 Improve homepage.
Alexandre Segura <mex.zktk@gmail.com>
parents: 506
diff changeset
    46
        best_contributors = []
accd1fded1a5 Improve homepage.
Alexandre Segura <mex.zktk@gmail.com>
parents: 506
diff changeset
    47
        for count_contribution in count_contributions:
accd1fded1a5 Improve homepage.
Alexandre Segura <mex.zktk@gmail.com>
parents: 506
diff changeset
    48
            author = User.objects.get(id=count_contribution['author'])
accd1fded1a5 Improve homepage.
Alexandre Segura <mex.zktk@gmail.com>
parents: 506
diff changeset
    49
            best_contributors.append({
accd1fded1a5 Improve homepage.
Alexandre Segura <mex.zktk@gmail.com>
parents: 506
diff changeset
    50
                'author': author,
accd1fded1a5 Improve homepage.
Alexandre Segura <mex.zktk@gmail.com>
parents: 506
diff changeset
    51
                'contributions': count_contribution['contributions']
accd1fded1a5 Improve homepage.
Alexandre Segura <mex.zktk@gmail.com>
parents: 506
diff changeset
    52
            })
accd1fded1a5 Improve homepage.
Alexandre Segura <mex.zktk@gmail.com>
parents: 506
diff changeset
    53
        context['best_contributors'] = best_contributors
accd1fded1a5 Improve homepage.
Alexandre Segura <mex.zktk@gmail.com>
parents: 506
diff changeset
    54
accd1fded1a5 Improve homepage.
Alexandre Segura <mex.zktk@gmail.com>
parents: 506
diff changeset
    55
        # Most accurate tags (tags with accuracy >= 4)
515
c1077e8f595d Add search in tags only.
Alexandre Segura <mex.zktk@gmail.com>
parents: 514
diff changeset
    56
        # SELECT ti.tag_id, ar.title, COUNT(DISTINCT(a.id)) AS cnt
c1077e8f595d Add search in tags only.
Alexandre Segura <mex.zktk@gmail.com>
parents: 514
diff changeset
    57
        # FROM iconolab_tagginginfo ti
c1077e8f595d Add search in tags only.
Alexandre Segura <mex.zktk@gmail.com>
parents: 514
diff changeset
    58
        # JOIN iconolab_annotationrevision ar ON ti.revision_id = ar.id
c1077e8f595d Add search in tags only.
Alexandre Segura <mex.zktk@gmail.com>
parents: 514
diff changeset
    59
        # JOIN iconolab_annotation a ON ar.annotation_id = a.id
c1077e8f595d Add search in tags only.
Alexandre Segura <mex.zktk@gmail.com>
parents: 514
diff changeset
    60
        # WHERE ti.accuracy >= 4
c1077e8f595d Add search in tags only.
Alexandre Segura <mex.zktk@gmail.com>
parents: 514
diff changeset
    61
        # GROUP BY ti.tag_id
c1077e8f595d Add search in tags only.
Alexandre Segura <mex.zktk@gmail.com>
parents: 514
diff changeset
    62
        # ORDER BY cnt desc
514
accd1fded1a5 Improve homepage.
Alexandre Segura <mex.zktk@gmail.com>
parents: 506
diff changeset
    63
        rows = TaggingInfo.objects\
accd1fded1a5 Improve homepage.
Alexandre Segura <mex.zktk@gmail.com>
parents: 506
diff changeset
    64
            .prefetch_related('revision', 'revision__annotation')\
accd1fded1a5 Improve homepage.
Alexandre Segura <mex.zktk@gmail.com>
parents: 506
diff changeset
    65
            .filter(accuracy__gte=4)\
accd1fded1a5 Improve homepage.
Alexandre Segura <mex.zktk@gmail.com>
parents: 506
diff changeset
    66
            .values('tag')\
accd1fded1a5 Improve homepage.
Alexandre Segura <mex.zktk@gmail.com>
parents: 506
diff changeset
    67
            .annotate(annotation_count=Count('revision__annotation', distinct=True))\
accd1fded1a5 Improve homepage.
Alexandre Segura <mex.zktk@gmail.com>
parents: 506
diff changeset
    68
            .order_by('-annotation_count')\
accd1fded1a5 Improve homepage.
Alexandre Segura <mex.zktk@gmail.com>
parents: 506
diff changeset
    69
            .all()[:10]
accd1fded1a5 Improve homepage.
Alexandre Segura <mex.zktk@gmail.com>
parents: 506
diff changeset
    70
accd1fded1a5 Improve homepage.
Alexandre Segura <mex.zktk@gmail.com>
parents: 506
diff changeset
    71
        most_accurate_tags = []
accd1fded1a5 Improve homepage.
Alexandre Segura <mex.zktk@gmail.com>
parents: 506
diff changeset
    72
        for row in rows:
accd1fded1a5 Improve homepage.
Alexandre Segura <mex.zktk@gmail.com>
parents: 506
diff changeset
    73
            tag = Tag.objects.get(id=row['tag'])
accd1fded1a5 Improve homepage.
Alexandre Segura <mex.zktk@gmail.com>
parents: 506
diff changeset
    74
            most_accurate_tags.append({
accd1fded1a5 Improve homepage.
Alexandre Segura <mex.zktk@gmail.com>
parents: 506
diff changeset
    75
                'tag': tag,
accd1fded1a5 Improve homepage.
Alexandre Segura <mex.zktk@gmail.com>
parents: 506
diff changeset
    76
                'annotation_count': row['annotation_count']
accd1fded1a5 Improve homepage.
Alexandre Segura <mex.zktk@gmail.com>
parents: 506
diff changeset
    77
            })
accd1fded1a5 Improve homepage.
Alexandre Segura <mex.zktk@gmail.com>
parents: 506
diff changeset
    78
        context['most_accurate_tags'] = most_accurate_tags
accd1fded1a5 Improve homepage.
Alexandre Segura <mex.zktk@gmail.com>
parents: 506
diff changeset
    79
376
3d48f9520c8d adds contact email as a setting variable from the app (instead of raw in the template
duong tam kien <tk@deveha.com>
parents: 343
diff changeset
    80
        context['contact'] = settings.CONTACT_EMAIL
298
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
    81
        context['homepage'] = True
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
    82
        return render(request, 'iconolab/home.html', context)
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
    83
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
    84
class TestView(View):
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
    85
    template_name = 'iconolab/compare.html'
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
    86
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
    87
    def get(self, request, *args, **kwargs):
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
    88
        return render(request, self.template_name)
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
    89
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
    90
# Class with check_kwargs method to fetch objects from database depending on what level in the app we're currently at
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
    91
class IconolabObjectView(object):
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
    92
    """
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
    93
        Superclass that defines method used in all object display views.
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
    94
    """
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
    95
    def check_kwargs(self, kwargs):
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
    96
        '''
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
    97
            Returns a boolean depending on wether (True) or not (False) the objects
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
    98
            were found and a tuple containing the objects, with a select_related/prefetch_related
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
    99
            on relevant related objects following this ordering:
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   100
            (collection, item, image, annotation, revision)
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   101
        '''
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   102
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   103
        objects_tuple = ()
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   104
        if 'collection_name' in kwargs.keys():
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   105
            try:
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   106
                objects_tuple += (Collection.objects.prefetch_related('items', 'items__images').get(name=kwargs.get('collection_name')),)
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   107
            except (ValueError, Collection.DoesNotExist):
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   108
                return False, RedirectView.as_view(url=reverse('404error'))
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   109
        if 'item_guid' in kwargs.keys():
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   110
            try:
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   111
                objects_tuple += (Item.objects.prefetch_related('images', 'metadatas', 'images__stats').get(item_guid=kwargs.get('item_guid')),)
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   112
            except (ValueError, Item.DoesNotExist):
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   113
                return False, RedirectView.as_view(url=reverse('404error'))
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   114
        if 'image_guid' in kwargs.keys():
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   115
            try:
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   116
                objects_tuple += (Image.objects.prefetch_related('annotations', 'item', 'stats').get(image_guid=kwargs.get('image_guid')),)
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   117
            except (ValueError, Image.DoesNotExist):
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   118
                return False, RedirectView.as_view(url=reverse('404error'))
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   119
        if 'annotation_guid' in kwargs.keys():
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   120
            try:
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   121
                objects_tuple += (Annotation.objects.prefetch_related('current_revision', 'stats', 'image').get(annotation_guid=kwargs.get('annotation_guid')),)
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   122
            except (ValueError, Annotation.DoesNotExist):
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   123
                return False, RedirectView.as_view(url=reverse('404error'))
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   124
        if 'revision_guid' in kwargs.keys():
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   125
            try:
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   126
                objects_tuple += (AnnotationRevision.objects.prefetch_related('parent_revision').get(revision_guid=kwargs.get('revision_guid')),)
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   127
            except (ValueError, AnnotationRevision.DoesNotExist):
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   128
                return False, RedirectView.as_view(url=reverse('404error'))
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   129
        return True, objects_tuple
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   130
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   131
    def get_pagination_data(self, list_to_paginate, page, perpage, adjacent_pages_count, perpage_range=[5, 10, 25, 100], trailing_qarg=""):
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   132
        """
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   133
            Takes a queryset or a list and returns a dict with pagination data for display purposes
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   134
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   135
            Dict will be of the format:
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   136
            {
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   137
                page: the page to load (integer)
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   138
                perpage_range: a list of the page links to display (list of integers)
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   139
                perpage: the item count per page (integer)
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   140
                perpage_range: a list of the perpage values to display next to the page list (list of integers)
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   141
                trailing_qarg: optional trailing qarg for the paginations links (used in collection home to remember the state of each list between page loads) (string)
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   142
                list: the item list to display (list of objects)
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   143
                show_first: used in template to display links, will be True if 1 is not in page_range
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   144
                show_last: used in template to display links, will be True if page_count is not in page_range
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   145
                ellipsis_first: used in template to display links, will be True if page_range starts at 3 or more
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   146
                ellipsis_last: used in template to display links, will be True if page_range ends at last_page - 2 or less
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   147
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   148
            }
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   149
        """
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   150
        pagination_data = {}
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   151
        pagination_data["page"] = page
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   152
        pagination_data["perpage"] = perpage
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   153
        pagination_data["perpage_range"] = perpage_range
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   154
        pagination_data["trailing_qarg"] = trailing_qarg
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   155
        paginator = Paginator(list_to_paginate, perpage)
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   156
        try:
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   157
            pagination_data["list"] = paginator.page(page)
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   158
        except PageNotAnInteger:
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   159
            pagination_data["list"] = paginator.page(1)
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   160
        except EmptyPage:
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   161
            pagination_data["list"] = paginator.page(paginator.num_pages)
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   162
        pagination_data["page_range"] = [
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   163
            n for n in \
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   164
            range(page - adjacent_pages_count, page + adjacent_pages_count + 1) \
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   165
                if n > 0 and n <= paginator.num_pages
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   166
        ]
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   167
        pagination_data["show_first"] = page - adjacent_pages_count > 1
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   168
        pagination_data["ellipsis_first"] = pagination_data["show_first"] and (page - adjacent_pages_count != 2)
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   169
        pagination_data["show_last"] = page + adjacent_pages_count < paginator.num_pages
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   170
        pagination_data["ellipsis_last"] = pagination_data["show_last"] and (page + adjacent_pages_count != paginator.num_pages - 1)
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   171
        return pagination_data
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   172
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   173
class CollectionHomepageView(View, ContextMixin, IconolabObjectView):
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   174
    """
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   175
        View that displays a collection and four panels to show relevant paginated lists for collection:
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   176
        * item lists
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   177
        * annotations ordered by creation date
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   178
        * annotations ordered by revisions count
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   179
        * annotations where a metacategory that notifies contributors was called
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   180
    """
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   181
    def get(self, request, *args, **kwargs):
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   182
        """
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   183
            Template is iconolab/collection_home.html
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   184
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   185
            Url args are:
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   186
                - collection_name: 'name' attribute of the requested collection
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   187
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   188
            Queryargs understood by the view are:
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   189
                - show : panel that will be shown on page load, one of ['items', 'recent', 'revised', 'contributions'], default to "items"
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   190
                - items_page : item list page to load
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   191
                - items_perpage : item count per page
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   192
                - recent_page : recent annotations list page to load
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   193
                - recent_perpage : recent annotations count per page
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   194
                - revised_page : most revised annotations list page to load
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   195
                - revised_perpage : most revised annotations count per page
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   196
                - contributions_page : annotations with the most contribution calls list page to load
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   197
                - contributions_perpage : annotations with the most contribution calls count per page for item list
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   198
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   199
            Context variables provided to the template are:
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   200
                - collection: the collection object for the requested collection
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   201
                - collection_name : the collection_name url arg
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   202
                - items_pagination_data: pagination data dict in the format of the IconolabObjectView.get_pagination_data() method for the items list
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   203
                - recent_pagination_data: pagination data dict in the format of the IconolabObjectView.get_pagination_data() method for the recent annotations list
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   204
                - revised_pagination_data: pagination data dict in the format of the IconolabObjectView.get_pagination_data() method for the revised annotations list
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   205
                - contributions_pagination_data: pagination data dict in the format of the IconolabObjectView.get_pagination_data() method for the contribution calls annotations list
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   206
        """
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   207
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   208
        success, result = self.check_kwargs(kwargs)
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   209
        if success:
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   210
            (collection,) = result
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   211
        else:
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   212
            return result(request)
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   213
        context = super(CollectionHomepageView, self).get_context_data(**kwargs)
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   214
        context['collection_name'] = self.kwargs.get('collection_name', '')
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   215
        context['collection'] = collection
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   216
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   217
        # get Pagination and navigation query args
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   218
        try:
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   219
            items_page = int(request.GET.get('items_page', '1'))
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   220
        except ValueError:
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   221
            items_page = 1
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   222
        try:
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   223
            items_per_page = int(request.GET.get('items_perpage', '12'))
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   224
        except ValueError:
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   225
            items_per_page = 12
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   226
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   227
        try:
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   228
            recent_page = int(request.GET.get('recent_page', '1'))
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   229
        except ValueError:
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   230
            recent_page = 1
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   231
        try:
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   232
            recent_per_page = int(request.GET.get('recent_perpage', '10'))
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   233
        except ValueError:
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   234
            recent_per_page = 10
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   235
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   236
        try:
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   237
            revised_page = int(request.GET.get('revised_page', '1'))
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   238
        except ValueError:
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   239
            revised_page = 1
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   240
        try:
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   241
            revised_per_page = int(request.GET.get('revised_perpage', '10'))
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   242
        except ValueError:
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   243
            revised_per_page = 10
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   244
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   245
        try:
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   246
            contributions_page = int(request.GET.get('contributions_page', '1'))
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   247
        except ValueError:
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   248
            contributions_page = 1
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   249
        try:
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   250
            contributions_per_page = int(request.GET.get('contributions_perpage', '10'))
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   251
        except ValueError:
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   252
            contributions_per_page = 10
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   253
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   254
        active_list = request.GET.get('show', 'items')
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   255
        if active_list not in ['items', 'recent', 'revised', 'contributions']:
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   256
            active_list = 'items'
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   257
        context["active_list"] = active_list
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   258
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   259
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   260
        # Pagination values
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   261
        adjacent_pages_count = 2
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   262
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   263
        # Paginated objects list
416
5daa15b87404 Introduce folders.
Alexandre Segura <mex.zktk@gmail.com>
parents: 407
diff changeset
   264
        items_list = collection.items.order_by("metadatas__inventory_number")
5daa15b87404 Introduce folders.
Alexandre Segura <mex.zktk@gmail.com>
parents: 407
diff changeset
   265
5daa15b87404 Introduce folders.
Alexandre Segura <mex.zktk@gmail.com>
parents: 407
diff changeset
   266
        folder = request.GET.get('folder', None)
5daa15b87404 Introduce folders.
Alexandre Segura <mex.zktk@gmail.com>
parents: 407
diff changeset
   267
5daa15b87404 Introduce folders.
Alexandre Segura <mex.zktk@gmail.com>
parents: 407
diff changeset
   268
        if folder is not None:
467
5d0879ffa7de Use folder GUID instead of primary key.
Alexandre Segura <mex.zktk@gmail.com>
parents: 465
diff changeset
   269
            items_list = items_list.filter(folders__folder_guid=folder)
486
869bb212631a Add link to show selected folder below collection title.
Alexandre Segura <mex.zktk@gmail.com>
parents: 482
diff changeset
   270
            context['folder_name'] = Folder.objects.get(folder_guid=folder).name
416
5daa15b87404 Introduce folders.
Alexandre Segura <mex.zktk@gmail.com>
parents: 407
diff changeset
   271
467
5d0879ffa7de Use folder GUID instead of primary key.
Alexandre Segura <mex.zktk@gmail.com>
parents: 465
diff changeset
   272
        context['folder_guid'] = folder
416
5daa15b87404 Introduce folders.
Alexandre Segura <mex.zktk@gmail.com>
parents: 407
diff changeset
   273
491
2b6699e96ab6 Fix pagination.
Alexandre Segura <mex.zktk@gmail.com>
parents: 486
diff changeset
   274
        items_pagination_args = '&'.join([
2b6699e96ab6 Fix pagination.
Alexandre Segura <mex.zktk@gmail.com>
parents: 486
diff changeset
   275
            "recent_page="+str(recent_page),
2b6699e96ab6 Fix pagination.
Alexandre Segura <mex.zktk@gmail.com>
parents: 486
diff changeset
   276
            "recent_perpage="+str(recent_per_page),
2b6699e96ab6 Fix pagination.
Alexandre Segura <mex.zktk@gmail.com>
parents: 486
diff changeset
   277
            "revised_page="+str(revised_page),
2b6699e96ab6 Fix pagination.
Alexandre Segura <mex.zktk@gmail.com>
parents: 486
diff changeset
   278
            "revised_perpage="+str(revised_per_page),
2b6699e96ab6 Fix pagination.
Alexandre Segura <mex.zktk@gmail.com>
parents: 486
diff changeset
   279
            "contributions_page="+str(contributions_page),
2b6699e96ab6 Fix pagination.
Alexandre Segura <mex.zktk@gmail.com>
parents: 486
diff changeset
   280
            "contributions_perpage="+str(contributions_per_page)
2b6699e96ab6 Fix pagination.
Alexandre Segura <mex.zktk@gmail.com>
parents: 486
diff changeset
   281
        ])
2b6699e96ab6 Fix pagination.
Alexandre Segura <mex.zktk@gmail.com>
parents: 486
diff changeset
   282
2b6699e96ab6 Fix pagination.
Alexandre Segura <mex.zktk@gmail.com>
parents: 486
diff changeset
   283
        if folder is not None:
2b6699e96ab6 Fix pagination.
Alexandre Segura <mex.zktk@gmail.com>
parents: 486
diff changeset
   284
            items_pagination_args += "&folder=" + folder
2b6699e96ab6 Fix pagination.
Alexandre Segura <mex.zktk@gmail.com>
parents: 486
diff changeset
   285
298
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   286
        context["items_pagination_data"] = self.get_pagination_data(
416
5daa15b87404 Introduce folders.
Alexandre Segura <mex.zktk@gmail.com>
parents: 407
diff changeset
   287
            items_list.all(),
298
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   288
            items_page,
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   289
            items_per_page,
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   290
            adjacent_pages_count,
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   291
            perpage_range=[6, 12, 48, 192],
491
2b6699e96ab6 Fix pagination.
Alexandre Segura <mex.zktk@gmail.com>
parents: 486
diff changeset
   292
            trailing_qarg=items_pagination_args
298
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   293
        )
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   294
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   295
        # Paginated recent annotations list
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   296
        recent_annotations = Annotation.objects.filter(image__item__collection__name=collection.name).prefetch_related(
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   297
            'current_revision',
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   298
            'stats'
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   299
        ).order_by('-current_revision__created')
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   300
        context["recent_pagination_data"] = self.get_pagination_data(
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   301
            recent_annotations,
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   302
            recent_page,
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   303
            recent_per_page,
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   304
            adjacent_pages_count,
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   305
            trailing_qarg="&items_page="+str(items_page)
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   306
            +"&items_perpage="+str(items_per_page)
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   307
            +"&revised_page="+str(revised_page)
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   308
            +"&revised_perpage="+str(revised_per_page)
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   309
            +"&contributions_page="+str(contributions_page)
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   310
            +"&contributions_perpage="+str(contributions_per_page)
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   311
        )
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   312
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   313
        # Paginated revised annotations list
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   314
        revised_annotations = Annotation.objects.filter(image__item__collection__name=collection.name).prefetch_related(
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   315
            'current_revision',
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   316
            'stats'
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   317
        ).annotate(revision_count=Count('revisions')).order_by('-revision_count')
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   318
        context["revised_pagination_data"] = self.get_pagination_data(
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   319
            revised_annotations,
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   320
            revised_page,
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   321
            revised_per_page,
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   322
            adjacent_pages_count,
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   323
            trailing_qarg="&items_page="+str(items_page)
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   324
            +"&items_perpage="+str(items_per_page)
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   325
            +"&recent_page="+str(recent_page)
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   326
            +"&recent_perpage="+str(recent_per_page)
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   327
            +"&contributions_page="+str(contributions_page)
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   328
            +"&contributions_perpage="+str(contributions_per_page)
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   329
        )
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   330
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   331
        # Paginated contribution calls annotation list
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   332
        contrib_calls_annotations_ids = list(set(MetaCategoryInfo.objects.filter(
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   333
            metacategory__collection__name=collection.name,
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   334
            metacategory__triggers_notifications=MetaCategory.CONTRIBUTORS
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   335
        ).order_by('comment__submit_date').values_list('comment__object_pk', flat=True)))
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   336
        collection_annotations = Annotation.objects.filter(id__in=contrib_calls_annotations_ids).all()
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   337
        collection_ann_dict = dict([(str(annotation.id), annotation) for annotation in collection_annotations])
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   338
        contributions_annotations = [collection_ann_dict[id] for id in contrib_calls_annotations_ids]
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   339
        context["contributions_pagination_data"] = self.get_pagination_data(
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   340
            contributions_annotations,
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   341
            contributions_page,
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   342
            contributions_per_page,
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   343
            adjacent_pages_count,
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   344
            trailing_qarg="&items_page="+str(items_page)
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   345
            +"&items_perpage="+str(items_per_page)
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   346
            +"&recent_page="+str(recent_page)
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   347
            +"&recent_perpage="+str(recent_per_page)
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   348
            +"&revised_page="+str(revised_page)
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   349
            +"&revised_perpage="+str(revised_per_page)
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   350
        )
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   351
506
4e18e1f69db9 Introduce bookmarks feature.
Alexandre Segura <mex.zktk@gmail.com>
parents: 491
diff changeset
   352
        context['bookmark_form'] = BookmarkForm(user=request.user)
4e18e1f69db9 Introduce bookmarks feature.
Alexandre Segura <mex.zktk@gmail.com>
parents: 491
diff changeset
   353
298
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   354
        return render(request, 'iconolab/collection_home.html', context)
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   355
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   356
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   357
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   358
class ShowItemView(View, ContextMixin, IconolabObjectView):
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   359
    """
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   360
        View that displays informations on an item with associated metadatas and stats. Also displays images and annotation list for each image.
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   361
    """
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   362
    def get(self, request, *args, **kwargs):
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   363
        """
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   364
            Template is iconolab/item_detail.html
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   365
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   366
            Url args are:
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   367
                - collection_name : name of the collection
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   368
                - item_guid: 'item_guid' attribute of the requested item
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   369
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   370
            Queryargs understood by the view are:
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   371
                - show: image_guid for the image to show on load
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   372
                - page: annotation list page on load for displayed image
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   373
                - perpage: annotation count per page on load for displayed image
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   374
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   375
            Context variables provided to the template are:
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   376
                - collection_name : the collection_name url arg
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   377
                - item_guid: the item_guid url arg
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   378
                - collection: the collection object for the requested collection
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   379
                - item: the item object for the requested item
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   380
                - display_image: the image_guid for the image to display on load
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   381
                - images: a list of dict for the item images data in the format:
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   382
                    {
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   383
                        'obj': the image object,
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   384
                        'annotations': the list of annotations on that image
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   385
                    }
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   386
        """
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   387
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   388
        success, result = self.check_kwargs(kwargs)
323
55c024fc7c60 Roughly implement annotation navigator.
Alexandre Segura <mex.zktk@gmail.com>
parents: 298
diff changeset
   389
298
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   390
        if success:
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   391
            (collection, item) = result
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   392
        else:
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   393
            return result(request)
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   394
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   395
        context = super(ShowItemView, self).get_context_data(**kwargs)
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   396
        image_guid_to_display = request.GET.get("show", str(item.images.first().image_guid))
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   397
        if image_guid_to_display not in [str(guid) for guid in item.images.all().values_list("image_guid", flat=True)]:
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   398
            image_guid_to_display = str(item.images.first().image_guid)
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   399
        context['display_image'] = image_guid_to_display
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   400
        try:
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   401
            displayed_annotations_page = int(request.GET.get('page', '1'))
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   402
        except ValueError:
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   403
            displayed_annotations_page = 1
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   404
        try:
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   405
            displayed_annotations_per_page = int(request.GET.get('perpage', '10'))
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   406
        except ValueError:
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   407
            displayed_annotations_per_page = 10
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   408
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   409
        context['collection_name'] = self.kwargs.get('collection_name', '')
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   410
        context['item_guid'] = self.kwargs.get('image_guid', '')
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   411
        context['collection'] = collection
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   412
        context['item'] = item
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   413
        context['images'] = []
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   414
        for image in item.images.all():
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   415
            if str(image.image_guid) == image_guid_to_display:
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   416
                page = displayed_annotations_page
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   417
                per_page = displayed_annotations_per_page
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   418
            else:
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   419
                page = 1
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   420
                per_page = 10
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   421
            annotations_paginator = Paginator(image.annotations.all(), per_page)
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   422
            try:
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   423
                annotations = annotations_paginator.page(page)
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   424
            except PageNotAnInteger:
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   425
                annotations = annotations_paginator.page(1)
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   426
            except EmptyPage:
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   427
                annotations = annotations_paginator.page(recent_paginator.num_pages)
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   428
            context['images'].append({
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   429
                'obj' : image,
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   430
                'annotations': annotations
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   431
            })
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   432
            image.stats.views_count += 1
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   433
            image.stats.save()
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   434
        return render(request, 'iconolab/detail_item.html', context);
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   435
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   436
class ShowImageView(View, ContextMixin, IconolabObjectView):
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   437
    """
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   438
        View that only displays an image and the associated annotations
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   439
    """
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   440
    def get(self, request, *args, **kwargs):
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   441
        success, result = self.check_kwargs(kwargs)
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   442
        if success:
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   443
            (collection, image) = result
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   444
        else:
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   445
            return result(request)
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   446
        context = super(ShowImageView, self).get_context_data(**kwargs)
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   447
        context['collection_name'] = self.kwargs.get('collection_name', '')
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   448
        context['image_guid'] = self.kwargs.get('image_guid', '')
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   449
        context['collection'] = collection
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   450
        context['image'] = image
407
74e0a5ea614a Display item metadata below image.
Alexandre Segura <mex.zktk@gmail.com>
parents: 376
diff changeset
   451
        context['item'] = image.item
323
55c024fc7c60 Roughly implement annotation navigator.
Alexandre Segura <mex.zktk@gmail.com>
parents: 298
diff changeset
   452
        context['form'] = AnnotationRevisionForm()
298
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   453
        return render(request, 'iconolab/detail_image.html', context)
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   454
506
4e18e1f69db9 Introduce bookmarks feature.
Alexandre Segura <mex.zktk@gmail.com>
parents: 491
diff changeset
   455
class BookmarkImageView(View, ContextMixin, IconolabObjectView):
4e18e1f69db9 Introduce bookmarks feature.
Alexandre Segura <mex.zktk@gmail.com>
parents: 491
diff changeset
   456
    def post(self, request, *args, **kwargs):
4e18e1f69db9 Introduce bookmarks feature.
Alexandre Segura <mex.zktk@gmail.com>
parents: 491
diff changeset
   457
        success, result = self.check_kwargs(kwargs)
4e18e1f69db9 Introduce bookmarks feature.
Alexandre Segura <mex.zktk@gmail.com>
parents: 491
diff changeset
   458
        if success:
4e18e1f69db9 Introduce bookmarks feature.
Alexandre Segura <mex.zktk@gmail.com>
parents: 491
diff changeset
   459
            (collection, image) = result
4e18e1f69db9 Introduce bookmarks feature.
Alexandre Segura <mex.zktk@gmail.com>
parents: 491
diff changeset
   460
        else:
4e18e1f69db9 Introduce bookmarks feature.
Alexandre Segura <mex.zktk@gmail.com>
parents: 491
diff changeset
   461
            return result(request)
4e18e1f69db9 Introduce bookmarks feature.
Alexandre Segura <mex.zktk@gmail.com>
parents: 491
diff changeset
   462
4e18e1f69db9 Introduce bookmarks feature.
Alexandre Segura <mex.zktk@gmail.com>
parents: 491
diff changeset
   463
        context = super(BookmarkImageView, self).get_context_data(**kwargs)
4e18e1f69db9 Introduce bookmarks feature.
Alexandre Segura <mex.zktk@gmail.com>
parents: 491
diff changeset
   464
4e18e1f69db9 Introduce bookmarks feature.
Alexandre Segura <mex.zktk@gmail.com>
parents: 491
diff changeset
   465
        bookmark_form = BookmarkForm(request.POST, user=request.user)
4e18e1f69db9 Introduce bookmarks feature.
Alexandre Segura <mex.zktk@gmail.com>
parents: 491
diff changeset
   466
        if bookmark_form.is_valid():
4e18e1f69db9 Introduce bookmarks feature.
Alexandre Segura <mex.zktk@gmail.com>
parents: 491
diff changeset
   467
            bookmark = Bookmark(
4e18e1f69db9 Introduce bookmarks feature.
Alexandre Segura <mex.zktk@gmail.com>
parents: 491
diff changeset
   468
                image=image,
4e18e1f69db9 Introduce bookmarks feature.
Alexandre Segura <mex.zktk@gmail.com>
parents: 491
diff changeset
   469
                category=bookmark_form.cleaned_data['category']
4e18e1f69db9 Introduce bookmarks feature.
Alexandre Segura <mex.zktk@gmail.com>
parents: 491
diff changeset
   470
            )
4e18e1f69db9 Introduce bookmarks feature.
Alexandre Segura <mex.zktk@gmail.com>
parents: 491
diff changeset
   471
            bookmark.save()
4e18e1f69db9 Introduce bookmarks feature.
Alexandre Segura <mex.zktk@gmail.com>
parents: 491
diff changeset
   472
4e18e1f69db9 Introduce bookmarks feature.
Alexandre Segura <mex.zktk@gmail.com>
parents: 491
diff changeset
   473
        context['bookmark_form'] = bookmark_form
4e18e1f69db9 Introduce bookmarks feature.
Alexandre Segura <mex.zktk@gmail.com>
parents: 491
diff changeset
   474
4e18e1f69db9 Introduce bookmarks feature.
Alexandre Segura <mex.zktk@gmail.com>
parents: 491
diff changeset
   475
        redirect_url = reverse('collection_home', kwargs={'collection_name': self.kwargs.get('collection_name', '')})
4e18e1f69db9 Introduce bookmarks feature.
Alexandre Segura <mex.zktk@gmail.com>
parents: 491
diff changeset
   476
4e18e1f69db9 Introduce bookmarks feature.
Alexandre Segura <mex.zktk@gmail.com>
parents: 491
diff changeset
   477
        return redirect(redirect_url)
4e18e1f69db9 Introduce bookmarks feature.
Alexandre Segura <mex.zktk@gmail.com>
parents: 491
diff changeset
   478
298
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   479
class CreateAnnotationView(View, ContextMixin, IconolabObjectView):
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   480
    """
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   481
        View that displays annotation forms and handles annotation creation
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   482
    """
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   483
    def get_context_data(self, **kwargs):
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   484
        context = super(CreateAnnotationView, self).get_context_data(**kwargs)
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   485
        context['collection_name'] = self.kwargs.get('collection_name', '')
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   486
        context['image_guid'] = self.kwargs.get('image_guid', '')
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   487
        return context
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   488
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   489
    def get(self, request, *args, **kwargs):
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   490
        success, result = self.check_kwargs(kwargs)
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   491
        if success:
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   492
            (collection, image,) = result
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   493
        else:
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   494
            return result(request)
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   495
        annotation_form = AnnotationRevisionForm()
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   496
        context = self.get_context_data(**kwargs)
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   497
        context['image'] = image
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   498
        context['form'] = annotation_form
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   499
        context['tags_data'] = '[]'
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   500
        return render(request, 'iconolab/change_annotation.html', context)
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   501
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   502
    def post(self, request, *args, **kwargs):
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   503
        success, result = self.check_kwargs(kwargs)
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   504
        if success:
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   505
            (collection, image) = result
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   506
        else:
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   507
            return result(request)
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   508
        collection_name = kwargs['collection_name']
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   509
        image_guid = kwargs['image_guid']
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   510
        annotation_form = AnnotationRevisionForm(request.POST)
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   511
        if annotation_form.is_valid():
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   512
            author = request.user
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   513
            title = annotation_form.cleaned_data['title']
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   514
            description = annotation_form.cleaned_data['description']
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   515
            fragment = annotation_form.cleaned_data['fragment']
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   516
            tags_json = annotation_form.cleaned_data['tags']
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   517
            new_annotation = Annotation.objects.create_annotation(author, image, title=title, description=description, fragment=fragment, tags_json=tags_json)
323
55c024fc7c60 Roughly implement annotation navigator.
Alexandre Segura <mex.zktk@gmail.com>
parents: 298
diff changeset
   518
            redirect_url = reverse('image_detail', kwargs={'collection_name': collection_name, 'image_guid': image_guid})
343
6f901f3b1510 Redirect to annotation using hash.
Alexandre Segura <mex.zktk@gmail.com>
parents: 337
diff changeset
   519
            return redirect(redirect_url + '#' + str(new_annotation.annotation_guid))
298
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   520
        context = self.get_context_data(**kwargs)
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   521
        context['image'] = image
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   522
        context['form'] = annotation_form
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   523
        context['tags_data'] = '[]'
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   524
        return render(request, 'iconolab/change_annotation.html', context)
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   525
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   526
class ShowAnnotationView(View, ContextMixin, IconolabObjectView):
470
6c43539e5c67 Use browser history instead of hash for annotation URL.
Alexandre Segura <mex.zktk@gmail.com>
parents: 467
diff changeset
   527
    def get(self, request, *args, **kwargs):
6c43539e5c67 Use browser history instead of hash for annotation URL.
Alexandre Segura <mex.zktk@gmail.com>
parents: 467
diff changeset
   528
        success, result = self.check_kwargs(kwargs)
6c43539e5c67 Use browser history instead of hash for annotation URL.
Alexandre Segura <mex.zktk@gmail.com>
parents: 467
diff changeset
   529
        if success:
6c43539e5c67 Use browser history instead of hash for annotation URL.
Alexandre Segura <mex.zktk@gmail.com>
parents: 467
diff changeset
   530
            (collection, image, annotation) = result
6c43539e5c67 Use browser history instead of hash for annotation URL.
Alexandre Segura <mex.zktk@gmail.com>
parents: 467
diff changeset
   531
        else:
6c43539e5c67 Use browser history instead of hash for annotation URL.
Alexandre Segura <mex.zktk@gmail.com>
parents: 467
diff changeset
   532
            return result(request)
6c43539e5c67 Use browser history instead of hash for annotation URL.
Alexandre Segura <mex.zktk@gmail.com>
parents: 467
diff changeset
   533
        context = super(ShowAnnotationView, self).get_context_data(**kwargs)
6c43539e5c67 Use browser history instead of hash for annotation URL.
Alexandre Segura <mex.zktk@gmail.com>
parents: 467
diff changeset
   534
        context['collection_name'] = self.kwargs.get('collection_name', '')
6c43539e5c67 Use browser history instead of hash for annotation URL.
Alexandre Segura <mex.zktk@gmail.com>
parents: 467
diff changeset
   535
        context['image_guid'] = self.kwargs.get('image_guid', '')
6c43539e5c67 Use browser history instead of hash for annotation URL.
Alexandre Segura <mex.zktk@gmail.com>
parents: 467
diff changeset
   536
        context['collection'] = collection
6c43539e5c67 Use browser history instead of hash for annotation URL.
Alexandre Segura <mex.zktk@gmail.com>
parents: 467
diff changeset
   537
        context['image'] = image
6c43539e5c67 Use browser history instead of hash for annotation URL.
Alexandre Segura <mex.zktk@gmail.com>
parents: 467
diff changeset
   538
        context['item'] = image.item
6c43539e5c67 Use browser history instead of hash for annotation URL.
Alexandre Segura <mex.zktk@gmail.com>
parents: 467
diff changeset
   539
        context['annotation'] = annotation
6c43539e5c67 Use browser history instead of hash for annotation URL.
Alexandre Segura <mex.zktk@gmail.com>
parents: 467
diff changeset
   540
        context['form'] = AnnotationRevisionForm()
6c43539e5c67 Use browser history instead of hash for annotation URL.
Alexandre Segura <mex.zktk@gmail.com>
parents: 467
diff changeset
   541
        return render(request, 'iconolab/detail_image.html', context)
6c43539e5c67 Use browser history instead of hash for annotation URL.
Alexandre Segura <mex.zktk@gmail.com>
parents: 467
diff changeset
   542
6c43539e5c67 Use browser history instead of hash for annotation URL.
Alexandre Segura <mex.zktk@gmail.com>
parents: 467
diff changeset
   543
class ShowAnnotationViewOld(View, ContextMixin, IconolabObjectView):
298
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   544
    """
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   545
        View that show a given annotation with the corresponding data, links to
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   546
        submit new revisions and the paginated comments thread.
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   547
    """
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   548
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   549
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   550
    def get_context_data(self, **kwargs):
479
71cad12c0862 Fix class name.
Alexandre Segura <mex.zktk@gmail.com>
parents: 470
diff changeset
   551
        context = super(ShowAnnotationViewOld, self).get_context_data(**kwargs)
298
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   552
        context['collection_name'] = self.kwargs.get('collection_name', '')
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   553
        context['image_guid'] = self.kwargs.get('image_guid', '')
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   554
        context['annotation_guid'] = self.kwargs.get('annotation_guid', '')
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   555
        return context
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   556
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   557
    def get(self, request, *args, **kwargs):
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   558
        """
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   559
            Template is iconolab/detail_annotations.html
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   560
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   561
            Url args are:
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   562
                - collection_name: 'name' attribute of the requested collection
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   563
                - item_guid: 'item_guid' attribute of the requested item
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   564
                - annotation_guid: 'annotation_guid' attribute of the requested annotation
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   565
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   566
            Queryargs understood by the view are:
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   567
                - page: comment thread page on load
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   568
                - perpage: comment count per page on load
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   569
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   570
            Context variables provided to the template are:
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   571
                - collection: the collection object for the requested collection
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   572
                - image: the image object for the requested image
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   573
                - annotation: the annotation object for the requested annotation
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   574
                - tags_data: a json string describing tags for the annotation current revision
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   575
                - comments: the paginated comments list for the annotation according page and perpage queryargs
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   576
                - notification_comments_ids: the ids of the comments that are referenced by a notification for the authenticated user; This allows
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   577
                us to highlight comments that triggered a notification in the page
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   578
        """
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   579
        success, result = self.check_kwargs(kwargs)
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   580
        if success:
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   581
            (collection, image, annotation,) = result
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   582
        else:
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   583
            return result(request)
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   584
        context = self.get_context_data(**kwargs)
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   585
        context['collection'] = collection
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   586
        context['image'] = image
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   587
        context['annotation'] = annotation
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   588
        context['tags_data'] = annotation.current_revision.get_tags_json()
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   589
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   590
        page = request.GET.get('page', 1)
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   591
        per_page = request.GET.get('perpage', 10)
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   592
        full_comments_list = IconolabComment.objects.for_app_models('iconolab.annotation').filter(object_pk = annotation.pk).order_by('thread_id', '-order')
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   593
        paginator = Paginator(full_comments_list, per_page)
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   594
        try:
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   595
            comments_list = paginator.page(page)
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   596
        except PageNotAnInteger:
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   597
            comments_list = paginator.page(1)
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   598
        except EmptyPage:
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   599
            comments_list = paginator.page(paginator.num_pages)
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   600
        context['comments'] = comments_list
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   601
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   602
        if request.user.is_authenticated():
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   603
            user_comment_notifications = Notification.objects.filter(
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   604
                recipient=request.user,
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   605
                action_object_content_type__app_label='iconolab',
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   606
                action_object_content_type__model='iconolabcomment',
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   607
                target_content_type__app_label='iconolab',
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   608
                target_content_type__model='annotation',
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   609
                target_object_id=annotation.id
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   610
            ).unread()
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   611
            context['notifications_comments_ids'] = [int(val) for val in user_comment_notifications.values_list('action_object_object_id', flat=True)]
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   612
            comment_list_ids = [comment.id for comment in context['comments'] ]
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   613
            for notification in user_comment_notifications.all():
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   614
                if int(notification.action_object_object_id) in comment_list_ids:
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   615
                    notification.mark_as_read()
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   616
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   617
        image.stats.views_count += 1
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   618
        image.stats.save()
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   619
        annotation.stats.views_count += 1
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   620
        annotation.stats.save()
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   621
        return render(request, 'iconolab/detail_annotation.html', context)
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   622
482
b71475c27159 Add page with list of revisions.
Alexandre Segura <mex.zktk@gmail.com>
parents: 479
diff changeset
   623
class ShowRevisionsView(View, ContextMixin, IconolabObjectView):
b71475c27159 Add page with list of revisions.
Alexandre Segura <mex.zktk@gmail.com>
parents: 479
diff changeset
   624
    def get_context_data(self, **kwargs):
b71475c27159 Add page with list of revisions.
Alexandre Segura <mex.zktk@gmail.com>
parents: 479
diff changeset
   625
        context = super(ShowRevisionsView, self).get_context_data(**kwargs)
b71475c27159 Add page with list of revisions.
Alexandre Segura <mex.zktk@gmail.com>
parents: 479
diff changeset
   626
        context['collection_name'] = self.kwargs.get('collection_name', '')
b71475c27159 Add page with list of revisions.
Alexandre Segura <mex.zktk@gmail.com>
parents: 479
diff changeset
   627
        context['image_guid'] = self.kwargs.get('image_guid', '')
b71475c27159 Add page with list of revisions.
Alexandre Segura <mex.zktk@gmail.com>
parents: 479
diff changeset
   628
        context['annotation_guid'] = self.kwargs.get('annotation_guid', '')
b71475c27159 Add page with list of revisions.
Alexandre Segura <mex.zktk@gmail.com>
parents: 479
diff changeset
   629
        return context
b71475c27159 Add page with list of revisions.
Alexandre Segura <mex.zktk@gmail.com>
parents: 479
diff changeset
   630
b71475c27159 Add page with list of revisions.
Alexandre Segura <mex.zktk@gmail.com>
parents: 479
diff changeset
   631
    def get(self, request, *args, **kwargs):
b71475c27159 Add page with list of revisions.
Alexandre Segura <mex.zktk@gmail.com>
parents: 479
diff changeset
   632
        success, result = self.check_kwargs(kwargs)
b71475c27159 Add page with list of revisions.
Alexandre Segura <mex.zktk@gmail.com>
parents: 479
diff changeset
   633
        if success:
b71475c27159 Add page with list of revisions.
Alexandre Segura <mex.zktk@gmail.com>
parents: 479
diff changeset
   634
            (collection, image, annotation,) = result
b71475c27159 Add page with list of revisions.
Alexandre Segura <mex.zktk@gmail.com>
parents: 479
diff changeset
   635
        else:
b71475c27159 Add page with list of revisions.
Alexandre Segura <mex.zktk@gmail.com>
parents: 479
diff changeset
   636
            return result(request)
b71475c27159 Add page with list of revisions.
Alexandre Segura <mex.zktk@gmail.com>
parents: 479
diff changeset
   637
b71475c27159 Add page with list of revisions.
Alexandre Segura <mex.zktk@gmail.com>
parents: 479
diff changeset
   638
        context = self.get_context_data(**kwargs)
b71475c27159 Add page with list of revisions.
Alexandre Segura <mex.zktk@gmail.com>
parents: 479
diff changeset
   639
        context['collection'] = collection
b71475c27159 Add page with list of revisions.
Alexandre Segura <mex.zktk@gmail.com>
parents: 479
diff changeset
   640
        context['image'] = image
b71475c27159 Add page with list of revisions.
Alexandre Segura <mex.zktk@gmail.com>
parents: 479
diff changeset
   641
        context['annotation'] = annotation
b71475c27159 Add page with list of revisions.
Alexandre Segura <mex.zktk@gmail.com>
parents: 479
diff changeset
   642
b71475c27159 Add page with list of revisions.
Alexandre Segura <mex.zktk@gmail.com>
parents: 479
diff changeset
   643
        context['revisions'] = AnnotationRevision.objects.filter(annotation=annotation).order_by('-created')
b71475c27159 Add page with list of revisions.
Alexandre Segura <mex.zktk@gmail.com>
parents: 479
diff changeset
   644
b71475c27159 Add page with list of revisions.
Alexandre Segura <mex.zktk@gmail.com>
parents: 479
diff changeset
   645
        return render(request, 'iconolab/annotation_revisions.html', context)
298
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   646
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   647
class ReadonlyAnnotationView(View, ContextMixin, IconolabObjectView):
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   648
    """
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   649
        Same view as ShowAnnotationView but without the comments and links to the forms
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   650
    """
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   651
    def get_context_data(self, **kwargs):
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   652
        context = super(ReadonlyAnnotationView, self).get_context_data(**kwargs)
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   653
        context['collection_name'] = self.kwargs.get('collection_name', '')
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   654
        context['image_guid'] = self.kwargs.get('image_guid', '')
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   655
        context['annotation_guid'] = self.kwargs.get('annotation_guid', '')
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   656
        return context
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   657
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   658
    def get(self, request, *args, **kwargs):
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   659
        """
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   660
            Exactly the same as ShowAnnotationView but without all the data around comments
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   661
        """
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   662
        success, result = self.check_kwargs(kwargs)
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   663
        if success:
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   664
            (collection, image, annotation,) = result
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   665
        else:
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   666
            return result(request)
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   667
        context = self.get_context_data(**kwargs)
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   668
        context['collection'] = collection
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   669
        context['image'] = image
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   670
        context['annotation'] = annotation
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   671
        context['tags_data'] = annotation.current_revision.get_tags_json()
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   672
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   673
        image.stats.views_count += 1
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   674
        image.stats.save()
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   675
        annotation.stats.views_count += 1
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   676
        annotation.stats.save()
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   677
        return render(request, 'iconolab/detail_annotation_readonly.html', context)
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   678
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   679
class EditAnnotationView(View, ContextMixin, IconolabObjectView):
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   680
    """
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   681
        View that handles displaying the edition form and editing an annotation
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   682
    """
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   683
    def get_context_data(self, **kwargs):
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   684
        context = super(EditAnnotationView, self).get_context_data(**kwargs)
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   685
        context['collection_name'] = self.kwargs.get('collection_name', '')
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   686
        context['image_guid'] = self.kwargs.get('image_guid', '')
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   687
        context['annotation_guid'] = self.kwargs.get('annotation_guid', '')
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   688
        return context
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   689
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   690
    def get(self, request, *args, **kwargs):
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   691
        success, result = self.check_kwargs(kwargs)
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   692
        if success:
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   693
            (collection, image, annotation,) = result
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   694
        else:
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   695
            return result(request)
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   696
        annotation_form = AnnotationRevisionForm(instance=annotation.current_revision)
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   697
        context = self.get_context_data(**kwargs)
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   698
        context['image'] = image
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   699
        context['annotation'] = annotation
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   700
        context['form'] = annotation_form
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   701
        context['tags_data'] = annotation.current_revision.get_tags_json()
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   702
        return render(request, 'iconolab/change_annotation.html', context)
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   703
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   704
    def post(self, request, *args, **kwargs):
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   705
        success, result = self.check_kwargs(kwargs)
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   706
        if success:
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   707
            (collection, image, annotation) = result
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   708
        else:
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   709
            return result(request)
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   710
        collection_name = kwargs['collection_name']
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   711
        image_guid = kwargs['image_guid']
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   712
        annotation_guid = kwargs['annotation_guid']
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   713
        annotation_form = AnnotationRevisionForm(request.POST)
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   714
        if annotation_form.is_valid():
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   715
            revision_author = request.user
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   716
            revision_title = annotation_form.cleaned_data['title']
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   717
            revision_description = annotation_form.cleaned_data['description']
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   718
            revision_fragment = annotation_form.cleaned_data['fragment']
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   719
            revision_tags_json = annotation_form.cleaned_data['tags']
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   720
            new_revision = annotation.make_new_revision(revision_author, revision_title, revision_description, revision_fragment, revision_tags_json)
461
4701ee4556e3 Display message when revision needs approval.
Alexandre Segura <mex.zktk@gmail.com>
parents: 416
diff changeset
   721
4701ee4556e3 Display message when revision needs approval.
Alexandre Segura <mex.zktk@gmail.com>
parents: 416
diff changeset
   722
            if (annotation.author != revision_author):
465
ce9947e45d04 Fix typo.
Alexandre Segura <mex.zktk@gmail.com>
parents: 461
diff changeset
   723
                messages.add_message(request, messages.INFO, "Votre modification a été prise en compte. Le créateur de l'annotation a été notifié.")
461
4701ee4556e3 Display message when revision needs approval.
Alexandre Segura <mex.zktk@gmail.com>
parents: 416
diff changeset
   724
470
6c43539e5c67 Use browser history instead of hash for annotation URL.
Alexandre Segura <mex.zktk@gmail.com>
parents: 467
diff changeset
   725
            redirect_url = reverse('annotation_detail', kwargs={'collection_name': collection_name, 'image_guid': image_guid, 'annotation_guid': str(annotation.annotation_guid)})
6c43539e5c67 Use browser history instead of hash for annotation URL.
Alexandre Segura <mex.zktk@gmail.com>
parents: 467
diff changeset
   726
            return redirect(redirect_url)
298
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   727
        context = self.get_context_data(**kwargs)
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   728
        context['image'] = image
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   729
        context['form'] = annotation_form
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   730
        context['annotation'] = annotation
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   731
        context['tags_data'] = annotation.current_revision.get_tags_json()
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   732
        return render(request, 'iconolab/change_annotation.html', context)
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   733
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   734
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   735
class ShowRevisionView(View, ContextMixin, IconolabObjectView):
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   736
    """
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   737
        View that displays a given revision with its associated data and comment
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   738
    """
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   739
    def get_context_data(self, **kwargs):
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   740
        context = super(ShowRevisionView, self).get_context_data(**kwargs)
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   741
        context['collection_name'] = self.kwargs.get('collection_name', '')
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   742
        context['image_guid'] = self.kwargs.get('image_guid', '')
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   743
        context['annotation_guid'] = self.kwargs.get('annotation_guid', '')
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   744
        context['revision_guid'] = self.kwargs.get('revision_guid', '')
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   745
        return context
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   746
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   747
    def get(self, request, *args, **kwargs):
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   748
        """
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   749
            Template is iconolab/detail_annotations.html
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   750
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   751
            Url args are:
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   752
                - collection_name: 'name' attribute of the requested collection
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   753
                - item_guid: 'item_guid' attribute of the requested item
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   754
                - annotation_guid: 'annotation_guid' attribute of the requested annotation
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   755
                - revision_guid: 'revision_guid' attribute of the requested revision
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   756
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   757
            Context variables provided to the template are:
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   758
                - collection: the collection object for the requested collection
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   759
                - image: the image object for the requested image
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   760
                - annotation: the annotation object for the requested annotation
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   761
                - revision: the revision object for the requested annotation
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   762
                - tags_data: a json string describing tags for the annotation current revision
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   763
                - comment: the comment that was posted alongside the revision
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   764
                - notified_revision: if True, the revision is linked from one or more unread notifications for the
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   765
                current user, allowing us to highlight it in the template.
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   766
        """
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   767
        success, result = self.check_kwargs(kwargs)
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   768
        if success:
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   769
            (collection, image, annotation, revision,) = result
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   770
        else:
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   771
            return result(request)
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   772
        context = self.get_context_data(**kwargs)
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   773
        context['collection'] = collection
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   774
        context['image'] = image
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   775
        context['annotation'] = annotation
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   776
        context['revision'] = revision
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   777
        context['tags_data'] = revision.get_tags_json()
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   778
        context['comment'] = revision.creation_comment.first()
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   779
        if request.user.is_authenticated() and annotation.author == request.user:
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   780
            ann_author_notified = Notification.objects.filter(
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   781
                    recipient=request.user,
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   782
                    action_object_content_type__app_label='iconolab',
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   783
                    action_object_content_type__model='annotationrevision',
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   784
                    action_object_object_id=revision.id,
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   785
                    target_content_type__app_label='iconolab',
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   786
                    target_content_type__model='annotation',
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   787
                    target_object_id=annotation.id
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   788
                ).unread()
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   789
            if ann_author_notified:
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   790
                ann_author_notified.first().mark_as_read()
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   791
                context['notified_revision'] = True
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   792
        if request.user.is_authenticated() and revision.author == request.user:
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   793
            rev_author_notified = Notification.objects.filter(
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   794
                    recipient=request.user,
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   795
                    action_object_content_type__app_label='iconolab',
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   796
                    action_object_content_type__model='annotationrevision',
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   797
                    action_object_object_id=revision.id,
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   798
                    target_content_type__app_label='iconolab',
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   799
                    target_content_type__model='annotation',
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   800
                    target_object_id=annotation.id
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   801
                ).unread()
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   802
            if rev_author_notified:
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   803
                rev_author_notified.first().mark_as_read()
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   804
                context['notified_revision'] = True
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   805
        return render(request, 'iconolab/detail_revision.html', context)
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   806
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   807
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   808
class MergeProposalView(View, ContextMixin, IconolabObjectView):
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   809
    """
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   810
        View that displays the merge form, used when a user wants to "study" a revision because it was submitted from an older revision than the current revision (thus
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   811
        the two revisions don't have the same parents and there is a conflict)
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   812
    """
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   813
    def get_context_data(self, **kwargs):
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   814
        context = super(MergeProposalView, self).get_context_data(**kwargs)
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   815
        context['collection_name'] = self.kwargs.get('collection_name', '')
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   816
        context['image_guid'] = self.kwargs.get('image_guid', '')
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   817
        context['annotation_guid'] = self.kwargs.get('annotation_guid', '')
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   818
        context['revision_guid'] = self.kwargs.get('revision_guid', '')
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   819
        return context
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   820
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   821
    def get(self, request, *args, **kwargs):
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   822
        success, result = self.check_kwargs(kwargs)
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   823
        if success:
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   824
            (collection, image, annotation, revision,) = result
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   825
        else:
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   826
            return result(request)
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   827
        # Only show merge form if there is a revision to merge AND the current user is the annotation author
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   828
        if revision.state != AnnotationRevision.AWAITING or request.user != annotation.author:
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   829
            return RedirectView.as_view(
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   830
                url=reverse('revision_detail',
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   831
                    kwargs={
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   832
                        'collection_name': collection.name,
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   833
                        'image_guid': image.image_guid,
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   834
                        'annotation_guid': annotation.annotation_guid,
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   835
                        'revision_guid': revision.revision_guid
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   836
                    }
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   837
                )
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   838
            )(request)
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   839
        # Auto-accepts the revision only if the proper query arg is set and only if the revision parent is the current revision
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   840
        if 'auto_accept' in request.GET and request.GET['auto_accept'] in ['True', 'true', '1', 'yes'] and revision.parent_revision == annotation.current_revision:
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   841
            annotation.validate_existing_revision(revision)
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   842
            return RedirectView.as_view(
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   843
                url=reverse('annotation_detail',
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   844
                    kwargs={
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   845
                        'collection_name': collection.name,
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   846
                        'image_guid': image.image_guid,
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   847
                        'annotation_guid': annotation.annotation_guid
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   848
                    }
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   849
                )
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   850
            )(request)
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   851
        # Auto-reject the revision only if the proper query arg is set
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   852
        if 'auto_reject' in request.GET and request.GET['auto_reject'] in ['True', 'true', '1', 'yes']:
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   853
            annotation.reject_existing_revision(revision)
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   854
            return RedirectView.as_view(
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   855
                url=reverse('annotation_detail',
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   856
                    kwargs={
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   857
                        'collection_name': collection.name,
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   858
                        'image_guid': image.image_guid,
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   859
                        'annotation_guid': annotation.annotation_guid
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   860
                    }
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   861
                )
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   862
            )(request)
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   863
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   864
        context = self.get_context_data(**kwargs)
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   865
        context['collection'] = collection
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   866
        context['image'] = image
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   867
        context['annotation'] = annotation
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   868
        # Proposal data
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   869
        context['proposal_revision'] = revision
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   870
        context['proposal_tags_data'] = revision.get_tags_json()
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   871
        context['proposal_comment'] = revision.creation_comment.first()
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   872
        # Parent data
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   873
        context['parent_revision'] = revision.parent_revision
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   874
        context['parent_tags_data'] = revision.parent_revision.get_tags_json()
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   875
        context['parent_comment'] = revision.parent_revision.creation_comment.first()
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   876
        # Current data
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   877
        context['current_revision'] = annotation.current_revision
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   878
        context['current_tags_data'] = annotation.current_revision.get_tags_json()
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   879
        context['current_comment'] = annotation.current_revision.creation_comment.first()
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   880
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   881
        merge_form = AnnotationRevisionForm(instance=revision)
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   882
        context['merge_form'] = merge_form
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   883
        return render(request, 'iconolab/merge_revision.html', context)
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   884
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   885
    def post(self, request, *args, **kwargs):
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   886
        # Handle merge form submit here
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   887
        success, result = self.check_kwargs(kwargs)
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   888
        if success:
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   889
            (collection, image, annotation, revision) = result
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   890
        else:
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   891
            return result(request)
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   892
        collection_name = kwargs['collection_name']
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   893
        image_guid = kwargs['image_guid']
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   894
        annotation_guid = kwargs['annotation_guid']
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   895
        revision_guid = kwargs['revision_guid']
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   896
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   897
        merge_revision_form = AnnotationRevisionForm(request.POST)
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   898
        if merge_revision_form.is_valid():
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   899
            revision_title = merge_revision_form.cleaned_data['title']
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   900
            revision_description = merge_revision_form.cleaned_data['description']
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   901
            revision_fragment = merge_revision_form.cleaned_data['fragment']
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   902
            revision_tags_json = merge_revision_form.cleaned_data['tags']
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   903
            new_revision = annotation.merge_existing_revision(revision_title, revision_description, revision_fragment, revision_tags_json, revision)
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   904
            revision_comment = merge_revision_form.cleaned_data['comment']
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   905
            comment = IconolabComment.objects.create(
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   906
                comment = revision_comment,
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   907
                revision = new_revision,
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   908
                content_type = ContentType.objects.get(app_label='iconolab', model='annotation'),
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   909
                content_object = annotation,
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   910
                site = Site.objects.get(id=settings.SITE_ID),
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   911
                object_pk = annotation.id,
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   912
                user = request.user,
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   913
                user_name = request.user.username
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   914
            )
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   915
            return RedirectView.as_view(url=reverse('annotation_detail', kwargs={'collection_name': collection_name, 'image_guid': image_guid, 'annotation_guid': annotation_guid}))(request)
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   916
        context = self.get_context_data(**kwargs)
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   917
        context['image'] = image
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   918
        context['merge_form'] = merge_revision_form
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   919
        context['annotation'] = annotation
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   920
        # Proposal data
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   921
        context['proposal_revision'] = revision
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   922
        context['proposal_tags_data'] = revision.get_tags_json()
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   923
        context['proposal_comment'] = revision.creation_comment.first()
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   924
        # Parent data
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   925
        context['parent_revision'] = revision.parent_revision
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   926
        context['parent_tags_data'] = revision.parent_revision.get_tags_json()
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   927
        context['parent_comment'] = revision.parent_revision.creation_comment.first()
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   928
        # Current data
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   929
        context['current_revision'] = annotation.current_revision
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   930
        context['current_tags_data'] = annotation.current_revision.get_tags_json()
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   931
        context['current_comment'] = annotation.current_revision.creation_comment.first()
97b805fc88f0 force unix line ending
ymh <ymh.work@gmail.com>
parents: 288
diff changeset
   932
        return render(request, 'iconolab/merge_revision.html', context)