Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
C
core
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
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
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to JiHu GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Chenhao Ma
core
Commits
a540040f
Commit
a540040f
authored
8 years ago
by
Megan Henning
Browse files
Options
Downloads
Patches
Plain Diff
Return required fields
parent
9a41dd4d
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
api/dao/containerstorage.py
+51
-6
51 additions, 6 deletions
api/dao/containerstorage.py
api/handlers/containerhandler.py
+6
-30
6 additions, 30 deletions
api/handlers/containerhandler.py
with
57 additions
and
36 deletions
api/dao/containerstorage.py
+
51
−
6
View file @
a540040f
...
...
@@ -18,6 +18,23 @@ CHILD_MAP = {
'
sessions
'
:
'
acquisitions
'
}
# All "containers" are required to return these fields
# 'All' includes users
BASE_DEFAULTS
=
{
'
_id
'
:
None
,
'
created
'
:
None
,
'
modified
'
:
None
}
# All containers that inherit from 'container' in the DM
CONTAINER_DEFAULTS
=
{
'
permissions
'
:
[],
'
files
'
:
[],
'
notes
'
:
[],
'
tags
'
:
[],
'
info
'
:
{}
}
class
ContainerStorage
(
object
):
"""
This class provides access to mongodb collection elements (called containers).
...
...
@@ -30,7 +47,6 @@ class ContainerStorage(object):
self
.
use_object_id
=
use_object_id
self
.
dbc
=
config
.
db
[
cont_name
]
@staticmethod
def
factory
(
cont_name
,
use_object_id
=
False
):
"""
...
...
@@ -48,6 +64,15 @@ class ContainerStorage(object):
else
:
return
ContainerStorage
(
cont_name
,
use_object_id
)
def
_fill_default_values
(
self
,
cont
):
if
cont
:
defaults
=
BASE_DEFAULTS
.
copy
()
if
self
.
cont_name
not
in
[
'
groups
'
,
'
users
'
]:
defaults
.
update
(
CONTAINER_DEFAULTS
)
defaults
.
update
(
cont
)
cont
=
defaults
return
cont
def
get_container
(
self
,
_id
,
projection
=
None
,
get_children
=
False
):
cont
=
self
.
get_el
(
_id
,
projection
=
projection
)
if
cont
is
None
:
...
...
@@ -84,9 +109,9 @@ class ContainerStorage(object):
data_op
=
payload
or
{
'
_id
'
:
_id
}
check
(
data_op
)
if
action
==
'
GET
'
and
_id
:
return
self
.
get_el
(
_id
,
projection
=
projection
)
return
self
.
get_el
(
_id
,
projection
=
projection
,
fill_defaults
=
True
)
if
action
==
'
GET
'
:
return
self
.
get_all_el
(
query
,
user
,
projection
)
return
self
.
get_all_el
(
query
,
user
,
projection
,
fill_defaults
=
True
)
if
action
==
'
DELETE
'
:
return
self
.
delete_el
(
_id
)
if
action
==
'
PUT
'
:
...
...
@@ -142,15 +167,18 @@ class ContainerStorage(object):
raise
APIStorageException
(
e
.
message
)
return
self
.
dbc
.
delete_one
({
'
_id
'
:
_id
})
def
get_el
(
self
,
_id
,
projection
=
None
):
def
get_el
(
self
,
_id
,
projection
=
None
,
fill_defaults
=
False
):
if
self
.
use_object_id
:
try
:
_id
=
bson
.
objectid
.
ObjectId
(
_id
)
except
bson
.
errors
.
InvalidId
as
e
:
raise
APIStorageException
(
e
.
message
)
return
self
.
_from_mongo
(
self
.
dbc
.
find_one
(
_id
,
projection
))
cont
=
self
.
_from_mongo
(
self
.
dbc
.
find_one
(
_id
,
projection
))
if
fill_defaults
:
cont
=
self
.
_fill_default_values
(
cont
)
return
cont
def
get_all_el
(
self
,
query
,
user
,
projection
):
def
get_all_el
(
self
,
query
,
user
,
projection
,
fill_defaults
=
False
):
if
user
:
if
query
.
get
(
'
permissions
'
):
query
[
'
$and
'
]
=
[{
'
permissions
'
:
{
'
$elemMatch
'
:
user
}},
{
'
permissions
'
:
query
.
pop
(
'
permissions
'
)}]
...
...
@@ -161,6 +189,8 @@ class ContainerStorage(object):
results
=
list
(
self
.
dbc
.
find
(
query
,
projection
))
for
cont
in
results
:
cont
=
self
.
_from_mongo
(
cont
)
if
fill_defaults
:
cont
=
self
.
_fill_default_values
(
cont
)
return
results
class
GroupStorage
(
ContainerStorage
):
...
...
@@ -168,6 +198,13 @@ class GroupStorage(ContainerStorage):
def
__init__
(
self
):
super
(
GroupStorage
,
self
).
__init__
(
'
groups
'
,
use_object_id
=
False
)
def
_fill_default_values
(
self
,
cont
):
cont
=
super
(
GroupStorage
,
self
).
_fill_default_values
(
cont
)
if
cont
:
if
'
roles
'
not
in
cont
:
cont
[
'
roles
'
]
=
[]
return
cont
def
create_el
(
self
,
payload
):
log
.
debug
(
payload
)
roles
=
payload
.
pop
(
'
roles
'
)
...
...
@@ -252,6 +289,14 @@ class SessionStorage(ContainerStorage):
def
__init__
(
self
):
super
(
SessionStorage
,
self
).
__init__
(
'
sessions
'
,
use_object_id
=
True
)
def
_fill_default_values
(
self
,
cont
):
cont
=
super
(
SessionStorage
,
self
).
_fill_default_values
(
cont
)
if
cont
:
s_defaults
=
{
'
analyses
'
:
[],
'
subject
'
:{}}
s_defaults
.
update
(
cont
)
cont
=
s_defaults
return
cont
def
create_el
(
self
,
payload
):
project
=
ProjectStorage
().
get_container
(
payload
[
'
project
'
])
if
project
.
get
(
'
template
'
):
...
...
This diff is collapsed.
Click to expand it.
api/handlers/containerhandler.py
+
6
−
30
View file @
a540040f
...
...
@@ -64,7 +64,7 @@ class ContainerHandler(base.RequestHandler):
'
parent_storage
'
:
containerstorage
.
ProjectStorage
(),
'
storage_schema_file
'
:
'
session.json
'
,
'
payload_schema_file
'
:
'
session.json
'
,
'
list_projection
'
:
{
'
info
'
:
0
},
'
list_projection
'
:
{
'
info
'
:
0
,
'
analyses
'
:
0
},
'
propagated_properties
'
:
[
'
archived
'
],
'
children_cont
'
:
'
acquisitions
'
},
...
...
@@ -74,7 +74,7 @@ class ContainerHandler(base.RequestHandler):
'
parent_storage
'
:
containerstorage
.
SessionStorage
(),
'
storage_schema_file
'
:
'
acquisition.json
'
,
'
payload_schema_file
'
:
'
acquisition.json
'
,
'
list_projection
'
:
{
'
info
'
:
0
}
'
list_projection
'
:
{
'
info
'
:
0
,
'
collections
'
:
0
}
}
}
...
...
@@ -83,30 +83,6 @@ class ContainerHandler(base.RequestHandler):
self
.
storage
=
None
self
.
config
=
None
def
_fill_required_fields
(
self
,
cont
,
cont_name
):
"""
Fills in any missing container fields required for GET requests
"""
base_cont
=
{
'
_id
'
:
None
,
'
created
'
:
None
,
'
modified
'
:
None
,
'
views
'
:
None
,
'
downloads
'
:
None
,
'
notes
'
:
[],
'
files
'
:
[],
'
analyses
'
:
[],
'
permissions
'
:
[],
'
tags
'
:
[],
'
info
'
:
{}
}
base_cont
.
update
(
cont
)
if
cont_name
==
'
sessions
'
:
if
base_cont
.
get
(
'
subject
'
,
None
)
is
None
:
base_cont
[
'
subject
'
]
=
{}
return
base_cont
def
get
(
self
,
cont_name
,
**
kwargs
):
_id
=
kwargs
.
pop
(
'
cid
'
)
self
.
config
=
self
.
container_handler_configurations
[
cont_name
]
...
...
@@ -130,7 +106,7 @@ class ContainerHandler(base.RequestHandler):
if
cont_name
==
'
sessions
'
:
result
=
self
.
handle_analyses
(
result
)
return
self
.
_fill_required_fields
(
self
.
handle_origin
(
result
)
,
cont_name
)
return
self
.
handle_origin
(
result
)
def
handle_origin
(
self
,
result
):
"""
...
...
@@ -252,8 +228,8 @@ class ContainerHandler(base.RequestHandler):
self
.
storage
=
self
.
config
[
'
storage
'
]
projection
=
self
.
config
[
'
list_projection
'
]
if
self
.
is_true
(
'
metadata
'
):
projection
.
pop
(
'
metadata
'
)
if
self
.
is_true
(
'
info
'
):
projection
.
pop
(
'
info
'
)
if
not
projection
:
projection
=
None
...
...
@@ -295,7 +271,7 @@ class ContainerHandler(base.RequestHandler):
result
=
self
.
handle_analyses
(
result
)
if
self
.
is_true
(
'
stats
'
):
result
=
containerutil
.
get_stats
(
result
,
cont_name
)
result
=
self
.
_fill_required_fields
(
self
.
handle_origin
(
result
)
,
cont_name
)
result
=
self
.
handle_origin
(
result
)
modified_results
.
append
(
result
)
return
modified_results
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment