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
22
Open
Draft:Generic api v1
David Beniamine
requested to merge
generic-api-v1
into
master
Feb 1, 2023
Overview
0
Commits
18
Pipelines
0
Changes
12
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
Feb 1, 2023
by
David Beniamine
0
0
Merge request reports
Compare
version 3
version 8
a013bb51
Feb 15, 2023
version 7
afc7e288
Feb 15, 2023
version 6
58fea514
Feb 15, 2023
version 5
f4a41998
Feb 8, 2023
version 4
621ae247
Feb 8, 2023
version 3
7f41aa0f
Feb 2, 2023
version 2
23021b81
Feb 2, 2023
version 1
24d8bec9
Feb 1, 2023
master (HEAD)
and
version 6
latest version
b96e67e1
18 commits,
Feb 15, 2023
version 8
a013bb51
17 commits,
Feb 15, 2023
version 7
afc7e288
17 commits,
Feb 15, 2023
version 6
58fea514
16 commits,
Feb 15, 2023
version 5
f4a41998
15 commits,
Feb 8, 2023
version 4
621ae247
14 commits,
Feb 8, 2023
version 3
7f41aa0f
12 commits,
Feb 2, 2023
version 2
23021b81
10 commits,
Feb 2, 2023
version 1
24d8bec9
9 commits,
Feb 1, 2023
Show latest version
12 files
+
305
−
74
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
Files
12
Mirador_backend/resources/base.py
+
57
−
28
View file @ 58fea514
Edit in single-file editor
Open in Web IDE
from
apispec.ext.marshmallow
import
MarshmallowPlugin
from
flask_apispec.extension
import
FlaskApiSpec
from
marshmallow
import
Schema
,
fields
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_apispec.views
import
MethodResource
from
flask_apispec
import
marshal_with
,
doc
,
use_kwargs
from
flask_apispec
import
doc
from
flask_restful
import
marshal
,
fields
from
Mirador_backend.models
import
BaseModel
class
SimpleResponse
(
Schema
):
message
=
fields
.
Str
(
default
=
'
Success
'
)
from
Mirador_backend.models
import
BaseModel
,
NotFound
class
ResourceGroup
(
MethodResource
,
Resource
):
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
):
return
self
.
Model
.
query
.
filter
().
all
()
"""
Handle a get request
"""
data
=
{
x
.
id
:
x
for
x
in
self
.
get_queryset
()}
list_fields
=
{
x
:
fields
.
Nested
(
self
.
Model
.
fields
)
for
x
in
data
.
keys
()}
return
marshal
(
data
,
list_fields
),
200
def
post
(
self
):
# TODO
return
{
'
method
'
:
'
post
'
,
'
mode
'
:
'
group
'
}
"""
Handle a post request
"""
m
=
Model
().
create
(
self
.
parseData
(
request
))
return
marshal
(
m
,
self
.
Model
.
fields
),
200
def
genDoc
(
descr
,
tags
):
doc
(
description
=
f
'
Get all
{
descr
}
'
,
tags
=
tags
)(
ResourceGroup
.
get
)
doc
(
description
=
f
'
Create a
{
descr
}
'
,
tags
=
tags
)(
ResourceGroup
.
post
)
# TODO response on JSON object given in param
marshal_with
(
SimpleResponse
)(
ResourceGroup
.
post
)
# TODO marshal collection get response using given object
class
ResourceItem
(
MethodResource
,
Resource
):
Model
=
BaseModel
def
get
(
self
,
id
):
return
self
.
Model
.
query
.
filter
(
self
.
Model
.
id
==
id
).
first
()
def
get_queryset
(
self
,
id
):
"""
Get the queryset for the request
"""
return
self
.
Model
.
find
(
id
)
def
post
(
self
,
id
):
# TODO
return
{
'
method
'
:
'
post
'
,
'
id
'
:
id
,
'
mode
'
:
'
item
'
}
def
get_queryset_or_abort
(
self
,
id
):
"""
Get the queryset for the request or aborts with a 404
"""
try
:
return
self
.
get_queryset
(
id
)
except
NotFound
:
abort
(
404
)
def
parseData
(
self
,
request
):
"""
Read the request and return only the useful data
"""
return
request
.
form
def
get
(
self
,
id
):
"""
Handle a get request
"""
return
marshal
(
self
.
get_queryset_or_abort
(
id
),
self
.
Model
.
fields
),
200
def
put
(
self
,
id
):
# TODO
return
{
'
method
'
:
'
put
'
,
'
id
'
:
id
,
'
mode
'
:
'
item
'
}
"""
Handle a put request
"""
data
=
self
.
parseData
(
request
)
try
:
m
=
self
.
get_queryset
(
id
)
m
.
clear
()
m
.
fill
(
data
)
m
=
m
.
save
()
except
NotFound
:
m
=
self
.
Model
.
create
(
data
)
return
marshal
(
model
,
self
.
Model
.
fields
),
200
def
patch
(
self
,
id
):
# TODO
return
{
'
method
'
:
'
patch
'
,
'
id
'
:
id
,
'
mode
'
:
'
item
'
}
"""
Handle a patch request
"""
m
=
self
.
get_queryset_or_abort
(
id
)
m
.
clear
()
m
.
fill
(
self
.
parseData
(
request
))
return
marshal
(
m
.
save
(),
self
.
Model
.
fields
),
200
def
delete
(
self
,
id
):
# TODO
return
{
'
method
'
:
'
delete
'
,
'
id
'
:
id
,
'
mode
'
:
'
item
'
}
"""
Handle a delete request
"""
m
=
self
.
get_queryset_or_abort
(
id
)
return
marshal
(
m
.
delete
(),
fields
.
Boolean
),
200
def
genDoc
(
descr
,
tags
):
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
'
Updates a
{
descr
}
by id
'
,
tags
=
tags
)(
ResourceItem
.
put
)
doc
(
description
=
f
'
Create or Update a
{
descr
}
by id
'
,
tags
=
tags
)(
ResourceItem
.
put
)
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
)
# TODO marshal
Loading