web/hdalab/views/ajax.py
author veltr
Wed, 22 Feb 2012 18:55:35 +0100
changeset 122 fde8335a037c
parent 119 e3ebe3545f72
child 127 8642f1fb6499
permissions -rw-r--r--
Added Geographic Inclusion in the Django version (server only)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
119
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
# -*- coding: utf-8 -*-
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
'''
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
Created on Jan 31, 2012
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
     4
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
     5
@author: ymh
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
     6
'''
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
from django.conf import settings
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
     8
from django.db import connection
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
     9
from django.db.models import Q, Count, Sum
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    10
from django.http import HttpResponse
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    11
from hdabo.models import Tag, Datasheet, TaggedSheet
122
fde8335a037c Added Geographic Inclusion in the Django version (server only)
veltr
parents: 119
diff changeset
    12
from hdalab.models import TagLinks, HdaSession, Country, GeoInclusion, TagYears
119
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    13
import django.utils.simplejson as json
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    14
import hmac
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    15
import uuid
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
def taginfo(request):
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
    label = request.GET.get('label', None)
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
    
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    20
    resobj = {'requested_label' : label}
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    21
        
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
    resobj["content_count"] = Datasheet.objects.filter(taggedsheet__tag__label__iexact = label).distinct().count()
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    23
    
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    24
    res = Tag.objects.select_related('dbpedia_fields').filter(label__iexact = label).order_by('-dbpedia_uri')[0:1]
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    25
    if len(res) == 1:
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    26
        restag = res.get()
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    27
        resobj["dbpedia_uri"] = restag.dbpedia_uri
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    28
        if resobj["dbpedia_uri"] is not None and restag.dbpedia_fields is not None:
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    29
            dbfield = restag.dbpedia_fields
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    30
            resobj["abstract"] = dbfield.abstract
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    31
            resobj["dbpedia_label"] = dbfield.label
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    32
            resobj["thumbnail"] = dbfield.thumbnail
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    33
    res = Tag.objects.filter(label__iexact = label).order_by('-wikipedia_url')[0:1]
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    34
    if len(res) == 1:
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    35
        resobj["wikipedia_url"] = res.get().wikipedia_url
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    36
        
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    37
    resobj["links"] = [{'subject':tl.subject.label, 'object':tl.object.label} for tl in TagLinks.objects.select_related().filter(Q(subject__label__iexact = label) | Q(object__label__iexact = label))]
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    38
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    39
    return HttpResponse(content=json.dumps(resobj), mimetype='application/json')
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    40
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    41
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    42
def sessioninfo(request):
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    43
    
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    44
    data = json.loads(request.GET.get('data', "{}"))
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    45
    write = False
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    46
    
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    47
    if 'sessionid' in request.GET:
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    48
        request.session['sessionid'] = request.GET['sessionid']
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    49
    if 'sessionkey' in request.GET:
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    50
        request.session['sessionkey'] = request.GET['sessionkey']
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    51
        
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    52
    if 'sessionid' in request.session:
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    53
        sessionid = request.session['sessionid']
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    54
        
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    55
        if HdaSession.objects.filter(sessionid=sessionid).count() == 1:
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    56
            sessionkey = request.session.get('sessionkey',None)
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    57
            hm = hmac.new(settings.SECRET_KEY, sessionid)
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    58
            if hm.hexdigest() == sessionkey:
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    59
                write = True            
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    60
        else:
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    61
            del request.session['sessionid']
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    62
        
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    63
    if 'sessionid' not in request.session:
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    64
        sessionid = unicode(uuid.uuid1())
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    65
        HdaSession.objects.create(sessionid=sessionid, data=json.dumps({}))
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    66
        write = True
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    67
        request.session['sessionid'] = sessionid
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    68
        request.session['sessionkey'] = hmac.new(settings.SECRET_KEY, sessionid).hexdigest()
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    69
        
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    70
    if write and data:
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    71
        HdaSession.objects.filter(sessionid=sessionid).update(data=json.dumps(data))
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    72
    else:
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    73
        data = HdaSession.objects.get(sessionid=sessionid).data
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    74
        data = json.loads(data) if data else {}           
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    75
         
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    76
    resobj = {'data': data, "write_allowed" : write, "sessionid": sessionid }
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    77
    if write:
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    78
        resobj['sessionkey'] = request.session['sessionkey']
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    79
        
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    80
    return HttpResponse(content=json.dumps(resobj), mimetype='application/json')
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    81
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    82
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    83
def tagsearch(request):
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    84
    
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    85
    q = request.GET.get('term',None)
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    86
    qs = Tag.objects.filter(label__icontains = q) if q else Tag.objects.all()        
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    87
    res = qs.annotate(nb=Count('datasheet')).order_by('-nb')[:20]
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    88
    
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    89
    return HttpResponse(content=json.dumps([{'label':t.label,'nb':t.nb} for t in res]), mimetype='application/json')
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    90
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    91
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    92
def filter(request):
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    93
    periode = request.GET.get('period',None)
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    94
    label = request.GET.get('label', None)
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    95
    contentlist = request.GET.get('contentlist', None)
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    96
    max_tag_order = request.GET.get('mto', 12)
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    97
    content_count = request.GET.get('contentcount', 12)
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    98
    tag_count = request.GET.get('tagcount', 30)
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    99
    cont_count = 0;
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   100
    contenus = {}
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   101
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   102
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   103
    if label or periode or contentlist :
122
fde8335a037c Added Geographic Inclusion in the Django version (server only)
veltr
parents: 119
diff changeset
   104
119
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   105
        qs = Datasheet.objects.filter(validated=True)
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   106
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   107
        globalids = []
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   108
        
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   109
        if periode:
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   110
            years = periode.split(",")
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   111
            start_year = int(years[0])
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   112
            end_year = int(years[0:2][-1])
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   113
            delta = max(1, (end_year-start_year)/2)
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   114
            minstart = start_year - delta;
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   115
            maxend = end_year + delta;
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   116
            
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   117
            tagqs = Tag.objects.filter(years__end_year__gte = start_year, 
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   118
                                       years__start_year__lte = end_year,
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   119
                                       years__end_year__lte = maxend,
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   120
                                       years__start_year__gte = minstart)
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   121
            
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   122
            globalids += [t.id for t in tagqs]
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   123
            
122
fde8335a037c Added Geographic Inclusion in the Django version (server only)
veltr
parents: 119
diff changeset
   124
            qs = qs.filter(taggedsheet__tag__in = tagqs)
119
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   125
            
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   126
        if label:
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   127
            for txtlbl in label.split(","):
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   128
                taglblqs = Tag.objects.filter(label__iexact = txtlbl)
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   129
                globalids += [t.id for t in taglblqs if t.id not in globalids]
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   130
                qs = qs.filter(taggedsheet__tag__in = taglblqs)
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   131
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   132
        if label or periode:
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   133
            qs = qs.filter(taggedsheet__order__lte = max_tag_order)
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   134
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   135
        if contentlist:
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   136
            qs = qs.filter(id__in = contentlist.split(","))
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   137
        
122
fde8335a037c Added Geographic Inclusion in the Django version (server only)
veltr
parents: 119
diff changeset
   138
#        if contentlist is None:
fde8335a037c Added Geographic Inclusion in the Django version (server only)
veltr
parents: 119
diff changeset
   139
#            qs = qs.order_by('?')
119
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   140
        
122
fde8335a037c Added Geographic Inclusion in the Django version (server only)
veltr
parents: 119
diff changeset
   141
        qs = qs.distinct()
119
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   142
        
122
fde8335a037c Added Geographic Inclusion in the Django version (server only)
veltr
parents: 119
diff changeset
   143
        contenus = dict([(content.id, {'score' : 0, 'tags' : [], 'id':content.id, 'title': content.title, 'description': content.description, 'url': content.url}) for content in qs])
119
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   144
122
fde8335a037c Added Geographic Inclusion in the Django version (server only)
veltr
parents: 119
diff changeset
   145
        #countries = dict([ (cc.label, {'isocode':cc.isocode, 'score':0}) for cc in CountryCode.objects.all() ])
