Skip to content
Snippets Groups Projects
Verified Commit 0377e402 authored by Nik | Klampfradler's avatar Nik | Klampfradler
Browse files

Add ExtensibleModel to allow apps to patch new code into existing models.

parent b35c00c5
No related branches found
No related tags found
No related merge requests found
from typing import Any, Callable, Optional
from django.contrib.contenttypes.models import ContentType
from django.db import models
from django.db.models import QuerySet
......@@ -7,6 +9,30 @@ from easyaudit.models import CRUDEvent
from .util.core_helpers import get_current_school
class ExtensibleModel(object):
""" Mixin that adds class methods for glrofied monkey-patching. """
@classmethod
def property(cls, func: Callable[[], Any], name: Optional[str] = None) -> None:
""" Adds the passed callable as a property. """
# Decide the name for the property
if name is None:
prop_name = func.__name__
else:
if name.isidentifier():
prop_name = name
else:
raise ValueError('%s is not a valid name.' % name)
# Verify that property name does not clash with other names in the class
if hasattr(cls, prop_name):
raise ValueError('%s already used.' % prop_name)
# Add function wrapped in property decorator if we got here
setattr(cls, prop_name, property(func))
class SchoolRelated(models.Model):
class Meta:
abstract = True
......
......@@ -7,7 +7,7 @@ from django.utils.translation import ugettext_lazy as _
from image_cropping import ImageCropField, ImageRatioField
from phonenumber_field.modelfields import PhoneNumberField
from .mixins import SchoolRelated
from .mixins import ExtensibleModel, SchoolRelated
class School(models.Model):
......@@ -42,9 +42,9 @@ class SchoolTerm(SchoolRelated):
'Effective start date of term'), null=True)
date_end = models.DateField(verbose_name=_(
'Effective end date of term'), null=True)
class Person(SchoolRelated):
class Person(SchoolRelated, ExtensibleModel):
""" A model describing any person related to a school, including, but not
limited to, students, teachers and guardians (parents).
"""
......@@ -132,7 +132,7 @@ class Person(SchoolRelated):
return self.full_name
class Group(SchoolRelated):
class Group(SchoolRelated, ExtensibleModel):
"""Any kind of group of persons in a school, including, but not limited
classes, clubs, and the like.
"""
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment