Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
GraceDB Server
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
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 GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Michael William Coughlin
GraceDB Server
Commits
cb7d63a4
Commit
cb7d63a4
authored
6 years ago
by
Tanner Prestegard
Committed by
GraceDB
6 years ago
Browse files
Options
Downloads
Patches
Plain Diff
abstracting get_object method in several views to a generic mixin
parent
af0fcb01
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
gracedb/superevents/api/mixins.py
+16
-0
16 additions, 0 deletions
gracedb/superevents/api/mixins.py
gracedb/superevents/api/views.py
+8
-40
8 additions, 40 deletions
gracedb/superevents/api/views.py
with
24 additions
and
40 deletions
gracedb/superevents/api/mixins.py
+
16
−
0
View file @
cb7d63a4
...
...
@@ -28,3 +28,19 @@ class GetParentSupereventMixin(GetParentMixin):
# Currently, superevent_id ~ S0001, where 1 is the PK
return
{
'
id
'
:
int
(
superevent_id
[
1
:])}
class
BaseGetObjectMixin
(
object
):
query_field
=
None
def
__init__
(
self
,
*
args
,
**
kwargs
):
super
(
BaseGetObjectMixin
,
self
).
__init__
(
*
args
,
**
kwargs
)
if
self
.
query_field
is
None
:
self
.
query_field
=
self
.
lookup_field
def
get_object
(
self
):
# TODO: do we need some kind of permissions check in here somewhere?
queryset
=
self
.
filter_queryset
(
self
.
get_queryset
())
query_value
=
self
.
kwargs
.
get
(
self
.
lookup_field
)
filter_kwargs
=
{
self
.
query_field
:
query_value
}
obj
=
get_object_or_404
(
queryset
,
**
filter_kwargs
)
return
obj
This diff is collapsed.
Click to expand it.
gracedb/superevents/api/views.py
+
8
−
40
View file @
cb7d63a4
...
...
@@ -22,7 +22,7 @@ from events.view_utils import reverse as gracedb_reverse
#from events.api.views import IsAuthorizedForPipeline, LigoLwRenderer
from
events.api.backends
import
LigoAuthentication
from
.mixins
import
GetParentSupereventMixin
from
.mixins
import
GetParentSupereventMixin
,
BaseGetObjectMixin
from
.paginators
import
BasePaginationFactory
,
CustomLabelPagination
,
\
CustomLogTagPagination
,
CustomSupereventPagination
from
.serializers
import
SupereventSerializer
,
SupereventUpdateSerializer
,
\
...
...
@@ -111,11 +111,13 @@ class SupereventEventViewSet(mixins.ListModelMixin,
class
SupereventLabelViewSet
(
GetParentSupereventMixin
,
BaseGetObjectMixin
,
viewsets
.
ModelViewSet
):
"""
Superevent labels
"""
serializer_class
=
SupereventLabelSerializer
pagination_class
=
CustomLabelPagination
lookup_field
=
'
label_name
'
query_field
=
'
label__name
'
def
get_queryset
(
self
):
superevent
=
self
.
get_parent
()
...
...
@@ -123,13 +125,6 @@ class SupereventLabelViewSet(GetParentSupereventMixin,
queryset
=
superevent
.
labelling_set
.
all
()
return
queryset
def
get_object
(
self
):
queryset
=
self
.
filter_queryset
(
self
.
get_queryset
())
label_name
=
self
.
kwargs
.
get
(
self
.
lookup_field
,
None
)
filter_kwargs
=
{
'
label__name
'
:
label_name
}
obj
=
get_object_or_404
(
queryset
,
**
filter_kwargs
)
return
obj
def
destroy
(
self
,
request
,
*
args
,
**
kwargs
):
instance
=
self
.
get_object
()
remove_label_from_superevent
(
instance
,
request
.
user
,
...
...
@@ -141,6 +136,7 @@ class SupereventLogViewSet(mixins.ListModelMixin,
mixins
.
RetrieveModelMixin
,
mixins
.
CreateModelMixin
,
GetParentSupereventMixin
,
BaseGetObjectMixin
,
viewsets
.
GenericViewSet
):
"""
View for log messages attached to a superevent.
...
...
@@ -157,18 +153,9 @@ class SupereventLogViewSet(mixins.ListModelMixin,
# filter for those tagged with external access tagname if is_external(request.user)
return
queryset
# TODO: generalize this method, can be used for superevent, superevent-event, etc.
def
get_object
(
self
):
logger
.
warning
(
'
can probably do this generically on some mixin
'
)
queryset
=
self
.
filter_queryset
(
self
.
get_queryset
())
N
=
self
.
kwargs
.
get
(
self
.
lookup_field
,
None
)
filter_kwargs
=
{
'
N
'
:
N
}
obj
=
get_object_or_404
(
queryset
,
**
filter_kwargs
)
logger
.
warning
(
'
check permissions here? or just in get_queryset?
'
)
return
obj
class
SupereventLogTagViewSet
(
GetParentSupereventMixin
,
BaseGetObjectMixin
,
viewsets
.
ModelViewSet
):
"""
View for tags attached to a log message which is attached to a superevent.
...
...
@@ -176,6 +163,7 @@ class SupereventLogTagViewSet(GetParentSupereventMixin,
serializer_class
=
SupereventLogTagSerializer
pagination_class
=
CustomLogTagPagination
lookup_field
=
'
tag_name
'
query_field
=
'
name
'
def
get_parent_log
(
self
):
# TODO: check superevent permissions here
...
...
@@ -187,18 +175,12 @@ class SupereventLogTagViewSet(GetParentSupereventMixin,
parent_log
=
self
.
get_parent_log
()
return
parent_log
.
tags
.
all
()
def
get_object
(
self
):
queryset
=
self
.
filter_queryset
(
self
.
get_queryset
())
tag_name
=
self
.
kwargs
.
get
(
self
.
lookup_field
,
None
)
filter_kwargs
=
{
'
name
'
:
tag_name
}
obj
=
get_object_or_404
(
queryset
,
**
filter_kwargs
)
return
obj
def
perform_destroy
(
self
,
instance
):
parent_log
=
self
.
get_parent_log
()
remove_tag_from_log
(
parent_log
,
instance
,
self
.
request
.
user
,
add_log_message
=
True
,
issue_alert
=
True
)
# TODO: add permissions to this viewset
class
SupereventFileViewSet
(
GetParentSupereventMixin
,
viewsets
.
ViewSet
):
...
...
@@ -261,6 +243,7 @@ class SupereventVOEventViewSet(mixins.ListModelMixin,
mixins
.
RetrieveModelMixin
,
mixins
.
CreateModelMixin
,
GetParentSupereventMixin
,
BaseGetObjectMixin
,
viewsets
.
GenericViewSet
):
"""
View for VOEvents attached to a superevent.
...
...
@@ -275,14 +258,6 @@ class SupereventVOEventViewSet(mixins.ListModelMixin,
# filter for those tagged with external access tagname if is_external(request.user)
return
queryset
# TODO: generalize this method, can be used for superevent, superevent-event, etc.
def
get_object
(
self
):
queryset
=
self
.
filter_queryset
(
self
.
get_queryset
())
N
=
self
.
kwargs
.
get
(
self
.
lookup_field
,
None
)
filter_kwargs
=
{
'
N
'
:
N
}
obj
=
get_object_or_404
(
queryset
,
**
filter_kwargs
)
return
obj
class
SupereventEMObservationViewSet
(
mixins
.
ListModelMixin
,
mixins
.
RetrieveModelMixin
,
...
...
@@ -302,10 +277,3 @@ class SupereventEMObservationViewSet(mixins.ListModelMixin,
# filter for those tagged with external access tagname if is_external(request.user)
return
queryset
# TODO: generalize this method, can be used for superevent, superevent-event, etc.
def
get_object
(
self
):
queryset
=
self
.
filter_queryset
(
self
.
get_queryset
())
N
=
self
.
kwargs
.
get
(
self
.
lookup_field
,
None
)
filter_kwargs
=
{
'
N
'
:
N
}
obj
=
get_object_or_404
(
queryset
,
**
filter_kwargs
)
return
obj
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