Commit 93957939 authored by Mathieu Rodic's avatar Mathieu Rodic

[BUG] corpora are now deletable from `/projects/(\d+)`

[FEAT] implemented a kind of simple front-end REST library
parent d6b0b08b
......@@ -63,6 +63,9 @@ class NodeListResource(APIView):
'records': [dict(zip(parameters['fields'], node)) for node in query]
})
def post(self, request):
pass
def delete(self, request):
"""Removes the list of nodes corresponding to the query.
WARNING! THIS IS TOTALLY UNTESTED!!!!!
......@@ -76,6 +79,7 @@ class NodeListResource(APIView):
'count': count,
}, 200)
class NodeResource(APIView):
def _query(self, request, node_id):
......@@ -88,7 +92,6 @@ class NodeResource(APIView):
return user, node
def get(self, request, node_id):
from sqlalchemy import delete
user, node = self._query(request, node_id)
return JsonHttpResponse({
'id': node.id,
......@@ -98,6 +101,7 @@ class NodeResource(APIView):
})
def delete(self, request, node_id):
from sqlalchemy import delete
user, node = self._query(request, node_id)
result = session.execute(
delete(Node).where(Node.id == node_id)
......
// check if an object is an array
if (typeof Array.isArray != 'function') {
Array.isArray = function(stuff) {
return Object.prototype.toString.call(stuff) == '[object Array]';
};
}
// CSRF token management for Django
var getCookie = function(name) {
var cookieValue = null;
if (document.cookie && document.cookie != '') {
var cookies = document.cookie.split(';');
for (var i = 0; i < cookies.length; i++) {
var cookie = jQuery.trim(cookies[i]);
// Does this cookie string begin with the name we want?
if (cookie.substring(0, name.length + 1) == (name + '=')) {
cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
break;
}
}
}
return cookieValue;
}
var csrftoken = getCookie('csrftoken');
$.ajaxSetup({
beforeSend: function(xhr, settings) {
xhr.setRequestHeader("X-CSRFToken", csrftoken);
}
});
// Resource class
var Resource = function(url_path) {
// retrieve one or many items
this.get = function(criteria, callback) {
var url = url_path;
switch (typeof criteria) {
// get the list, according to the criteria passed as parameters
case 'object':
var url_parameters = '';
$.each(criteria, function(key, value) {
if (Array.isArray(value)) {
$.each(value, function(i, item) {
url_parameters += url_parameters.length ? '&' : '?';
url_parameters += encodeURIComponent(key) + '[]=' + encodeURIComponent(item);
});
} else {
url_parameters += url_parameters.length ? '&' : '?';
url_parameters += encodeURIComponent(key) + '=' + encodeURIComponent(value);
}
});
url += url_parameters;
break;
// get the list, without paramters
case 'function':
callback = criteria;
break;
case 'number':
case 'string':
url += '/' + criteria;
break;
}
$.ajax({
url: url,
type: 'GET',
success: callback
});
};
// change an item
this.change = this.update = function(item, callback) {
$.ajax({
url: url_path + '/' + item.id,
type: 'PATCH',
success: callback
});
};
// remove an item
this.delete = this.remove = function(id, callback) {
if (id.id != undefined) {
id = id.id;
}
$.ajax({
url: url_path + '/' + id,
type: 'DELETE',
success: callback
});
};
// add an item
this.add = this.append = function(value, callback) {
$.ajax({
url: url_path + '/' + id,
type: 'POST',
success: callback
});
};
};
var GarganRest = function(base_path, path_list) {
var that = this;
$.each(path_list, function(p, path){
that[path] = new Resource(base_path + path);
});
};
garganrest = new GarganRest('/api/', ['nodes']);
// var log = function(result){console.log(result);};
// garganrest.nodes.get(log);
// garganrest.nodes.get(167, log);
// garganrest.nodes.delete(167, log);
// garganrest.nodes.get({
// pagination_offset: 0,
// pagination_limit: 10,
// type: ['DOCUMENT'],
// parent_id: 2,
// }, log);
......@@ -4,6 +4,7 @@
{% load staticfiles %}
<link rel="stylesheet" href="{% static "css/bootstrap.css" %}">
<script type="text/javascript" src="{% static "js/jquery/jquery.min.js" %}"></script>
<script type="text/javascript" src="{% static "js/gargantext/garganrest.js" %}"></script>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.11.2/themes/smoothness/jquery-ui.css">
<script type="text/javascript" src="{% static "js/morris.min.js" %}"></script>
......@@ -75,7 +76,7 @@
{{ key }}
<ul>
{% for corpus in corpora %}
<li>
<li id="corpus_{{corpus.id}}">
{% ifequal corpus.processing 1 %}
{{corpus.name}}:
<img width="20px" src="{% static "js/libs/img2/loading-bar.gif" %}">
......@@ -84,13 +85,16 @@
<a href="/projects/{{project.id}}/corpora/{{corpus.id}}"> {{corpus.name}} , {{ corpus.count }} documents</a>
{% endifequal %}
<button type="button" class="btn btn-xs btn-default" data-container="body" data-toggle="popover" data-placement="bottom"
data-content='
data-content="
<ul>
<li> Rename </li>
<li> Add new documents </li>
<li><a href="/delete/{{corpus.id}}">Delete</a></li>
<li>Rename</li>
<li>Add new documents</li>
<li onclick=&quot;
garganrest.nodes.delete({{corpus.id}}, function(){$('#corpus_'+{{corpus.id}}).remove()});
$(this).parent().parent().parent().remove();
&quot;><a href=&quot;#&quot;>Delete</a></li>
</ul>
'>Manage</button>
">Manage</button>
</li>
{% endfor %}
</ul>
......
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