Commit c2c84b8d authored by Elias's avatar Elias

Annotations : cleaned views, added NgramCreate view and urlconf, refresh the...

Annotations : cleaned views, added NgramCreate view and urlconf, refresh the data after every POST or DELETE
parent eec99d07
...@@ -30,8 +30,8 @@ ...@@ -30,8 +30,8 @@
* Controls the menu over the current mouse selection * Controls the menu over the current mouse selection
*/ */
annotationsAppHighlight.controller('TextSelectionMenuController', annotationsAppHighlight.controller('TextSelectionMenuController',
['$scope', '$rootScope', '$element', '$timeout', 'NgramHttpService', ['$scope', '$rootScope', '$element', '$timeout', 'NgramHttpService', 'NgramListHttpService',
function ($scope, $rootScope, $element, $timeout, NgramHttpService) { function ($scope, $rootScope, $element, $timeout, NgramHttpService, NgramListHttpService) {
/* /*
* Universal text selection * Universal text selection
*/ */
...@@ -178,15 +178,21 @@ ...@@ -178,15 +178,21 @@
'listId': listId, 'listId': listId,
'ngramId': $scope.selection_text.uuid 'ngramId': $scope.selection_text.uuid
}, function(data) { }, function(data) {
$.each($rootScope.annotations, function(index, element) { // Refresh the annotationss
if (element.list_id == listId && element.uuid == $scope.selection_text.uuid) { NgramListHttpService.get(
$rootScope.annotations.splice(index, 1); {
return false; 'corpusId': $rootScope.corpusId,
'docId': $rootScope.docId
},
function(data) {
$rootScope.annotations = data[$rootScope.corpusId.toString()][$rootScope.docId.toString()];
},
function(data) {
console.error("unable to get the list of ngrams");
} }
}); );
}, function(data) { }, function(data) {
console.log(data); console.error("unable to edit the Ngram " + $scope.selection_text.text);
console.error("unable to edit the Ngram " + $scope.selection_text);
} }
); );
...@@ -195,14 +201,25 @@ ...@@ -195,14 +201,25 @@
NgramHttpService.post( NgramHttpService.post(
{ {
'listId': listId, 'listId': listId,
'ngramId': 'new' 'ngramId': 'create'
}, },
{ {
'annotation' : {'text': $scope.selection_text.trim()} 'text': $scope.selection_text.trim()
}, function(data) { }, function(data) {
$rootScope.annotations.push(data); // Refresh the annotationss
NgramListHttpService.get(
{
'corpusId': $rootScope.corpusId,
'docId': $rootScope.docId
},
function(data) {
$rootScope.annotations = data[$rootScope.corpusId.toString()][$rootScope.docId.toString()];
},
function(data) {
console.error("unable to get the list of ngrams");
}
);
}, function(data) { }, function(data) {
console.log(data);
console.error("unable to edit the Ngram " + $scope.selection_text); console.error("unable to edit the Ngram " + $scope.selection_text);
} }
); );
...@@ -218,8 +235,8 @@ ...@@ -218,8 +235,8 @@
* Text highlighting controller * Text highlighting controller
*/ */
annotationsAppHighlight.controller('NGramHighlightController', annotationsAppHighlight.controller('NGramHighlightController',
['$scope', '$rootScope', '$compile', 'NgramHttpService', ['$scope', '$rootScope', '$compile', 'NgramHttpService', 'NgramListHttpService',
function ($scope, $rootScope, $compile, NgramHttpService) { function ($scope, $rootScope, $compile, NgramHttpService, NgramListHttpService) {
var counter = 0; var counter = 0;
/* /*
* Replace the text by an html template for ngram keywords * Replace the text by an html template for ngram keywords
...@@ -352,9 +369,6 @@ ...@@ -352,9 +369,6 @@
/* /*
* Listen changes on the ngram data * Listen changes on the ngram data
*/ */
// $rootScope.$watchCollection('annotations', function (newValue, oldValue) {
// refreshDisplay();
// });
$rootScope.$watchCollection('activeLists', function (newValue, oldValue) { $rootScope.$watchCollection('activeLists', function (newValue, oldValue) {
refreshDisplay(); refreshDisplay();
}); });
...@@ -371,16 +385,28 @@ ...@@ -371,16 +385,28 @@
NgramHttpService.post( NgramHttpService.post(
{ {
'listId': listId, 'listId': listId,
'ngramId': 'new' 'ngramId': 'create'
}, },
{ {
'annotation' : {'text': value} 'text': value
}, },
function(data) { function(data) {
// on success // on success
if (data) { if (data) {
$rootScope.annotations.push(data);
$(inputEltId).val(""); $(inputEltId).val("");
// Refresh the annotationss
NgramListHttpService.get(
{
'corpusId': $rootScope.corpusId,
'docId': $rootScope.docId
},
function(data) {
$rootScope.annotations = data[$rootScope.corpusId.toString()][$rootScope.docId.toString()];
},
function(data) {
console.error("unable to get the list of ngrams");
}
);
} }
}, function(data) { }, function(data) {
// on error // on error
......
...@@ -5,6 +5,7 @@ from annotations import views ...@@ -5,6 +5,7 @@ from annotations import views
urlpatterns = patterns('', urlpatterns = patterns('',
url(r'^document/(?P<doc_id>[0-9]+)$', views.Document.as_view()), # document view url(r'^document/(?P<doc_id>[0-9]+)$', views.Document.as_view()), # document view
url(r'^corpus/(?P<corpus_id>[0-9]+)/document/(?P<doc_id>[0-9]+)$', views.NgramList.as_view()), # the list associated with an ngram url(r'^corpus/(?P<corpus_id>[0-9]+)/document/(?P<doc_id>[0-9]+)$', views.NgramList.as_view()), # the list associated with an ngram
url(r'^lists/(?P<list_id>[0-9]+)/ngrams(?:/(?P<ngram_id>[0-9]+))?$', views.NgramEdit.as_view()), # url(r'^lists/(?P<list_id>[0-9]+)/ngrams/(?P<ngram_id>[0-9]+)$', views.NgramEdit.as_view()), #
url(r'^lists/(?P<list_id>[0-9]+)/multiple?$', views.deleteMultiple ), # url(r'^lists/(?P<list_id>[0-9]+)/ngrams/create$', views.NgramCreate.as_view()), #
url(r'^lists/(?P<list_id>[0-9]+)/multiple?$', views.deleteMultiple ), # FIXME What's this ?
) )
...@@ -17,7 +17,6 @@ from node.models import Node ...@@ -17,7 +17,6 @@ from node.models import Node
from gargantext_web.db import * from gargantext_web.db import *
from ngram.lists import listIds, listNgramIds, ngramList from ngram.lists import listIds, listNgramIds, ngramList
from gargantext_web.api import JsonHttpResponse from gargantext_web.api import JsonHttpResponse
import json
@login_required @login_required
...@@ -47,11 +46,6 @@ class NgramList(APIView): ...@@ -47,11 +46,6 @@ class NgramList(APIView):
# ngrams of list_id of corpus_id: # ngrams of list_id of corpus_id:
doc_ngram_list = listNgramIds(corpus_id=corpus_id, doc_id=doc_id, user_id=request.user.id) doc_ngram_list = listNgramIds(corpus_id=corpus_id, doc_id=doc_id, user_id=request.user.id)
# TODO remove these debug values
doc_ngram_list = [(i, 'miam', i, 1931) for i in range(500)] + [(700, 'syndromes', 700, 1931)]
doc_ngram_list += [(i, 'stop', i, 1932) for i in range(501, 600)] + [(701, 'VCAM-1', 701, 1932)]
# doc_ngram_list = [(1, 'miam', 2, 1931), (2, 'stop', 2, 1932), (3, 'Potassium channels', 4, 1931)]
data = { '%s' % corpus_id : { data = { '%s' % corpus_id : {
'%s' % doc_id : [ '%s' % doc_id : [
{ {
...@@ -68,41 +62,29 @@ class NgramList(APIView): ...@@ -68,41 +62,29 @@ class NgramList(APIView):
class NgramEdit(APIView): class NgramEdit(APIView):
""" """
Actions on one Ngram in one list Actions on one existing Ngram in one list
""" """
renderer_classes = (JSONRenderer,) renderer_classes = (JSONRenderer,)
authentication_classes = (SessionAuthentication, BasicAuthentication) authentication_classes = (SessionAuthentication, BasicAuthentication)
def post(self, request, list_id, ngram_id): def post(self, request, list_id, ngram_id):
""" """
Add a ngram in a list Edit an existing NGram in a given list
""" """
# TODO - if Ngram is in miam-list, and adding it to stop-list,
# then remove it from the previous list
list_id = int(list_id) list_id = int(list_id)
# format the ngram's text ngram_id = int(ngram_id)
ngram_text = request.data.get('annotation', {}).get('text', None) # TODO remove the node_ngram from another conflicting list
ngram_text = ngram_text.strip().lower() # FIXME session.query(Node_Ngram).filter(Node_Ngram.ngram_id==ngram_id).delete()
ngram_text = ' '.join(ngram_text.split()) # add the ngram to the list
# retrieve the ngram's id node_ngram = Node_Ngram(node_id=list_id, ngram_id=ngram_id, weight=1.0)
ngram = session.query(Ngram).filter(Ngram.terms == ngram_text).first() session.add(node_ngram)
if ngram is None: session.commit()
ngram = Ngram(n=len(ngram_text.split()), terms=ngram_text)
session.add(ngram)
session.commit()
ngram_id = ngram.id
# add the ngram to the list if not already done
node_ngram = session.query(Node_Ngram).filter(Node_Ngram.node_id==list_id).filter(Node_Ngram.ngram_id==ngram_id).first()
if node_ngram is None:
node_ngram = Node_Ngram(node_id=list_id, ngram_id=ngram_id, weight=1.0)
session.add(node_ngram)
session.commit()
ngram_occurrences = node_ngram.weight
# return the response # return the response
return Response({ return Response({
'uuid': ngram_id, 'uuid': ngram_id,
'text': ngram_text, 'text': ngram_text,
'occurrences': ngram_occurrences, 'occurrences': node_ngram.weight,
'list_id': list_id, 'list_id': list_id,
}) })
...@@ -111,8 +93,56 @@ class NgramEdit(APIView): ...@@ -111,8 +93,56 @@ class NgramEdit(APIView):
Delete a ngram from a list Delete a ngram from a list
""" """
session.query(Node_Ngram).filter(Node_Ngram.node_id==list_id).filter(Node_Ngram.ngram_id==ngram_id).delete() session.query(Node_Ngram).filter(Node_Ngram.node_id==list_id).filter(Node_Ngram.ngram_id==ngram_id).delete()
session.commit()
return Response(None, 204) return Response(None, 204)
class NgramCreate(APIView):
"""
Create a new Ngram in one list
"""
renderer_classes = (JSONRenderer,)
authentication_classes = (SessionAuthentication, BasicAuthentication)
def post(self, request, list_id):
"""
create NGram in a given list
"""
list_id = int(list_id)
# format the ngram's text
ngram_text = request.data.get('text', None)
if ngram_text is not None:
ngram_text = ngram_text.strip().lower()
ngram_text = ' '.join(ngram_text.split())
else:
raise APIException("Could not create a new Ngram without one \
text key in the json body")
# check if the ngram exists with the same terms
ngram = session.query(Ngram).filter(Ngram.terms == ngram_text).first()
if ngram is None:
ngram = Ngram(n=len(ngram_text.split()), terms=ngram_text)
else:
# make sure the n value is correct
ngram.n = len(ngram_text.split())
session.add(ngram)
session.commit()
ngram_id = ngram.id
# create the new node_ngram relation
# TODO check existing ?
node_ngram = Node_Ngram(node_id=list_id, ngram_id=ngram_id, weight=1.0)
session.add(node_ngram)
session.commit()
# return the response
return Response({
'uuid': ngram_id,
'text': ngram_text,
'list_id': list_id,
})
def deleteMultiple(request, list_id): def deleteMultiple(request, list_id):
results = ["hola","mundo"] results = ["hola","mundo"]
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment