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
Mike Gabriel
AlekSIS-Core
Commits
f46ab9a0
Commit
f46ab9a0
authored
4 years ago
by
Hangzhi Yu
Browse files
Options
Downloads
Plain Diff
Merge branch '273-let-apps-add-extra-permissions-to-models' into 291-rename-template-references
parents
65840620
57956aac
Branches
291-rename-template-references
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
aleksis/core/mixins.py
+9
-0
9 additions, 0 deletions
aleksis/core/mixins.py
aleksis/core/util/apps.py
+12
-0
12 additions, 0 deletions
aleksis/core/util/apps.py
with
21 additions
and
0 deletions
aleksis/core/mixins.py
+
9
−
0
View file @
f46ab9a0
...
...
@@ -43,6 +43,8 @@ class _ExtensibleModelBase(models.base.ModelBase):
# Register all non-abstract models with django-reversion
mcls
=
reversion
.
register
(
mcls
)
mcls
.
extra_permissions
=
[]
return
mcls
...
...
@@ -100,6 +102,8 @@ class ExtensibleModel(models.Model, metaclass=_ExtensibleModelBase):
objects
=
CurrentSiteManager
()
objects_all_sites
=
models
.
Manager
()
extra_permissions
=
[]
def
get_absolute_url
(
self
)
->
str
:
"""
Get the URL o a view representing this model instance.
"""
pass
...
...
@@ -226,6 +230,11 @@ class ExtensibleModel(models.Model, metaclass=_ExtensibleModelBase):
"""
Collect all fields that can be synced on a model.
"""
return
lazy
(
cls
.
syncable_fields_choices
,
tuple
)
@classmethod
def
add_permission
(
cls
,
name
:
str
,
verbose_name
:
str
):
"""
Dynamically add a new permission to a model.
"""
cls
.
extra_permissions
.
append
((
name
,
verbose_name
))
class
Meta
:
abstract
=
True
...
...
This diff is collapsed.
Click to expand it.
aleksis/core/util/apps.py
+
12
−
0
View file @
f46ab9a0
...
...
@@ -189,6 +189,9 @@ class AppConfig(django.apps.AppConfig):
pass
def
_maintain_default_data
(
self
):
from
django.contrib.auth.models
import
Permission
from
django.contrib.contenttypes.models
import
ContentType
if
not
self
.
models_module
:
# This app does not have any models, so bail out early
return
...
...
@@ -197,3 +200,12 @@ class AppConfig(django.apps.AppConfig):
if
hasattr
(
model
,
"
maintain_default_data
"
):
# Method implemented by each model object; can be left out
model
.
maintain_default_data
()
if
hasattr
(
model
,
"
extra_permissions
"
):
ct
=
ContentType
.
objects
.
get_for_model
(
model
)
for
perm
,
verbose_name
in
model
.
extra_permissions
:
Permission
.
objects
.
get_or_create
(
codename
=
perm
,
name
=
verbose_name
,
content_type
=
ct
)
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