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