SciPost Code Repository
Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
SciPost
Manage
Activity
Members
Labels
Plan
Issues
118
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
0
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
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
SciPost
SciPost
Commits
92283ca6
Commit
92283ca6
authored
1 year ago
by
George Katsikas
Browse files
Options
Downloads
Patches
Plain Diff
add notes managers
parent
f2788657
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
scipost_django/pins/managers.py
+54
-0
54 additions, 0 deletions
scipost_django/pins/managers.py
scipost_django/pins/models.py
+4
-0
4 additions, 0 deletions
scipost_django/pins/models.py
with
58 additions
and
0 deletions
scipost_django/pins/managers.py
0 → 100644
+
54
−
0
View file @
92283ca6
__copyright__
=
"
Copyright © Stichting SciPost (SciPost Foundation)
"
__license__
=
"
AGPL v3
"
from
typing
import
Optional
from
django.contrib.auth.models
import
User
from
django.contrib.contenttypes.models
import
ContentType
from
django.db
import
models
class
NotesQuerySet
(
models
.
QuerySet
):
def
visible_to
(
self
,
user
:
Optional
[
User
]
=
None
,
model
:
Optional
[
models
.
Model
]
=
None
,
):
"""
Filter out notes which are not visible to the given user.
"""
if
user
is
None
:
# Without specifying a user, only public notes are visible
return
self
.
filter
(
visibility
=
self
.
model
.
VISIBILITY_PUBLIC
)
else
:
# Filter non-author users from viewing private notes
self
=
self
.
exclude
(
models
.
Q
(
visibility
=
self
.
model
.
VISIBILITY_PRIVATE
)
&
~
models
.
Q
(
author
=
user
.
contributor
)
)
# Filter out internal notes unless the user has the default "manager"
# permission for the given object, e.g. "can_manage_subsidies"
# If no model is given, just filter out all of them
model_plural
=
str
(
model
.
_meta
.
verbose_name_plural
).
lower
()
if
model
else
""
if
model
is
None
or
not
user
.
has_perm
(
f
"
pins.can_manage_
{
model_plural
}
"
):
self
=
self
.
exclude
(
visibility
=
self
.
model
.
VISIBILITY_INTERNAL
)
return
self
def
for_object
(
self
,
content_type
,
object_id
):
"""
Filter notes for a given object.
"""
return
self
.
filter
(
regarding_content_type
=
content_type
,
regarding_object_id
=
object_id
,
)
def
visible_for
(
self
,
user
,
content_type
,
object_id
):
"""
Filter notes for a given object, accessible to the given user.
"""
model
=
ContentType
.
objects
.
get_for_id
(
content_type
)
return
self
.
for_object
(
content_type
,
object_id
).
visible_to
(
user
,
model
)
This diff is collapsed.
Click to expand it.
scipost_django/pins/models.py
+
4
−
0
View file @
92283ca6
...
@@ -4,6 +4,8 @@ __license__ = "AGPL v3"
...
@@ -4,6 +4,8 @@ __license__ = "AGPL v3"
from
django.db
import
models
from
django.db
import
models
from
django.contrib.contenttypes.fields
import
GenericForeignKey
from
django.contrib.contenttypes.fields
import
GenericForeignKey
from
.managers
import
NotesQuerySet
class
Note
(
models
.
Model
):
class
Note
(
models
.
Model
):
"""
"""
...
@@ -46,6 +48,8 @@ class Note(models.Model):
...
@@ -46,6 +48,8 @@ class Note(models.Model):
created
=
models
.
DateTimeField
(
auto_now_add
=
True
)
created
=
models
.
DateTimeField
(
auto_now_add
=
True
)
modified
=
models
.
DateTimeField
(
auto_now
=
True
)
modified
=
models
.
DateTimeField
(
auto_now
=
True
)
objects
=
NotesQuerySet
.
as_manager
()
def
__str__
(
self
):
def
__str__
(
self
):
return
self
.
title
return
self
.
title
...
...
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