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
d75341a5
Verified
Commit
d75341a5
authored
4 years ago
by
Jonathan Weth
Browse files
Options
Downloads
Patches
Plain Diff
Add some mixins for class-based views and school years
parent
3dbb5980
No related branches found
Branches containing commit
No related tags found
Tags containing commit
1 merge request
!299
Resolve "Link data to school term"
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
aleksis/core/mixins.py
+61
-0
61 additions, 0 deletions
aleksis/core/mixins.py
with
61 additions
and
0 deletions
aleksis/core/mixins.py
+
61
−
0
View file @
d75341a5
...
...
@@ -4,13 +4,19 @@ from datetime import datetime
from
typing
import
Any
,
Callable
,
List
,
Optional
,
Tuple
,
Union
from
django.conf
import
settings
from
django.contrib
import
messages
from
django.contrib.contenttypes.models
import
ContentType
from
django.contrib.sites.managers
import
CurrentSiteManager
from
django.contrib.sites.models
import
Site
from
django.db
import
models
from
django.db.models
import
QuerySet
from
django.forms.forms
import
BaseForm
from
django.forms.models
import
ModelForm
,
ModelFormMetaclass
from
django.http
import
HttpResponse
from
django.utils.functional
import
lazy
from
django.utils.translation
import
gettext
as
_
from
django.views.generic
import
CreateView
,
UpdateView
from
django.views.generic.edit
import
ModelFormMixin
import
reversion
from
easyaudit.models
import
CRUDEvent
...
...
@@ -19,6 +25,8 @@ from jsonstore.fields import JSONField, JSONFieldMixin
from
material.base
import
Layout
,
LayoutNode
from
rules.contrib.admin
import
ObjectPermissionsModelAdmin
from
aleksis.core.managers
import
CurrentSiteManagerWithoutMigrations
,
SchoolYearRelatedQuerySet
class
_ExtensibleModelBase
(
models
.
base
.
ModelBase
):
"""
Ensure predefined behaviour on model creation.
...
...
@@ -279,3 +287,56 @@ class BaseModelAdmin(GuardedModelAdmin, ObjectPermissionsModelAdmin):
"""
A base class for ModelAdmin combining django-guardian and rules.
"""
pass
class
SuccessMessageMixin
(
ModelFormMixin
):
success_message
:
Optional
[
str
]
=
None
def
form_valid
(
self
,
form
:
BaseForm
)
->
HttpResponse
:
print
(
"
Hi
"
)
if
self
.
success_message
:
messages
.
success
(
self
.
request
,
self
.
success_message
)
return
super
().
form_valid
(
form
)
class
AdvancedCreateView
(
CreateView
,
SuccessMessageMixin
):
pass
class
AdvancedEditView
(
UpdateView
,
SuccessMessageMixin
):
pass
class
SchoolYearRelatedExtensibleModel
(
ExtensibleModel
):
"""
Add relation to school year.
"""
objects
=
CurrentSiteManagerWithoutMigrations
.
from_queryset
(
SchoolYearRelatedQuerySet
)()
school_year
=
models
.
ForeignKey
(
"
core.SchoolYear
"
,
on_delete
=
models
.
CASCADE
,
related_name
=
"
+
"
,
verbose_name
=
_
(
"
Linked school year
"
),
blank
=
True
,
null
=
True
,
)
class
Meta
:
abstract
=
True
class
SchoolYearRelatedExtensibleForm
(
ExtensibleForm
):
"""
Extensible form for school year related data.
.. warning::
This doesn
'
t automatically include the field `school_year` in `fields` or `layout`,
it just sets an initial value.
"""
def
__init__
(
self
,
*
args
,
**
kwargs
):
from
aleksis.core.models
import
SchoolYear
# noqa
if
"
instance
"
not
in
kwargs
:
kwargs
[
"
initial
"
]
=
{
"
school_year
"
:
SchoolYear
.
current
}
super
().
__init__
(
*
args
,
**
kwargs
)
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