mirror of
https://gitlab.crans.org/bde/nk20
synced 2025-06-20 17:41:55 +02:00
Merge branch 'master' into tranfer_front
This commit is contained in:
@ -12,6 +12,7 @@ from activity.api.urls import register_activity_urls
|
||||
from api.viewsets import ReadProtectedModelViewSet
|
||||
from member.api.urls import register_members_urls
|
||||
from note.api.urls import register_note_urls
|
||||
from treasury.api.urls import register_treasury_urls
|
||||
from logs.api.urls import register_logs_urls
|
||||
from permission.api.urls import register_permission_urls
|
||||
|
||||
@ -74,6 +75,7 @@ router.register('user', UserViewSet)
|
||||
register_members_urls(router, 'members')
|
||||
register_activity_urls(router, 'activity')
|
||||
register_note_urls(router, 'note')
|
||||
register_treasury_urls(router, 'treasury')
|
||||
register_permission_urls(router, 'permission')
|
||||
register_logs_urls(router, 'logs')
|
||||
|
||||
|
@ -5,7 +5,7 @@
|
||||
"fields": {
|
||||
"name": "BDE",
|
||||
"email": "tresorerie.bde@example.com",
|
||||
"membership_fee": 5,
|
||||
"membership_fee": 500,
|
||||
"membership_duration": "396 00:00:00",
|
||||
"membership_start": "213 00:00:00",
|
||||
"membership_end": "273 00:00:00"
|
||||
@ -17,7 +17,7 @@
|
||||
"fields": {
|
||||
"name": "Kfet",
|
||||
"email": "tresorerie.bde@example.com",
|
||||
"membership_fee": 35,
|
||||
"membership_fee": 3500,
|
||||
"membership_duration": "396 00:00:00",
|
||||
"membership_start": "213 00:00:00",
|
||||
"membership_end": "273 00:00:00"
|
||||
|
@ -4,6 +4,7 @@
|
||||
import datetime
|
||||
|
||||
from django.conf import settings
|
||||
from django.core.exceptions import ValidationError
|
||||
from django.db import models
|
||||
from django.urls import reverse, reverse_lazy
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
@ -67,6 +68,13 @@ class Club(models.Model):
|
||||
email = models.EmailField(
|
||||
verbose_name=_('email'),
|
||||
)
|
||||
parent_club = models.ForeignKey(
|
||||
'self',
|
||||
null=True,
|
||||
blank=True,
|
||||
on_delete=models.PROTECT,
|
||||
verbose_name=_('parent club'),
|
||||
)
|
||||
|
||||
# Memberships
|
||||
membership_fee = models.PositiveIntegerField(
|
||||
@ -158,6 +166,12 @@ class Membership(models.Model):
|
||||
else:
|
||||
return self.date_start.toordinal() <= datetime.datetime.now().toordinal()
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
if self.club.parent_club is not None:
|
||||
if not Membership.objects.filter(user=self.user, club=self.club.parent_club):
|
||||
raise ValidationError(_('User is not a member of the parent club'))
|
||||
super().save(*args, **kwargs)
|
||||
|
||||
class Meta:
|
||||
verbose_name = _('membership')
|
||||
verbose_name_plural = _('memberships')
|
||||
|
@ -17,6 +17,7 @@ class ClubTable(tables.Table):
|
||||
fields = ('id', 'name', 'email')
|
||||
row_attrs = {
|
||||
'class': 'table-row',
|
||||
'id': lambda record: "row-" + str(record.pk),
|
||||
'data-href': lambda record: record.pk
|
||||
}
|
||||
|
||||
|
@ -12,12 +12,14 @@ urlpatterns = [
|
||||
path('club/<int:pk>/', views.ClubDetailView.as_view(), name="club_detail"),
|
||||
path('club/<int:pk>/add_member/', views.ClubAddMemberView.as_view(), name="club_add_member"),
|
||||
path('club/create/', views.ClubCreateView.as_view(), name="club_create"),
|
||||
path('club/<int:pk>/update', views.ClubUpdateView.as_view(), name="club_update"),
|
||||
path('club/<int:pk>/update_pic', views.ClubPictureUpdateView.as_view(), name="club_update_pic"),
|
||||
path('club/<int:pk>/aliases', views.ClubAliasView.as_view(), name="club_alias"),
|
||||
path('user/', views.UserListView.as_view(), name="user_list"),
|
||||
path('user/<int:pk>', views.UserDetailView.as_view(), name="user_detail"),
|
||||
path('user/<int:pk>/update', views.UserUpdateView.as_view(), name="user_update_profile"),
|
||||
path('user/<int:pk>/update_pic', views.ProfilePictureUpdateView.as_view(), name="user_update_pic"),
|
||||
path('user/<int:pk>/aliases', views.AliasView.as_view(), name="user_alias"),
|
||||
path('user/aliases/delete/<int:pk>', views.DeleteAliasView.as_view(), name="user_alias_delete"),
|
||||
path('user/<int:pk>/aliases', views.ProfileAliasView.as_view(), name="user_alias"),
|
||||
path('manage-auth-token/', views.ManageAuthTokens.as_view(), name='auth_token'),
|
||||
# API for the user autocompleter
|
||||
path('user/user-autocomplete', views.UserAutocomplete.as_view(), name="user_autocomplete"),
|
||||
|
@ -20,7 +20,8 @@ from django.views.generic import CreateView, DetailView, UpdateView, TemplateVie
|
||||
from django.views.generic.edit import FormMixin
|
||||
from django_tables2.views import SingleTableView
|
||||
from rest_framework.authtoken.models import Token
|
||||
from note.forms import AliasForm, ImageForm
|
||||
from note.forms import ImageForm
|
||||
#from note.forms import AliasForm, ImageForm
|
||||
from note.models import Alias, NoteUser
|
||||
from note.models.transactions import Transaction
|
||||
from note.tables import HistoryTable, AliasTable
|
||||
@ -143,10 +144,6 @@ class UserDetailView(LoginRequiredMixin, DetailView):
|
||||
club_list = \
|
||||
Membership.objects.all().filter(user=user).only("club")
|
||||
context['club_list'] = ClubTable(club_list)
|
||||
context['title'] = _("Account #%(id)s: %(username)s") % {
|
||||
'id': user.pk,
|
||||
'username': user.username,
|
||||
}
|
||||
return context
|
||||
|
||||
|
||||
@ -171,62 +168,20 @@ class UserListView(LoginRequiredMixin, SingleTableView):
|
||||
context["filter"] = self.filter
|
||||
return context
|
||||
|
||||
|
||||
class AliasView(LoginRequiredMixin, FormMixin, DetailView):
|
||||
|
||||
class ProfileAliasView(LoginRequiredMixin, DetailView):
|
||||
model = User
|
||||
template_name = 'member/profile_alias.html'
|
||||
context_object_name = 'user_object'
|
||||
form_class = AliasForm
|
||||
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
context = super().get_context_data(**kwargs)
|
||||
note = context['user_object'].note
|
||||
note = context['object'].note
|
||||
context["aliases"] = AliasTable(note.alias_set.all())
|
||||
return context
|
||||
|
||||
def get_success_url(self):
|
||||
return reverse_lazy('member:user_alias', kwargs={'pk': self.object.id})
|
||||
|
||||
def post(self, request, *args, **kwargs):
|
||||
self.object = self.get_object()
|
||||
form = self.get_form()
|
||||
if form.is_valid():
|
||||
return self.form_valid(form)
|
||||
else:
|
||||
return self.form_invalid(form)
|
||||
|
||||
def form_valid(self, form):
|
||||
alias = form.save(commit=False)
|
||||
alias.note = self.object.note
|
||||
alias.save()
|
||||
return super().form_valid(form)
|
||||
|
||||
|
||||
class DeleteAliasView(LoginRequiredMixin, DeleteView):
|
||||
model = Alias
|
||||
|
||||
def delete(self, request, *args, **kwargs):
|
||||
try:
|
||||
self.object = self.get_object()
|
||||
self.object.delete()
|
||||
except ValidationError as e:
|
||||
# TODO: pass message to redirected view.
|
||||
messages.error(self.request, str(e))
|
||||
else:
|
||||
messages.success(self.request, _("Alias successfully deleted"))
|
||||
return HttpResponseRedirect(self.get_success_url())
|
||||
|
||||
def get_success_url(self):
|
||||
return reverse_lazy('member:user_alias', kwargs={'pk': self.object.note.user.pk})
|
||||
|
||||
def get(self, request, *args, **kwargs):
|
||||
return self.post(request, *args, **kwargs)
|
||||
|
||||
|
||||
class ProfilePictureUpdateView(LoginRequiredMixin, FormMixin, DetailView):
|
||||
model = User
|
||||
template_name = 'member/profile_picture_update.html'
|
||||
context_object_name = 'user_object'
|
||||
class PictureUpdateView(LoginRequiredMixin, FormMixin, DetailView):
|
||||
form_class = ImageForm
|
||||
|
||||
def get_context_data(self, *args, **kwargs):
|
||||
@ -273,6 +228,12 @@ class ProfilePictureUpdateView(LoginRequiredMixin, FormMixin, DetailView):
|
||||
return super().form_valid(form)
|
||||
|
||||
|
||||
class ProfilePictureUpdateView(PictureUpdateView):
|
||||
model = User
|
||||
template_name = 'member/profile_picture_update.html'
|
||||
context_object_name = 'user_object'
|
||||
|
||||
|
||||
class ManageAuthTokens(LoginRequiredMixin, TemplateView):
|
||||
"""
|
||||
Affiche le jeton d'authentification, et permet de le regénérer
|
||||
@ -329,10 +290,11 @@ class ClubCreateView(LoginRequiredMixin, CreateView):
|
||||
"""
|
||||
model = Club
|
||||
form_class = ClubForm
|
||||
success_url = reverse_lazy('member:club_list')
|
||||
|
||||
def form_valid(self, form):
|
||||
return super().form_valid(form)
|
||||
|
||||
|
||||
|
||||
class ClubListView(LoginRequiredMixin, SingleTableView):
|
||||
"""
|
||||
@ -364,6 +326,34 @@ class ClubDetailView(LoginRequiredMixin, DetailView):
|
||||
context['member_list'] = club_member
|
||||
return context
|
||||
|
||||
class ClubAliasView(LoginRequiredMixin, DetailView):
|
||||
model = Club
|
||||
template_name = 'member/club_alias.html'
|
||||
context_object_name = 'club'
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
context = super().get_context_data(**kwargs)
|
||||
note = context['object'].note
|
||||
context["aliases"] = AliasTable(note.alias_set.all())
|
||||
return context
|
||||
|
||||
|
||||
class ClubUpdateView(LoginRequiredMixin, UpdateView):
|
||||
model = Club
|
||||
context_object_name = "club"
|
||||
form_class = ClubForm
|
||||
template_name = "member/club_form.html"
|
||||
success_url = reverse_lazy("member:club_detail")
|
||||
|
||||
|
||||
class ClubPictureUpdateView(PictureUpdateView):
|
||||
model = Club
|
||||
template_name = 'member/club_picture_update.html'
|
||||
context_object_name = 'club'
|
||||
|
||||
def get_success_url(self):
|
||||
return reverse_lazy('member:club_detail', kwargs={'pk': self.object.id})
|
||||
|
||||
|
||||
class ClubAddMemberView(LoginRequiredMixin, CreateView):
|
||||
model = Membership
|
||||
@ -374,12 +364,12 @@ class ClubAddMemberView(LoginRequiredMixin, CreateView):
|
||||
return super().get_queryset().filter(PermissionBackend.filter_queryset(self.request.user, Membership, "view")
|
||||
| PermissionBackend.filter_queryset(self.request.user, Membership,
|
||||
"change"))
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
club = Club.objects.get(pk=self.kwargs["pk"])
|
||||
context = super().get_context_data(**kwargs)
|
||||
context['formset'] = MemberFormSet()
|
||||
context['helper'] = FormSetHelper()
|
||||
|
||||
context['club'] = club
|
||||
context['no_cache'] = True
|
||||
|
||||
return context
|
||||
|
@ -78,7 +78,11 @@ class AliasSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = Alias
|
||||
fields = '__all__'
|
||||
read_only_fields = ('note', )
|
||||
|
||||
def validate(self, attrs):
|
||||
instance = Alias(**attrs)
|
||||
instance.clean()
|
||||
return attrs
|
||||
|
||||
|
||||
class NotePolymorphicSerializer(PolymorphicSerializer):
|
||||
|
@ -2,8 +2,13 @@
|
||||
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
from django.db.models import Q
|
||||
from django.core.exceptions import ValidationError
|
||||
from django_filters.rest_framework import DjangoFilterBackend
|
||||
from rest_framework.filters import OrderingFilter, SearchFilter
|
||||
from rest_framework import viewsets
|
||||
from rest_framework.response import Response
|
||||
from rest_framework import status
|
||||
|
||||
from api.viewsets import ReadProtectedModelViewSet, ReadOnlyProtectedModelViewSet
|
||||
|
||||
from .serializers import NotePolymorphicSerializer, AliasSerializer, ConsumerSerializer,\
|
||||
@ -52,6 +57,22 @@ class AliasViewSet(ReadProtectedModelViewSet):
|
||||
search_fields = ['$normalized_name', '$name', '$note__polymorphic_ctype__model', ]
|
||||
ordering_fields = ['name', 'normalized_name']
|
||||
|
||||
def get_serializer_class(self):
|
||||
serializer_class = self.serializer_class
|
||||
if self.request.method in ['PUT', 'PATCH']:
|
||||
#alias owner cannot be change once establish
|
||||
setattr(serializer_class.Meta, 'read_only_fields', ('note',))
|
||||
return serializer_class
|
||||
|
||||
def destroy(self, request, *args, **kwargs):
|
||||
instance = self.get_object()
|
||||
try:
|
||||
self.perform_destroy(instance)
|
||||
except ValidationError as e:
|
||||
print(e)
|
||||
return Response({e.code:e.message},status.HTTP_400_BAD_REQUEST)
|
||||
return Response(status=status.HTTP_204_NO_CONTENT)
|
||||
|
||||
def get_queryset(self):
|
||||
"""
|
||||
Parse query and apply filters.
|
||||
@ -104,7 +125,7 @@ class TemplateCategoryViewSet(ReadProtectedModelViewSet):
|
||||
search_fields = ['$name', ]
|
||||
|
||||
|
||||
class TransactionTemplateViewSet(ReadProtectedModelViewSet):
|
||||
class TransactionTemplateViewSet(viewsets.ModelViewSet):
|
||||
"""
|
||||
REST API View set.
|
||||
The djangorestframework plugin will get all `TransactionTemplate` objects, serialize it to JSON with the given serializer,
|
||||
@ -112,8 +133,9 @@ class TransactionTemplateViewSet(ReadProtectedModelViewSet):
|
||||
"""
|
||||
queryset = TransactionTemplate.objects.all()
|
||||
serializer_class = TransactionTemplateSerializer
|
||||
filter_backends = [DjangoFilterBackend]
|
||||
filter_backends = [SearchFilter, DjangoFilterBackend]
|
||||
filterset_fields = ['name', 'amount', 'display', 'category', ]
|
||||
search_fields = ['$name', ]
|
||||
|
||||
|
||||
class TransactionViewSet(ReadProtectedModelViewSet):
|
||||
|
58
apps/note/fixtures/button.json
Normal file
58
apps/note/fixtures/button.json
Normal file
@ -0,0 +1,58 @@
|
||||
[
|
||||
{
|
||||
"model": "note.templatecategory",
|
||||
"pk": 1,
|
||||
"fields": {
|
||||
"name": "Soft"
|
||||
}
|
||||
},
|
||||
{
|
||||
"model": "note.templatecategory",
|
||||
"pk": 2,
|
||||
"fields": {
|
||||
"name": "Pulls"
|
||||
}
|
||||
},
|
||||
{
|
||||
"model": "note.templatecategory",
|
||||
"pk": 3,
|
||||
"fields": {
|
||||
"name": "Gala"
|
||||
}
|
||||
},
|
||||
{
|
||||
"model": "note.templatecategory",
|
||||
"pk": 4,
|
||||
"fields": {
|
||||
"name": "Clubs"
|
||||
}
|
||||
},
|
||||
{
|
||||
"model": "note.templatecategory",
|
||||
"pk": 5,
|
||||
"fields": {
|
||||
"name": "Bouffe"
|
||||
}
|
||||
},
|
||||
{
|
||||
"model": "note.templatecategory",
|
||||
"pk": 6,
|
||||
"fields": {
|
||||
"name": "BDA"
|
||||
}
|
||||
},
|
||||
{
|
||||
"model": "note.templatecategory",
|
||||
"pk": 7,
|
||||
"fields": {
|
||||
"name": "Autre"
|
||||
}
|
||||
},
|
||||
{
|
||||
"model": "note.templatecategory",
|
||||
"pk": 8,
|
||||
"fields": {
|
||||
"name": "Alcool"
|
||||
}
|
||||
}
|
||||
]
|
@ -70,7 +70,7 @@
|
||||
"balance": 0,
|
||||
"last_negative": null,
|
||||
"is_active": true,
|
||||
"display_image": "",
|
||||
"display_image": "pic/default.png",
|
||||
"created_at": "2020-02-20T20:09:38.615Z"
|
||||
}
|
||||
},
|
||||
@ -85,23 +85,8 @@
|
||||
"balance": 0,
|
||||
"last_negative": null,
|
||||
"is_active": true,
|
||||
"display_image": "",
|
||||
"created_at": "2020-02-20T20:16:14.753Z"
|
||||
}
|
||||
},
|
||||
{
|
||||
"model": "note.note",
|
||||
"pk": 7,
|
||||
"fields": {
|
||||
"polymorphic_ctype": [
|
||||
"note",
|
||||
"noteuser"
|
||||
],
|
||||
"balance": 0,
|
||||
"last_negative": null,
|
||||
"is_active": true,
|
||||
"display_image": "pic/default.png",
|
||||
"created_at": "2020-03-22T13:01:35.680Z"
|
||||
"created_at": "2020-02-20T20:16:14.753Z"
|
||||
}
|
||||
},
|
||||
{
|
||||
@ -199,61 +184,5 @@
|
||||
"normalized_name": "kfet",
|
||||
"note": 6
|
||||
}
|
||||
},
|
||||
{
|
||||
"model": "note.templatecategory",
|
||||
"pk": 1,
|
||||
"fields": {
|
||||
"name": "Soft"
|
||||
}
|
||||
},
|
||||
{
|
||||
"model": "note.templatecategory",
|
||||
"pk": 2,
|
||||
"fields": {
|
||||
"name": "Pulls"
|
||||
}
|
||||
},
|
||||
{
|
||||
"model": "note.templatecategory",
|
||||
"pk": 3,
|
||||
"fields": {
|
||||
"name": "Gala"
|
||||
}
|
||||
},
|
||||
{
|
||||
"model": "note.templatecategory",
|
||||
"pk": 4,
|
||||
"fields": {
|
||||
"name": "Clubs"
|
||||
}
|
||||
},
|
||||
{
|
||||
"model": "note.templatecategory",
|
||||
"pk": 5,
|
||||
"fields": {
|
||||
"name": "Bouffe"
|
||||
}
|
||||
},
|
||||
{
|
||||
"model": "note.templatecategory",
|
||||
"pk": 6,
|
||||
"fields": {
|
||||
"name": "BDA"
|
||||
}
|
||||
},
|
||||
{
|
||||
"model": "note.templatecategory",
|
||||
"pk": 7,
|
||||
"fields": {
|
||||
"name": "Autre"
|
||||
}
|
||||
},
|
||||
{
|
||||
"model": "note.templatecategory",
|
||||
"pk": 8,
|
||||
"fields": {
|
||||
"name": "Alcool"
|
||||
}
|
||||
}
|
||||
]
|
||||
]
|
@ -9,17 +9,6 @@ from .models import Alias
|
||||
from .models import TransactionTemplate
|
||||
|
||||
|
||||
class AliasForm(forms.ModelForm):
|
||||
class Meta:
|
||||
model = Alias
|
||||
fields = ("name",)
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
self.fields["name"].label = False
|
||||
self.fields["name"].widget.attrs = {"placeholder": _('New Alias')}
|
||||
|
||||
|
||||
class ImageForm(forms.Form):
|
||||
image = forms.ImageField(required=False,
|
||||
label=_('select an image'),
|
||||
|
@ -3,12 +3,12 @@
|
||||
|
||||
from .notes import Alias, Note, NoteClub, NoteSpecial, NoteUser
|
||||
from .transactions import MembershipTransaction, Transaction, \
|
||||
TemplateCategory, TransactionTemplate, RecurrentTransaction
|
||||
TemplateCategory, TransactionTemplate, RecurrentTransaction, SpecialTransaction
|
||||
|
||||
__all__ = [
|
||||
# Notes
|
||||
'Alias', 'Note', 'NoteClub', 'NoteSpecial', 'NoteUser',
|
||||
# Transactions
|
||||
'MembershipTransaction', 'Transaction', 'TemplateCategory', 'TransactionTemplate',
|
||||
'RecurrentTransaction',
|
||||
'RecurrentTransaction', 'SpecialTransaction',
|
||||
]
|
||||
|
@ -228,7 +228,7 @@ class Alias(models.Model):
|
||||
for cat in {'M', 'P', 'Z', 'C'})).casefold()
|
||||
|
||||
def clean(self):
|
||||
normalized_name = Alias.normalize(self.name)
|
||||
normalized_name = self.normalize(self.name)
|
||||
if len(normalized_name) >= 255:
|
||||
raise ValidationError(_('Alias is too long.'),
|
||||
code='alias_too_long')
|
||||
@ -242,8 +242,12 @@ class Alias(models.Model):
|
||||
pass
|
||||
self.normalized_name = normalized_name
|
||||
|
||||
def save(self,*args,**kwargs):
|
||||
self.normalized_name = self.normalize(self.name)
|
||||
super().save(*args,**kwargs)
|
||||
|
||||
def delete(self, using=None, keep_parents=False):
|
||||
if self.name == str(self.note):
|
||||
raise ValidationError(_("You can't delete your main alias."),
|
||||
code="cant_delete_main_alias")
|
||||
code="main_alias")
|
||||
return super().delete(using, keep_parents)
|
||||
|
@ -2,6 +2,7 @@
|
||||
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
from django.db import models
|
||||
from django.db.models import F
|
||||
from django.urls import reverse
|
||||
from django.utils import timezone
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
@ -93,12 +94,26 @@ class Transaction(PolymorphicModel):
|
||||
related_name='+',
|
||||
verbose_name=_('source'),
|
||||
)
|
||||
|
||||
source_alias = models.CharField(
|
||||
max_length=255,
|
||||
default="", # Will be remplaced by the name of the note on save
|
||||
verbose_name=_('used alias'),
|
||||
)
|
||||
|
||||
destination = models.ForeignKey(
|
||||
Note,
|
||||
on_delete=models.PROTECT,
|
||||
related_name='+',
|
||||
verbose_name=_('destination'),
|
||||
)
|
||||
|
||||
destination_alias = models.CharField(
|
||||
max_length=255,
|
||||
default="", # Will be remplaced by the name of the note on save
|
||||
verbose_name=_('used alias'),
|
||||
)
|
||||
|
||||
created_at = models.DateTimeField(
|
||||
verbose_name=_('created at'),
|
||||
default=timezone.now,
|
||||
@ -115,11 +130,19 @@ class Transaction(PolymorphicModel):
|
||||
verbose_name=_('reason'),
|
||||
max_length=255,
|
||||
)
|
||||
|
||||
valid = models.BooleanField(
|
||||
verbose_name=_('valid'),
|
||||
default=True,
|
||||
)
|
||||
|
||||
invalidity_reason = models.CharField(
|
||||
verbose_name=_('invalidity reason'),
|
||||
max_length=255,
|
||||
default=None,
|
||||
null=True,
|
||||
)
|
||||
|
||||
class Meta:
|
||||
verbose_name = _("transaction")
|
||||
verbose_name_plural = _("transactions")
|
||||
@ -134,6 +157,13 @@ class Transaction(PolymorphicModel):
|
||||
When saving, also transfer money between two notes
|
||||
"""
|
||||
|
||||
# If the aliases are not entered, we assume that the used alias is the name of the note
|
||||
if not self.source_alias:
|
||||
self.source_alias = str(self.source)
|
||||
|
||||
if not self.destination_alias:
|
||||
self.destination_alias = str(self.destination)
|
||||
|
||||
if self.source.pk == self.destination.pk:
|
||||
# When source == destination, no money is transfered
|
||||
super().save(*args, **kwargs)
|
||||
@ -152,6 +182,10 @@ class Transaction(PolymorphicModel):
|
||||
self.source.balance -= to_transfer
|
||||
self.destination.balance += to_transfer
|
||||
|
||||
# When a transaction is declared valid, we ensure that the invalidity reason is null, if it was
|
||||
# previously invalid
|
||||
self.invalidity_reason = None
|
||||
|
||||
# We save first the transaction, in case of the user has no right to transfer money
|
||||
super().save(*args, **kwargs)
|
||||
|
||||
|
@ -5,11 +5,12 @@ import html
|
||||
|
||||
import django_tables2 as tables
|
||||
from django.db.models import F
|
||||
from django.utils.html import format_html
|
||||
from django_tables2.utils import A
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
from .models.notes import Alias
|
||||
from .models.transactions import Transaction
|
||||
from .models.transactions import Transaction, TransactionTemplate
|
||||
from .templatetags.pretty_money import pretty_money
|
||||
|
||||
|
||||
@ -20,19 +21,48 @@ class HistoryTable(tables.Table):
|
||||
'table table-condensed table-striped table-hover'
|
||||
}
|
||||
model = Transaction
|
||||
exclude = ("id", "polymorphic_ctype", )
|
||||
exclude = ("id", "polymorphic_ctype", "invalidity_reason", "source_alias", "destination_alias",)
|
||||
template_name = 'django_tables2/bootstrap4.html'
|
||||
sequence = ('...', 'type', 'total', 'valid', )
|
||||
sequence = ('...', 'type', 'total', 'valid',)
|
||||
orderable = False
|
||||
|
||||
source = tables.Column(
|
||||
attrs={
|
||||
"td": {
|
||||
"data-toggle": "tooltip",
|
||||
"title": lambda record: _("used alias").capitalize() + " : " + record.source_alias,
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
destination = tables.Column(
|
||||
attrs={
|
||||
"td": {
|
||||
"data-toggle": "tooltip",
|
||||
"title": lambda record: _("used alias").capitalize() + " : " + record.destination_alias,
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
type = tables.Column()
|
||||
|
||||
total = tables.Column() # will use Transaction.total() !!
|
||||
|
||||
valid = tables.Column(attrs={"td": {"id": lambda record: "validate_" + str(record.id),
|
||||
"class": lambda record: str(record.valid).lower() + ' validate',
|
||||
"onclick": lambda record: 'de_validate(' + str(record.id) + ', '
|
||||
+ str(record.valid).lower() + ')'}})
|
||||
valid = tables.Column(
|
||||
attrs={
|
||||
"td": {
|
||||
"id": lambda record: "validate_" + str(record.id),
|
||||
"class": lambda record: str(record.valid).lower() + ' validate',
|
||||
"data-toggle": "tooltip",
|
||||
"title": lambda record: _("Click to invalidate") if record.valid else _("Click to validate"),
|
||||
"onclick": lambda record: 'in_validate(' + str(record.id) + ', ' + str(record.valid).lower() + ')',
|
||||
"onmouseover": lambda record: '$("#invalidity_reason_'
|
||||
+ str(record.id) + '").show();$("#invalidity_reason_'
|
||||
+ str(record.id) + '").focus();',
|
||||
"onmouseout": lambda record: '$("#invalidity_reason_' + str(record.id) + '").hide()',
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
def order_total(self, queryset, is_descending):
|
||||
# needed for rendering
|
||||
@ -53,15 +83,32 @@ class HistoryTable(tables.Table):
|
||||
def render_reason(self, value):
|
||||
return html.unescape(value)
|
||||
|
||||
def render_valid(self, value):
|
||||
return "✔" if value else "✖"
|
||||
def render_valid(self, value, record):
|
||||
"""
|
||||
When the validation status is hovered, an input field is displayed to let the user specify an invalidity reason
|
||||
"""
|
||||
val = "✔" if value else "✖"
|
||||
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 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)
|
||||
|
||||
|
||||
# function delete_button(id) provided in template file
|
||||
DELETE_TEMPLATE = """
|
||||
<button id="{{ record.pk }}" class="btn btn-danger btn-sm" onclick="delete_button(this.id)"> {{ delete_trans }}</button>
|
||||
"""
|
||||
|
||||
|
||||
class AliasTable(tables.Table):
|
||||
class Meta:
|
||||
attrs = {
|
||||
'class':
|
||||
'table table condensed table-striped table-hover'
|
||||
'table table condensed table-striped table-hover',
|
||||
'id':"alias_table"
|
||||
}
|
||||
model = Alias
|
||||
fields = ('name',)
|
||||
@ -69,9 +116,37 @@ class AliasTable(tables.Table):
|
||||
|
||||
show_header = False
|
||||
name = tables.Column(attrs={'td': {'class': 'text-center'}})
|
||||
delete = tables.LinkColumn('member:user_alias_delete',
|
||||
args=[A('pk')],
|
||||
attrs={
|
||||
'td': {'class': 'col-sm-2'},
|
||||
'a': {'class': 'btn btn-danger'}},
|
||||
text='delete', accessor='pk')
|
||||
|
||||
delete_col = tables.TemplateColumn(template_code=DELETE_TEMPLATE,
|
||||
extra_context={"delete_trans": _('delete')},
|
||||
attrs={'td': {'class': 'col-sm-1'}})
|
||||
|
||||
|
||||
|
||||
class ButtonTable(tables.Table):
|
||||
class Meta:
|
||||
attrs = {
|
||||
'class':
|
||||
'table table-bordered condensed table-hover'
|
||||
}
|
||||
row_attrs = {
|
||||
'class': lambda record: 'table-row ' + 'table-success' if record.display else 'table-danger',
|
||||
'id': lambda record: "row-" + str(record.pk),
|
||||
'data-href': lambda record: record.pk
|
||||
}
|
||||
|
||||
model = TransactionTemplate
|
||||
|
||||
edit = tables.LinkColumn('note:template_update',
|
||||
args=[A('pk')],
|
||||
attrs={'td': {'class': 'col-sm-1'},
|
||||
'a': {'class': 'btn btn-sm btn-primary'}},
|
||||
text=_('edit'),
|
||||
accessor='pk')
|
||||
|
||||
delete_col = tables.TemplateColumn(template_code=DELETE_TEMPLATE,
|
||||
extra_context={"delete_trans": _('delete')},
|
||||
attrs={'td': {'class': 'col-sm-1'}})
|
||||
|
||||
def render_amount(self, value):
|
||||
return pretty_money(value)
|
||||
|
@ -18,5 +18,10 @@ def pretty_money(value):
|
||||
)
|
||||
|
||||
|
||||
def cents_to_euros(value):
|
||||
return "{:.02f}".format(value / 100) if value else ""
|
||||
|
||||
|
||||
register = template.Library()
|
||||
register.filter('pretty_money', pretty_money)
|
||||
register.filter('cents_to_euros', cents_to_euros)
|
||||
|
@ -8,7 +8,7 @@ from .models import Note
|
||||
|
||||
app_name = 'note'
|
||||
urlpatterns = [
|
||||
path('transfer/', views.TransactionCreate.as_view(), name='transfer'),
|
||||
path('transfer/', views.TransactionCreateView.as_view(), name='transfer'),
|
||||
path('buttons/create/', views.TransactionTemplateCreateView.as_view(), name='template_create'),
|
||||
path('buttons/update/<int:pk>/', views.TransactionTemplateUpdateView.as_view(), name='template_update'),
|
||||
path('buttons/', views.TransactionTemplateListView.as_view(), name='template_list'),
|
||||
|
@ -6,22 +6,25 @@ from django.contrib.auth.mixins import LoginRequiredMixin
|
||||
from django.contrib.contenttypes.models import ContentType
|
||||
from django.db.models import Q
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
from django.views.generic import CreateView, ListView, UpdateView
|
||||
from django.views.generic import CreateView, UpdateView
|
||||
from django_tables2 import SingleTableView
|
||||
from django.urls import reverse_lazy
|
||||
from permission.backends import PermissionBackend
|
||||
|
||||
from .forms import TransactionTemplateForm
|
||||
from .models import Transaction, TransactionTemplate, Alias, RecurrentTransaction, NoteSpecial
|
||||
from .models.transactions import SpecialTransaction
|
||||
from .tables import HistoryTable
|
||||
from .tables import HistoryTable, ButtonTable
|
||||
|
||||
|
||||
class TransactionCreate(LoginRequiredMixin, SingleTableView):
|
||||
class TransactionCreateView(LoginRequiredMixin, SingleTableView):
|
||||
"""
|
||||
Show transfer page
|
||||
View for the creation of Transaction between two note which are not :models:`transactions.RecurrentTransaction`.
|
||||
e.g. for donation/transfer between people and clubs or for credit/debit with :models:`note.NoteSpecial`
|
||||
"""
|
||||
template_name = "note/transaction_form.html"
|
||||
|
||||
model = Transaction
|
||||
# Transaction history table
|
||||
table_class = HistoryTable
|
||||
table_pagination = {"per_page": 50}
|
||||
@ -46,13 +49,14 @@ class TransactionCreate(LoginRequiredMixin, SingleTableView):
|
||||
|
||||
class NoteAutocomplete(autocomplete.Select2QuerySetView):
|
||||
"""
|
||||
Auto complete note by aliases
|
||||
Auto complete note by aliases. Used in every search field for note
|
||||
ex: :view:`ConsoView`, :view:`TransactionCreateView`
|
||||
"""
|
||||
|
||||
def get_queryset(self):
|
||||
"""
|
||||
Quand une personne cherche un alias, une requête est envoyée sur l'API dédiée à l'auto-complétion.
|
||||
Cette fonction récupère la requête, et renvoie la liste filtrée des aliases.
|
||||
When someone look for an :models:`note.Alias`, a query is sent to the dedicated API.
|
||||
This function handles the result and return a filtered list of aliases.
|
||||
"""
|
||||
# Un utilisateur non connecté n'a accès à aucune information
|
||||
if not self.request.user.is_authenticated:
|
||||
@ -81,6 +85,10 @@ class NoteAutocomplete(autocomplete.Select2QuerySetView):
|
||||
return qs
|
||||
|
||||
def get_result_label(self, result):
|
||||
"""
|
||||
Show the selected alias and the username associated
|
||||
<Alias> (aka. <Username> )
|
||||
"""
|
||||
# Gère l'affichage de l'alias dans la recherche
|
||||
res = result.name
|
||||
note_name = str(result.note)
|
||||
@ -89,7 +97,9 @@ class NoteAutocomplete(autocomplete.Select2QuerySetView):
|
||||
return res
|
||||
|
||||
def get_result_value(self, result):
|
||||
# Le résultat renvoyé doit être l'identifiant de la note, et non de l'alias
|
||||
"""
|
||||
The value used for the transactions will be the id of the Note.
|
||||
"""
|
||||
return str(result.note.pk)
|
||||
|
||||
|
||||
@ -99,14 +109,15 @@ class TransactionTemplateCreateView(LoginRequiredMixin, CreateView):
|
||||
"""
|
||||
model = TransactionTemplate
|
||||
form_class = TransactionTemplateForm
|
||||
success_url = reverse_lazy('note:template_list')
|
||||
|
||||
|
||||
class TransactionTemplateListView(LoginRequiredMixin, ListView):
|
||||
class TransactionTemplateListView(LoginRequiredMixin, SingleTableView):
|
||||
"""
|
||||
List TransactionsTemplates
|
||||
"""
|
||||
model = TransactionTemplate
|
||||
form_class = TransactionTemplateForm
|
||||
table_class = ButtonTable
|
||||
|
||||
|
||||
class TransactionTemplateUpdateView(LoginRequiredMixin, UpdateView):
|
||||
@ -114,11 +125,13 @@ class TransactionTemplateUpdateView(LoginRequiredMixin, UpdateView):
|
||||
"""
|
||||
model = TransactionTemplate
|
||||
form_class = TransactionTemplateForm
|
||||
success_url = reverse_lazy('note:template_list')
|
||||
|
||||
|
||||
class ConsoView(LoginRequiredMixin, SingleTableView):
|
||||
"""
|
||||
Consume
|
||||
The Magic View that make people pay their beer and burgers.
|
||||
(Most of the magic happens in the dark world of Javascript see consos.js)
|
||||
"""
|
||||
template_name = "note/conso_form.html"
|
||||
|
||||
|
@ -28,4 +28,3 @@ class RolePermissionsAdmin(admin.ModelAdmin):
|
||||
Admin customisation for RolePermissions
|
||||
"""
|
||||
list_display = ('role', )
|
||||
|
||||
|
@ -2,8 +2,8 @@
|
||||
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
from django_filters.rest_framework import DjangoFilterBackend
|
||||
|
||||
from api.viewsets import ReadOnlyProtectedModelViewSet
|
||||
|
||||
from .serializers import PermissionSerializer
|
||||
from ..models import Permission
|
||||
|
||||
|
@ -327,7 +327,7 @@
|
||||
"note",
|
||||
"transaction"
|
||||
],
|
||||
"query": "[\"AND\", {\"source\": [\"user\", \"note\"]}, {\"amount__lte\": [\"user\", \"note\", \"balance\"]}]",
|
||||
"query": "[\"AND\", {\"source\": [\"user\", \"note\"]}, [\"OR\", {\"amount__lte\": [\"user\", \"note\", \"balance\"]}, {\"valid\": false}]]",
|
||||
"type": "add",
|
||||
"mask": 1,
|
||||
"field": "",
|
||||
@ -387,7 +387,7 @@
|
||||
"note",
|
||||
"recurrenttransaction"
|
||||
],
|
||||
"query": "[\"AND\", {\"destination\": [\"club\", \"note\"]}, {\"amount__lte\": {\"F\": [\"ADD\", [\"F\", \"source__balance\"], 5000]}}]",
|
||||
"query": "[\"AND\", {\"destination\": [\"club\", \"note\"]}, [\"OR\", {\"amount__lte\": {\"F\": [\"ADD\", [\"F\", \"source__balance\"], 5000]}}, {\"valid\": false}]]",
|
||||
"type": "add",
|
||||
"mask": 2,
|
||||
"field": "",
|
||||
|
@ -10,7 +10,6 @@ from django.core.exceptions import ValidationError
|
||||
from django.db import models
|
||||
from django.db.models import F, Q, Model
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
from member.models import Role
|
||||
|
||||
|
||||
@ -281,4 +280,3 @@ class RolePermissions(models.Model):
|
||||
|
||||
def __str__(self):
|
||||
return str(self.role)
|
||||
|
||||
|
@ -3,6 +3,8 @@
|
||||
|
||||
from rest_framework.permissions import DjangoObjectPermissions
|
||||
|
||||
from .backends import PermissionBackend
|
||||
|
||||
SAFE_METHODS = ('HEAD', 'OPTIONS', )
|
||||
|
||||
|
||||
@ -41,8 +43,8 @@ class StrongDjangoObjectPermissions(DjangoObjectPermissions):
|
||||
user = request.user
|
||||
|
||||
perms = self.get_required_object_permissions(request.method, model_cls)
|
||||
|
||||
if not user.has_perms(perms, obj):
|
||||
# if not user.has_perms(perms, obj):
|
||||
if not all(PermissionBackend().has_perm(user, perm, obj) for perm in perms):
|
||||
# If the user does not have permissions we need to determine if
|
||||
# they have read permissions to see 403, or not, and simply see
|
||||
# a 404 response.
|
||||
|
@ -3,10 +3,9 @@
|
||||
|
||||
from django.core.exceptions import PermissionDenied
|
||||
from django.db.models.signals import pre_save, pre_delete, post_save, post_delete
|
||||
|
||||
from logs import signals as logs_signals
|
||||
from permission.backends import PermissionBackend
|
||||
from note_kfet.middlewares import get_current_authenticated_user
|
||||
from permission.backends import PermissionBackend
|
||||
|
||||
|
||||
EXCLUDED = [
|
||||
|
@ -3,10 +3,8 @@
|
||||
|
||||
from django.contrib.contenttypes.models import ContentType
|
||||
from django.template.defaultfilters import stringfilter
|
||||
|
||||
from note_kfet.middlewares import get_current_authenticated_user, get_current_session
|
||||
from django import template
|
||||
|
||||
from note_kfet.middlewares import get_current_authenticated_user, get_current_session
|
||||
from permission.backends import PermissionBackend
|
||||
|
||||
|
||||
|
4
apps/treasury/__init__.py
Normal file
4
apps/treasury/__init__.py
Normal file
@ -0,0 +1,4 @@
|
||||
# Copyright (C) 2018-2020 by BDE ENS Paris-Saclay
|
||||
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
default_app_config = 'treasury.apps.TreasuryConfig'
|
27
apps/treasury/admin.py
Normal file
27
apps/treasury/admin.py
Normal file
@ -0,0 +1,27 @@
|
||||
# Copyright (C) 2018-2020 by BDE ENS Paris-Saclay
|
||||
# SPDX-License-Identifier: GPL-3.0-or-lateré
|
||||
|
||||
from django.contrib import admin
|
||||
|
||||
from .models import RemittanceType, Remittance
|
||||
|
||||
|
||||
@admin.register(RemittanceType)
|
||||
class RemittanceTypeAdmin(admin.ModelAdmin):
|
||||
"""
|
||||
Admin customisation for RemiitanceType
|
||||
"""
|
||||
list_display = ('note', )
|
||||
|
||||
|
||||
@admin.register(Remittance)
|
||||
class RemittanceAdmin(admin.ModelAdmin):
|
||||
"""
|
||||
Admin customisation for Remittance
|
||||
"""
|
||||
list_display = ('remittance_type', 'date', 'comment', 'count', 'amount', 'closed', )
|
||||
|
||||
def has_change_permission(self, request, obj=None):
|
||||
if not obj:
|
||||
return True
|
||||
return not obj.closed and super().has_change_permission(request, obj)
|
0
apps/treasury/api/__init__.py
Normal file
0
apps/treasury/api/__init__.py
Normal file
62
apps/treasury/api/serializers.py
Normal file
62
apps/treasury/api/serializers.py
Normal file
@ -0,0 +1,62 @@
|
||||
# Copyright (C) 2018-2020 by BDE ENS Paris-Saclay
|
||||
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
from rest_framework import serializers
|
||||
from note.api.serializers import SpecialTransactionSerializer
|
||||
|
||||
from ..models import Invoice, Product, RemittanceType, Remittance
|
||||
|
||||
|
||||
class ProductSerializer(serializers.ModelSerializer):
|
||||
"""
|
||||
REST API Serializer for Product types.
|
||||
The djangorestframework plugin will analyse the model `Product` and parse all fields in the API.
|
||||
"""
|
||||
|
||||
class Meta:
|
||||
model = Product
|
||||
fields = '__all__'
|
||||
|
||||
|
||||
class InvoiceSerializer(serializers.ModelSerializer):
|
||||
"""
|
||||
REST API Serializer for Invoice types.
|
||||
The djangorestframework plugin will analyse the model `Invoice` and parse all fields in the API.
|
||||
"""
|
||||
class Meta:
|
||||
model = Invoice
|
||||
fields = '__all__'
|
||||
read_only_fields = ('bde',)
|
||||
|
||||
products = serializers.SerializerMethodField()
|
||||
|
||||
def get_products(self, obj):
|
||||
return serializers.ListSerializer(child=ProductSerializer())\
|
||||
.to_representation(Product.objects.filter(invoice=obj).all())
|
||||
|
||||
|
||||
class RemittanceTypeSerializer(serializers.ModelSerializer):
|
||||
"""
|
||||
REST API Serializer for RemittanceType types.
|
||||
The djangorestframework plugin will analyse the model `RemittanceType` and parse all fields in the API.
|
||||
"""
|
||||
|
||||
class Meta:
|
||||
model = RemittanceType
|
||||
fields = '__all__'
|
||||
|
||||
|
||||
class RemittanceSerializer(serializers.ModelSerializer):
|
||||
"""
|
||||
REST API Serializer for Remittance types.
|
||||
The djangorestframework plugin will analyse the model `Remittance` and parse all fields in the API.
|
||||
"""
|
||||
|
||||
transactions = serializers.SerializerMethodField()
|
||||
|
||||
class Meta:
|
||||
model = Remittance
|
||||
fields = '__all__'
|
||||
|
||||
def get_transactions(self, obj):
|
||||
return serializers.ListSerializer(child=SpecialTransactionSerializer()).to_representation(obj.transactions)
|
14
apps/treasury/api/urls.py
Normal file
14
apps/treasury/api/urls.py
Normal file
@ -0,0 +1,14 @@
|
||||
# Copyright (C) 2018-2020 by BDE ENS Paris-Saclay
|
||||
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
from .views import InvoiceViewSet, ProductViewSet, RemittanceViewSet, RemittanceTypeViewSet
|
||||
|
||||
|
||||
def register_treasury_urls(router, path):
|
||||
"""
|
||||
Configure router for treasury REST API.
|
||||
"""
|
||||
router.register(path + '/invoice', InvoiceViewSet)
|
||||
router.register(path + '/product', ProductViewSet)
|
||||
router.register(path + '/remittance_type', RemittanceTypeViewSet)
|
||||
router.register(path + '/remittance', RemittanceViewSet)
|
53
apps/treasury/api/views.py
Normal file
53
apps/treasury/api/views.py
Normal file
@ -0,0 +1,53 @@
|
||||
# Copyright (C) 2018-2020 by BDE ENS Paris-Saclay
|
||||
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
from django_filters.rest_framework import DjangoFilterBackend
|
||||
from rest_framework.filters import SearchFilter
|
||||
from api.viewsets import ReadProtectedModelViewSet
|
||||
|
||||
from .serializers import InvoiceSerializer, ProductSerializer, RemittanceTypeSerializer, RemittanceSerializer
|
||||
from ..models import Invoice, Product, RemittanceType, Remittance
|
||||
|
||||
|
||||
class InvoiceViewSet(ReadProtectedModelViewSet):
|
||||
"""
|
||||
REST API View set.
|
||||
The djangorestframework plugin will get all `Invoice` objects, serialize it to JSON with the given serializer,
|
||||
then render it on /api/treasury/invoice/
|
||||
"""
|
||||
queryset = Invoice.objects.all()
|
||||
serializer_class = InvoiceSerializer
|
||||
filter_backends = [DjangoFilterBackend]
|
||||
filterset_fields = ['bde', ]
|
||||
|
||||
|
||||
class ProductViewSet(ReadProtectedModelViewSet):
|
||||
"""
|
||||
REST API View set.
|
||||
The djangorestframework plugin will get all `Product` objects, serialize it to JSON with the given serializer,
|
||||
then render it on /api/treasury/product/
|
||||
"""
|
||||
queryset = Product.objects.all()
|
||||
serializer_class = ProductSerializer
|
||||
filter_backends = [SearchFilter]
|
||||
search_fields = ['$designation', ]
|
||||
|
||||
|
||||
class RemittanceTypeViewSet(ReadProtectedModelViewSet):
|
||||
"""
|
||||
REST API View set.
|
||||
The djangorestframework plugin will get all `RemittanceType` objects, serialize it to JSON with the given serializer
|
||||
then render it on /api/treasury/remittance_type/
|
||||
"""
|
||||
queryset = RemittanceType.objects.all()
|
||||
serializer_class = RemittanceTypeSerializer
|
||||
|
||||
|
||||
class RemittanceViewSet(ReadProtectedModelViewSet):
|
||||
"""
|
||||
REST API View set.
|
||||
The djangorestframework plugin will get all `Remittance` objects, serialize it to JSON with the given serializer,
|
||||
then render it on /api/treasury/remittance/
|
||||
"""
|
||||
queryset = Remittance.objects.all()
|
||||
serializer_class = RemittanceSerializer
|
33
apps/treasury/apps.py
Normal file
33
apps/treasury/apps.py
Normal file
@ -0,0 +1,33 @@
|
||||
# Copyright (C) 2018-2020 by BDE ENS Paris-Saclay
|
||||
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
from django.apps import AppConfig
|
||||
from django.db.models import Q
|
||||
from django.db.models.signals import post_save, post_migrate
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
|
||||
class TreasuryConfig(AppConfig):
|
||||
name = 'treasury'
|
||||
verbose_name = _('Treasury')
|
||||
|
||||
def ready(self):
|
||||
"""
|
||||
Define app internal signals to interact with other apps
|
||||
"""
|
||||
|
||||
from . import signals
|
||||
from note.models import SpecialTransaction, NoteSpecial
|
||||
from treasury.models import SpecialTransactionProxy
|
||||
post_save.connect(signals.save_special_transaction, sender=SpecialTransaction)
|
||||
|
||||
def setup_specialtransactions_proxies(**kwargs):
|
||||
# If the treasury app was disabled for any reason during a certain amount of time,
|
||||
# we ensure that each special transaction is linked to a proxy
|
||||
for transaction in SpecialTransaction.objects.filter(
|
||||
source__in=NoteSpecial.objects.filter(~Q(remittancetype=None)),
|
||||
specialtransactionproxy=None,
|
||||
):
|
||||
SpecialTransactionProxy.objects.create(transaction=transaction, remittance=None)
|
||||
|
||||
post_migrate.connect(setup_specialtransactions_proxies, sender=SpecialTransactionProxy)
|
9
apps/treasury/fixtures/initial.json
Normal file
9
apps/treasury/fixtures/initial.json
Normal file
@ -0,0 +1,9 @@
|
||||
[
|
||||
{
|
||||
"model": "treasury.remittancetype",
|
||||
"pk": 1,
|
||||
"fields": {
|
||||
"note": 3
|
||||
}
|
||||
}
|
||||
]
|
156
apps/treasury/forms.py
Normal file
156
apps/treasury/forms.py
Normal file
@ -0,0 +1,156 @@
|
||||
# Copyright (C) 2018-2020 by BDE ENS Paris-Saclay
|
||||
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
import datetime
|
||||
|
||||
from crispy_forms.helper import FormHelper
|
||||
from crispy_forms.layout import Submit
|
||||
from django import forms
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
from .models import Invoice, Product, Remittance, SpecialTransactionProxy
|
||||
|
||||
|
||||
class InvoiceForm(forms.ModelForm):
|
||||
"""
|
||||
Create and generate invoices.
|
||||
"""
|
||||
|
||||
# Django forms don't support date fields. We have to add it manually
|
||||
date = forms.DateField(
|
||||
initial=datetime.date.today,
|
||||
widget=forms.TextInput(attrs={'type': 'date'})
|
||||
)
|
||||
|
||||
def clean_date(self):
|
||||
self.instance.date = self.data.get("date")
|
||||
|
||||
class Meta:
|
||||
model = Invoice
|
||||
exclude = ('bde', )
|
||||
|
||||
|
||||
# Add a subform per product in the invoice form, and manage correctly the link between the invoice and
|
||||
# its products. The FormSet will search automatically the ForeignKey in the Product model.
|
||||
ProductFormSet = forms.inlineformset_factory(
|
||||
Invoice,
|
||||
Product,
|
||||
fields='__all__',
|
||||
extra=1,
|
||||
)
|
||||
|
||||
|
||||
class ProductFormSetHelper(FormHelper):
|
||||
"""
|
||||
Specify some template informations for the product form.
|
||||
"""
|
||||
|
||||
def __init__(self, form=None):
|
||||
super().__init__(form)
|
||||
self.form_tag = False
|
||||
self.form_method = 'POST'
|
||||
self.form_class = 'form-inline'
|
||||
self.template = 'bootstrap4/table_inline_formset.html'
|
||||
|
||||
|
||||
class RemittanceForm(forms.ModelForm):
|
||||
"""
|
||||
Create remittances.
|
||||
"""
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
|
||||
self.helper = FormHelper()
|
||||
|
||||
# We can't update the type of the remittance once created.
|
||||
if self.instance.pk:
|
||||
self.fields["remittance_type"].disabled = True
|
||||
self.fields["remittance_type"].required = False
|
||||
|
||||
# We display the submit button iff the remittance is open,
|
||||
# the close button iff it is open and has a linked transaction
|
||||
if not self.instance.closed:
|
||||
self.helper.add_input(Submit('submit', _("Submit"), attr={'class': 'btn btn-block btn-primary'}))
|
||||
if self.instance.transactions:
|
||||
self.helper.add_input(Submit("close", _("Close"), css_class='btn btn-success'))
|
||||
else:
|
||||
# If the remittance is closed, we can't change anything
|
||||
self.fields["comment"].disabled = True
|
||||
self.fields["comment"].required = False
|
||||
|
||||
def clean(self):
|
||||
# We can't update anything if the remittance is already closed.
|
||||
if self.instance.closed:
|
||||
self.add_error("comment", _("Remittance is already closed."))
|
||||
|
||||
cleaned_data = super().clean()
|
||||
|
||||
if self.instance.pk and cleaned_data.get("remittance_type") != self.instance.remittance_type:
|
||||
self.add_error("remittance_type", _("You can't change the type of the remittance."))
|
||||
|
||||
# The close button is manually handled
|
||||
if "close" in self.data:
|
||||
self.instance.closed = True
|
||||
self.cleaned_data["closed"] = True
|
||||
|
||||
return cleaned_data
|
||||
|
||||
class Meta:
|
||||
model = Remittance
|
||||
fields = ('remittance_type', 'comment',)
|
||||
|
||||
|
||||
class LinkTransactionToRemittanceForm(forms.ModelForm):
|
||||
"""
|
||||
Attach a special transaction to a remittance.
|
||||
"""
|
||||
|
||||
# Since we use a proxy model for special transactions, we add manually the fields related to the transaction
|
||||
last_name = forms.CharField(label=_("Last name"))
|
||||
|
||||
first_name = forms.Field(label=_("First name"))
|
||||
|
||||
bank = forms.Field(label=_("Bank"))
|
||||
|
||||
amount = forms.IntegerField(label=_("Amount"), min_value=0)
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
self.helper = FormHelper()
|
||||
# Add submit button
|
||||
self.helper.add_input(Submit('submit', _("Submit"), attr={'class': 'btn btn-block btn-primary'}))
|
||||
|
||||
self.fields["remittance"].queryset = Remittance.objects.filter(closed=False)
|
||||
|
||||
def clean_last_name(self):
|
||||
"""
|
||||
Replace the first name in the information of the transaction.
|
||||
"""
|
||||
self.instance.transaction.last_name = self.data.get("last_name")
|
||||
self.instance.transaction.clean()
|
||||
|
||||
def clean_first_name(self):
|
||||
"""
|
||||
Replace the last name in the information of the transaction.
|
||||
"""
|
||||
self.instance.transaction.first_name = self.data.get("first_name")
|
||||
self.instance.transaction.clean()
|
||||
|
||||
def clean_bank(self):
|
||||
"""
|
||||
Replace the bank in the information of the transaction.
|
||||
"""
|
||||
self.instance.transaction.bank = self.data.get("bank")
|
||||
self.instance.transaction.clean()
|
||||
|
||||
def clean_amount(self):
|
||||
"""
|
||||
Replace the amount of the transaction.
|
||||
"""
|
||||
self.instance.transaction.amount = self.data.get("amount")
|
||||
self.instance.transaction.clean()
|
||||
|
||||
class Meta:
|
||||
model = SpecialTransactionProxy
|
||||
fields = ('remittance', )
|
0
apps/treasury/migrations/__init__.py
Normal file
0
apps/treasury/migrations/__init__.py
Normal file
189
apps/treasury/models.py
Normal file
189
apps/treasury/models.py
Normal file
@ -0,0 +1,189 @@
|
||||
# Copyright (C) 2018-2020 by BDE ENS Paris-Saclay
|
||||
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
from django.core.exceptions import ValidationError
|
||||
from django.db import models
|
||||
from django.db.models import Q
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
from note.models import NoteSpecial, SpecialTransaction
|
||||
|
||||
|
||||
class Invoice(models.Model):
|
||||
"""
|
||||
An invoice model that can generates a true invoice.
|
||||
"""
|
||||
|
||||
id = models.PositiveIntegerField(
|
||||
primary_key=True,
|
||||
verbose_name=_("Invoice identifier"),
|
||||
)
|
||||
|
||||
bde = models.CharField(
|
||||
max_length=32,
|
||||
default='Saperlistpopette.png',
|
||||
choices=(
|
||||
('Saperlistpopette.png', 'Saper[list]popette'),
|
||||
('Finalist.png', 'Fina[list]'),
|
||||
('Listorique.png', '[List]orique'),
|
||||
('Satellist.png', 'Satel[list]'),
|
||||
('Monopolist.png', 'Monopo[list]'),
|
||||
('Kataclist.png', 'Katac[list]'),
|
||||
),
|
||||
verbose_name=_("BDE"),
|
||||
)
|
||||
|
||||
object = models.CharField(
|
||||
max_length=255,
|
||||
verbose_name=_("Object"),
|
||||
)
|
||||
|
||||
description = models.TextField(
|
||||
verbose_name=_("Description")
|
||||
)
|
||||
|
||||
name = models.CharField(
|
||||
max_length=255,
|
||||
verbose_name=_("Name"),
|
||||
)
|
||||
|
||||
address = models.TextField(
|
||||
verbose_name=_("Address"),
|
||||
)
|
||||
|
||||
date = models.DateField(
|
||||
auto_now_add=True,
|
||||
verbose_name=_("Place"),
|
||||
)
|
||||
|
||||
acquitted = models.BooleanField(
|
||||
verbose_name=_("Acquitted"),
|
||||
)
|
||||
|
||||
|
||||
class Product(models.Model):
|
||||
"""
|
||||
Product that appears on an invoice.
|
||||
"""
|
||||
|
||||
invoice = models.ForeignKey(
|
||||
Invoice,
|
||||
on_delete=models.PROTECT,
|
||||
)
|
||||
|
||||
designation = models.CharField(
|
||||
max_length=255,
|
||||
verbose_name=_("Designation"),
|
||||
)
|
||||
|
||||
quantity = models.PositiveIntegerField(
|
||||
verbose_name=_("Quantity")
|
||||
)
|
||||
|
||||
amount = models.IntegerField(
|
||||
verbose_name=_("Unit price")
|
||||
)
|
||||
|
||||
@property
|
||||
def amount_euros(self):
|
||||
return self.amount / 100
|
||||
|
||||
@property
|
||||
def total(self):
|
||||
return self.quantity * self.amount
|
||||
|
||||
@property
|
||||
def total_euros(self):
|
||||
return self.total / 100
|
||||
|
||||
|
||||
class RemittanceType(models.Model):
|
||||
"""
|
||||
Store what kind of remittances can be stored.
|
||||
"""
|
||||
|
||||
note = models.OneToOneField(
|
||||
NoteSpecial,
|
||||
on_delete=models.CASCADE,
|
||||
)
|
||||
|
||||
def __str__(self):
|
||||
return str(self.note)
|
||||
|
||||
|
||||
class Remittance(models.Model):
|
||||
"""
|
||||
Treasurers want to regroup checks or bank transfers in bank remittances.
|
||||
"""
|
||||
|
||||
date = models.DateTimeField(
|
||||
auto_now_add=True,
|
||||
verbose_name=_("Date"),
|
||||
)
|
||||
|
||||
remittance_type = models.ForeignKey(
|
||||
RemittanceType,
|
||||
on_delete=models.PROTECT,
|
||||
verbose_name=_("Type"),
|
||||
)
|
||||
|
||||
comment = models.CharField(
|
||||
max_length=255,
|
||||
verbose_name=_("Comment"),
|
||||
)
|
||||
|
||||
closed = models.BooleanField(
|
||||
default=False,
|
||||
verbose_name=_("Closed"),
|
||||
)
|
||||
|
||||
@property
|
||||
def transactions(self):
|
||||
"""
|
||||
:return: Transactions linked to this remittance.
|
||||
"""
|
||||
if not self.pk:
|
||||
return SpecialTransaction.objects.none()
|
||||
return SpecialTransaction.objects.filter(specialtransactionproxy__remittance=self)
|
||||
|
||||
def count(self):
|
||||
"""
|
||||
Linked transactions count.
|
||||
"""
|
||||
return self.transactions.count()
|
||||
|
||||
@property
|
||||
def amount(self):
|
||||
"""
|
||||
Total amount of the remittance.
|
||||
"""
|
||||
return sum(transaction.total for transaction in self.transactions.all())
|
||||
|
||||
def save(self, force_insert=False, force_update=False, using=None, update_fields=None):
|
||||
# Check if all transactions have the right type.
|
||||
if self.transactions.filter(~Q(source=self.remittance_type.note)).exists():
|
||||
raise ValidationError("All transactions in a remittance must have the same type")
|
||||
|
||||
return super().save(force_insert, force_update, using, update_fields)
|
||||
|
||||
def __str__(self):
|
||||
return _("Remittance #{:d}: {}").format(self.id, self.comment, )
|
||||
|
||||
|
||||
class SpecialTransactionProxy(models.Model):
|
||||
"""
|
||||
In order to keep modularity, we don't that the Note app depends on the treasury app.
|
||||
That's why we create a proxy in this app, to link special transactions and remittances.
|
||||
If it isn't very clean, that makes what we want.
|
||||
"""
|
||||
|
||||
transaction = models.OneToOneField(
|
||||
SpecialTransaction,
|
||||
on_delete=models.CASCADE,
|
||||
)
|
||||
|
||||
remittance = models.ForeignKey(
|
||||
Remittance,
|
||||
on_delete=models.PROTECT,
|
||||
null=True,
|
||||
verbose_name=_("Remittance"),
|
||||
)
|
12
apps/treasury/signals.py
Normal file
12
apps/treasury/signals.py
Normal file
@ -0,0 +1,12 @@
|
||||
# Copyright (C) 2018-2020 by BDE ENS Paris-Saclay
|
||||
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
from treasury.models import SpecialTransactionProxy, RemittanceType
|
||||
|
||||
|
||||
def save_special_transaction(instance, created, **kwargs):
|
||||
"""
|
||||
When a special transaction is created, we create its linked proxy
|
||||
"""
|
||||
if created and RemittanceType.objects.filter(note=instance.source).exists():
|
||||
SpecialTransactionProxy.objects.create(transaction=instance, remittance=None).save()
|
103
apps/treasury/tables.py
Normal file
103
apps/treasury/tables.py
Normal file
@ -0,0 +1,103 @@
|
||||
# Copyright (C) 2018-2020 by BDE ENS Paris-Saclay
|
||||
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
import django_tables2 as tables
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
from django_tables2 import A
|
||||
from note.models import SpecialTransaction
|
||||
from note.templatetags.pretty_money import pretty_money
|
||||
|
||||
from .models import Invoice, Remittance
|
||||
|
||||
|
||||
class InvoiceTable(tables.Table):
|
||||
"""
|
||||
List all invoices.
|
||||
"""
|
||||
id = tables.LinkColumn("treasury:invoice_update",
|
||||
args=[A("pk")],
|
||||
text=lambda record: _("Invoice #{:d}").format(record.id), )
|
||||
|
||||
invoice = tables.LinkColumn("treasury:invoice_render",
|
||||
verbose_name=_("Invoice"),
|
||||
args=[A("pk")],
|
||||
accessor="pk",
|
||||
text="",
|
||||
attrs={
|
||||
'a': {'class': 'fa fa-file-pdf-o'},
|
||||
'td': {'data-turbolinks': 'false'}
|
||||
})
|
||||
|
||||
class Meta:
|
||||
attrs = {
|
||||
'class': 'table table-condensed table-striped table-hover'
|
||||
}
|
||||
model = Invoice
|
||||
template_name = 'django_tables2/bootstrap4.html'
|
||||
fields = ('id', 'name', 'object', 'acquitted', 'invoice',)
|
||||
|
||||
|
||||
class RemittanceTable(tables.Table):
|
||||
"""
|
||||
List all remittances.
|
||||
"""
|
||||
|
||||
count = tables.Column(verbose_name=_("Transaction count"))
|
||||
|
||||
amount = tables.Column(verbose_name=_("Amount"))
|
||||
|
||||
view = tables.LinkColumn("treasury:remittance_update",
|
||||
verbose_name=_("View"),
|
||||
args=[A("pk")],
|
||||
text=_("View"),
|
||||
attrs={
|
||||
'a': {'class': 'btn btn-primary'}
|
||||
}, )
|
||||
|
||||
def render_amount(self, value):
|
||||
return pretty_money(value)
|
||||
|
||||
class Meta:
|
||||
attrs = {
|
||||
'class': 'table table-condensed table-striped table-hover'
|
||||
}
|
||||
model = Remittance
|
||||
template_name = 'django_tables2/bootstrap4.html'
|
||||
fields = ('id', 'date', 'remittance_type', 'comment', 'count', 'amount', 'view',)
|
||||
|
||||
|
||||
class SpecialTransactionTable(tables.Table):
|
||||
"""
|
||||
List special credit transactions that are (or not, following the queryset) attached to a remittance.
|
||||
"""
|
||||
|
||||
# Display add and remove buttons. Use the `exclude` field to select what is needed.
|
||||
remittance_add = tables.LinkColumn("treasury:link_transaction",
|
||||
verbose_name=_("Remittance"),
|
||||
args=[A("specialtransactionproxy.pk")],
|
||||
text=_("Add"),
|
||||
attrs={
|
||||
'a': {'class': 'btn btn-primary'}
|
||||
}, )
|
||||
|
||||
remittance_remove = tables.LinkColumn("treasury:unlink_transaction",
|
||||
verbose_name=_("Remittance"),
|
||||
args=[A("specialtransactionproxy.pk")],
|
||||
text=_("Remove"),
|
||||
attrs={
|
||||
'a': {'class': 'btn btn-primary btn-danger'}
|
||||
}, )
|
||||
|
||||
def render_id(self, record):
|
||||
return record.specialtransactionproxy.pk
|
||||
|
||||
def render_amount(self, value):
|
||||
return pretty_money(value)
|
||||
|
||||
class Meta:
|
||||
attrs = {
|
||||
'class': 'table table-condensed table-striped table-hover'
|
||||
}
|
||||
model = SpecialTransaction
|
||||
template_name = 'django_tables2/bootstrap4.html'
|
||||
fields = ('id', 'source', 'destination', 'last_name', 'first_name', 'bank', 'amount', 'reason',)
|
24
apps/treasury/urls.py
Normal file
24
apps/treasury/urls.py
Normal file
@ -0,0 +1,24 @@
|
||||
# Copyright (C) 2018-2020 by BDE ENS Paris-Saclay
|
||||
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
from django.urls import path
|
||||
|
||||
from .views import InvoiceCreateView, InvoiceListView, InvoiceUpdateView, InvoiceRenderView, RemittanceListView,\
|
||||
RemittanceCreateView, RemittanceUpdateView, LinkTransactionToRemittanceView, UnlinkTransactionToRemittanceView
|
||||
|
||||
app_name = 'treasury'
|
||||
urlpatterns = [
|
||||
# Invoice app paths
|
||||
path('invoice/', InvoiceListView.as_view(), name='invoice_list'),
|
||||
path('invoice/create/', InvoiceCreateView.as_view(), name='invoice_create'),
|
||||
path('invoice/<int:pk>/', InvoiceUpdateView.as_view(), name='invoice_update'),
|
||||
path('invoice/render/<int:pk>/', InvoiceRenderView.as_view(), name='invoice_render'),
|
||||
|
||||
# Remittance app paths
|
||||
path('remittance/', RemittanceListView.as_view(), name='remittance_list'),
|
||||
path('remittance/create/', RemittanceCreateView.as_view(), name='remittance_create'),
|
||||
path('remittance/<int:pk>/', RemittanceUpdateView.as_view(), name='remittance_update'),
|
||||
path('remittance/link_transaction/<int:pk>/', LinkTransactionToRemittanceView.as_view(), name='link_transaction'),
|
||||
path('remittance/unlink_transaction/<int:pk>/', UnlinkTransactionToRemittanceView.as_view(),
|
||||
name='unlink_transaction'),
|
||||
]
|
316
apps/treasury/views.py
Normal file
316
apps/treasury/views.py
Normal file
@ -0,0 +1,316 @@
|
||||
# Copyright (C) 2018-2020 by BDE ENS Paris-Saclay
|
||||
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
import os
|
||||
import shutil
|
||||
import subprocess
|
||||
from tempfile import mkdtemp
|
||||
|
||||
from crispy_forms.helper import FormHelper
|
||||
from django.contrib.auth.mixins import LoginRequiredMixin
|
||||
from django.core.exceptions import ValidationError
|
||||
from django.db.models import Q
|
||||
from django.http import HttpResponse
|
||||
from django.shortcuts import redirect
|
||||
from django.template.loader import render_to_string
|
||||
from django.urls import reverse_lazy
|
||||
from django.views.generic import CreateView, UpdateView
|
||||
from django.views.generic.base import View, TemplateView
|
||||
from django_tables2 import SingleTableView
|
||||
from note.models import SpecialTransaction, NoteSpecial
|
||||
from note_kfet.settings.base import BASE_DIR
|
||||
|
||||
from .forms import InvoiceForm, ProductFormSet, ProductFormSetHelper, RemittanceForm, LinkTransactionToRemittanceForm
|
||||
from .models import Invoice, Product, Remittance, SpecialTransactionProxy
|
||||
from .tables import InvoiceTable, RemittanceTable, SpecialTransactionTable
|
||||
|
||||
|
||||
class InvoiceCreateView(LoginRequiredMixin, CreateView):
|
||||
"""
|
||||
Create Invoice
|
||||
"""
|
||||
model = Invoice
|
||||
form_class = InvoiceForm
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
context = super().get_context_data(**kwargs)
|
||||
|
||||
form = context['form']
|
||||
form.helper = FormHelper()
|
||||
# Remove form tag on the generation of the form in the template (already present on the template)
|
||||
form.helper.form_tag = False
|
||||
# The formset handles the set of the products
|
||||
form_set = ProductFormSet(instance=form.instance)
|
||||
context['formset'] = form_set
|
||||
context['helper'] = ProductFormSetHelper()
|
||||
context['no_cache'] = True
|
||||
|
||||
return context
|
||||
|
||||
def form_valid(self, form):
|
||||
ret = super().form_valid(form)
|
||||
|
||||
kwargs = {}
|
||||
|
||||
# The user type amounts in cents. We convert it in euros.
|
||||
for key in self.request.POST:
|
||||
value = self.request.POST[key]
|
||||
if key.endswith("amount") and value:
|
||||
kwargs[key] = str(int(100 * float(value)))
|
||||
elif value:
|
||||
kwargs[key] = value
|
||||
|
||||
# For each product, we save it
|
||||
formset = ProductFormSet(kwargs, instance=form.instance)
|
||||
if formset.is_valid():
|
||||
for f in formset:
|
||||
# We don't save the product if the designation is not entered, ie. if the line is empty
|
||||
if f.is_valid() and f.instance.designation:
|
||||
f.save()
|
||||
f.instance.save()
|
||||
else:
|
||||
f.instance = None
|
||||
|
||||
return ret
|
||||
|
||||
def get_success_url(self):
|
||||
return reverse_lazy('treasury:invoice_list')
|
||||
|
||||
|
||||
class InvoiceListView(LoginRequiredMixin, SingleTableView):
|
||||
"""
|
||||
List existing Invoices
|
||||
"""
|
||||
model = Invoice
|
||||
table_class = InvoiceTable
|
||||
|
||||
|
||||
class InvoiceUpdateView(LoginRequiredMixin, UpdateView):
|
||||
"""
|
||||
Create Invoice
|
||||
"""
|
||||
model = Invoice
|
||||
form_class = InvoiceForm
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
context = super().get_context_data(**kwargs)
|
||||
|
||||
form = context['form']
|
||||
form.helper = FormHelper()
|
||||
# Remove form tag on the generation of the form in the template (already present on the template)
|
||||
form.helper.form_tag = False
|
||||
# Fill the intial value for the date field, with the initial date of the model instance
|
||||
form.fields['date'].initial = form.instance.date
|
||||
# The formset handles the set of the products
|
||||
form_set = ProductFormSet(instance=form.instance)
|
||||
context['formset'] = form_set
|
||||
context['helper'] = ProductFormSetHelper()
|
||||
context['no_cache'] = True
|
||||
|
||||
return context
|
||||
|
||||
def form_valid(self, form):
|
||||
ret = super().form_valid(form)
|
||||
|
||||
kwargs = {}
|
||||
# The user type amounts in cents. We convert it in euros.
|
||||
for key in self.request.POST:
|
||||
value = self.request.POST[key]
|
||||
if key.endswith("amount") and value:
|
||||
kwargs[key] = str(int(100 * float(value)))
|
||||
elif value:
|
||||
kwargs[key] = value
|
||||
|
||||
formset = ProductFormSet(kwargs, instance=form.instance)
|
||||
saved = []
|
||||
# For each product, we save it
|
||||
if formset.is_valid():
|
||||
for f in formset:
|
||||
# We don't save the product if the designation is not entered, ie. if the line is empty
|
||||
if f.is_valid() and f.instance.designation:
|
||||
f.save()
|
||||
f.instance.save()
|
||||
saved.append(f.instance.pk)
|
||||
else:
|
||||
f.instance = None
|
||||
# Remove old products that weren't given in the form
|
||||
Product.objects.filter(~Q(pk__in=saved), invoice=form.instance).delete()
|
||||
|
||||
return ret
|
||||
|
||||
def get_success_url(self):
|
||||
return reverse_lazy('treasury:invoice_list')
|
||||
|
||||
|
||||
class InvoiceRenderView(LoginRequiredMixin, View):
|
||||
"""
|
||||
Render Invoice as a generated PDF with the given information and a LaTeX template
|
||||
"""
|
||||
|
||||
def get(self, request, **kwargs):
|
||||
pk = kwargs["pk"]
|
||||
invoice = Invoice.objects.get(pk=pk)
|
||||
products = Product.objects.filter(invoice=invoice).all()
|
||||
|
||||
# Informations of the BDE. Should be updated when the school will move.
|
||||
invoice.place = "Cachan"
|
||||
invoice.my_name = "BDE ENS Cachan"
|
||||
invoice.my_address_street = "61 avenue du Président Wilson"
|
||||
invoice.my_city = "94230 Cachan"
|
||||
invoice.bank_code = 30003
|
||||
invoice.desk_code = 3894
|
||||
invoice.account_number = 37280662
|
||||
invoice.rib_key = 14
|
||||
invoice.bic = "SOGEFRPP"
|
||||
|
||||
# Replace line breaks with the LaTeX equivalent
|
||||
invoice.description = invoice.description.replace("\r", "").replace("\n", "\\\\ ")
|
||||
invoice.address = invoice.address.replace("\r", "").replace("\n", "\\\\ ")
|
||||
# Fill the template with the information
|
||||
tex = render_to_string("treasury/invoice_sample.tex", dict(obj=invoice, products=products))
|
||||
|
||||
try:
|
||||
os.mkdir(BASE_DIR + "/tmp")
|
||||
except FileExistsError:
|
||||
pass
|
||||
# We render the file in a temporary directory
|
||||
tmp_dir = mkdtemp(prefix=BASE_DIR + "/tmp/")
|
||||
|
||||
try:
|
||||
with open("{}/invoice-{:d}.tex".format(tmp_dir, pk), "wb") as f:
|
||||
f.write(tex.encode("UTF-8"))
|
||||
del tex
|
||||
|
||||
# The file has to be rendered twice
|
||||
for _ in range(2):
|
||||
error = subprocess.Popen(
|
||||
["pdflatex", "invoice-{}.tex".format(pk)],
|
||||
cwd=tmp_dir,
|
||||
stdin=open(os.devnull, "r"),
|
||||
stderr=open(os.devnull, "wb"),
|
||||
stdout=open(os.devnull, "wb"),
|
||||
).wait()
|
||||
|
||||
if error:
|
||||
raise IOError("An error attempted while generating a invoice (code=" + str(error) + ")")
|
||||
|
||||
# Display the generated pdf as a HTTP Response
|
||||
pdf = open("{}/invoice-{}.pdf".format(tmp_dir, pk), 'rb').read()
|
||||
response = HttpResponse(pdf, content_type="application/pdf")
|
||||
response['Content-Disposition'] = "inline;filename=invoice-{:d}.pdf".format(pk)
|
||||
except IOError as e:
|
||||
raise e
|
||||
finally:
|
||||
# Delete all temporary files
|
||||
shutil.rmtree(tmp_dir)
|
||||
|
||||
return response
|
||||
|
||||
|
||||
class RemittanceCreateView(LoginRequiredMixin, CreateView):
|
||||
"""
|
||||
Create Remittance
|
||||
"""
|
||||
model = Remittance
|
||||
form_class = RemittanceForm
|
||||
|
||||
def get_success_url(self):
|
||||
return reverse_lazy('treasury:remittance_list')
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
ctx = super().get_context_data(**kwargs)
|
||||
|
||||
ctx["table"] = RemittanceTable(data=Remittance.objects.all())
|
||||
ctx["special_transactions"] = SpecialTransactionTable(data=SpecialTransaction.objects.none())
|
||||
|
||||
return ctx
|
||||
|
||||
|
||||
class RemittanceListView(LoginRequiredMixin, TemplateView):
|
||||
"""
|
||||
List existing Remittances
|
||||
"""
|
||||
template_name = "treasury/remittance_list.html"
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
ctx = super().get_context_data(**kwargs)
|
||||
|
||||
ctx["opened_remittances"] = RemittanceTable(data=Remittance.objects.filter(closed=False).all())
|
||||
ctx["closed_remittances"] = RemittanceTable(data=Remittance.objects.filter(closed=True).reverse().all())
|
||||
|
||||
ctx["special_transactions_no_remittance"] = SpecialTransactionTable(
|
||||
data=SpecialTransaction.objects.filter(source__in=NoteSpecial.objects.filter(~Q(remittancetype=None)),
|
||||
specialtransactionproxy__remittance=None).all(),
|
||||
exclude=('remittance_remove', ))
|
||||
ctx["special_transactions_with_remittance"] = SpecialTransactionTable(
|
||||
data=SpecialTransaction.objects.filter(source__in=NoteSpecial.objects.filter(~Q(remittancetype=None)),
|
||||
specialtransactionproxy__remittance__closed=False).all(),
|
||||
exclude=('remittance_add', ))
|
||||
|
||||
return ctx
|
||||
|
||||
|
||||
class RemittanceUpdateView(LoginRequiredMixin, UpdateView):
|
||||
"""
|
||||
Update Remittance
|
||||
"""
|
||||
model = Remittance
|
||||
form_class = RemittanceForm
|
||||
|
||||
def get_success_url(self):
|
||||
return reverse_lazy('treasury:remittance_list')
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
ctx = super().get_context_data(**kwargs)
|
||||
|
||||
ctx["table"] = RemittanceTable(data=Remittance.objects.all())
|
||||
data = SpecialTransaction.objects.filter(specialtransactionproxy__remittance=self.object).all()
|
||||
ctx["special_transactions"] = SpecialTransactionTable(
|
||||
data=data,
|
||||
exclude=('remittance_add', 'remittance_remove', ) if self.object.closed else ('remittance_add', ))
|
||||
|
||||
return ctx
|
||||
|
||||
|
||||
class LinkTransactionToRemittanceView(LoginRequiredMixin, UpdateView):
|
||||
"""
|
||||
Attach a special transaction to a remittance
|
||||
"""
|
||||
|
||||
model = SpecialTransactionProxy
|
||||
form_class = LinkTransactionToRemittanceForm
|
||||
|
||||
def get_success_url(self):
|
||||
return reverse_lazy('treasury:remittance_list')
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
ctx = super().get_context_data(**kwargs)
|
||||
|
||||
form = ctx["form"]
|
||||
form.fields["last_name"].initial = self.object.transaction.last_name
|
||||
form.fields["first_name"].initial = self.object.transaction.first_name
|
||||
form.fields["bank"].initial = self.object.transaction.bank
|
||||
form.fields["amount"].initial = self.object.transaction.amount
|
||||
form.fields["remittance"].queryset = form.fields["remittance"] \
|
||||
.queryset.filter(remittance_type__note=self.object.transaction.source)
|
||||
|
||||
return ctx
|
||||
|
||||
|
||||
class UnlinkTransactionToRemittanceView(LoginRequiredMixin, View):
|
||||
"""
|
||||
Unlink a special transaction and its remittance
|
||||
"""
|
||||
|
||||
def get(self, *args, **kwargs):
|
||||
pk = kwargs["pk"]
|
||||
transaction = SpecialTransactionProxy.objects.get(pk=pk)
|
||||
|
||||
# The remittance must be open (or inexistant)
|
||||
if transaction.remittance and transaction.remittance.closed:
|
||||
raise ValidationError("Remittance is already closed.")
|
||||
|
||||
transaction.remittance = None
|
||||
transaction.save()
|
||||
|
||||
return redirect('treasury:remittance_list')
|
Reference in New Issue
Block a user