fde8335a037c Added Geographic Inclusion in the Django version (server only)
veltr
parents: 119
diff changeset
   146
        #disciplines = dict([ (d.label, {'label':d.label, 'score':0}) for d in Tag.objects.filter(category__label = "Discipline artistique")])
fde8335a037c Added Geographic Inclusion in the Django version (server only)
veltr
parents: 119
diff changeset
   147
        
fde8335a037c Added Geographic Inclusion in the Django version (server only)
veltr
parents: 119
diff changeset
   148
        countries = {}
fde8335a037c Added Geographic Inclusion in the Django version (server only)
veltr
parents: 119
diff changeset
   149
        disciplines = {}
119
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   150
        
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   151
        tags = {}
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   152
        
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   153
        contentids = contenus.keys()
122
fde8335a037c Added Geographic Inclusion in the Django version (server only)
veltr
parents: 119
diff changeset
   154
        cont_count = len(contentids)
fde8335a037c Added Geographic Inclusion in the Django version (server only)
veltr
parents: 119
diff changeset
   155
        
fde8335a037c Added Geographic Inclusion in the Django version (server only)
veltr
parents: 119
diff changeset
   156
        qs = TaggedSheet.objects.select_related('tag','tag__category','tag__locatedin__country').filter(datasheet__in = contentids, order__lte = max_tag_order).order_by('order')
119
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   157
        for ts in qs:
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   158
            match_tag = ts.tag.id in globalids
122
fde8335a037c Added Geographic Inclusion in the Django version (server only)
veltr
parents: 119
diff changeset
   159
            contenutags = {'id': ts.tag.id, 'label':ts.tag.label, 'order':ts.order, 'match': match_tag }
fde8335a037c Added Geographic Inclusion in the Django version (server only)
veltr
parents: 119
diff changeset
   160
            
fde8335a037c Added Geographic Inclusion in the Django version (server only)
veltr
parents: 119
diff changeset
   161
#            if ts.tag.category is not None:
fde8335a037c Added Geographic Inclusion in the Django version (server only)
veltr
parents: 119
diff changeset
   162
#                contenutags['category'] = ts.tag.category.label
fde8335a037c Added Geographic Inclusion in the Django version (server only)
veltr
parents: 119
diff changeset
   163
            
fde8335a037c Added Geographic Inclusion in the Django version (server only)
veltr
parents: 119
diff changeset
   164
            contenus[ts.datasheet.id]['tags'].append(contenutags)
fde8335a037c Added Geographic Inclusion in the Django version (server only)
veltr
parents: 119
diff changeset
   165
                
119
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   166
            tagscore = 2*max_tag_order - ts.order
122
fde8335a037c Added Geographic Inclusion in the Django version (server only)
veltr
parents: 119
diff changeset
   167
            if ts.tag.category is not None and ts.tag.category.label not in [u'Discipline artistique', u'Datation', u'Localisation']:
fde8335a037c Added Geographic Inclusion in the Django version (server only)
veltr
parents: 119
diff changeset
   168
                if ts.tag.id not in tags:
fde8335a037c Added Geographic Inclusion in the Django version (server only)
veltr
parents: 119
diff changeset
   169
                    tags[ts.tag.id] = {'id':ts.tag.id,'label':ts.tag.label, 'score':0}
fde8335a037c Added Geographic Inclusion in the Django version (server only)
veltr
parents: 119
diff changeset
   170
                tags[ts.tag.id]['score'] += 1
fde8335a037c Added Geographic Inclusion in the Django version (server only)
veltr
parents: 119
diff changeset
   171
                if match_tag:
fde8335a037c Added Geographic Inclusion in the Django version (server only)
veltr
parents: 119
diff changeset
   172
                    tags[ts.tag.id]['match'] = True
fde8335a037c Added Geographic Inclusion in the Django version (server only)
veltr
parents: 119
diff changeset
   173
                
119
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   174
            if match_tag:
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   175
                contenus[ts.datasheet.id]['score'] += tagscore
122
fde8335a037c Added Geographic Inclusion in the Django version (server only)
veltr
parents: 119
diff changeset
   176
                
