src/jocondelab/views.py
author ymh <ymh.work@gmail.com>
Tue, 02 Jul 2013 10:15:50 +0200
changeset 57 a82c3913c1fc
parent 55 bcbd95da9be2
child 61 0048668779c0
permissions -rw-r--r--
add direct link to notice on ref field
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
# -*- coding: utf-8 -*-
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
'''
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
Created on Jun 12, 2013
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
     4
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
     5
@author: ymh
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
     6
'''
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
     8
from .forms import (ModifyWpLinkForm, ValidateTermForm, RemoveWpLinkForm, 
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
     9
    TermFilterForm)
5
580fd386c4b9 Improve design, add context
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    10
from .utils import JocondePaginator
0
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
    11
from core.models import Term, TERM_URL_STATUS_DICT
35
859862939996 add qualifier on the wikipedia link
ymh <ymh.work@gmail.com>
parents: 26
diff changeset
    12
from core.models.term import (TERM_WK_LINK_SEMANTIC_LEVEL_DICT, 
859862939996 add qualifier on the wikipedia link
ymh <ymh.work@gmail.com>
parents: 26
diff changeset
    13
    TERM_WK_LINK_SEMANTIC_LEVEL_CHOICES)
0
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
    14
from core.wp_utils import process_term as wp_process_term
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
    15
from django.conf import settings
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
from django.http.response import HttpResponse
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
from django.views.generic import ListView, DetailView, View
35
859862939996 add qualifier on the wikipedia link
ymh <ymh.work@gmail.com>
parents: 26
diff changeset
    18
from jocondelab.forms import WikipediaEditionForm, LinkSemanticLevelForm
0
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
import logging
37
b6a04c4ab494 Correct joconde search link
ymh <ymh.work@gmail.com>
parents: 35
diff changeset
    20
import urllib
51
8890db197b37 add prev and next buttons
ymh <ymh.work@gmail.com>
parents: 48
diff changeset
    21
from django.views.generic.list import MultipleObjectMixin
0
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
    23
logger = logging.getLogger(__name__)
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
    24
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
    25
class TermListView(ListView):
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
    26
    
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
    27
    model = Term
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
    28
    paginate_by = settings.TERM_LIST_PAGE_SIZE
5
580fd386c4b9 Improve design, add context
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    29
    paginator_class = JocondePaginator
0
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
    30
    template_name = "jocondelab/term_list.html"
5
580fd386c4b9 Improve design, add context
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    31
    filter_form_class = TermFilterForm
0
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
    32
    
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
    33
    def get_filter_form(self):
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
    34
        initial = { 'order_by':'label',
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
    35
                    'order_dir': 'asc',
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
    36
                    'thesaurus': None,
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
    37
                    'label': None,
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
    38
                    'link_status': -1,
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
    39
                    'validated': None}
5
580fd386c4b9 Improve design, add context
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    40
        return self.filter_form_class(self.request.GET, initial=initial, auto_id=True)
0
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
    41
    
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
    42
    def get_context_data(self, **kwargs):
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
    43
        context = ListView.get_context_data(self, **kwargs)
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
    44
        context['filter_form'] = self.get_filter_form()
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
    45
        return context
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
    46
    
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
    47
    def get_queryset(self):
48
f4fadc1b9d70 cache the nb_notice query to improve perf.
ymh <ymh.work@gmail.com>
parents: 43
diff changeset
    48
        qs = super(TermListView, self).get_queryset()
0
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
    49
        filter_form = self.get_filter_form()
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
    50
        if filter_form.is_valid():
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
    51
            return filter_form.get_filter_qs(qs)
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
    52
        else:
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
    53
            return None
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
    54
5
580fd386c4b9 Improve design, add context
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    55
class TermListTableView(TermListView):
580fd386c4b9 Improve design, add context
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    56
    
580fd386c4b9 Improve design, add context
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    57
    template_name = "jocondelab/partial/term_list_table.html"
580fd386c4b9 Improve design, add context
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    58
    
580fd386c4b9 Improve design, add context
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    59
    
580fd386c4b9 Improve design, add context
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    60
51
8890db197b37 add prev and next buttons
ymh <ymh.work@gmail.com>
parents: 48
diff changeset
    61
class TermEditView(DetailView, MultipleObjectMixin):
0
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
    62
    
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
    63
    queryset = Term.objects.select_related()
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
    64
    pk_url_kwarg = "term_id"
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
    65
    context_object_name = "term"
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
    66
    template_name = "jocondelab/term_edit.html"
5
580fd386c4b9 Improve design, add context
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    67
    filter_form_class = TermFilterForm
51
8890db197b37 add prev and next buttons
ymh <ymh.work@gmail.com>
parents: 48
diff changeset
    68
    model = Term
8890db197b37 add prev and next buttons
ymh <ymh.work@gmail.com>
parents: 48
diff changeset
    69
    paginate_by = settings.TERM_LIST_PAGE_SIZE
8890db197b37 add prev and next buttons
ymh <ymh.work@gmail.com>
parents: 48
diff changeset
    70
    paginator_class = JocondePaginator
8890db197b37 add prev and next buttons
ymh <ymh.work@gmail.com>
parents: 48
diff changeset
    71
8890db197b37 add prev and next buttons
ymh <ymh.work@gmail.com>
parents: 48
diff changeset
    72
    def get_object(self, queryset=None):
8890db197b37 add prev and next buttons
ymh <ymh.work@gmail.com>
parents: 48
diff changeset
    73
        
8890db197b37 add prev and next buttons
ymh <ymh.work@gmail.com>
parents: 48
diff changeset
    74
        if queryset is None:
8890db197b37 add prev and next buttons
ymh <ymh.work@gmail.com>
parents: 48
diff changeset
    75
            queryset = self.queryset 
8890db197b37 add prev and next buttons
ymh <ymh.work@gmail.com>
parents: 48
diff changeset
    76
        
8890db197b37 add prev and next buttons
ymh <ymh.work@gmail.com>
parents: 48
diff changeset
    77
        return DetailView.get_object(self, queryset)
8890db197b37 add prev and next buttons
ymh <ymh.work@gmail.com>
parents: 48
diff changeset
    78
8890db197b37 add prev and next buttons
ymh <ymh.work@gmail.com>
parents: 48
diff changeset
    79
    def get_queryset(self):
8890db197b37 add prev and next buttons
ymh <ymh.work@gmail.com>
parents: 48
diff changeset
    80
        qs = self.queryset._clone()
8890db197b37 add prev and next buttons
ymh <ymh.work@gmail.com>
parents: 48
diff changeset
    81
        filter_form = self.get_filter_form()
8890db197b37 add prev and next buttons
ymh <ymh.work@gmail.com>
parents: 48
diff changeset
    82
        if filter_form.is_valid():
8890db197b37 add prev and next buttons
ymh <ymh.work@gmail.com>
parents: 48
diff changeset
    83
            return filter_form.get_filter_qs(qs)
8890db197b37 add prev and next buttons
ymh <ymh.work@gmail.com>
parents: 48
diff changeset
    84
        else:
8890db197b37 add prev and next buttons
ymh <ymh.work@gmail.com>
parents: 48
diff changeset
    85
            return None
8890db197b37 add prev and next buttons
ymh <ymh.work@gmail.com>
parents: 48
diff changeset
    86
5
580fd386c4b9 Improve design, add context
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    87
    
580fd386c4b9 Improve design, add context
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    88
    def get_filter_form(self):
580fd386c4b9 Improve design, add context
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    89
        initial = { 'order_by':'label',
580fd386c4b9 Improve design, add context
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    90
                    'order_dir': 'asc',
580fd386c4b9 Improve design, add context
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    91
                    'thesaurus': None,
580fd386c4b9 Improve design, add context
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    92
                    'label': None,
580fd386c4b9 Improve design, add context
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    93
                    'link_status': -1,
580fd386c4b9 Improve design, add context
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    94
                    'validated': None}
580fd386c4b9 Improve design, add context
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    95
        return self.filter_form_class(self.request.GET, initial=initial, auto_id=True)
580fd386c4b9 Improve design, add context
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    96
580fd386c4b9 Improve design, add context
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    97
    
580fd386c4b9 Improve design, add context
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    98
    def get_context_data(self, **kwargs):        
51
8890db197b37 add prev and next buttons
ymh <ymh.work@gmail.com>
parents: 48
diff changeset
    99
        
8890db197b37 add prev and next buttons
ymh <ymh.work@gmail.com>
parents: 48
diff changeset
   100
        self.object_list = self.get_queryset()
8890db197b37 add prev and next buttons
ymh <ymh.work@gmail.com>
parents: 48
diff changeset
   101
        if kwargs is None :
8890db197b37 add prev and next buttons
ymh <ymh.work@gmail.com>
parents: 48
diff changeset
   102
            kwargs = {}
8890db197b37 add prev and next buttons
ymh <ymh.work@gmail.com>
parents: 48
diff changeset
   103
        kwargs['object_list'] = self.object_list         
8890db197b37 add prev and next buttons
ymh <ymh.work@gmail.com>
parents: 48
diff changeset
   104
8890db197b37 add prev and next buttons
ymh <ymh.work@gmail.com>
parents: 48
diff changeset
   105
        # Beware: because of multiple inheritance this call MultipleObjectMixin.get_context_data(self, **context)
5
580fd386c4b9 Improve design, add context
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   106
        context = DetailView.get_context_data(self, **kwargs)
55
bcbd95da9be2 add preview of notices + add missing img for wp
ymh <ymh.work@gmail.com>
parents: 51
diff changeset
   107
        
bcbd95da9be2 add preview of notices + add missing img for wp
ymh <ymh.work@gmail.com>
parents: 51
diff changeset
   108
        context['notices'] = self.object.notices.select_related().all().prefetch_related('images')[:10]
51
8890db197b37 add prev and next buttons
ymh <ymh.work@gmail.com>
parents: 48
diff changeset
   109
                 
5
580fd386c4b9 Improve design, add context
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   110
        context['filter_form'] = self.get_filter_form()
37
b6a04c4ab494 Correct joconde search link
ymh <ymh.work@gmail.com>
parents: 35
diff changeset
   111
        context['link_semantic_level_choice'] = TERM_WK_LINK_SEMANTIC_LEVEL_CHOICES
57
a82c3913c1fc add direct link to notice on ref field
ymh <ymh.work@gmail.com>
parents: 55
diff changeset
   112
        context['JOCONDE_IMG_BASE_URL'] = settings.JOCONDE_IMG_BASE_URL
a82c3913c1fc add direct link to notice on ref field
ymh <ymh.work@gmail.com>
parents: 55
diff changeset
   113
        context['JOCONDE_NOTICE_BASE_URL'] = settings.JOCONDE_NOTICE_BASE_URL
37
b6a04c4ab494 Correct joconde search link
ymh <ymh.work@gmail.com>
parents: 35
diff changeset
   114
        
b6a04c4ab494 Correct joconde search link
ymh <ymh.work@gmail.com>
parents: 35
diff changeset
   115
        field_index = {
b6a04c4ab494 Correct joconde search link
ymh <ymh.work@gmail.com>
parents: 35
diff changeset
   116
            'DOMN' : 1,
b6a04c4ab494 Correct joconde search link
ymh <ymh.work@gmail.com>
parents: 35
diff changeset
   117
            'AUTR' : 3,
b6a04c4ab494 Correct joconde search link
ymh <ymh.work@gmail.com>
parents: 35
diff changeset
   118
            'ECOL' : 4,
b6a04c4ab494 Correct joconde search link
ymh <ymh.work@gmail.com>
parents: 35
diff changeset
   119
            'REPR' : 5,
b6a04c4ab494 Correct joconde search link
ymh <ymh.work@gmail.com>
parents: 35
diff changeset
   120
            'PERI' : 6,
b6a04c4ab494 Correct joconde search link
ymh <ymh.work@gmail.com>
parents: 35
diff changeset
   121
            'EPOQ' : 6,
b6a04c4ab494 Correct joconde search link
ymh <ymh.work@gmail.com>
parents: 35
diff changeset
   122
            'LIEUX': 4,
b6a04c4ab494 Correct joconde search link
ymh <ymh.work@gmail.com>
parents: 35
diff changeset
   123
            'SREP' : 9
b6a04c4ab494 Correct joconde search link
ymh <ymh.work@gmail.com>
parents: 35
diff changeset
   124
        }[self.object.thesaurus.label]
b6a04c4ab494 Correct joconde search link
ymh <ymh.work@gmail.com>
parents: 35
diff changeset
   125
        
38
7abb8925853d Correct joconde search link, try to improve thesaurus name
ymh <ymh.work@gmail.com>
parents: 37
diff changeset
   126
        field_name = {
7abb8925853d Correct joconde search link, try to improve thesaurus name
ymh <ymh.work@gmail.com>
parents: 37
diff changeset
   127
            'SREP' :  u"Source sujet représenté"
7abb8925853d Correct joconde search link, try to improve thesaurus name
ymh <ymh.work@gmail.com>
parents: 37
diff changeset
   128
        }.get(self.object.thesaurus.label, self.object.thesaurus.label) 
7abb8925853d Correct joconde search link, try to improve thesaurus name
ymh <ymh.work@gmail.com>
parents: 37
diff changeset
   129
37
b6a04c4ab494 Correct joconde search link
ymh <ymh.work@gmail.com>
parents: 35
diff changeset
   130
        encoded_label = self.object.label.encode('latin1') if self.object.label is not None else ""
b6a04c4ab494 Correct joconde search link
ymh <ymh.work@gmail.com>
parents: 35
diff changeset
   131
        
38
7abb8925853d Correct joconde search link, try to improve thesaurus name
ymh <ymh.work@gmail.com>
parents: 37
diff changeset
   132
        context['encoded_term_label_query_parameter'] = urllib. urlencode({
7abb8925853d Correct joconde search link, try to improve thesaurus name
ymh <ymh.work@gmail.com>
parents: 37
diff changeset
   133
                'FIELD_%d' % field_index: field_name.encode('latin1'),
7abb8925853d Correct joconde search link, try to improve thesaurus name
ymh <ymh.work@gmail.com>
parents: 37
diff changeset
   134
                'VALUE_%d' % field_index: encoded_label}).replace('+','%20')
51
8890db197b37 add prev and next buttons
ymh <ymh.work@gmail.com>
parents: 48
diff changeset
   135
                
8890db197b37 add prev and next buttons
ymh <ymh.work@gmail.com>
parents: 48
diff changeset
   136
        #prev_id, nex_id, prev_page, next_page
8890db197b37 add prev and next buttons
ymh <ymh.work@gmail.com>
parents: 48
diff changeset
   137
        page = context['page_obj']
8890db197b37 add prev and next buttons
ymh <ymh.work@gmail.com>
parents: 48
diff changeset
   138
        
8890db197b37 add prev and next buttons
ymh <ymh.work@gmail.com>
parents: 48
diff changeset
   139
        prev_id = None
8890db197b37 add prev and next buttons
ymh <ymh.work@gmail.com>
parents: 48
diff changeset
   140
        prev_page = 0
8890db197b37 add prev and next buttons
ymh <ymh.work@gmail.com>
parents: 48
diff changeset
   141
        next_id = None
8890db197b37 add prev and next buttons
ymh <ymh.work@gmail.com>
parents: 48
diff changeset
   142
        next_page = 0
8890db197b37 add prev and next buttons
ymh <ymh.work@gmail.com>
parents: 48
diff changeset
   143
        
8890db197b37 add prev and next buttons
ymh <ymh.work@gmail.com>
parents: 48
diff changeset
   144
        
8890db197b37 add prev and next buttons
ymh <ymh.work@gmail.com>
parents: 48
diff changeset
   145
        object_list_ids = [obj.id for obj in list(page.object_list)]
8890db197b37 add prev and next buttons
ymh <ymh.work@gmail.com>
parents: 48
diff changeset
   146
        current_index = object_list_ids.index(self.object.id)
8890db197b37 add prev and next buttons
ymh <ymh.work@gmail.com>
parents: 48
diff changeset
   147
8890db197b37 add prev and next buttons
ymh <ymh.work@gmail.com>
parents: 48
diff changeset
   148
        if current_index > 0:
8890db197b37 add prev and next buttons
ymh <ymh.work@gmail.com>
parents: 48
diff changeset
   149
            prev_id = object_list_ids[current_index-1]
8890db197b37 add prev and next buttons
ymh <ymh.work@gmail.com>
parents: 48
diff changeset
   150
            prev_page = page.number
8890db197b37 add prev and next buttons
ymh <ymh.work@gmail.com>
parents: 48
diff changeset
   151
        elif page.has_previous():
8890db197b37 add prev and next buttons
ymh <ymh.work@gmail.com>
parents: 48
diff changeset
   152
            prev_page = page.previous_page_number()
8890db197b37 add prev and next buttons
ymh <ymh.work@gmail.com>
parents: 48
diff changeset
   153
            prev_id = page.paginator.object_list[page.start_index() - 2].id
8890db197b37 add prev and next buttons
ymh <ymh.work@gmail.com>
parents: 48
diff changeset
   154
8890db197b37 add prev and next buttons
ymh <ymh.work@gmail.com>
parents: 48
diff changeset
   155
        if current_index < (len(page)-1):
8890db197b37 add prev and next buttons
ymh <ymh.work@gmail.com>
parents: 48
diff changeset
   156
            next_id = object_list_ids[current_index+1]
8890db197b37 add prev and next buttons
ymh <ymh.work@gmail.com>
parents: 48
diff changeset
   157
            next_page = page.number
8890db197b37 add prev and next buttons
ymh <ymh.work@gmail.com>
parents: 48
diff changeset
   158
        elif page.has_next():
8890db197b37 add prev and next buttons
ymh <ymh.work@gmail.com>
parents: 48
diff changeset
   159
            next_page = page.next_page_number()
8890db197b37 add prev and next buttons
ymh <ymh.work@gmail.com>
parents: 48
diff changeset
   160
            next_id = page.paginator.object_list[page.end_index()].id
8890db197b37 add prev and next buttons
ymh <ymh.work@gmail.com>
parents: 48
diff changeset
   161
8890db197b37 add prev and next buttons
ymh <ymh.work@gmail.com>
parents: 48
diff changeset
   162
        context.update({
8890db197b37 add prev and next buttons
ymh <ymh.work@gmail.com>
parents: 48
diff changeset
   163
            'prev_id': prev_id,
8890db197b37 add prev and next buttons
ymh <ymh.work@gmail.com>
parents: 48
diff changeset
   164
            'prev_page': prev_page,
8890db197b37 add prev and next buttons
ymh <ymh.work@gmail.com>
parents: 48
diff changeset
   165
            'next_id': next_id,
8890db197b37 add prev and next buttons
ymh <ymh.work@gmail.com>
parents: 48
diff changeset
   166
            'next_page': next_page
8890db197b37 add prev and next buttons
ymh <ymh.work@gmail.com>
parents: 48
diff changeset
   167
        })
8890db197b37 add prev and next buttons
ymh <ymh.work@gmail.com>
parents: 48
diff changeset
   168
8890db197b37 add prev and next buttons
ymh <ymh.work@gmail.com>
parents: 48
diff changeset
   169
        return context 
0
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
   170
        
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
   171
        
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
   172
    
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
   173
class TermUpdateView(View):
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
   174
    
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
   175
    form_class = None
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
   176
    http_method_names = ['post']
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
   177
    
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
   178
    def __init__(self, **kwargs):
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
   179
        View.__init__(self, **kwargs)
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
   180
        self.form = None
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
   181
        self.form_values = None
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
   182
        self.term = None
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
   183
    
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
   184
    def post(self, request, *args, **kwargs):
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
   185
        self.form = self.form_class(request.POST)
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
   186
        if not self.form.is_valid():
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
   187
            return HttpResponse("Parameters not valid : %s" % (self.form.cleaned_data), status=400)
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
   188
        
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
   189
        self.form_values = self.form.cleaned_data
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
   190
        
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
   191
        try:
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
   192
            self.term = Term.objects.get(id=self.form_values['term_id'])
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
   193
        except Term.DoesNotExist:
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
   194
            return HttpResponse("Term %d not found" % self.form_values['term_id'],status=404)
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
   195
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
   196
        return self.process_term(request)
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
   197
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
   198
    def process_term(self, request):
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
   199
        raise NotImplemented()
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
   200
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
   201
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
   202
class TermValidate(TermUpdateView):
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
   203
    
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
   204
    form_class = ValidateTermForm
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
   205
    
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
   206
    def process_term(self, request):
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
   207
        if self.form_values['validation_val']:
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
   208
            self.term.validate(request.user)            
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
   209
        else:
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
   210
            self.term.unvalidate()
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
   211
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
   212
        return HttpResponse(status=204)
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
   213
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
   214
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
   215
class TermRemoveWpLink(TermUpdateView):
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
   216
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
   217
    form_class = RemoveWpLinkForm
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
   218
    
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
   219
    def process_term(self, request):
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
   220
                        
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
   221
        self.term.wp_label = None
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
   222
        self.term.wp_alternative_label = None
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
   223
        self.term.alternative_wikipedia_url = None
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
   224
        self.term.alternative_wikipedia_pageid = None
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
   225
        self.term.wikipedia_url =None
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
   226
        self.term.wikipedia_pageid = None
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
   227
        self.term.dbpedia_uri = None
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
   228
        self.term.wikipedia_revision_id = None
24
1d20eaea6169 small change of status link unsemantized.
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   229
        self.term.url_status = TERM_URL_STATUS_DICT["unsemantized"]
35
859862939996 add qualifier on the wikipedia link
ymh <ymh.work@gmail.com>
parents: 26
diff changeset
   230
        self.link_semantic_level = TERM_WK_LINK_SEMANTIC_LEVEL_DICT['--']
0
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
   231
        
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
   232
        self.term.save()
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
   233
                
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
   234
        return HttpResponse(status=204)
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
   235
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
   236
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
   237
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
   238
class TermModifyWpLink(TermUpdateView):    
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
   239
    
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
   240
    form_class = ModifyWpLinkForm
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
   241
        
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
   242
    def process_term(self, request):
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
   243
        
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
   244
        label = self.form_values['label']
26
758b9289aa9a add check box to indicate that a wk page should be created
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
   245
                
0
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
   246
        wp_process_term(None, self.term, label)
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
   247
        
26
758b9289aa9a add check box to indicate that a wk page should be created
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
   248
        return HttpResponse(status=204)
758b9289aa9a add check box to indicate that a wk page should be created
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
   249
758b9289aa9a add check box to indicate that a wk page should be created
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
   250
758b9289aa9a add check box to indicate that a wk page should be created
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
   251
class TermWikipediaEdition(TermUpdateView):    
758b9289aa9a add check box to indicate that a wk page should be created
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
   252
    
758b9289aa9a add check box to indicate that a wk page should be created
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
   253
    form_class = WikipediaEditionForm
758b9289aa9a add check box to indicate that a wk page should be created
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
   254
        
758b9289aa9a add check box to indicate that a wk page should be created
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
   255
    def process_term(self, request):
758b9289aa9a add check box to indicate that a wk page should be created
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
   256
        
758b9289aa9a add check box to indicate that a wk page should be created
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
   257
        self.term.wikipedia_edition = self.form_values['wikipedia_edition']
758b9289aa9a add check box to indicate that a wk page should be created
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
   258
        self.term.save()
758b9289aa9a add check box to indicate that a wk page should be created
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
   259
        
758b9289aa9a add check box to indicate that a wk page should be created
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
   260
        return HttpResponse(status=204)
35
859862939996 add qualifier on the wikipedia link
ymh <ymh.work@gmail.com>
parents: 26
diff changeset
   261
 
859862939996 add qualifier on the wikipedia link
ymh <ymh.work@gmail.com>
parents: 26
diff changeset
   262
class TermLinkSemanticLevelEdition(TermUpdateView):
859862939996 add qualifier on the wikipedia link
ymh <ymh.work@gmail.com>
parents: 26
diff changeset
   263
859862939996 add qualifier on the wikipedia link
ymh <ymh.work@gmail.com>
parents: 26
diff changeset
   264
    form_class = LinkSemanticLevelForm
859862939996 add qualifier on the wikipedia link
ymh <ymh.work@gmail.com>
parents: 26
diff changeset
   265
     
859862939996 add qualifier on the wikipedia link
ymh <ymh.work@gmail.com>
parents: 26
diff changeset
   266
    def process_term(self, request):
859862939996 add qualifier on the wikipedia link
ymh <ymh.work@gmail.com>
parents: 26
diff changeset
   267
        
859862939996 add qualifier on the wikipedia link
ymh <ymh.work@gmail.com>
parents: 26
diff changeset
   268
        self.term.link_semantic_level = self.form_values['link_semantic_level']
859862939996 add qualifier on the wikipedia link
ymh <ymh.work@gmail.com>
parents: 26
diff changeset
   269
        self.term.save()
859862939996 add qualifier on the wikipedia link
ymh <ymh.work@gmail.com>
parents: 26
diff changeset
   270
        
859862939996 add qualifier on the wikipedia link
ymh <ymh.work@gmail.com>
parents: 26
diff changeset
   271
        return HttpResponse(status=204)
859862939996 add qualifier on the wikipedia link
ymh <ymh.work@gmail.com>
parents: 26
diff changeset
   272