Use model polymorphism

This commit is contained in:
Alexandre Iooss 2019-07-17 11:17:50 +02:00
parent 14282427af
commit 5110d6a16b
No known key found for this signature in database
GPG Key ID: 6C79278F3FCDCC02
5 changed files with 20 additions and 13 deletions

View File

@ -4,7 +4,7 @@
from django.contrib import admin from django.contrib import admin
from .models.notes import Alias, NoteClub, NoteSpecial, NoteUser from .models.notes import Alias, Note, NoteClub, NoteSpecial, NoteUser
from .models.transactions import MembershipTransaction, Transaction, \ from .models.transactions import MembershipTransaction, Transaction, \
TransactionTemplate TransactionTemplate
@ -26,8 +26,8 @@ class NoteClubAdmin(admin.ModelAdmin):
list_filter = ('is_active',) list_filter = ('is_active',)
search_fields = ['club__name'] search_fields = ['club__name']
# We can't change club after creation # We can't change club after creation or the balance
readonly_fields = ('club',) readonly_fields = ('club', 'balance')
def has_add_permission(self, request): def has_add_permission(self, request):
""" """
@ -62,8 +62,8 @@ class NoteUserAdmin(admin.ModelAdmin):
date_hierarchy = 'user__date_joined' date_hierarchy = 'user__date_joined'
ordering = ['-user__date_joined'] ordering = ['-user__date_joined']
# We can't change user after creation # We can't change user after creation or the balance
readonly_fields = ('user',) readonly_fields = ('user', 'balance')
def has_add_permission(self, request): def has_add_permission(self, request):
""" """

View File

@ -7,17 +7,16 @@ from django.db import models
from django.db.models.signals import post_save from django.db.models.signals import post_save
from django.dispatch import receiver from django.dispatch import receiver
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
from polymorphic.models import PolymorphicModel
""" """
Defines each note types Defines each note types
""" """
class Note(models.Model): class Note(PolymorphicModel):
""" """
An model, use to add transactions capabilities An model, use to add transactions capabilities
We do not use an abstract model to simplify the transfer between two notes.
""" """
balance = models.IntegerField( balance = models.IntegerField(
verbose_name=_('account balance'), verbose_name=_('account balance'),
@ -57,6 +56,9 @@ class NoteUser(Note):
verbose_name = _("one's note") verbose_name = _("one's note")
verbose_name_plural = _("users note") verbose_name_plural = _("users note")
def __str__(self):
return _("%(user)s's note") % {'user': str(self.user)}
class NoteClub(Note): class NoteClub(Note):
""" """

View File

@ -26,6 +26,7 @@ class TransactionTemplate(models.Model):
) )
amount = models.PositiveIntegerField( amount = models.PositiveIntegerField(
verbose_name=_('amount'), verbose_name=_('amount'),
help_text=_('in centimes'),
) )
template_type = models.CharField( template_type = models.CharField(
verbose_name=_('type'), verbose_name=_('type'),

View File

@ -32,6 +32,11 @@ INSTALLED_APPS = [
# Theme overrides Django Admin templates # Theme overrides Django Admin templates
'theme', 'theme',
# External apps
'polymorphic',
'guardian',
'reversion',
# Django contrib # Django contrib
'django.contrib.admin', 'django.contrib.admin',
'django.contrib.admindocs', 'django.contrib.admindocs',
@ -42,10 +47,6 @@ INSTALLED_APPS = [
'django.contrib.messages', 'django.contrib.messages',
'django.contrib.staticfiles', 'django.contrib.staticfiles',
# External apps
'guardian',
'reversion',
# Note apps # Note apps
'activity', 'activity',
'member', 'member',
@ -120,6 +121,8 @@ AUTHENTICATION_BACKENDS = (
'guardian.backends.ObjectPermissionBackend', 'guardian.backends.ObjectPermissionBackend',
) )
GUARDIAN_GET_CONTENT_TYPE = 'polymorphic.contrib.guardian.get_polymorphic_base_content_type'
# Internationalization # Internationalization
# https://docs.djangoproject.com/en/2.2/topics/i18n/ # https://docs.djangoproject.com/en/2.2/topics/i18n/

View File

@ -5,4 +5,5 @@ Pillow==6.1.0
pytz==2019.1 pytz==2019.1
six==1.12.0 six==1.12.0
sqlparse==0.3.0 sqlparse==0.3.0
django-reversion==3.0.3 django-reversion==3.0.3
django-polymorphic==2.0.3