Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
B
backend
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
IIIF
backend
Merge requests
!2
Draft:Generic api v1
Code
Review changes
Check out branch
Download
Patches
Plain diff
Expand sidebar
Open
Draft:Generic api v1
generic-api-v1
into
master
Overview
0
Commits
18
Pipelines
0
Changes
21
Open
David Beniamine
requested to merge
generic-api-v1
into
master
2 years ago
Overview
0
Commits
18
Pipelines
0
Changes
8
Adds a generic api URIs and models
Prefix path with api version
Generic config
Database initialization
Generic resources
Use generic Model
Auto documentation for parameter
Auto documentation for return values
Generic routes
group
GET
POST
item
GET
POST
PUT
PATCH
DELETE
ORM
tests
test infrastucture and simple exemple
Add data
Test storing data
Work on fake database
Edited
2 years ago
by
David Beniamine
0
0
Merge request reports
Compare
version 3
version 8
a013bb51
2 years ago
version 7
afc7e288
2 years ago
version 6
58fea514
2 years ago
version 5
f4a41998
2 years ago
version 4
621ae247
2 years ago
version 3
7f41aa0f
2 years ago
version 2
23021b81
2 years ago
version 1
24d8bec9
2 years ago
master (HEAD)
and
version 4
latest version
b96e67e1
18 commits,
2 years ago
version 8
a013bb51
17 commits,
2 years ago
version 7
afc7e288
17 commits,
2 years ago
version 6
58fea514
16 commits,
2 years ago
version 5
f4a41998
15 commits,
2 years ago
version 4
621ae247
14 commits,
2 years ago
version 3
7f41aa0f
12 commits,
2 years ago
version 2
23021b81
10 commits,
2 years ago
version 1
24d8bec9
9 commits,
2 years ago
Show latest version
8 files
+
234
−
41
Side-by-side
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
Files
8
Mirador_backend/resources/base.py
+
47
−
27
View file @ 621ae247
Edit in single-file editor
Open in Web IDE
Show full file
@@ -3,62 +3,82 @@ from flask_apispec.extension import FlaskApiSpec
@@ -3,62 +3,82 @@ from flask_apispec.extension import FlaskApiSpec
from
marshmallow
import
Schema
,
fields
from
marshmallow
import
Schema
,
fields
from
apispec
import
APISpec
from
apispec
import
APISpec
from
flask_restful
import
Resource
from
flask
import
request
from
flask_restful
import
Resource
,
abort
from
flask_restful_swagger
import
swagger
from
flask_restful_swagger
import
swagger
from
flask_apispec.views
import
MethodResource
from
flask_apispec.views
import
MethodResource
from
flask_apispec
import
marshal_with
,
doc
,
use_kwargs
from
flask_apispec
import
marshal_with
,
doc
,
use_kwargs
from
Mirador_backend.models
import
BaseModel
from
Mirador_backend.models
import
BaseModel
,
NotFound
class
SimpleResponse
(
Schema
):
message
=
fields
.
Str
(
default
=
'
Success
'
)
class
ResourceGroup
(
MethodResource
,
Resource
):
class
ResourceGroup
(
MethodResource
,
Resource
):
Model
=
BaseModel
Model
=
BaseModel
def
parseData
(
self
,
request
):
"""
Read the request and return only the useful data
"""
return
request
.
form
def
get_queryset
(
self
):
"""
Get the queryset for the request
"""
return
self
.
Model
.
all
()
def
get
(
self
):
def
get
(
self
):
return
self
.
Model
.
query
.
filter
().
all
()
"""
Handle a get request
"""
return
[
x
.
serialized
for
x
in
self
.
get_queryset
()]
def
post
(
self
):
def
post
(
self
):
# TODO
"""
Handle a post request
"""
return
{
'
method
'
:
'
post
'
,
'
mode
'
:
'
group
'
}
return
Model
().
create
(
self
.
parseData
(
request
)).
serialized
def
genDoc
(
descr
,
tags
):
def
genDoc
(
descr
,
tags
,
klass
):
doc
(
description
=
f
'
Get all
{
descr
}
'
,
tags
=
tags
)(
ResourceGroup
.
get
)
doc
(
description
=
f
'
Get all
{
descr
}
'
,
tags
=
tags
)(
ResourceGroup
.
get
)
doc
(
description
=
f
'
Create a
{
descr
}
'
,
tags
=
tags
)(
ResourceGroup
.
post
)
doc
(
description
=
f
'
Create a
{
descr
}
'
,
tags
=
tags
)(
ResourceGroup
.
post
)
# TODO response on JSON object given in param
klass_fields
=
{
x
:
fields
.
Raw
()
for
x
in
klass
.
data_fields
}
marshal_with
(
SimpleResponse
)(
ResourceGroup
.
post
)
# TODO marshal
# TODO marshal collection get response using given object
#marshal_with(klass_fields)(ResourceGroup.post)
#marshal_with(fields.List(klass_fields))(ResourceGroup.get)
class
ResourceItem
(
MethodResource
,
Resource
):
class
ResourceItem
(
MethodResource
,
Resource
):
Model
=
BaseModel
Model
=
BaseModel
def
get
(
self
,
id
):
def
get_queryset
(
self
,
id
):
return
self
.
Model
.
query
.
filter
(
self
.
Model
.
id
==
id
).
first
()
"""
Get the queryset for the request
"""
try
:
return
self
.
Model
.
find
(
id
).
serialized
except
NotFound
:
abort
(
404
)
def
post
(
self
,
id
):
def
parseData
(
self
,
request
):
# TODO
"""
Read the request and return only the useful data
"""
return
{
'
method
'
:
'
post
'
,
'
id
'
:
id
,
'
mode
'
:
'
item
'
}
return
request
.
form
def
get
(
self
,
id
):
"""
Handle a get request
"""
return
self
.
get_queryset
(
id
).
serialized
def
put
(
self
,
id
):
def
put
(
self
,
id
):
# TODO
"""
Handle a put request
"""
return
{
'
method
'
:
'
put
'
,
'
id
'
:
id
,
'
mode
'
:
'
item
'
}
m
=
self
.
get_queryset
(
id
)
m
.
clear
()
m
.
fill
(
self
.
parseData
(
request
))
return
m
.
save
().
serialized
def
patch
(
self
,
id
):
def
patch
(
self
,
id
):
# TODO
"""
Handle a patch request
"""
return
{
'
method
'
:
'
patch
'
,
'
id
'
:
id
,
'
mode
'
:
'
item
'
}
m
=
self
.
get_queryset
(
id
)
m
.
clear
()
m
.
fill
(
self
.
parseData
(
request
))
return
m
.
save
().
serialized
def
delete
(
self
,
id
):
def
delete
(
self
,
id
):
# TODO
"""
Handle a delete request
"""
return
{
'
method
'
:
'
delete
'
,
'
id
'
:
id
,
'
mode
'
:
'
item
'
}
return
self
.
get_queryset
(
id
).
delete
()
def
genDoc
(
descr
,
tags
):
def
genDoc
(
descr
,
tags
,
klass
):
doc
(
description
=
f
'
Get
{
descr
}
by id
'
,
tags
=
tags
)(
ResourceItem
.
get
)
doc
(
description
=
f
'
Get
{
descr
}
by id
'
,
tags
=
tags
)(
ResourceItem
.
get
)
doc
(
description
=
f
'
Create or update a
{
descr
}
by id
'
,
tags
=
tags
)(
ResourceItem
.
post
)
doc
(
description
=
f
'
Create or Update a
{
descr
}
by id
'
,
tags
=
tags
)(
ResourceItem
.
put
)
doc
(
description
=
f
'
Updates a
{
descr
}
by id
'
,
tags
=
tags
)(
ResourceItem
.
put
)
doc
(
description
=
f
'
Updates partially a
{
descr
}
by id
'
,
tags
=
tags
)(
ResourceItem
.
patch
)
doc
(
description
=
f
'
Updates partially a
{
descr
}
by id
'
,
tags
=
tags
)(
ResourceItem
.
patch
)
doc
(
description
=
f
'
deletes a
{
descr
}
by id
'
,
tags
=
tags
)(
ResourceItem
.
delete
)
doc
(
description
=
f
'
deletes a
{
descr
}
by id
'
,
tags
=
tags
)(
ResourceItem
.
delete
)
# TODO marshal
# TODO marshal
Loading