Split BaseModel into RandomPkModel and StampedModel.
This commit is contained in:
parent
926d61f534
commit
388ff18461
@ -24,14 +24,15 @@ from decimal import Decimal
|
|||||||
from typing import Dict, List, Optional, Mapping
|
from typing import Dict, List, Optional, Mapping
|
||||||
|
|
||||||
from dirtyfields import DirtyFieldsMixin
|
from dirtyfields import DirtyFieldsMixin
|
||||||
from django.db import models, transaction
|
from django.db import models
|
||||||
from django.db.models import Q, Max
|
from django.db.models import Q, Max
|
||||||
from django.http import HttpRequest
|
from django.http import HttpRequest
|
||||||
|
|
||||||
from mia_core.models import BaseModel, L10nModel, LocalizedModel
|
from mia_core.models import L10nModel, LocalizedModel, StampedModel, \
|
||||||
|
RandomPkModel
|
||||||
|
|
||||||
|
|
||||||
class Account(DirtyFieldsMixin, LocalizedModel, BaseModel):
|
class Account(DirtyFieldsMixin, LocalizedModel, StampedModel, RandomPkModel):
|
||||||
"""An account."""
|
"""An account."""
|
||||||
parent = models.ForeignKey(
|
parent = models.ForeignKey(
|
||||||
"self", on_delete=models.PROTECT, null=True,
|
"self", on_delete=models.PROTECT, null=True,
|
||||||
@ -108,13 +109,13 @@ class Account(DirtyFieldsMixin, LocalizedModel, BaseModel):
|
|||||||
self._is_in_use = value
|
self._is_in_use = value
|
||||||
|
|
||||||
|
|
||||||
class AccountL10n(DirtyFieldsMixin, L10nModel, BaseModel):
|
class AccountL10n(DirtyFieldsMixin, L10nModel, StampedModel, RandomPkModel):
|
||||||
"""The localization content of an account."""
|
"""The localization content of an account."""
|
||||||
master = models.ForeignKey(
|
master = models.ForeignKey(
|
||||||
Account, on_delete=models.CASCADE, related_name="l10n_set")
|
Account, on_delete=models.CASCADE, related_name="l10n_set")
|
||||||
|
|
||||||
|
|
||||||
class Transaction(DirtyFieldsMixin, BaseModel):
|
class Transaction(DirtyFieldsMixin, StampedModel, RandomPkModel):
|
||||||
"""An accounting transaction."""
|
"""An accounting transaction."""
|
||||||
date = models.DateField()
|
date = models.DateField()
|
||||||
ord = models.PositiveSmallIntegerField(default=1)
|
ord = models.PositiveSmallIntegerField(default=1)
|
||||||
@ -422,7 +423,7 @@ class Transaction(DirtyFieldsMixin, BaseModel):
|
|||||||
return "transfer"
|
return "transfer"
|
||||||
|
|
||||||
|
|
||||||
class Record(DirtyFieldsMixin, BaseModel):
|
class Record(DirtyFieldsMixin, StampedModel, RandomPkModel):
|
||||||
"""An accounting record."""
|
"""An accounting record."""
|
||||||
transaction = models.ForeignKey(
|
transaction = models.ForeignKey(
|
||||||
Transaction, on_delete=models.CASCADE)
|
Transaction, on_delete=models.CASCADE)
|
||||||
|
@ -22,15 +22,30 @@ from typing import Any, Dict, List
|
|||||||
|
|
||||||
from dirtyfields import DirtyFieldsMixin
|
from dirtyfields import DirtyFieldsMixin
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
|
from django.core.exceptions import ObjectDoesNotExist
|
||||||
from django.db import models
|
from django.db import models
|
||||||
|
|
||||||
from mia_core.utils import new_pk, Language
|
from mia_core.utils import new_pk, Language
|
||||||
|
|
||||||
|
|
||||||
class BaseModel(models.Model):
|
class RandomPkModel(models.Model):
|
||||||
"""The common abstract base model that has id, created_at, created_by,
|
"""The abstract data model that uses 9-digit random primary keys."""
|
||||||
updated_at, and updated_by."""
|
|
||||||
id = models.PositiveIntegerField(primary_key=True)
|
id = models.PositiveIntegerField(primary_key=True)
|
||||||
|
|
||||||
|
def save(self, force_insert=False, force_update=False, using=None,
|
||||||
|
update_fields=None):
|
||||||
|
if self.pk is None:
|
||||||
|
self.pk = new_pk(self.__class__)
|
||||||
|
super().save(force_insert=force_insert, force_update=force_update,
|
||||||
|
using=using, update_fields=update_fields)
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
abstract = True
|
||||||
|
|
||||||
|
|
||||||
|
class StampedModel(models.Model):
|
||||||
|
"""The abstract base model that has created_at, created_by, updated_at, and
|
||||||
|
updated_by."""
|
||||||
created_at = models.DateTimeField(auto_now_add=True)
|
created_at = models.DateTimeField(auto_now_add=True)
|
||||||
created_by = models.ForeignKey(
|
created_by = models.ForeignKey(
|
||||||
settings.AUTH_USER_MODEL, on_delete=models.PROTECT,
|
settings.AUTH_USER_MODEL, on_delete=models.PROTECT,
|
||||||
@ -46,8 +61,9 @@ class BaseModel(models.Model):
|
|||||||
|
|
||||||
def save(self, force_insert=False, force_update=False, using=None,
|
def save(self, force_insert=False, force_update=False, using=None,
|
||||||
update_fields=None):
|
update_fields=None):
|
||||||
if self.pk is None:
|
try:
|
||||||
self.pk = new_pk(self.__class__)
|
self.created_by
|
||||||
|
except ObjectDoesNotExist as e:
|
||||||
if self.current_user is not None:
|
if self.current_user is not None:
|
||||||
self.created_by = self.current_user
|
self.created_by = self.current_user
|
||||||
if self.current_user is not None:
|
if self.current_user is not None:
|
||||||
@ -122,7 +138,8 @@ class LocalizedModel(models.Model):
|
|||||||
super().save(force_insert=force_insert, force_update=force_update,
|
super().save(force_insert=force_insert, force_update=force_update,
|
||||||
using=using, update_fields=update_fields)
|
using=using, update_fields=update_fields)
|
||||||
for l10n_rec in l10n_to_save:
|
for l10n_rec in l10n_to_save:
|
||||||
if isinstance(self, BaseModel) and isinstance(l10n_rec, BaseModel):
|
if isinstance(self, StampedModel)\
|
||||||
|
and isinstance(l10n_rec, StampedModel):
|
||||||
l10n_rec.current_user = self.current_user
|
l10n_rec.current_user = self.current_user
|
||||||
l10n_rec.save(force_insert=force_insert, force_update=force_update,
|
l10n_rec.save(force_insert=force_insert, force_update=force_update,
|
||||||
using=using, update_fields=update_fields)
|
using=using, update_fields=update_fields)
|
||||||
|
@ -35,7 +35,7 @@ from django.views.generic import DeleteView as CoreDeleteView, \
|
|||||||
from django.views.generic.base import View
|
from django.views.generic.base import View
|
||||||
|
|
||||||
from . import stored_post, utils
|
from . import stored_post, utils
|
||||||
from .models import BaseModel
|
from .models import StampedModel
|
||||||
from .utils import UrlBuilder
|
from .utils import UrlBuilder
|
||||||
|
|
||||||
|
|
||||||
@ -141,7 +141,7 @@ class FormView(View):
|
|||||||
"""Fills in the data model from the form."""
|
"""Fills in the data model from the form."""
|
||||||
for name in form.fields:
|
for name in form.fields:
|
||||||
setattr(obj, name, form[name].value())
|
setattr(obj, name, form[name].value())
|
||||||
if isinstance(obj, BaseModel):
|
if isinstance(obj, StampedModel):
|
||||||
obj.current_user = self.request.user
|
obj.current_user = self.request.user
|
||||||
|
|
||||||
def form_invalid(self, form: forms.Form) -> HttpResponseRedirect:
|
def form_invalid(self, form: forms.Form) -> HttpResponseRedirect:
|
||||||
|
Loading…
Reference in New Issue
Block a user