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 @@
* Controls the menu over the current mouse selection
*/
annotationsAppHighlight.controller('TextSelectionMenuController',
['$scope', '$rootScope', '$element', '$timeout', 'NgramHttpService',
function ($scope, $rootScope, $element, $timeout, NgramHttpService) {
['$scope', '$rootScope', '$element', '$timeout', 'NgramHttpService', 'NgramListHttpService',
function ($scope, $rootScope, $element, $timeout, NgramHttpService, NgramListHttpService) {
/*
* Universal text selection
*/
......@@ -178,15 +178,21 @@
'listId': listId,
'ngramId': $scope.selection_text.uuid
}, function(data) {
$.each($rootScope.annotations, function(index, element) {
if (element.list_id == listId && element.uuid == $scope.selection_text.uuid) {
$rootScope.annotations.splice(index, 1);
return false;
// 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) {
console.log(data);
console.error("unable to edit the Ngram " + $scope.selection_text);
console.error("unable to edit the Ngram " + $scope.selection_text.text);
}
);
......@@ -195,14 +201,25 @@
NgramHttpService.post(
{
'listId': listId,
'ngramId': 'new'
'ngramId': 'create'
},
{
'annotation' : {'text': $scope.selection_text.trim()}
'text': $scope.selection_text.trim()
}, 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) {
console.log(data);
console.error("unable to edit the Ngram " + $scope.selection_text);
}
);
......@@ -218,8 +235,8 @@
* Text highlighting controller
*/
annotationsAppHighlight.controller('NGramHighlightController',
['$scope', '$rootScope', '$compile', 'NgramHttpService',
function ($scope, $rootScope, $compile, NgramHttpService) {
['$scope', '$rootScope', '$compile', 'NgramHttpService', 'NgramListHttpService',
function ($scope, $rootScope, $compile, NgramHttpService, NgramListHttpService) {
var counter = 0;
/*
* Replace the text by an html template for ngram keywords
......@@ -352,9 +369,6 @@
/*
* Listen changes on the ngram data
*/
// $rootScope.$watchCollection('annotations', function (newValue, oldValue) {
// refreshDisplay();
// });
$rootScope.$watchCollection('activeLists', function (newValue, oldValue) {
refreshDisplay();
});
......@@ -371,16 +385,28 @@
NgramHttpService.post(
{
'listId': listId,
'ngramId': 'new'
'ngramId': 'create'
},
{
'annotation' : {'text': value}
'text': value
},
function(data) {
// on success
if (data) {
$rootScope.annotations.push(data);
$(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) {
// on error
......
......@@ -5,6 +5,7 @@ from annotations import views
urlpatterns = patterns('',
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'^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/(?P<ngram_id>[0-9]+)$', views.NgramEdit.as_view()), #
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
from gargantext_web.db import *
from ngram.lists import listIds, listNgramIds, ngramList
from gargantext_web.api import JsonHttpResponse
import json
@login_required
......@@ -47,11 +46,6 @@ class NgramList(APIView):
# ngrams of list_id of corpus_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 : {
'%s' % doc_id : [
{
......@@ -68,41 +62,29 @@ class NgramList(APIView):
class NgramEdit(APIView):
"""
Actions on one Ngram in one list
Actions on one existing Ngram in one list
"""
renderer_classes = (JSONRenderer,)
authentication_classes = (SessionAuthentication, BasicAuthentication)
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)
# format the ngram's text
ngram_text = request.data.get('annotation', {}).get('text', None)
ngram_text = ngram_text.strip().lower()
ngram_text = ' '.join(ngram_text.split())
# retrieve the ngram's id
ngram = session.query(Ngram).filter(Ngram.terms == ngram_text).first()
if ngram is None:
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
ngram_id = int(ngram_id)
# TODO remove the node_ngram from another conflicting list
# FIXME session.query(Node_Ngram).filter(Node_Ngram.ngram_id==ngram_id).delete()
# add the ngram to the list
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,
'occurrences': ngram_occurrences,
'occurrences': node_ngram.weight,
'list_id': list_id,
})
......@@ -111,8 +93,56 @@ class NgramEdit(APIView):
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.commit()
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):
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