- correct import pour indexation
authorymh <ymh.work@gmail.com>
Thu, 26 Sep 2013 15:24:41 +0200
changeset 119 ece69ca3ac24
parent 118 d33a072d974b
child 120 6ec0300b626e
- correct import pour indexation - improve new record management
src/p4l/api/views.py
src/p4l/config.py.tmpl
src/p4l/management/commands/import_record.py
src/p4l/mapping/parsers.py
src/p4l/models/signals.py
src/p4l/search/signals.py
src/p4l/settings.py
src/p4l/static/p4l/js/p4l.js
src/p4l/templates/p4l/home.html
src/p4l/templates/p4l/record_update_form.html
src/p4l/templates/registration/login.html
src/p4l/urls.py
src/p4l/views.py
--- a/src/p4l/api/views.py	Wed Sep 25 23:22:29 2013 +0200
+++ b/src/p4l/api/views.py	Thu Sep 26 15:24:41 2013 +0200
@@ -5,7 +5,7 @@
 @author: ymh
 '''
 from p4l.api.serializers import RecordSerializer
-from p4l.models import Record
+from p4l.models import Record, signals
 from rest_framework import viewsets
 
 class RecordViewSet(viewsets.ModelViewSet):
@@ -13,10 +13,15 @@
     API endpoint that allow Record to be viewed or edited
     '''
     def pre_save(self, obj):
-        viewsets.ModelViewSet.pre_save(self, obj)
+        super(RecordViewSet, self).pre_save(obj)
         if self.request and hasattr(self.request, 'user') and self.request.user:
             obj.modified_by =  self.request.user
 
+    def post_save(self, obj, created=False):
+        super(RecordViewSet, self).post_save(obj, created=created)
+        signals.record_saved.send(Record, instance=obj, created=created)
+        
+
     queryset = Record.objects.all()
     serializer_class = RecordSerializer
     lookup_field = 'identifier'
--- a/src/p4l/config.py.tmpl	Wed Sep 25 23:22:29 2013 +0200
+++ b/src/p4l/config.py.tmpl	Thu Sep 26 15:24:41 2013 +0200
@@ -25,7 +25,7 @@
 
 # Hosts/domain names that are valid for this site; required if DEBUG is False
 # See https://docs.djangoproject.com/en/1.5/ref/settings/#allowed-hosts
-ALLOWED_HOSTS = []
+ALLOWED_HOSTS = ["*"]
 
 # Local time zone for this installation. Choices can be found here:
 # http://en.wikipedia.org/wiki/List_of_tz_zones_by_name
@@ -143,6 +143,8 @@
     },
 }
 
+REALTIME_INDEXING = True
+
 NB_RECORDS_BY_PAGE = 20
 SPARQL_QUERY_ENDPOINT = "http://localhost:8080/openrdf-sesame/repositories/plan4learning"
 
--- a/src/p4l/management/commands/import_record.py	Wed Sep 25 23:22:29 2013 +0200
+++ b/src/p4l/management/commands/import_record.py	Thu Sep 26 15:24:41 2013 +0200
@@ -126,10 +126,9 @@
         self.index = options.get("index", False)
         
         if not self.index:
-            old_haystack_signal_processor = getattr(settings, "HAYSTACK_SIGNAL_PROCESSOR", None)
+            old_realtime_indexing = getattr(settings, "REALTIME_INDEXING", None)
             #this is not recommended by the django manual, but in case of management command it seems to work
-            if old_haystack_signal_processor:
-                settings.HAYSTACK_SIGNAL_PROCESSOR = 'haystack.signals.BaseSignalProcessor' 
+            settings.REALTIME_INDEXING = False 
         
         transaction.enter_transaction_management()
         transaction.managed(True)
@@ -143,6 +142,6 @@
 
         transaction.leave_transaction_management()
         
-        if not self.index and old_haystack_signal_processor:
-            settings.HAYSTACK_SIGNAL_PROCESSOR = old_haystack_signal_processor
+        if not self.index and old_realtime_indexing:
+            settings.REALTIME_INDEXING = old_realtime_indexing
 
--- a/src/p4l/mapping/parsers.py	Wed Sep 25 23:22:29 2013 +0200
+++ b/src/p4l/mapping/parsers.py	Thu Sep 26 15:24:41 2013 +0200
@@ -7,6 +7,7 @@
 from rdflib.plugins.sparql.processor import prepareQuery
 from rdflib.term import URIRef
 from p4l.models.data import Language, Record
+from p4l.models import signals
 
 
 class QueryCache(object):
@@ -275,5 +276,7 @@
             ['address', 'display'],
             "SELECT ?a ?d WHERE { [ iiep:url ?bnode ]. OPTIONAL { ?bnode iiep:address ?a }. OPTIONAL { ?bnode iiep:display ?d }.}",
         )
+        
+        signals.record_saved.send(Record, instance=record, created=True)
 
         return record
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/p4l/models/signals.py	Thu Sep 26 15:24:41 2013 +0200
@@ -0,0 +1,10 @@
+# -*- coding: utf-8 -*-
+'''
+Created on Sep 26, 2013
+
+@author: ymh
+'''
+
+import django.dispatch
+
+record_saved = django.dispatch.Signal(providing_args=["instance", "created"])
--- a/src/p4l/search/signals.py	Wed Sep 25 23:22:29 2013 +0200
+++ b/src/p4l/search/signals.py	Thu Sep 26 15:24:41 2013 +0200
@@ -4,18 +4,29 @@
 
 @author: ymh
 '''
+from django.conf import settings
 from django.db import models
 from haystack import signals
 
+import p4l.models.signals
+
 
 class P4lSignalProcessor(signals.BaseSignalProcessor):
     
-    def __connect_signals(self, klass):
-        models.signals.post_save.connect(self.handle_save, sender=klass)
+    def handle_delete(self, sender, instance, **kwargs):
+        if getattr(settings, "REALTIME_INDEXING", True):
+            signals.BaseSignalProcessor.handle_delete(self, sender, instance, **kwargs)
+    
+    def handle_save(self, sender, instance, **kwargs):
+        if getattr(settings, "REALTIME_INDEXING", True):
+            signals.BaseSignalProcessor.handle_save(self, sender, instance, **kwargs)
+    
+    def __connect_signals(self, klass):        
+        p4l.models.signals.record_saved.connect(self.handle_save, sender=klass)
         models.signals.post_delete.connect(self.handle_delete, sender=klass)        
 
     def __disconnect_signals(self, klass):
-        models.signals.post_save.disconnect(self.handle_save, sender=klass)
+        p4l.models.signals.record_saved.disconnect(self.handle_save, sender=klass)
         models.signals.post_delete.disconnect(self.handle_delete, sender=klass)
 
     
--- a/src/p4l/settings.py	Wed Sep 25 23:22:29 2013 +0200
+++ b/src/p4l/settings.py	Thu Sep 26 15:24:41 2013 +0200
@@ -187,6 +187,8 @@
 HAYSTACK_SIGNAL_PROCESSOR = 'p4l.search.signals.P4lSignalProcessor'
 HAYSTACK_SEARCH_RESULTS_PER_PAGE = NB_RECORDS_BY_PAGE
 
+REALTIME_INDEXING = True
+
 CACHES = {
     'default' : {
         'BACKEND': 'django.core.cache.backends.dummy.DummyCache',
--- a/src/p4l/static/p4l/js/p4l.js	Wed Sep 25 23:22:29 2013 +0200
+++ b/src/p4l/static/p4l/js/p4l.js	Thu Sep 26 15:24:41 2013 +0200
@@ -1,7 +1,11 @@
 "use strict";
 // initialize the app
 
-var app = angular.module("recordApp", ['ngResource', 'ngRoute', 'pascalprecht.translate'])
+var app = angular.module("recordApp", ['ngResource', 'ngRoute', 'pascalprecht.translate']);
+
+app.config(function($locationProvider) {
+    $locationProvider.html5Mode(true);
+});
 
 app.service("Api", function($resource, context) {
     this.record = $resource(context.urls.record_api,
@@ -287,7 +291,7 @@
     };
 });
 
-app.controller("RecordCtrl", function($translate, $scope, RecordModel, context){
+app.controller("RecordCtrl", function($translate, $scope, $location, RecordModel, context){
     
     $scope.record = RecordModel.record;
     $scope.uriLabels = RecordModel.uriLabels;
@@ -296,16 +300,18 @@
         
     $scope.submitRecord = function() {
         $scope.saving = true;
-        $scope.record.$save(
-        	{ recordId: context.record_id },
-        	function(){
-        		//console.log("SUCCESS");
-        	},
-        	function(){
-        		alert($translate("An error occured. Somes datas may be incorrect or incomplete."));
-        		$scope.saving=false;
-        	})
-            .then(function(response) {
+        $scope.record.$save({ recordId: context.record_id })
+            .then(
+                function(response) {
+                    if(context.is_create_view) {
+                        $location.path(decodeURI(context.urls.record_edit.replace(":recordId", context.record_id))).search({previous:$scope.getPreviousUrl()}).replace();
+                    }
+                },
+                function(reason){
+                    alert($translate("An error occured. Somes datas may be incorrect or incomplete."));
+                }
+            )
+            .finally(function() {
                 $scope.saving=false;
             });
     }
--- a/src/p4l/templates/p4l/home.html	Wed Sep 25 23:22:29 2013 +0200
+++ b/src/p4l/templates/p4l/home.html	Thu Sep 26 15:24:41 2013 +0200
@@ -16,7 +16,7 @@
 	    <div class="form-group">
 	      <button type="submit" class="btn btn-default"><i class="glyphicon glyphicon-search"></i></button>
 	    </div>
-      - <a class="btn btn-default" href="{% url 'p4l_record_new' %}">{% trans 'New record' %} <i class="glyphicon glyphicon-plus-sign"></i></a>
+      - <a class="btn btn-default" href="{% url 'p4l_record_new' %}?previous={{request.get_full_path|urlencode}}">{% trans 'New record' %} <i class="glyphicon glyphicon-plus-sign"></i></a>
 	  </form>
 	</div>
 </div>
@@ -51,7 +51,7 @@
           {% endfor %}</ul></td>
       <td>{{ result.get_stored_fields.years|join:", "}}</td>
       <td>
-        <a class="btn btn-default" href="{% url 'p4l_record_view' slug=result.get_stored_fields.identifier %}"><i class="glyphicon glyphicon-eye-open"></i></a>
+        <a class="btn btn-default" href="{% url 'p4l_record_view' slug=result.get_stored_fields.identifier %}?previous={{request.get_full_path|urlencode}}"><i class="glyphicon glyphicon-eye-open"></i></a>
         <a class="btn btn-default" href="{% url 'p4l_record_edit' slug=result.get_stored_fields.identifier %}?previous={{request.get_full_path|urlencode}}"><i class="glyphicon glyphicon-pencil"></i></a>
       </td>
     </tr>
--- a/src/p4l/templates/p4l/record_update_form.html	Wed Sep 25 23:22:29 2013 +0200
+++ b/src/p4l/templates/p4l/record_update_form.html	Thu Sep 26 15:24:41 2013 +0200
@@ -30,11 +30,13 @@
             urls: {
                 base_static: "{% get_static_prefix %}",
                 record_api: "{% url 'record-detail' identifier=':recordId' %}".replace("\%3A",":"),
+                record_edit: "{% url 'p4l_record_edit' slug=':recordId' %}".replace("\%3A",":"),
                 previous: "{{request.GET.previous}}",
                 home: "{% url 'p4l_home' %}"
             },
-            query_dicts: angular.fromJson('{{ query_dicts | safe | addslashes}}'),
-            languages_list: angular.fromJson('{{ languages_list | safe | addslashes}}'),
+            query_dicts: angular.fromJson('{{query_dicts | safe | addslashes}}'),
+            languages_list: angular.fromJson('{{languages_list | safe | addslashes}}'),
+            is_create_view: angular.fromJson('{{is_create_view | safe | addslashes}}')
         })
         .config(['$translateProvider', function($translateProvider) {
             $translateProvider.translations(catalog); //catalog is declared in django.views.i18n.javascript_catalog
@@ -52,14 +54,14 @@
         <span ng-class="['glyphicon', saving?'spinner':'glyphicon-save']"></span>
         <span>{% trans 'Save' %}</span>
     </button>
-    <a class="btn btn-primary" id="record-back" href="{% templatetag openvariable %}getPreviousUrl(){% templatetag closevariable %}">
+    <a class="btn btn-primary" id="record-back" href="{% templatetag openvariable %}getPreviousUrl(){% templatetag closevariable %}" target="_self">
         <span class="glyphicon glyphicon-arrow-left"></span>
         <span>{% trans 'Cancel' %}</span>
     </a>
   </div>
   <div class="col-md-6 text-right">
-    <a class="btn btn-default" href="{% url 'p4l_record_view' slug=record.identifier %}">{% trans 'View the record' %} <i class="glyphicon glyphicon-eye-open"></i></a>
-    <a class="btn btn-default" href="{% url 'p4l_record_delete' slug=record.identifier %}" onclick="return confirm('{% trans 'Are your sure you want to delete this record ? This action is irreversible.' %}')">{% trans 'Delete the record' %} <i class="glyphicon glyphicon-trash"></i></a>
+    <a class="btn btn-default" href="{% url 'p4l_record_view' slug=record.identifier %}" target="_self">{% trans 'View the record' %} <i class="glyphicon glyphicon-eye-open"></i></a>
+    <a class="btn btn-default" href="{% url 'p4l_record_delete' slug=record.identifier %}" onclick="return confirm('{% trans 'Are your sure you want to delete this record ? This action is irreversible.' %}')" target="_self">{% trans 'Delete the record' %} <i class="glyphicon glyphicon-trash"></i></a>
   </div>
 </div>
 {% verbatim %}
@@ -239,15 +241,15 @@
 	    <span ng-class="['glyphicon', saving?'spinner':'glyphicon-save']"></span>
 	    <span>{{ 'Save' | translate }}</span>
 	</button>
-	<a class="btn btn-primary" id="record-back" href="{{getPreviousUrl()}}">
+	<a class="btn btn-primary" id="record-back" href="{{getPreviousUrl()}}" target="_self">
 	    <span class="glyphicon glyphicon-arrow-left"></span>
 	    <span>{{ 'Cancel' | translate }}</span>
 	</a>
   </div>
 {% endverbatim %}
   <div class="col-md-6 text-right">
-    <a class="btn btn-default" href="{% url 'p4l_record_view' slug=record.identifier %}">{% trans 'View the record' %} <i class="glyphicon glyphicon-eye-open"></i></a>
-    <a class="btn btn-default" href="{% url 'p4l_record_delete' slug=record.identifier %}" onclick="return confirm('{% trans 'Are your sure you want to delete this record ? This action is irreversible.' %}')">{% trans 'Delete the record' %} <i class="glyphicon glyphicon-trash"></i></a>
+    <a class="btn btn-default" href="{% url 'p4l_record_view' slug=record.identifier %}" target="_self">{% trans 'View the record' %} <i class="glyphicon glyphicon-eye-open"></i></a>
+    <a class="btn btn-default" href="{% url 'p4l_record_delete' slug=record.identifier %}" onclick="return confirm('{% trans 'Are your sure you want to delete this record ? This action is irreversible.' %}')" target="_self">{% trans 'Delete the record' %} <i class="glyphicon glyphicon-trash"></i></a>
   </div>
 </div>
 </form>
--- a/src/p4l/templates/registration/login.html	Wed Sep 25 23:22:29 2013 +0200
+++ b/src/p4l/templates/registration/login.html	Thu Sep 26 15:24:41 2013 +0200
@@ -1,4 +1,4 @@
-{% extends "p4l/p4l_base.html" %}
+{% extends "p4l/base.html" %}
 {% load i18n %}
 
 {% block page_title %}{% trans 'login' %}{% endblock %}
--- a/src/p4l/urls.py	Wed Sep 25 23:22:29 2013 +0200
+++ b/src/p4l/urls.py	Thu Sep 26 15:24:41 2013 +0200
@@ -18,7 +18,7 @@
     url(r'^$', login_required(RecordSearchView.as_view()), name='p4l_home'),
     url(r'^auth/', include(auth_urls)),
     url(r'^record/view/(?P<slug>\w+)$', login_required(RecordDetailView.as_view()), name='p4l_record_view'),
-    url(r'^record/edit/(?P<slug>\w+)$', login_required(RecordEditView.as_view()), name='p4l_record_edit'),
+    url(r'^record/edit/(?P<slug>[\w\:]+)$', login_required(RecordEditView.as_view()), name='p4l_record_edit'),
     url(r'^record/new$', login_required(RecordEditView.as_view(is_create_view=True)), name='p4l_record_new'),
     url(r'^record/delete/(?P<slug>\w+)$', login_required(RecordDeleteView.as_view()), name='p4l_record_delete'),
     url(r'^api/', include('p4l.api.urls')),
--- a/src/p4l/views.py	Wed Sep 25 23:22:29 2013 +0200
+++ b/src/p4l/views.py	Thu Sep 26 15:24:41 2013 +0200
@@ -120,6 +120,8 @@
         
         # 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