Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
AlekSIS-Core
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Terraform modules
Monitor
Service Desk
Analyze
Contributor analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
AlekSIS®
Official
AlekSIS-Core
Commits
9a24025b
Verified
Commit
9a24025b
authored
4 years ago
by
Jonathan Weth
Browse files
Options
Downloads
Patches
Plain Diff
Add cached content type getter
parent
060e8cb7
No related branches found
No related tags found
1 merge request
!368
Add cached content type getter
Pipeline
#3769
failed
4 years ago
Stage: test
Stage: build
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
aleksis/core/util/core_helpers.py
+12
-0
12 additions, 0 deletions
aleksis/core/util/core_helpers.py
aleksis/core/util/predicates.py
+2
-8
2 additions, 8 deletions
aleksis/core/util/predicates.py
with
14 additions
and
8 deletions
aleksis/core/util/core_helpers.py
+
12
−
0
View file @
9a24025b
...
...
@@ -361,6 +361,18 @@ def handle_uploaded_file(f, filename: str):
destination
.
write
(
chunk
)
@cache_memoize
(
3600
)
def
get_content_type_by_perm
(
perm
:
str
)
->
Union
[
"
ContentType
"
,
None
]:
from
django.contrib.contenttypes.models
import
ContentType
# noqa
try
:
return
ContentType
.
objects
.
get
(
app_label
=
perm
.
split
(
"
.
"
,
1
)[
0
],
permission__codename
=
perm
.
split
(
"
.
"
,
1
)[
1
]
)
except
ContentType
.
DoesNotExist
:
return
None
@cache_memoize
(
3600
)
def
queryset_rules_filter
(
obj
:
Union
[
HttpRequest
,
Model
],
queryset
:
QuerySet
,
perm
:
str
...
...
This diff is collapsed.
Click to expand it.
aleksis/core/util/predicates.py
+
2
−
8
View file @
9a24025b
from
django.contrib.auth.backends
import
ModelBackend
from
django.contrib.auth.models
import
User
from
django.contrib.contenttypes.models
import
ContentType
from
django.db.models
import
Model
from
django.http
import
HttpRequest
...
...
@@ -9,7 +8,7 @@ from guardian.shortcuts import get_objects_for_user
from
rules
import
predicate
from
..models
import
Group
from
.core_helpers
import
get_site_preferences
from
.core_helpers
import
get_content_type_by_perm
,
get_site_preferences
from
.core_helpers
import
has_person
as
has_person_helper
from
.core_helpers
import
queryset_rules_filter
...
...
@@ -65,12 +64,7 @@ def has_any_object(perm: str, klass):
@predicate
(
name
)
def
fn
(
user
:
User
)
->
bool
:
try
:
ct_perm
=
ContentType
.
objects
.
get
(
app_label
=
perm
.
split
(
"
.
"
,
1
)[
0
],
permission__codename
=
perm
.
split
(
"
.
"
,
1
)[
1
]
)
except
ContentType
.
DoesNotExist
:
ct_perm
=
None
ct_perm
=
get_content_type_by_perm
(
perm
)
if
ct_perm
and
ct_perm
.
model_class
()
==
klass
:
return
get_objects_for_user
(
user
,
perm
,
klass
).
exists
()
else
:
...
...
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