| author | veltr |
| Mon, 16 Sep 2013 10:38:08 +0200 | |
| changeset 108 | c0d227e8b0c4 |
| parent 106 | 219e27e5e26d |
| child 110 | 597fa9d09973 |
| permissions | -rw-r--r-- |
| 96 | 1 |
# -*- coding: utf-8 -*- |
2 |
''' |
|
3 |
Created on Aug 20, 2013 |
|
4 |
||
5 |
@author: rvelt |
|
6 |
''' |
|
7 |
||
| 101 | 8 |
from jocondelab.models import DbpediaYears, DbpediaGeo, DbpediaFields, ContributedTerm, ContributedFields, Contribution |
9 |
from core.models import Notice, Term, Thesaurus |
|
| 96 | 10 |
import django.utils.simplejson as json |
| 106 | 11 |
from django.core.paginator import Paginator |
| 96 | 12 |
from django.http import HttpResponse |
| 97 | 13 |
from django.template.response import TemplateResponse |
|
100
25636bb50756
Grouped geographical locations and years by dbpedia uri
veltr
parents:
98
diff
changeset
|
14 |
from django.db.models import Sum |
| 96 | 15 |
from django.conf import settings |
16 |
||
| 104 | 17 |
def terms(request): |
18 |
||
19 |
lang = request.GET.get('lang', request.LANGUAGE_CODE)[:2] |
|
20 |
q = request.GET.get('term', None) |
|
21 |
count = request.GET.get('count', 20) |
|
| 108 | 22 |
qs = DbpediaFields.objects.filter(language_code=lang,label__icontains=q).values('dbpedia_uri','label').distinct().order_by('label')[:count] |
| 104 | 23 |
res = [{"dbpedia_uri": r['dbpedia_uri'], "label": r['label']} for r in qs] |
24 |
return HttpResponse(content=json.dumps(res), mimetype='application/json') |
|
25 |
||
| 96 | 26 |
def years(request): |
27 |
||
|
100
25636bb50756
Grouped geographical locations and years by dbpedia uri
veltr
parents:
98
diff
changeset
|
28 |
lang = request.GET.get('lang', request.LANGUAGE_CODE)[:2] |
| 96 | 29 |
from_year = request.GET.get('from_year', None) |
30 |
to_year = request.GET.get('to_year', None) |
|
31 |
count = request.GET.get('count', 20) |
|
32 |
||
|
100
25636bb50756
Grouped geographical locations and years by dbpedia uri
veltr
parents:
98
diff
changeset
|
33 |
qs = DbpediaYears.objects.filter(term__dbpedia_fields__language_code=lang) |
| 96 | 34 |
if to_year: |
| 98 | 35 |
qs = qs.filter(start_year__lte=to_year) |
| 96 | 36 |
if from_year: |
| 98 | 37 |
qs = qs.filter(end_year__gte=from_year) |
|
100
25636bb50756
Grouped geographical locations and years by dbpedia uri
veltr
parents:
98
diff
changeset
|
38 |
qs = qs.values('start_year','end_year','term__dbpedia_fields__label','term__dbpedia_uri') |
|
25636bb50756
Grouped geographical locations and years by dbpedia uri
veltr
parents:
98
diff
changeset
|
39 |
qs = qs.annotate(sum_notices=Sum('term__nb_notice')).order_by('-sum_notices') |
| 96 | 40 |
|
41 |
qs = qs[:count] |
|
42 |
||
43 |
results = [{ |
|
|
100
25636bb50756
Grouped geographical locations and years by dbpedia uri
veltr
parents:
98
diff
changeset
|
44 |
"start_year": y["start_year"], |
|
25636bb50756
Grouped geographical locations and years by dbpedia uri
veltr
parents:
98
diff
changeset
|
45 |
"end_year": y["end_year"], |
|
25636bb50756
Grouped geographical locations and years by dbpedia uri
veltr
parents:
98
diff
changeset
|
46 |
"label": y["term__dbpedia_fields__label"], |
|
25636bb50756
Grouped geographical locations and years by dbpedia uri
veltr
parents:
98
diff
changeset
|
47 |
"sum_notices": y["sum_notices"], |
|
25636bb50756
Grouped geographical locations and years by dbpedia uri
veltr
parents:
98
diff
changeset
|
48 |
"dbpedia_uri": y["term__dbpedia_uri"] |
| 96 | 49 |
} for y in qs] |
50 |
||
51 |
return HttpResponse(content=json.dumps(results), mimetype='application/json') |
|
52 |
||
53 |
def geo_coords(request): |
|
54 |
||
|
100
25636bb50756
Grouped geographical locations and years by dbpedia uri
veltr
parents:
98
diff
changeset
|
55 |
lang = request.GET.get('lang', request.LANGUAGE_CODE)[:2] |
| 96 | 56 |
min_lat = request.GET.get('min_lat', None) |
57 |
max_lat = request.GET.get('max_lat', None) |
|
58 |
min_lng = request.GET.get('min_lng', None) |
|
59 |
max_lng = request.GET.get('max_lng', None) |
|
60 |
count = request.GET.get('count', 20) |
|
61 |
||
|
100
25636bb50756
Grouped geographical locations and years by dbpedia uri
veltr
parents:
98
diff
changeset
|
62 |
qs = DbpediaGeo.objects.filter(term__dbpedia_fields__language_code=lang) |
| 96 | 63 |
|
64 |
if min_lat: |
|
65 |
qs = qs.filter(latitude__gt=min_lat) |
|
66 |
if max_lat: |
|
| 97 | 67 |
qs = qs.filter(latitude__lt=max_lat) |
| 96 | 68 |
if min_lng: |
69 |
qs = qs.filter(longitude__gt=min_lng) |
|
70 |
if max_lng: |
|
| 97 | 71 |
qs = qs.filter(longitude__lt=max_lng) |
| 96 | 72 |
|
|
100
25636bb50756
Grouped geographical locations and years by dbpedia uri
veltr
parents:
98
diff
changeset
|
73 |
qs = qs.values('latitude','longitude','term__dbpedia_fields__label','term__dbpedia_uri') |
|
25636bb50756
Grouped geographical locations and years by dbpedia uri
veltr
parents:
98
diff
changeset
|
74 |
qs = qs.annotate(sum_notices=Sum('term__nb_notice')).order_by('-sum_notices') |
|
25636bb50756
Grouped geographical locations and years by dbpedia uri
veltr
parents:
98
diff
changeset
|
75 |
|
| 96 | 76 |
qs = qs[:count] |
77 |
||
78 |
results = [{ |
|
|
100
25636bb50756
Grouped geographical locations and years by dbpedia uri
veltr
parents:
98
diff
changeset
|
79 |
"latitude": y["latitude"], |
|
25636bb50756
Grouped geographical locations and years by dbpedia uri
veltr
parents:
98
diff
changeset
|
80 |
"longitude": y["longitude"], |
|
25636bb50756
Grouped geographical locations and years by dbpedia uri
veltr
parents:
98
diff
changeset
|
81 |
"label": y["term__dbpedia_fields__label"], |
|
25636bb50756
Grouped geographical locations and years by dbpedia uri
veltr
parents:
98
diff
changeset
|
82 |
"sum_notices": y["sum_notices"], |
|
25636bb50756
Grouped geographical locations and years by dbpedia uri
veltr
parents:
98
diff
changeset
|
83 |
"dbpedia_uri": y["term__dbpedia_uri"] |
| 96 | 84 |
} for y in qs] |
85 |
||
86 |
return HttpResponse(content=json.dumps(results), mimetype='application/json') |
|
87 |
||
| 97 | 88 |
def geo_search(request): |
89 |
||
|
100
25636bb50756
Grouped geographical locations and years by dbpedia uri
veltr
parents:
98
diff
changeset
|
90 |
lang = request.GET.get('lang', request.LANGUAGE_CODE)[:2] |
| 97 | 91 |
q = request.GET.get('term', None) |
92 |
count = request.GET.get('count', 20) |
|
93 |
||
| 108 | 94 |
qs = DbpediaGeo.objects.filter(term__dbpedia_fields__language_code=lang, term__dbpedia_fields__label__icontains=q) |
|
100
25636bb50756
Grouped geographical locations and years by dbpedia uri
veltr
parents:
98
diff
changeset
|
95 |
qs = qs.values('latitude','longitude','term__dbpedia_fields__label','term__dbpedia_uri') |
|
25636bb50756
Grouped geographical locations and years by dbpedia uri
veltr
parents:
98
diff
changeset
|
96 |
qs = qs.annotate(sum_notices=Sum('term__nb_notice')).order_by('-sum_notices')[:count] |
| 97 | 97 |
|
98 |
results = [{ |
|
|
100
25636bb50756
Grouped geographical locations and years by dbpedia uri
veltr
parents:
98
diff
changeset
|
99 |
"latitude": y["latitude"], |
|
25636bb50756
Grouped geographical locations and years by dbpedia uri
veltr
parents:
98
diff
changeset
|
100 |
"longitude": y["longitude"], |
|
25636bb50756
Grouped geographical locations and years by dbpedia uri
veltr
parents:
98
diff
changeset
|
101 |
"label": y["term__dbpedia_fields__label"], |
|
25636bb50756
Grouped geographical locations and years by dbpedia uri
veltr
parents:
98
diff
changeset
|
102 |
"sum_notices": y["sum_notices"], |
|
25636bb50756
Grouped geographical locations and years by dbpedia uri
veltr
parents:
98
diff
changeset
|
103 |
"dbpedia_uri": y["term__dbpedia_uri"] |
|
25636bb50756
Grouped geographical locations and years by dbpedia uri
veltr
parents:
98
diff
changeset
|
104 |
} for y in qs] |
|
25636bb50756
Grouped geographical locations and years by dbpedia uri
veltr
parents:
98
diff
changeset
|
105 |
results.sort(key=lambda y: y["label"]) |
| 96 | 106 |
|
107 |
return HttpResponse(content=json.dumps(results), mimetype='application/json') |
|
| 101 | 108 |
|
109 |
def contribute(request): |
|
110 |
||
111 |
notice_id = request.POST.get('notice_id', None) |
|
112 |
dbpedia_language = request.POST.get('dbpedia_language', None) |
|
113 |
dbpedia_uri = request.POST.get('dbpedia_uri', None) |
|
114 |
thesaurus_label = request.POST.get('thesaurus_label', None) |
|
115 |
translations = json.loads(request.POST.get('translations', '[]')) |
|
116 |
||
| 108 | 117 |
thobj = Thesaurus.objects.get(label=thesaurus_label) if thesaurus_label else None |
| 101 | 118 |
notobj = Notice.objects.get(id=notice_id) |
119 |
termobj, created = ContributedTerm.objects.get_or_create(dbpedia_uri=dbpedia_uri, dbpedia_language=dbpedia_language) |
|
120 |
controbj, created = Contribution.objects.get_or_create(term=termobj, thesaurus=thobj, notice=notobj, defaults={'contribution_count': 1}) |
|
121 |
if not created: |
|
122 |
controbj.contribution_count += 1 |
|
123 |
controbj.save() |
|
124 |
for t in translations: |
|
125 |
fieldsobj, created = ContributedFields.objects.get_or_create(term=termobj, dbpedia_uri=dbpedia_uri, language_code=t['language'], defaults={'abstract': t['abstract'], 'label': t['label'], 'thumbnail': t['thumbnail']}) |
|
126 |
if not created: |
|
127 |
fieldsobj.abstract = t['abstract'] |
|
128 |
fieldsobj.thumbnail = t['thumbnail'] |
|
129 |
fieldsobj.label = t['label'] |
|
130 |
fieldsobj.save() |
|
131 |
||
| 108 | 132 |
return HttpResponse(content=json.dumps({ "contribution_id": controbj.id, "contribution_count": controbj.contribution_count }), mimetype='application/json') |
133 |
||
134 |
def contribution_vote(request, grade): |
|
135 |
||
136 |
contribution_id = request.POST.get('contribution_id', None) |
|
137 |
controbj = ContributedTerm.objects.get(id=contribution_id) |
|
138 |
controbj.contribution_count += grade |
|
139 |
controbj.save() |
|
140 |
||
141 |
return HttpResponse(content=json.dumps({ "contribution_id": controbj.id, "contribution_count": controbj.contribution_count }), mimetype='application/json') |
|
142 |
||
143 |
def upvote(request): |
|
144 |
||
145 |
return contribution_vote(1) |
|
146 |
||
147 |
def downvote(request): |
|
148 |
||
149 |
return contribution_vote(-1) |
|
| 96 | 150 |