fde8335a037c Added Geographic Inclusion in the Django version (server only)
veltr
parents: 119
diff changeset
   177
            if ts.tag.category is not None and ts.tag.category.label == u'Discipline artistique':
fde8335a037c Added Geographic Inclusion in the Django version (server only)
veltr
parents: 119
diff changeset
   178
                if ts.tag.label not in disciplines:
fde8335a037c Added Geographic Inclusion in the Django version (server only)
veltr
parents: 119
diff changeset
   179
                    disciplines[ts.tag.label] = {'label':ts.tag.label, 'score':0}
fde8335a037c Added Geographic Inclusion in the Django version (server only)
veltr
parents: 119
diff changeset
   180
                disciplines[ts.tag.label]['score'] += 1
fde8335a037c Added Geographic Inclusion in the Django version (server only)
veltr
parents: 119
diff changeset
   181
                
fde8335a037c Added Geographic Inclusion in the Django version (server only)
veltr
parents: 119
diff changeset
   182
            if ts.tag.locatedin is not None:
fde8335a037c Added Geographic Inclusion in the Django version (server only)
veltr
parents: 119
diff changeset
   183
                country_id = ts.tag.locatedin.country.id
fde8335a037c Added Geographic Inclusion in the Django version (server only)
veltr
parents: 119
diff changeset
   184
                if country_id not in countries:
fde8335a037c Added Geographic Inclusion in the Django version (server only)
veltr
parents: 119
diff changeset
   185
                    countries[country_id] = {'id':country_id, 'dbpedia_uri':ts.tag.locatedin.country.dbpedia_uri, 'score': 0}
fde8335a037c Added Geographic Inclusion in the Django version (server only)
veltr
parents: 119
diff changeset
   186
                countries[country_id]['score'] += 1
fde8335a037c Added Geographic Inclusion in the Django version (server only)
veltr
parents: 119
diff changeset
   187
            
fde8335a037c Added Geographic Inclusion in the Django version (server only)
veltr
parents: 119
diff changeset
   188
        if contentlist is None:
fde8335a037c Added Geographic Inclusion in the Django version (server only)
veltr
parents: 119
diff changeset
   189
            contenus = sorted(contenus.values(),key=lambda e: -e['score'])[0:content_count]
fde8335a037c Added Geographic Inclusion in the Django version (server only)
veltr
parents: 119
diff changeset
   190
            contenus = [contenu for contenu in contenus if contenu['score']]
fde8335a037c Added Geographic Inclusion in the Django version (server only)
veltr
parents: 119
diff changeset
   191
            
119
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   192
        countries = countries.values()
122
fde8335a037c Added Geographic Inclusion in the Django version (server only)
veltr
parents: 119
diff changeset
   193
        tags = sorted(tags.values(), key=lambda e: -e['score'])[0:tag_count]
fde8335a037c Added Geographic Inclusion in the Django version (server only)
veltr
parents: 119
diff changeset
   194
        disciplines = sorted(disciplines.values(), key=lambda e: -e['score'])[0:10]
119
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   195
        years = {}
122
fde8335a037c Added Geographic Inclusion in the Django version (server only)
veltr
parents: 119
diff changeset
   196
        
119
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   197
        if contentids:
122
fde8335a037c Added Geographic Inclusion in the Django version (server only)
veltr
parents: 119
diff changeset
   198
            qs = TagYears.objects.filter(tag__taggedsheet__datasheet__in = contentids).annotate(nb=Count('tag__taggedsheet'))
119
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   199
            for ty in qs:
122
fde8335a037c Added Geographic Inclusion in the Django version (server only)
veltr
parents: 119
diff changeset
   200
                for year in range(ty.start_year, ty.end_year):
fde8335a037c Added Geographic Inclusion in the Django version (server only)
veltr
parents: 119
diff changeset
   201
                    years[year] = ty.nb + years[year] if year in years else 0
119
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   202
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   203
    else:
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   204
        
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   205
        for ds in Datasheet.objects.order_by("?")[:content_count]:
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   206
            contenus[ds.id] = {'id':ds.id, 'title':ds.title, 'description':ds.description, 'url':ds.url, 'tags':[]}
122
fde8335a037c Added Geographic Inclusion in the Django version (server only)
veltr
parents: 119
diff changeset
   207
            
119
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   208
        cont_count = Datasheet.objects.count()
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   209
        
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   210
        qs = TaggedSheet.objects.select_related('tag','datasheet').filter(datasheet__id__in = contenus.keys(), order__lte = max_tag_order).order_by("order").only('order','tag__label','tag__id','datasheet__id')
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   211
        for ts in qs:
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   212
            contenus[ts.datasheet.id]['tags'].append({'id': ts.tag.id, 'label':ts.tag.label, 'order':ts.order})
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   213
        contenus = contenus.values()
122
fde8335a037c Added Geographic Inclusion in the Django version (server only)
veltr
parents: 119
diff changeset
   214
        
fde8335a037c Added Geographic Inclusion in the Django version (server only)
veltr
parents: 119
diff changeset
   215
        qs = Tag.objects.exclude(category__label = u"Localisation").exclude(category__label = u"Datation").exclude(category__label = u"Discipline artistice").annotate(nb=Count('datasheet')).order_by('-nb')[:tag_count]
fde8335a037c Added Geographic Inclusion in the Django version (server only)
veltr
parents: 119
diff changeset
   216
        tags = [{'id': tag.id, 'label': tag.label, 'score': tag.nb} for tag in qs]
fde8335a037c Added Geographic Inclusion in the Django version (server only)
veltr
parents: 119
diff changeset
   217
            
fde8335a037c Added Geographic Inclusion in the Django version (server only)
veltr
parents: 119
diff changeset
   218
        qs = Country.objects.annotate(nb=Count('includes__tag__taggedsheet')).order_by('-nb')
fde8335a037c Added Geographic Inclusion in the Django version (server only)
veltr
parents: 119
diff changeset
   219
        countries = [{'id': country.id, 'dbpedia_uri': country.dbpedia_uri, 'score': country.nb} for country in qs]
119
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   220
122
fde8335a037c Added Geographic Inclusion in the Django version (server only)
veltr
parents: 119
diff changeset
   221
        qs = Tag.objects.annotate(nb=Count('taggedsheet')).filter(category = 5).order_by('-nb')[:10]
fde8335a037c Added Geographic Inclusion in the Django version (server only)
veltr
parents: 119
diff changeset
   222
        disciplines = [{'label':tag.label,'score':tag.nb} for tag in qs]
119
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   223
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   224
        years = {}
122
fde8335a037c Added Geographic Inclusion in the Django version (server only)
veltr
parents: 119
diff changeset
   225
        qs = TagYears.objects.annotate(nb=Count('tag__taggedsheet'))
119
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   226
        for ty in qs:
122
fde8335a037c Added Geographic Inclusion in the Django version (server only)
veltr
parents: 119
diff changeset
   227
            for year in range(ty.start_year, ty.end_year):
fde8335a037c Added Geographic Inclusion in the Django version (server only)
veltr
parents: 119
diff changeset
   228
                years[year] = ty.nb + years[year] if year in years else 0
119
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   229
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   230
    yearchange = []
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   231
    for year in sorted(years.keys()):
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   232
        score = years[year]
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   233
        if year < 2011:
122
fde8335a037c Added Geographic Inclusion in the Django version (server only)
veltr
parents: 119
diff changeset
   234
            if (year-1 not in years and score != 0) or (year-1 in years and years[year-1] != score):
119
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   235
                yearchange.append({'year': year, 'score': score})
122
fde8335a037c Added Geographic Inclusion in the Django version (server only)
veltr
parents: 119
diff changeset
   236
            if year+1 not in years and year != -1 and score != 0:
119
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   237
                yearchange.append({'year': year+1, 'score': 0})
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   238
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   239
    output = {'count': cont_count, 'contents': contenus, 'tags':tags, 'sparkline':yearchange, 'countries':countries, 'disciplines':disciplines}
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   240
    
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   241
    return HttpResponse(content=json.dumps(output), mimetype='application/json')