src/p4l/views.py
author ymh <ymh.work@gmail.com>
Sat, 28 Sep 2013 02:55:26 +0200
changeset 120 6ec0300b626e
parent 119 ece69ca3ac24
child 126 a345f1a67bf1
permissions -rw-r--r--
- update django - clear search input - footer

# -*- coding: utf-8 -*-
'''
Created on Aug 27, 2013

@author: tc
'''
import json
import logging
import time

from django.conf import settings
from django.shortcuts import redirect, get_object_or_404
from django.views.generic import DetailView, View
from rest_framework.renderers import UnicodeJSONRenderer

from p4l.api.serializers import RecordSerializer
from p4l.models import Record
from p4l.utils import get_labels_for_uris


logger = logging.getLogger(__name__)

QUERY_FIELDS_TRANSLATION = {
    'url': 'dataurl',
    'filter': 'dataquery',
    'root': 'datarootquery',
    'childs': 'datachildsquery',
    'child-count': 'datachildcountquery'                            
}


def get_record_uri_labels(record, lang):
    uri_labels = get_labels_for_uris([s.uri for s in record.subjects.all()], settings.RDF_SCHEMES['subjects'], lang, False)
    uri_labels.update(get_labels_for_uris([s.uri for s in record.themes.all()], settings.RDF_SCHEMES['themes'], lang, False))
    uri_labels.update(get_labels_for_uris([s.uri for s in record.countries.all()], settings.RDF_SCHEMES['countries'], lang, False))
    uri_labels.update(get_labels_for_uris([record.language.uri] if record.language else [], settings.RDF_SCHEMES['languages'], lang, False))
    uri_labels.update(get_labels_for_uris([s.uri for s in record.otherLanguages.all()], settings.RDF_SCHEMES['languages'], lang, False))
    uri_labels.update(get_labels_for_uris([s.uri for s in record.projectNames.all()], settings.RDF_SCHEMES['projects'], None, True))
    uri_labels.update(get_labels_for_uris([s.uri for s in record.subjectCorporateBodies.all()], settings.RDF_SCHEMES['organizations'], None, True))
    uri_labels.update(get_labels_for_uris([s.uri for s in record.corporateAuthors.all()], settings.RDF_SCHEMES['organizations'], None, True))
    uri_labels.update(get_labels_for_uris([record.recordType] if record.recordType else [], settings.RDF_SCHEMES['types'], lang, False))
    uri_labels.update(get_labels_for_uris([s.uri for s in record.audiences.all()], settings.RDF_SCHEMES['audiences'], None, True))
    return uri_labels


class RecordDetailView(DetailView):
    
    model = Record
    template_name = "p4l/record_view.html"
    slug_field = "identifier"
    
    def get_context_data(self, **kwargs):
        context = DetailView.get_context_data(self, **kwargs)
        # We get the language, "fr" by default
        lang = "fr"
        if "lang" in self.request.GET:
            lang = self.request.GET["lang"]
        elif hasattr(self.request, "LANGUAGE_CODE") and self.request.LANGUAGE_CODE in ["fr","en","es"]:
            lang = self.request.LANGUAGE_CODE

        context['uri_labels'] = get_record_uri_labels(self.object,lang)
        
        
        return context

class RecordEditView(DetailView):
    http_method_names = ['get']
    template_name = 'p4l/record_update_form.html'
    model = Record
    slug_field = "identifier"
    is_create_view = False
    
    def get_empty_object(self):
        
        obj = Record()
        obj.id = -1
        obj.identifier = "T" + str(int(time.time()))
        obj.uri = "http://www.iiep.unesco.org/plan4learning/record/" + obj.identifier
        
        return obj
    
    
    def get_object(self, *args, **kwargs):
        """
        Returns the object the view is displaying.

        By default this requires `self.queryset` and a `pk` or `slug` argument
        in the URLconf, but subclasses can override this to return any object.
        """
        if self.is_create_view:
            return self.get_empty_object()
        
        return super(RecordEditView, self).get_object(*args, **kwargs)
        

    def get_context_data(self, **kwargs):
        
        context = super(RecordEditView, self).get_context_data(**kwargs)
        
        if self.object:
            serializer = RecordSerializer(self.object)
            context['object_json'] = UnicodeJSONRenderer().render(serializer.data)
        else:
            context['object_json'] = "null"
        
        # We get the language, "fr" by default
        lang = "fr"
        if "lang" in self.request.GET:
            lang = self.request.GET["lang"]
        elif hasattr(self.request, "LANGUAGE_CODE") and self.request.LANGUAGE_CODE in ["fr","en","es"]:
            lang = self.request.LANGUAGE_CODE
        
        context['uri_labels'] = json.dumps(get_record_uri_labels(self.object,lang))
        
        # lang must be like "XX" in the sparql request 
        lang = '"' + lang + '"'

        query_dicts = dict([(k , dict([ (QUERY_FIELDS_TRANSLATION[key],value.format(lang=lang)) for key,value in v.items()  ])) for k,v in settings.SPARQL_REF_QUERIES.items()])
        context['query_dicts'] = json.dumps(query_dicts)
        
        # Languages list used in drop down list
        context['languages_list'] = json.dumps(settings.LANGUAGES_LIST)
        
        context['is_create_view'] = json.dumps(self.is_create_view)
                
        return context


class RecordDeleteView(View):
    def get(self, request, slug, **kwargs):
        rec = get_object_or_404(Record, identifier=slug)
        rec.delete()
        return redirect('p4l_home')