Commit 7f7ac788 authored by Romain Loth's avatar Romain Loth

annotations: working delete from lists (added "ngramlists/change?list=..." as...

annotations: working delete from lists (added "ngramlists/change?list=..." as an external service + added chained calls to manage consequences)
parent 5f0581a2
...@@ -32,7 +32,10 @@ ...@@ -32,7 +32,10 @@
}, },
function(data) { function(data) {
$rootScope.annotations = data[$rootScope.corpusId.toString()][$rootScope.docId.toString()]; $rootScope.annotations = data[$rootScope.corpusId.toString()][$rootScope.docId.toString()];
// eg id => 'MAPLIST'
$rootScope.lists = data[$rootScope.corpusId.toString()].lists; $rootScope.lists = data[$rootScope.corpusId.toString()].lists;
// inverted 'MAPLIST' => id
$rootScope.listIds = _.invert($rootScope.lists)
$scope.dataLoading = false ; $scope.dataLoading = false ;
}, },
function(data) { function(data) {
......
This diff is collapsed.
...@@ -13,7 +13,7 @@ ...@@ -13,7 +13,7 @@
* *
* route: annotations/documents/@d_id * route: annotations/documents/@d_id
* ------ * ------
* * TODO use external: api/nodes/@d_id?fields[]=hyperdata
* exemple: * exemple:
* -------- * --------
* { * {
...@@ -86,13 +86,19 @@ ...@@ -86,13 +86,19 @@
); );
}); });
/* /*
* NgramHttpService: Create, modify or delete 1 Ngram * NgramHttpService: Create, modify or delete 1 Ngram
* ================= * =================
* *
* TODO REACTIVATE IN urls.py * TODO add a create case separately and then remove service
*
* NB : replaced by external api: (MainApiChangeNgramHttpService)
* api/ngramlists/change?list=LISTID&ngrams=ID1,ID2..
* *
* if new ngram: * old logic:
* ----------
* if new ngram
* -> ngram_id will be "create" * -> ngram_id will be "create"
* -> route: annotations/lists/@node_id/ngrams/create * -> route: annotations/lists/@node_id/ngrams/create
* -> will land on views.NgramCreate * -> will land on views.NgramCreate
...@@ -100,24 +106,61 @@ ...@@ -100,24 +106,61 @@
* else: * else:
* -> ngram_id is a real ngram id * -> ngram_id is a real ngram id
* -> route: annotations/lists/@node_id/ngrams/@ngram_id * -> route: annotations/lists/@node_id/ngrams/@ngram_id
* -> will land on views.NgramCreate * -> will land on views.NgramEdit
*
*/
// http.factory('NgramHttpService', function ($resource) {
// return $resource(
// window.ANNOTATION_API_URL + 'lists/:listId/ngrams/:ngramId',
// {
// listId: '@listId',
// ngramId: '@id'
// },
// {
// post: {
// method: 'POST',
// params: {'listId': '@listId', 'ngramId': '@ngramId'}
// },
// delete: {
// method: 'DELETE',
// params: {'listId': '@listId', 'ngramId': '@ngramId'}
// }
// }
// );
// });
/*
* MainApiChangeNgramHttpService: Add/remove ngrams from lists
* =============================
* route: api/ngramlists/change?list=LISTID&ngrams=ID1,ID2...
*
* (same route used in ngrams table)
*
* /!\ for this route we reach out of this annotation module
* and send directly to the gargantext api route for list change
* (cross origin request with http protocol scheme)
* ------
* *
*/ */
http.factory('NgramHttpService', function ($resource) {
http.factory('MainApiChangeNgramHttpService', function($resource) {
return $resource( return $resource(
window.ANNOTATION_API_URL + 'lists/:listId/ngrams/:ngramId', // adding explicit "http://" b/c this a cross origin request
{ 'http://' + window.GARG_ROOT_URL
+ "/api/ngramlists/change?list=:listId&ngrams=:ngramIdList",
{
listId: '@listId', listId: '@listId',
ngramId: '@id' ngramIdList: '@ngramIdList' // list in str form (sep=","): "12,25,30"
// (usually in this app just 1 id): "12"
}, },
{ {
post: { put: {
method: 'POST', method: 'PUT',
params: {'listId': '@listId', 'ngramId': '@ngramId'} params: {listId: '@listId', ngramIdList: '@ngramIdList'}
}, },
delete: { delete: {
method: 'DELETE', method: 'DELETE',
params: {'listId': '@listId', 'ngramId': '@ngramId'} params: {listId: '@listId', ngramIdList: '@ngramIdList'}
} }
} }
); );
......
...@@ -7,39 +7,101 @@ ...@@ -7,39 +7,101 @@
* Controls one Ngram displayed in the flat lists (called "extra-text") * Controls one Ngram displayed in the flat lists (called "extra-text")
*/ */
annotationsAppNgramList.controller('NgramController', annotationsAppNgramList.controller('NgramController',
['$scope', '$rootScope', 'NgramHttpService', 'NgramListHttpService', ['$scope', '$rootScope', 'MainApiChangeNgramHttpService', 'NgramListHttpService',
function ($scope, $rootScope, NgramHttpService, NgramListHttpService) { function ($scope, $rootScope, MainApiChangeNgramHttpService, NgramListHttpService) {
/* /*
* Click on the 'delete' cross button * Click on the 'delete' cross button
* (NB: we have different delete consequences depending on list)
*/ */
$scope.onDeleteClick = function () { $scope.onDeleteClick = function () {
NgramHttpService.delete({ var listName = $scope.keyword.listName
'listId': $scope.keyword.list_id, var thisListId = $scope.keyword.list_id
'ngramId': $scope.keyword.uuid var thisNgramId = $scope.keyword.uuid
}, function(data) { var crudActions = [] ;
// Refresh the annotationss
NgramListHttpService.get( if (listName == 'MAPLIST') {
{ crudActions = [
'corpusId': $rootScope.corpusId, ["delete", thisListId]
'docId': $rootScope.docId ]
}, }
function(data) { else if (listName == 'MAINLIST') {
// $rootScope.annotations crudActions = [
// ---------------------- ["delete", thisListId],
// is the union of all lists, one being later "active" ["put", $rootScope.listIds.STOPLIST],
// (then used for left-side flatlist AND inline annots) ]
$rootScope.annotations = data[$rootScope.corpusId.toString()][$rootScope.docId.toString()]; }
// TODO £NEW : lookup obj[list_id][term_text] = {terminfo} else if (listName == 'STOPLIST') {
// $rootScope.lookup = crudActions = [
$rootScope.refreshDisplay(); ["delete", thisListId],
}, ["put", $rootScope.listIds.MAINLIST],
function(data) { ]
console.error("unable to refresh the list of ngrams"); }
}
); // using recursion to make chained calls,
}, function(data) { // todo factorize with highlight.js
console.error("unable to remove the Ngram " + $scope.keyword.text);
}); var lastCallback = function() {
// Refresh the annotationss
NgramListHttpService.get(
{'corpusId': $rootScope.corpusId,
'docId': $rootScope.docId},
function(data) {
// $rootScope.annotations
// ----------------------
// is the union of all lists, one being later "active"
// (then used for left-side flatlist AND inline annots)
$rootScope.annotations = data[$rootScope.corpusId.toString()][$rootScope.docId.toString()];
// TODO £NEW : lookup obj[list_id][term_text] = {terminfo}
// $rootScope.lookup =
$rootScope.refreshDisplay();
},
function(data) {
console.error("unable to refresh the list of ngrams");
}
);
}
// chained recursion to do several actions then callback (eg refresh)
function makeChainedCalls (i, listOfActions, finalCallback) {
// each action couple has 2 elts
var action = listOfActions[i][0]
var listId = listOfActions[i][1]
MainApiChangeNgramHttpService[action](
{'listId': thisListId,
'ngramIdList': thisNgramId},
// on success
function(data) {
// case NEXT
// ----
// when chained actions
if (listOfActions.length > i+1) {
console.log("calling next action ("+(i+1)+")")
// ==============================================
makeChainedCalls(i+1, listOfActions, finalCallback)
// ==============================================
}
// case LAST
// ------
// when last action
else {
finalCallback()
}
},
// on error
function(data) {
console.error("unable to edit the Ngram \""+ngramText+"\""
+"(ngramId "+ngramId+")"+"at crud no "+i
+" ("+action+" on list "+listId+")");
}
);
}
// run the loop by calling the initial recursion step
makeChainedCalls(0, crudActions, lastCallback)
}; };
}]); }]);
...@@ -82,8 +144,8 @@ ...@@ -82,8 +144,8 @@
* new NGram from the user input * new NGram from the user input
*/ */
annotationsAppNgramList.controller('NgramInputController', annotationsAppNgramList.controller('NgramInputController',
['$scope', '$rootScope', '$element', 'NgramHttpService', 'NgramListHttpService', ['$scope', '$rootScope', '$element', 'NgramListHttpService',
function ($scope, $rootScope, $element, NgramHttpService, NgramListHttpService) { function ($scope, $rootScope, $element, NgramListHttpService) {
/* /*
* Add a new NGram from the user input in the extra-text list * Add a new NGram from the user input in the extra-text list
*/ */
...@@ -114,46 +176,47 @@ ...@@ -114,46 +176,47 @@
// --------------------------------------------------------------- // ---------------------------------------------------------------
// will check if there's a preexisting ngramId for this value // will check if there's a preexisting ngramId for this value
// TODO: reconnect separately from list addition
// TODO: if maplist => also add to miam // TODO: if maplist => also add to miam
NgramHttpService.post( // NgramHttpService.post(
{ // {
'listId': listId, // 'listId': listId,
'ngramId': 'create' // 'ngramId': 'create'
}, // },
{ // {
'text': value // 'text': value
}, // },
function(data) { // function(data) {
console.warn("refresh attempt"); // console.warn("refresh attempt");
// on success // // on success
if (data) { // if (data) {
angular.element(inputEltId).val(""); // angular.element(inputEltId).val("");
// Refresh the annotationss // // Refresh the annotationss
NgramListHttpService.get( // NgramListHttpService.get(
{ // {
'corpusId': $rootScope.corpusId, // 'corpusId': $rootScope.corpusId,
'docId': $rootScope.docId // 'docId': $rootScope.docId
}, // },
function(data) { // function(data) {
$rootScope.annotations = data[$rootScope.corpusId.toString()][$rootScope.docId.toString()]; // $rootScope.annotations = data[$rootScope.corpusId.toString()][$rootScope.docId.toString()];
//
// TODO £NEW : lookup obj[list_id][term_text] = {terminfo} // // TODO £NEW : lookup obj[list_id][term_text] = {terminfo}
// $rootScope.lookup = // // $rootScope.lookup =
//
//
$rootScope.refreshDisplay(); // $rootScope.refreshDisplay();
}, // },
function(data) { // function(data) {
console.error("unable to get the list of ngrams"); // console.error("unable to get the list of ngrams");
} // }
); // );
} // }
}, function(data) { // }, function(data) {
// on error // // on error
angular.element(inputEltId).parent().addClass("has-error"); // angular.element(inputEltId).parent().addClass("has-error");
console.error("error adding Ngram "+ value); // console.error("error adding Ngram "+ value);
} // }
); // );
}; };
}]); }]);
......
...@@ -118,7 +118,7 @@ ...@@ -118,7 +118,7 @@
<!-- this menu is over the text on mouse selection --> <!-- this menu is over the text on mouse selection -->
<div ng-controller="TextSelectionMenuController" id="selection" class="selection-menu"> <div ng-controller="TextSelectionMenuController" id="selection" class="selection-menu">
<ul class="noselection"> <ul class="noselection">
<li ng-repeat="item in menuItems" class="{[{item.listName}]}" ng-click="onMenuClick($event, item.action, item.listId)">{[{item.verb}]} {[{item.listName}]}</li> <li ng-repeat="item in menuItems" class="{[{item.tgtListName}]}" ng-click="onMenuClick($event, item.crudActions)">Move to {[{item.tgtListName}]}</li>
</ul> </ul>
</div> </div>
</div> </div>
......
...@@ -13,13 +13,10 @@ urlpatterns = [ ...@@ -13,13 +13,10 @@ urlpatterns = [
url(r'^documents/(?P<doc_id>[0-9]+)$', views.Document.as_view()), # document view url(r'^documents/(?P<doc_id>[0-9]+)$', views.Document.as_view()), # document view
# GET [NgramListHttpService] # GET [NgramListHttpService]
# was : lists ∩ document (ngram_ids intersection if connected to list node_id and doc node_id) # ngrams from {lists ∩ document}
# fixed 2016-01: just lists (because document doesn't get updated by POST create cf. ngram.lists.DocNgram filter commented)
url(r'^corpora/(?P<corpus_id>[0-9]+)/documents/(?P<doc_id>[0-9]+)$', views.NgramList.as_view()), # the list associated with an ngram url(r'^corpora/(?P<corpus_id>[0-9]+)/documents/(?P<doc_id>[0-9]+)$', views.NgramList.as_view()), # the list associated with an ngram
# 2016-03-24: refactoring, deactivated NgramEdit and NgramCreate # 2016-03-24: refactoring, deactivated NgramEdit and NgramCreate
# # 2016-05-27: removed NgramEdit: replaced the local httpservice by api/ngramlists
# url(r'^lists/(?P<list_id>[0-9]+)/ngrams/(?P<ngram_ids>[0-9,\+]+)+$', views.NgramEdit.as_view()),
# POST (fixed 2015-12-16)
# url(r'^lists/(?P<list_id>[0-9]+)/ngrams/create$', views.NgramCreate.as_view()), # # url(r'^lists/(?P<list_id>[0-9]+)/ngrams/create$', views.NgramCreate.as_view()), #
] ]
...@@ -93,79 +93,8 @@ class NgramList(APIView): ...@@ -93,79 +93,8 @@ class NgramList(APIView):
# 2016-03-24: refactoring, deactivated NgramEdit and NgramCreate # 2016-03-24: refactoring, deactivated NgramEdit and NgramCreate
# 2016-05-27: removed NgramEdit: replaced the local httpservice by api/ngramlists
# ------------------------------------ # ------------------------------------
# class NgramEdit(APIView):
# """
# Actions on one existing Ngram in one list
# """
# renderer_classes = (JSONRenderer,)
# authentication_classes = (SessionAuthentication, BasicAuthentication)
#
# def post(self, request, list_id, ngram_ids):
# """
# Edit an existing NGram in a given list
# """
# # implicit global session
# list_id = int(list_id)
# list_node = session.query(Node).filter(Node.id==list_id).first()
# # TODO add 1 for MapList social score ?
# if list_node.type_id == cache.NodeType['MiamList']:
# weight=1.0
# elif list_node.type_id == cache.NodeType['StopList']:
# weight=-1.0
#
# # TODO remove the node_ngram from another conflicting list
# for ngram_id in ngram_ids.split('+'):
# ngram_id = int(ngram_id)
# node_ngram = NodeNgram(node_id=list_id, ngram_id=ngram_id, weight=weight)
# session.add(node_ngram)
#
# session.commit()
#
# # return the response
# return Response({
# 'uuid': ngram_id,
# 'list_id': list_id,
# } for ngram_id in ngram_ids)
#
# def put(self, request, list_id, ngram_ids):
# return Response(None, 204)
#
# def delete(self, request, list_id, ngram_ids):
# """
# Delete a ngram from a list
# """
# # implicit global session
# print("to del",ngram_ids)
# for ngram_id in ngram_ids.split('+'):
# print('ngram_id', ngram_id)
# ngram_id = int(ngram_id)
# (session.query(NodeNgram)
# .filter(NodeNgram.node_id==list_id)
# .filter(NodeNgram.ngram_id==ngram_id).delete()
# )
#
# session.commit()
#
# # [ = = = = del from map-list = = = = ]
# list_id = session.query(Node).filter(Node.id==list_id).first()
# corpus = session.query(Node).filter(Node.id==list_id.parent_id , Node.type_id==cache.NodeType['Corpus'].id).first()
# node_mapList = get_or_create_node(nodetype='MapList', corpus=corpus )
# results = session.query(NodeNgram).filter(NodeNgram.node_id==node_mapList.id ).all()
# ngram_2del = [int(i) for i in ngram_ids.split('+')]
# ngram_2del_ = session.query(NodeNgram).filter(NodeNgram.node_id==node_mapList.id , NodeNgram.ngram_id.in_(ngram_2del) ).all()
# for map_node in ngram_2del_:
# session.delete(map_node)
# session.commit()
#
# node_stopList = get_or_create_node(nodetype='StopList', corpus=corpus )
# for ngram_id in ngram_2del:
# stop_node = NodeNgram( weight=1.0, ngram_id=ngram_id , node_id=node_stopList.id)
# session.add(stop_node)
# session.commit()
# # [ = = = = / del from map-list = = = = ]
#
# return Response(None, 204)
# #
# class NgramCreate(APIView): # class NgramCreate(APIView):
# """ # """
......
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