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