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
7
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
7
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
Viewing commit
1d240327
Prev
Next
Show latest version
7 files
+
175
−
13
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
Files
7
Verified
1d240327
ORM working with a bit of documentation
· 1d240327
David Beniamine
authored
Feb 8, 2023
Mirador_backend/resources/base.py
0 → 100644
+
96
−
0
View file @ 57ce660a
Edit in single-file editor
Open in Web IDE
from
apispec.ext.marshmallow
import
MarshmallowPlugin
from
flask_apispec.extension
import
FlaskApiSpec
from
apispec
import
APISpec
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
doc
from
flask_restful
import
marshal
,
fields
from
Mirador_backend.models
import
BaseModel
,
NotFound
from
Mirador_backend.utils.app
import
getApp
logger
=
getApp
().
logger
class
ResourceGroup
(
MethodResource
,
Resource
):
Model
=
BaseModel
def
parseData
(
self
,
request
):
"""
Read the request and return only the useful data
"""
return
request
.
json
def
get_queryset
(
self
):
"""
Get the queryset for the request
"""
return
self
.
Model
.
all
()
def
get
(
self
):
"""
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
):
"""
Handle a post request
"""
m
=
self
.
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
)
class
ResourceItem
(
MethodResource
,
Resource
):
Model
=
BaseModel
def
get_queryset
(
self
,
id
):
"""
Get the queryset for the request
"""
return
self
.
Model
.
find
(
id
)
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
.
json
def
get
(
self
,
id
):
"""
Handle a get request
"""
return
marshal
(
self
.
get_queryset_or_abort
(
id
),
self
.
Model
.
fields
),
200
def
put
(
self
,
id
):
"""
Handle a put request
"""
data
=
self
.
parseData
(
request
)
try
:
m
=
self
.
get_queryset
(
id
)
m
.
clear
()
m
.
fill
(
data
)
m
=
m
.
save
()
except
NotFound
:
data
[
'
id
'
]
=
id
m
=
self
.
Model
().
create
(
data
)
return
marshal
(
m
,
self
.
Model
.
fields
),
200
def
patch
(
self
,
id
):
"""
Handle a patch request
"""
m
=
self
.
get_queryset_or_abort
(
id
)
m
.
fill
(
self
.
parseData
(
request
))
m
.
save
()
return
marshal
(
m
,
self
.
Model
.
fields
),
200
def
delete
(
self
,
id
):
"""
Handle a delete request
"""
m
=
self
.
get_queryset_or_abort
(
id
)
return
marshal
({
'
success
'
:
m
.
delete
()},
{
'
success
'
:
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
.
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
)
Loading