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
7aadc2db
Commit
7aadc2db
authored
Nov 15, 2014
by
Mathieu Rodic
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[FEATURE] Added a RESTful URL : /api/corpus/95/ngrams/metadata
parent
17c17579
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
42 additions
and
6 deletions
+42
-6
api.py
gargantext_web/api.py
+40
-5
urls.py
gargantext_web/urls.py
+2
-1
No files found.
gargantext_web/api.py
View file @
7aadc2db
from
django.http
import
HttpResponseNotFound
,
HttpResponse
,
HttpResponseBadRequest
from
django.http
import
HttpResponseNotFound
,
HttpResponse
,
Http404
from
django.core.exceptions
import
PermissionDenied
,
SuspiciousOperation
from
django.core.exceptions
import
ValidationError
from
django.db.models
import
Avg
,
Max
,
Min
,
Count
from
node.models
import
NodeType
,
Node
,
Ngram
from
django.db
import
connection
# from node.models import Language, ResourceType, Resource
# from node.models import Node, NodeType, Node_Resource, Project, Corpus
# from node.admin import CorpusForm, ProjectForm, ResourceForm
...
...
@@ -14,23 +18,26 @@ def JsonHttpResponse(data, status=200):
content_type
=
"application/json"
,
status
=
status
)
Http400
=
SuspiciousOperation
Http403
=
PermissionDenied
_ngrams_order_columns
=
{
"frequency"
:
"-count"
,
"alphabetical"
:
"terms"
}
def
ngrams
(
request
,
corpus_id
):
def
corpus_ngrams
(
request
,
corpus_id
):
# parameters retrieval and control
corpusQuery
=
Node
.
objects
.
filter
(
id
=
corpus_id
)
if
not
corpusQuery
:
r
eturn
HttpResponseNotFound
(
"No such corpus."
)
r
aise
Http404
(
"No such corpus."
)
corpus
=
corpusQuery
.
first
()
if
corpus
.
type
.
name
!=
'Corpus'
:
r
eturn
HttpResponseNotFound
(
"No such corpus."
)
r
aise
Http404
(
"No such corpus."
)
order
=
request
.
GET
.
get
(
'order'
,
'frequency'
)
if
order
not
in
_ngrams_order_columns
:
r
eturn
HttpResponseBadRequest
(
'The order parameter should take one of the following values: '
+
', '
.
join
(
_ngrams_order_columns
)
)
r
aise
ValidationError
(
'The order parameter should take one of the following values: '
+
', '
.
join
(
_ngrams_order_columns
),
400
)
order_column
=
_ngrams_order_columns
[
order
]
# query building
ngramsQuery
=
Ngram
.
objects
.
filter
(
...
...
@@ -46,4 +53,32 @@ def ngrams(request, corpus_id):
# response building
return
JsonHttpResponse
({
"list"
:
[
ngram
.
terms
for
ngram
in
ngramsQuery
],
})
def
corpus_metadata
(
request
,
corpus_id
):
# parameters retrieval and control
corpusQuery
=
Node
.
objects
.
filter
(
id
=
corpus_id
)
if
not
corpusQuery
:
raise
Http404
(
"No such corpus."
)
corpus
=
corpusQuery
.
first
()
if
corpus
.
type
.
name
!=
'Corpus'
:
raise
Http404
(
"No such corpus."
)
# query building
cursor
=
connection
.
cursor
()
cursor
.
execute
(
''' SELECT
key,
COUNT(*) AS count
FROM (
SELECT skeys(metadata) AS key
FROM
%
s
) AS keys
GROUP BY
key
ORDER BY
count DESC
'''
%
(
Node
.
_meta
.
db_table
,
))
# response building
return
JsonHttpResponse
({
"list"
:
[
row
[
0
]
for
row
in
cursor
.
fetchall
()],
})
\ No newline at end of file
gargantext_web/urls.py
View file @
7aadc2db
...
...
@@ -40,7 +40,8 @@ urlpatterns = patterns('',
url
(
r'^chart/corpus/(\d+)/data.csv$'
,
send_csv
),
url
(
r'^graph.json$'
,
send_graph
),
url
(
r'^api/corpus/(\d+)/ngrams$'
,
gargantext_web
.
api
.
ngrams
),
url
(
r'^api/corpus/(\d+)/ngrams$'
,
gargantext_web
.
api
.
corpus_ngrams
),
url
(
r'^api/corpus/(\d+)/metadata$'
,
gargantext_web
.
api
.
corpus_metadata
),
)
from
django.conf
import
settings
...
...
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