Split BaseModel into RandomPkModel and StampedModel.

This commit is contained in:
依瑪貓 2020-08-24 22:52:58 +08:00
parent 926d61f534
commit 388ff18461
3 changed files with 32 additions and 14 deletions

View File

@ -24,14 +24,15 @@ from decimal import Decimal
from typing import Dict, List, Optional, Mapping
from dirtyfields import DirtyFieldsMixin
from django.db import models, transaction
from django.db import models
from django.db.models import Q, Max
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."""
parent = models.ForeignKey(
"self", on_delete=models.PROTECT, null=True,
@ -108,13 +109,13 @@ class Account(DirtyFieldsMixin, LocalizedModel, BaseModel):
self._is_in_use = value
class AccountL10n(DirtyFieldsMixin, L10nModel, BaseModel):
class AccountL10n(DirtyFieldsMixin, L10nModel, StampedModel, RandomPkModel):
"""The localization content of an account."""
master = models.ForeignKey(
Account, on_delete=models.CASCADE, related_name="l10n_set")
class Transaction(DirtyFieldsMixin, BaseModel):
class Transaction(DirtyFieldsMixin, StampedModel, RandomPkModel):
"""An accounting transaction."""
date = models.DateField()
ord = models.PositiveSmallIntegerField(default=1)
@ -422,7 +423,7 @@ class Transaction(DirtyFieldsMixin, BaseModel):
return "transfer"
class Record(DirtyFieldsMixin, BaseModel):
class Record(DirtyFieldsMixin, StampedModel, RandomPkModel):
"""An accounting record."""
transaction = models.ForeignKey(
Transaction, on_delete=models.CASCADE)

View File

@ -22,15 +22,30 @@ from typing import Any, Dict, List
from dirtyfields import DirtyFieldsMixin
from django.conf import settings
from django.core.exceptions import ObjectDoesNotExist
from django.db import models
from mia_core.utils import new_pk, Language
class BaseModel(models.Model):
"""The common abstract base model that has id, created_at, created_by,
updated_at, and updated_by."""
class RandomPkModel(models.Model):
"""The abstract data model that uses 9-digit random primary keys."""
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_by = models.ForeignKey(
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,
update_fields=None):
if self.pk is None:
self.pk = new_pk(self.__class__)
try:
self.created_by
except ObjectDoesNotExist as e:
if self.current_user is not None:
self.created_by = self.current_user
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,
using=using, update_fields=update_fields)
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.save(force_insert=force_insert, force_update=force_update,
using=using, update_fields=update_fields)

View File

@ -35,7 +35,7 @@ from django.views.generic import DeleteView as CoreDeleteView, \
from django.views.generic.base import View
from . import stored_post, utils
from .models import BaseModel
from .models import StampedModel
from .utils import UrlBuilder
@ -141,7 +141,7 @@ class FormView(View):
"""Fills in the data model from the form."""
for name in form.fields:
setattr(obj, name, form[name].value())
if isinstance(obj, BaseModel):
if isinstance(obj, StampedModel):
obj.current_user = self.request.user
def form_invalid(self, form: forms.Form) -> HttpResponseRedirect: