Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
gargantext
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
humanities
gargantext
Commits
14034b42
Commit
14034b42
authored
Dec 13, 2014
by
Mathieu Rodic
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[FEATURE] Added the route `GET /nodes/{id}`
parent
bd4f3fd8
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
62 additions
and
72 deletions
+62
-72
api.py
gargantext_web/api.py
+53
-49
urls.py
gargantext_web/urls.py
+9
-23
No files found.
gargantext_web/api.py
View file @
14034b42
...
@@ -10,13 +10,13 @@ from sqlalchemy import text, distinct
...
@@ -10,13 +10,13 @@ from sqlalchemy import text, distinct
from
sqlalchemy.sql
import
func
from
sqlalchemy.sql
import
func
from
sqlalchemy.orm
import
aliased
from
sqlalchemy.orm
import
aliased
import
node.
models
from
node
import
models
NodeType
=
node
.
models
.
NodeType
.
sa
NodeType
=
models
.
NodeType
.
sa
Node
=
node
.
models
.
Node
.
sa
Node
=
models
.
Node
.
sa
Node_Ngram
=
node
.
models
.
Node_Ngram
.
sa
Node_Ngram
=
models
.
Node_Ngram
.
sa
Ngram
=
node
.
models
.
Ngram
.
sa
Ngram
=
models
.
Ngram
.
sa
Metadata
=
node
.
models
.
Metadata
.
sa
Metadata
=
models
.
Metadata
.
sa
Node_Metadata
=
node
.
models
.
Node_Metadata
.
sa
Node_Metadata
=
models
.
Node_Metadata
.
sa
# for debugging only
# for debugging only
def
literalquery
(
statement
,
dialect
=
None
):
def
literalquery
(
statement
,
dialect
=
None
):
...
@@ -181,8 +181,8 @@ class NodesChildrenMetatadata(APIView):
...
@@ -181,8 +181,8 @@ class NodesChildrenMetatadata(APIView):
.
group_by
(
Metadata
)
.
group_by
(
Metadata
)
)
)
# build a
data
with the metadata keys
# build a
collection
with the metadata keys
data
=
[]
collection
=
[]
for
metadata
in
metadata_query
:
for
metadata
in
metadata_query
:
valuesCount
=
0
valuesCount
=
0
values
=
None
values
=
None
...
@@ -212,8 +212,8 @@ class NodesChildrenMetatadata(APIView):
...
@@ -212,8 +212,8 @@ class NodesChildrenMetatadata(APIView):
values
=
[]
values
=
[]
values
=
map
(
lambda
x
:
x
.
isoformat
(),
values
)
values
=
map
(
lambda
x
:
x
.
isoformat
(),
values
)
# adding this metadata to the
data
# adding this metadata to the
collection
data
.
append
({
collection
.
append
({
'key'
:
metadata
.
name
,
'key'
:
metadata
.
name
,
'type'
:
metadata
.
type
,
'type'
:
metadata
.
type
,
'values'
:
values
,
'values'
:
values
,
...
@@ -223,7 +223,7 @@ class NodesChildrenMetatadata(APIView):
...
@@ -223,7 +223,7 @@ class NodesChildrenMetatadata(APIView):
})
})
return
JsonHttpResponse
({
return
JsonHttpResponse
({
'
data'
:
data
,
'
collection'
:
collection
,
})
})
...
@@ -368,7 +368,9 @@ class NodesChildrenQueries(APIView):
...
@@ -368,7 +368,9 @@ class NodesChildrenQueries(APIView):
else
:
else
:
field
=
getattr
(
Node
,
field_name
)
field
=
getattr
(
Node
,
field_name
)
fields_list
.
append
(
fields_list
.
append
(
field
.
label
(
field_name
)
field
.
label
(
field_name
if
'.'
in
field_name
else
'node.'
+
field_name
)
)
)
# starting the query!
# starting the query!
...
@@ -409,12 +411,9 @@ class NodesChildrenQueries(APIView):
...
@@ -409,12 +411,9 @@ class NodesChildrenQueries(APIView):
.
filter
(
metadata_alias
.
metadata_id
==
metadata
.
id
)
.
filter
(
metadata_alias
.
metadata_id
==
metadata
.
id
)
)
)
# filter query
# filter query
if
metadata
.
type
==
'datetime'
:
datetime_base
=
'2000-01-01 00:00:00'
value
=
value
[:
len
(
datetime_base
)]
value
=
value
+
datetime_base
[
len
(
value
):]
query
=
query
.
filter
(
operator
(
query
=
query
.
filter
(
operator
(
getattr
(
metadata_alias
,
'value_'
+
metadata
.
type
),
value
getattr
(
metadata_alias
,
'value_'
+
metadata
.
type
),
value
))
))
elif
field
[
0
]
==
'ngrams'
:
elif
field
[
0
]
==
'ngrams'
:
query
=
query
.
filter
(
query
=
query
.
filter
(
...
@@ -431,18 +430,13 @@ class NodesChildrenQueries(APIView):
...
@@ -431,18 +430,13 @@ class NodesChildrenQueries(APIView):
# TODO: date_trunc (psql) -> index also
# TODO: date_trunc (psql) -> index also
# groupping
# groupping
if
retrieve
[
'type'
]
==
'aggregates'
:
authorized_aggregates
=
{
'count'
:
func
.
count
(
Node
.
id
)}
authorized_aggregates
=
{
'count'
:
func
.
count
(
Node
.
id
)}
for
field_name
in
fields_names
:
for
field_name
in
fields_names
:
if
field_name
not
in
authorized_aggregates
:
if
field_name
not
in
authorized_aggregates
:
# query = query.group_by(text(field_name))
query
=
query
.
group_by
(
'"
%
s"'
%
(
field_name
,
))
query
=
query
.
group_by
(
'"
%
s"'
%
(
else
:
field_name
if
'.'
in
field_name
else
'node.'
+
field_name
for
field_name
in
fields_names
:
,
))
if
'.'
in
field_name
:
field
=
'"
%
s"'
%
(
field_name
,
)
else
:
field
=
getattr
(
Node
,
field_name
)
query
=
query
.
group_by
(
field
)
# sorting
# sorting
sort_fields_names
=
request
.
DATA
.
get
(
'sort'
,
[
'id'
])
sort_fields_names
=
request
.
DATA
.
get
(
'sort'
,
[
'id'
])
...
@@ -467,9 +461,7 @@ class NodesChildrenQueries(APIView):
...
@@ -467,9 +461,7 @@ class NodesChildrenQueries(APIView):
for
key
,
value
in
pagination
.
items
():
for
key
,
value
in
pagination
.
items
():
if
key
not
in
{
'limit'
,
'offset'
}:
if
key
not
in
{
'limit'
,
'offset'
}:
raise
APIException
(
'Unrecognized parameter in "pagination": "
%
s"'
%
(
key
,
),
400
)
raise
APIException
(
'Unrecognized parameter in "pagination": "
%
s"'
%
(
key
,
),
400
)
try
:
if
not
isinstance
(
value
,
int
):
pagination
[
key
]
=
int
(
value
)
except
:
raise
APIException
(
'In "pagination", "
%
s" should be an integer.'
%
(
key
,
),
400
)
raise
APIException
(
'In "pagination", "
%
s" should be an integer.'
%
(
key
,
),
400
)
if
'offset'
not
in
pagination
:
if
'offset'
not
in
pagination
:
pagination
[
'offset'
]
=
0
pagination
[
'offset'
]
=
0
...
@@ -480,7 +472,7 @@ class NodesChildrenQueries(APIView):
...
@@ -480,7 +472,7 @@ class NodesChildrenQueries(APIView):
# respond to client!
# respond to client!
# return DebugHttpResponse(str(query))
# return DebugHttpResponse(str(query))
# return DebugHttpResponse(literalquery(query))
# return DebugHttpResponse(literalquery(query))
data
=
[
results
=
[
dict
(
zip
(
fields_names
,
row
))
dict
(
zip
(
fields_names
,
row
))
for
row
in
(
for
row
in
(
query
[
pagination
[
"offset"
]:
pagination
[
"offset"
]
+
pagination
[
"limit"
]]
query
[
pagination
[
"offset"
]:
pagination
[
"offset"
]
+
pagination
[
"limit"
]]
...
@@ -493,32 +485,44 @@ class NodesChildrenQueries(APIView):
...
@@ -493,32 +485,44 @@ class NodesChildrenQueries(APIView):
"pagination"
:
pagination
,
"pagination"
:
pagination
,
"retrieve"
:
fields_names
,
"retrieve"
:
fields_names
,
"sorted"
:
sort_fields_names
,
"sorted"
:
sort_fields_names
,
"
data"
:
data
,
"
results"
:
results
,
},
201
)
},
201
)
class
Nodes
Controller
:
class
Nodes
List
(
APIView
)
:
@
classmethod
def
get
(
self
,
request
):
def
get
(
cls
,
request
):
query
=
Node
nodes_query
=
(
Node
.
query
(
Node
.
id
,
Node
.
name
,
NodeType
.
name
)
.
join
(
NodeType
)
)
if
'type'
in
request
.
GET
:
if
'type'
in
request
.
GET
:
nodes_query
=
nodes_query
.
filter
(
NodeType
.
name
==
request
.
GET
[
'type'
])
query
=
query
.
filter
(
type__name
=
request
.
GET
[
'type'
])
if
'parent'
in
request
.
GET
:
if
'parent'
in
request
.
GET
:
nodes_query
=
nodes_query
.
filter
(
Node
.
parent_id
==
request
.
GET
[
'parent'
]
)
query
=
query
.
filter
(
parent_id
=
int
(
request
.
GET
[
'parent'
])
)
collection
=
[]
collection
=
[]
for
row
in
nodes_query
:
for
child
in
query
.
all
():
type_name
=
child
.
type
.
name
collection
.
append
({
collection
.
append
({
'id'
:
row
[
0
],
'id'
:
child
.
id
,
'name'
:
row
[
1
],
'text'
:
child
.
name
,
'type'
:
row
[
2
]
'type'
:
type_name
,
'children'
:
type_name
is
not
'Document'
,
})
})
return
JsonHttpResponse
({
'data'
:
collection
})
return
JsonHttpResponse
(
collection
)
class
Nodes
(
APIView
):
def
get
(
self
,
request
,
node_id
):
node
=
models
.
Node
.
objects
.
filter
(
id
=
node_id
)
.
first
()
if
node
is
None
:
raise
APIException
(
'This node does not exist'
,
404
)
return
JsonHttpResponse
({
'id'
:
node
.
id
,
'name'
:
node
.
name
,
'type'
:
''
,
'metadata'
:
dict
(
node
.
metadata
),
})
...
...
gargantext_web/urls.py
View file @
14034b42
...
@@ -32,36 +32,22 @@ urlpatterns = patterns('',
...
@@ -32,36 +32,22 @@ urlpatterns = patterns('',
url
(
r'^corpus/(\d+)/explorer$'
,
views
.
explorer_graph
),
url
(
r'^corpus/(\d+)/explorer$'
,
views
.
explorer_graph
),
url
(
r'^corpus/(\d+)/matrix$'
,
views
.
explorer_matrix
),
url
(
r'^corpus/(\d+)/matrix$'
,
views
.
explorer_matrix
),
# Getting data
[which?]
# Getting data
url
(
r'^chart/corpus/(\d+)/data.csv$'
,
views
.
send_csv
),
url
(
r'^chart/corpus/(\d+)/data.csv$'
,
views
.
send_csv
),
url
(
r'^corpus/(\d+)/node_link.json$'
,
views
.
node_link
),
url
(
r'^corpus/(\d+)/node_link.json$'
,
views
.
node_link
),
url
(
r'^corpus/(\d+)/adjancy_matrix$'
,
views
.
node_link
),
url
(
r'^corpus/(\d+)/adjacency.json$'
,
views
.
adjacency
),
url
(
r'^corpus/(\d+)/adjacency.json$'
,
views
.
adjacency
),
url
(
r'^api$'
,
gargantext_web
.
api
.
Root
),
# TEST
# first steps with AngularJS
url
(
r'^tests/mvc$'
,
views
.
tests_mvc
),
# RESTful API
# retrieve all the metadata from a given node's children
url
(
r'^api/nodes/(\d+)/children/metadata$'
,
gargantext_web
.
api
.
NodesChildrenMetatadata
.
as_view
()),
url
(
r'^api/nodes/(\d+)/children/metadata$'
,
gargantext_web
.
api
.
NodesChildrenMetatadata
.
as_view
()),
# retrieve the ngrams from a given node's children
url
(
r'^api/nodes/(\d+)/ngrams$'
,
gargantext_web
.
api
.
CorpusController
.
ngrams
),
# perform a query on a given node's children
url
(
r'^api/nodes/(\d+)/children/queries$'
,
gargantext_web
.
api
.
NodesChildrenQueries
.
as_view
()),
url
(
r'^api/nodes/(\d+)/children/queries$'
,
gargantext_web
.
api
.
NodesChildrenQueries
.
as_view
()),
# get all the nodes
url
(
r'^api/nodes/(\d+)$'
,
gargantext_web
.
api
.
Nodes
.
as_view
()),
url
(
r'^api/nodes$'
,
gargantext_web
.
api
.
NodesController
.
get
),
url
(
r'^api/nodes$'
,
gargantext_web
.
api
.
NodesList
.
as_view
()),
url
(
r'^api/nodes/(\d+)/ngrams$'
,
gargantext_web
.
api
.
CorpusController
.
ngrams
),
url
(
r'^api/nodes/(\d+)/data$'
,
gargantext_web
.
api
.
CorpusController
.
data
),
# other (DEPRECATED, TO BE REMOVED)
url
(
r'^graph-it$'
,
views
.
graph_it
),
url
(
r'^graph-it$'
,
gargantext_web
.
views
.
graph_it
),
url
(
r'^ngrams$'
,
views
.
ngrams
),
url
(
r'^api/nodes$'
,
gargantext_web
.
api
.
NodesController
.
get
),
url
(
r'^api/corpus/(\d+)/ngrams$'
,
gargantext_web
.
api
.
CorpusController
.
ngrams
),
# url(r'^api/corpus/(\d+)/metadata$', gargantext_web.api.CorpusController.metadata),
url
(
r'^api/corpus/(\d+)/data$'
,
gargantext_web
.
api
.
CorpusController
.
data
),
)
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment