mirror of
https://gitlab.crans.org/bde/nk20
synced 2025-06-21 18:08:21 +02:00
✨ Add "Lock note" feature
This commit is contained in:
@ -1,7 +1,9 @@
|
||||
# Copyright (C) 2018-2020 by BDE ENS Paris-Saclay
|
||||
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
from rest_framework import serializers
|
||||
from rest_framework.exceptions import ValidationError
|
||||
from rest_polymorphic.serializers import PolymorphicSerializer
|
||||
from member.api.serializers import MembershipSerializer
|
||||
from member.models import Membership
|
||||
@ -23,7 +25,7 @@ class NoteSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = Note
|
||||
fields = '__all__'
|
||||
read_only_fields = [f.name for f in model._meta.get_fields()] # Notes are read-only protected
|
||||
read_only_fields = ('balance', 'last_negative', 'created_at', ) # Note balances are read-only protected
|
||||
|
||||
|
||||
class NoteClubSerializer(serializers.ModelSerializer):
|
||||
@ -36,7 +38,7 @@ class NoteClubSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = NoteClub
|
||||
fields = '__all__'
|
||||
read_only_fields = ('note', 'club', )
|
||||
read_only_fields = ('note', 'club', 'balance', 'last_negative', 'created_at', )
|
||||
|
||||
def get_name(self, obj):
|
||||
return str(obj)
|
||||
@ -52,7 +54,7 @@ class NoteSpecialSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = NoteSpecial
|
||||
fields = '__all__'
|
||||
read_only_fields = ('note', )
|
||||
read_only_fields = ('note', 'balance', 'last_negative', 'created_at', )
|
||||
|
||||
def get_name(self, obj):
|
||||
return str(obj)
|
||||
@ -68,7 +70,7 @@ class NoteUserSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = NoteUser
|
||||
fields = '__all__'
|
||||
read_only_fields = ('note', 'user', )
|
||||
read_only_fields = ('note', 'user', 'balance', 'last_negative', 'created_at', )
|
||||
|
||||
def get_name(self, obj):
|
||||
return str(obj)
|
||||
@ -170,13 +172,22 @@ class TransactionSerializer(serializers.ModelSerializer):
|
||||
REST API Serializer for Transactions.
|
||||
The djangorestframework plugin will analyse the model `Transaction` and parse all fields in the API.
|
||||
"""
|
||||
def validate_source(self, value):
|
||||
if value.is_active:
|
||||
raise ValidationError(_("The transaction can't be saved since the source note "
|
||||
"or the destination note is not active."))
|
||||
|
||||
def validate_destination(self, value):
|
||||
if value.is_active:
|
||||
raise ValidationError(_("The transaction can't be saved since the source note "
|
||||
"or the destination note is not active."))
|
||||
|
||||
class Meta:
|
||||
model = Transaction
|
||||
fields = '__all__'
|
||||
|
||||
|
||||
class RecurrentTransactionSerializer(serializers.ModelSerializer):
|
||||
class RecurrentTransactionSerializer(TransactionSerializer):
|
||||
"""
|
||||
REST API Serializer for Transactions.
|
||||
The djangorestframework plugin will analyse the model `RecurrentTransaction` and parse all fields in the API.
|
||||
@ -187,7 +198,7 @@ class RecurrentTransactionSerializer(serializers.ModelSerializer):
|
||||
fields = '__all__'
|
||||
|
||||
|
||||
class MembershipTransactionSerializer(serializers.ModelSerializer):
|
||||
class MembershipTransactionSerializer(TransactionSerializer):
|
||||
"""
|
||||
REST API Serializer for Membership transactions.
|
||||
The djangorestframework plugin will analyse the model `MembershipTransaction` and parse all fields in the API.
|
||||
@ -198,7 +209,7 @@ class MembershipTransactionSerializer(serializers.ModelSerializer):
|
||||
fields = '__all__'
|
||||
|
||||
|
||||
class SpecialTransactionSerializer(serializers.ModelSerializer):
|
||||
class SpecialTransactionSerializer(TransactionSerializer):
|
||||
"""
|
||||
REST API Serializer for Special transactions.
|
||||
The djangorestframework plugin will analyse the model `SpecialTransaction` and parse all fields in the API.
|
||||
|
@ -18,7 +18,7 @@ from ..models.notes import Note, Alias
|
||||
from ..models.transactions import TransactionTemplate, Transaction, TemplateCategory
|
||||
|
||||
|
||||
class NotePolymorphicViewSet(ReadOnlyProtectedModelViewSet):
|
||||
class NotePolymorphicViewSet(ReadProtectedModelViewSet):
|
||||
"""
|
||||
REST API View set.
|
||||
The djangorestframework plugin will get all `Note` objects (with polymorhism), serialize it to JSON with the given serializer,
|
||||
@ -36,25 +36,16 @@ class NotePolymorphicViewSet(ReadOnlyProtectedModelViewSet):
|
||||
Parse query and apply filters.
|
||||
:return: The filtered set of requested notes
|
||||
"""
|
||||
queryset = super().get_queryset()
|
||||
queryset = super().get_queryset().distinct()
|
||||
|
||||
alias = self.request.query_params.get("alias", ".*")
|
||||
queryset = queryset.filter(
|
||||
name__iregex="^" + alias
|
||||
).union(
|
||||
queryset.filter(
|
||||
Q(normalized_name__iregex="^" + Alias.normalize(alias))
|
||||
& ~Q(name__iregex="^" + alias)
|
||||
),
|
||||
all=True).union(
|
||||
queryset.filter(
|
||||
Q(normalized_name__iregex="^" + alias.lower())
|
||||
& ~Q(normalized_name__iregex="^" + Alias.normalize(alias))
|
||||
& ~Q(name__iregex="^" + alias)
|
||||
),
|
||||
all=True)
|
||||
Q(alias__name__iregex="^" + alias)
|
||||
| Q(alias__normalized_name__iregex="^" + Alias.normalize(alias))
|
||||
| Q(alias__normalized_name__iregex="^" + alias.lower())
|
||||
)
|
||||
|
||||
return queryset.distinct()
|
||||
return queryset
|
||||
|
||||
|
||||
class AliasViewSet(ReadProtectedModelViewSet):
|
||||
|
@ -25,24 +25,20 @@ class Note(PolymorphicModel):
|
||||
A Note can be searched find throught an :model:`note.Alias`
|
||||
|
||||
"""
|
||||
|
||||
balance = models.BigIntegerField(
|
||||
verbose_name=_('account balance'),
|
||||
help_text=_('in centimes, money credited for this instance'),
|
||||
default=0,
|
||||
)
|
||||
|
||||
last_negative = models.DateTimeField(
|
||||
verbose_name=_('last negative date'),
|
||||
help_text=_('last time the balance was negative'),
|
||||
null=True,
|
||||
blank=True,
|
||||
)
|
||||
is_active = models.BooleanField(
|
||||
_('active'),
|
||||
default=True,
|
||||
help_text=_(
|
||||
'Designates whether this note should be treated as active. '
|
||||
'Unselect this instead of deleting notes.'),
|
||||
)
|
||||
|
||||
display_image = models.ImageField(
|
||||
verbose_name=_('display image'),
|
||||
max_length=255,
|
||||
@ -51,11 +47,31 @@ class Note(PolymorphicModel):
|
||||
upload_to='pic/',
|
||||
default='pic/default.png'
|
||||
)
|
||||
|
||||
created_at = models.DateTimeField(
|
||||
verbose_name=_('created at'),
|
||||
default=timezone.now,
|
||||
)
|
||||
|
||||
is_active = models.BooleanField(
|
||||
_('active'),
|
||||
default=True,
|
||||
help_text=_(
|
||||
'Designates whether this note should be treated as active. '
|
||||
'Unselect this instead of deleting notes.'),
|
||||
)
|
||||
|
||||
inactivity_reason = models.CharField(
|
||||
max_length=255,
|
||||
choices=[
|
||||
('manual', _("The user blocked his/her note manually, eg. when he/she left the school for holidays. "
|
||||
"It can be reactivated at any time.")),
|
||||
('forced', _("The note is blocked by the the BDE and can't be manually reactivated.")),
|
||||
],
|
||||
null=True,
|
||||
default=None,
|
||||
)
|
||||
|
||||
class Meta:
|
||||
verbose_name = _("note")
|
||||
verbose_name_plural = _("notes")
|
||||
@ -141,9 +157,13 @@ class NoteUser(Note):
|
||||
old_note = NoteUser.objects.get(pk=self.pk)
|
||||
if old_note.balance >= 0:
|
||||
# Passage en négatif
|
||||
super().save(*args, **kwargs)
|
||||
self.last_negative = timezone.now()
|
||||
self._force_save = True
|
||||
self.save(*args, **kwargs)
|
||||
self.send_mail_negative_balance()
|
||||
super().save(*args, **kwargs)
|
||||
else:
|
||||
super().save(*args, **kwargs)
|
||||
|
||||
def send_mail_negative_balance(self):
|
||||
plain_text = render_to_string("note/mails/negative_balance.txt", dict(note=self))
|
||||
@ -178,7 +198,10 @@ class NoteClub(Note):
|
||||
old_note = NoteClub.objects.get(pk=self.pk)
|
||||
if old_note.balance >= 0:
|
||||
# Passage en négatif
|
||||
super().save(*args, **kwargs)
|
||||
self.last_negative = timezone.now()
|
||||
self._force_save = True
|
||||
self.save(*args, **kwargs)
|
||||
self.send_mail_negative_balance()
|
||||
super().save(*args, **kwargs)
|
||||
|
||||
|
@ -221,10 +221,8 @@ class Transaction(PolymorphicModel):
|
||||
diff_source, diff_dest = self.validate()
|
||||
|
||||
if not self.source.is_active or not self.destination.is_active:
|
||||
if 'force_insert' not in kwargs or not kwargs['force_insert']:
|
||||
if 'force_update' not in kwargs or not kwargs['force_update']:
|
||||
raise ValidationError(_("The transaction can't be saved since the source note "
|
||||
"or the destination note is not active."))
|
||||
raise ValidationError(_("The transaction can't be saved since the source note "
|
||||
"or the destination note is not active."))
|
||||
|
||||
# If the aliases are not entered, we assume that the used alias is the name of the note
|
||||
if not self.source_alias:
|
||||
|
@ -55,17 +55,19 @@ class HistoryTable(tables.Table):
|
||||
"id": lambda record: "validate_" + str(record.id),
|
||||
"class": lambda record:
|
||||
str(record.valid).lower()
|
||||
+ (' validate' if PermissionBackend.check_perm(get_current_authenticated_user(),
|
||||
"note.change_transaction_invalidity_reason",
|
||||
record) else ''),
|
||||
+ (' validate' if record.source.is_active and record.destination.is_active and PermissionBackend
|
||||
.check_perm(get_current_authenticated_user(), "note.change_transaction_invalidity_reason", record)
|
||||
else ''),
|
||||
"data-toggle": "tooltip",
|
||||
"title": lambda record: (_("Click to invalidate") if record.valid else _("Click to validate"))
|
||||
if PermissionBackend.check_perm(get_current_authenticated_user(),
|
||||
"note.change_transaction_invalidity_reason", record) else None,
|
||||
"note.change_transaction_invalidity_reason", record)
|
||||
and record.source.is_active and record.destination.is_active else None,
|
||||
"onclick": lambda record: 'de_validate(' + str(record.id) + ', ' + str(record.valid).lower()
|
||||
+ ', "' + str(record.__class__.__name__) + '")'
|
||||
if PermissionBackend.check_perm(get_current_authenticated_user(),
|
||||
"note.change_transaction_invalidity_reason", record) else None,
|
||||
"note.change_transaction_invalidity_reason", record)
|
||||
and record.source.is_active and record.destination.is_active else None,
|
||||
"onmouseover": lambda record: '$("#invalidity_reason_'
|
||||
+ str(record.id) + '").show();$("#invalidity_reason_'
|
||||
+ str(record.id) + '").focus();',
|
||||
@ -108,7 +110,7 @@ class HistoryTable(tables.Table):
|
||||
val += "<input type='text' class='form-control' id='invalidity_reason_" + str(record.id) \
|
||||
+ "' value='" + (html.escape(record.invalidity_reason)
|
||||
if record.invalidity_reason else ("" if value else str(_("No reason specified")))) \
|
||||
+ "'" + ("" if value and has_perm else " disabled") \
|
||||
+ "'" + ("" if value and record.source.is_active and record.destination.is_active else " disabled") \
|
||||
+ " placeholder='" + html.escape(_("invalidity reason").capitalize()) + "'" \
|
||||
+ " style='position: absolute; width: 15em; margin-left: -15.5em; margin-top: -2em; display: none;'>"
|
||||
return format_html(val)
|
||||
@ -135,16 +137,10 @@ class AliasTable(tables.Table):
|
||||
|
||||
delete_col = tables.TemplateColumn(template_code=DELETE_TEMPLATE,
|
||||
extra_context={"delete_trans": _('delete')},
|
||||
attrs=
|
||||
{'td':
|
||||
{'class':
|
||||
lambda record: 'col-sm-1'
|
||||
+ (' d-none' if not PermissionBackend
|
||||
.check_perm(get_current_authenticated_user(),
|
||||
"note.delete_alias", record) else '')
|
||||
}
|
||||
},
|
||||
verbose_name=_("Delete"), )
|
||||
attrs={'td': {'class': lambda record: 'col-sm-1' + (
|
||||
' d-none' if not PermissionBackend.check_perm(
|
||||
get_current_authenticated_user(), "note.delete_alias",
|
||||
record) else '')}}, verbose_name=_("Delete"), )
|
||||
|
||||
|
||||
class ButtonTable(tables.Table):
|
||||
|
Reference in New Issue
Block a user