src/core/views.py
author ymh <ymh.work@gmail.com>
Thu, 18 Jul 2013 10:39:26 +0200
changeset 67 5d9223bb3aab
parent 0 4095911a7830
child 334 169b7cfd1f58
permissions -rw-r--r--
Add other wikipedia. change joconde notice preview (as in bug #17508)
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
# Create your views here.
67
5d9223bb3aab Add other wikipedia.
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
     2
from core import settings
5d9223bb3aab Add other wikipedia.
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
     3
from django.http.response import HttpResponseNotFound, HttpResponse,\
5d9223bb3aab Add other wikipedia.
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
     4
    HttpResponseBadRequest
5d9223bb3aab Add other wikipedia.
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
     5
from django.views.generic import View
5d9223bb3aab Add other wikipedia.
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
     6
import requests
5d9223bb3aab Add other wikipedia.
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
     7
from requests.exceptions import HTTPError
5d9223bb3aab Add other wikipedia.
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
     8
5d9223bb3aab Add other wikipedia.
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
     9
class SparqlEndpointProxy(View):
5d9223bb3aab Add other wikipedia.
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    10
    
5d9223bb3aab Add other wikipedia.
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    11
    def get(self, request, wp_lang):
5d9223bb3aab Add other wikipedia.
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    12
        params = request.GET.dict()
5d9223bb3aab Add other wikipedia.
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    13
        if not wp_lang or wp_lang not in settings.WIKIPEDIA_URLS:
5d9223bb3aab Add other wikipedia.
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    14
                return HttpResponseBadRequest("The wp_lang parameter is compulsory and must be in %s" % repr(settings.WIKIPEDIA_URLS.keys()))  # @UndefinedVariable
5d9223bb3aab Add other wikipedia.
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    15
5d9223bb3aab Add other wikipedia.
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    16
        url = settings.WIKIPEDIA_URLS.get(wp_lang, {}).get('dbpedia_sparql_url', None)  # @UndefinedVariable
5d9223bb3aab Add other wikipedia.
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    17
        if not url or not url.startswith("http"):
5d9223bb3aab Add other wikipedia.
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    18
            return HttpResponseNotFound("No or bad url for %s: %s" % (wp_lang,repr(url)))
5d9223bb3aab Add other wikipedia.
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    19
        
5d9223bb3aab Add other wikipedia.
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    20
        try:
5d9223bb3aab Add other wikipedia.
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    21
            resp = requests.get(url, params=params)
5d9223bb3aab Add other wikipedia.
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    22
            return HttpResponse(content=resp.text, content_type=resp.headers.get('content-type'))
5d9223bb3aab Add other wikipedia.
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    23
        except HTTPError as e:
5d9223bb3aab Add other wikipedia.
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    24
            return HttpResponse(unicode(e), status=e.status_code)
5d9223bb3aab Add other wikipedia.
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    25
        
5d9223bb3aab Add other wikipedia.
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    26
        
5d9223bb3aab Add other wikipedia.
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    27