src/jocondelab/views/front_office.py
changeset 100 25636bb50756
parent 98 b174c4467f7c
child 101 19de76fe00a9
equal deleted inserted replaced
99:dc08f5692d80 100:25636bb50756
     6 '''
     6 '''
     7 
     7 
     8 from core.models import (Notice, Thesaurus, Term)
     8 from core.models import (Notice, Thesaurus, Term)
     9 from jocondelab.models import DbpediaFields
     9 from jocondelab.models import DbpediaFields
    10 from django.conf import settings
    10 from django.conf import settings
    11 from django.views.generic import View
    11 from django.views.generic import View, DetailView
    12 from django.views.generic.base import TemplateResponseMixin
    12 from django.views.generic.base import TemplateResponseMixin
    13 from django.db.models import Q, Count
    13 from django.db.models import Q, Count
    14 import re
    14 import re
    15 import time
    15 import time
    16 
    16 
    47     def get(self, request):
    47     def get(self, request):
    48         
    48         
    49         starttime = time.time()
    49         starttime = time.time()
    50         
    50         
    51         context = {}
    51         context = {}
    52         lang = request.GET.get('lang',request.LANGUAGE_CODE)
    52         lang = request.GET.get('lang',request.LANGUAGE_CODE)[:2]
    53         querystr = request.GET.get('q', "")
    53         querystr = request.GET.get('q', "")
    54         search_in_title = request.GET.get('search_in_title', True)
    54         search_in_title = request.GET.get('search_in_title', True)
    55         search_in_translations = request.GET.get('search_in_translations', True)
    55         search_in_translations = request.GET.get('search_in_translations', True)
    56         show_tagcloud = request.GET.get('show_tagcloud', True)
    56         show_tagcloud = request.GET.get('show_tagcloud', True)
    57         queryterms = [s.strip(" ") for s in re.split("[,;]",querystr) if s.strip(" ")] if (search_in_title or search_in_translations) else None
    57         queryterms = [s.strip(" ") for s in re.split("[,;]",querystr) if s.strip(" ")] if (search_in_title or search_in_translations) else None
    77             qs = Notice.objects.filter(image=True)
    77             qs = Notice.objects.filter(image=True)
    78             nbnotices = qs.count()
    78             nbnotices = qs.count()
    79             ns = qs.order_by('?')[:npp]
    79             ns = qs.order_by('?')[:npp]
    80         
    80         
    81         context["lang"] = lang
    81         context["lang"] = lang
    82         context["rescount"] = nbnotices
    82         context["count"] = nbnotices
    83         context["notices"] = get_notices(ns, lang)
    83         context["notices"] = get_notices(ns, lang)
    84         context["queryterms"] = querystr
    84         context["searchterm"] = querystr
    85         
    85         
    86         wpp = 50
    86         wpp = 50
    87         if show_tagcloud:
    87         if show_tagcloud:
    88             ts = Term.objects.filter(dbpedia_fields__language_code=lang).order_by('-nb_notice')[:wpp]
    88             ts = Term.objects.filter(dbpedia_fields__language_code=lang).order_by('-nb_notice')[:wpp]
    89             words = [{
    89             words = [{
   106         
   106         
   107         context['duration'] = "%.2f"%(time.time() - starttime)
   107         context['duration'] = "%.2f"%(time.time() - starttime)
   108         
   108         
   109         return self.render_to_response(context)
   109         return self.render_to_response(context)
   110 
   110 
   111 class NoticeView(View, TemplateResponseMixin):
   111 class NoticeView(DetailView):
   112     
   112     
       
   113     model = Notice
   113     template_name = "jocondelab/front_notice.html"
   114     template_name = "jocondelab/front_notice.html"
   114     
   115     
   115     def get(self, request, notice_id):
   116     def get_context_data(self, **kwargs):
   116         
   117         
   117         context = {}
   118         context = super(NoticeView, self).get_context_data(**kwargs)
   118         lang = request.GET.get('lang',request.LANGUAGE_CODE)
   119         lang = self.request.GET.get('lang',self.request.LANGUAGE_CODE)[:2]
   119         
       
   120         notice = Notice.objects.get(id=notice_id)
       
   121         
   120         
   122         context["lang"] = lang
   121         context["lang"] = lang
   123         context["notice"] = notice
   122         context["images"] = [settings.JOCONDE_IMG_BASE_URL + i.url for i in self.object.images.exclude(relative_url__endswith='v.jpg')]
   124         context["title"] = notice.titr if notice.titr else notice.deno
   123         context["title"] = self.object.titr if self.object.titr else self.object.deno
   125         context["images"] = [i.url for i in notice.images.exclude(relative_url__endswith='v.jpg')]
   124         
   126         terms = []
   125         terms = []
   127         for nt in notice.noticeterm_set.select_related('dbpedia_fields').all():
   126         for nt in self.object.noticeterm_set.select_related('term__dbpedia_fields').all():
   128             translated_term = nt.term.dbpedia_fields.filter(language_code = lang)
   127             translated_term = nt.term.dbpedia_fields.filter(language_code = lang)
   129             termdict = {
   128             termdict = {
   130                 "label": nt.term.label,
   129                 "label": nt.term.label,
   131                 "thesaurus": nt.term.thesaurus.label,
   130                 "thesaurus": nt.term.thesaurus.label,
   132                 "dbpedia_uri": nt.term.dbpedia_uri,
   131                 "dbpedia_uri": nt.term.dbpedia_uri,
   140                 termsbythesaurus[term["thesaurus"]] = {
   139                 termsbythesaurus[term["thesaurus"]] = {
   141                    "translated": [],
   140                    "translated": [],
   142                    "untranslated": []
   141                    "untranslated": []
   143                    }
   142                    }
   144             termsbythesaurus[term["thesaurus"]]["translated" if term["translated"] else "untranslated"].append(term)
   143             termsbythesaurus[term["thesaurus"]]["translated" if term["translated"] else "untranslated"].append(term)
       
   144          
       
   145         context["terms_by_thesaurus"] = termsbythesaurus
   145         
   146         
   146         context["terms_by_thesaurus"] = termsbythesaurus
   147         return context
   147         context['JOCONDE_IMG_BASE_URL'] = settings.JOCONDE_IMG_BASE_URL
       
   148         
       
   149         return self.render_to_response(context)
       
   150 
   148 
   151     
   149