mirror of https://gitlab.crans.org/bde/nk20
Fix money creation, closes #25
This commit is contained in:
parent
94c0971abf
commit
3764bc44ff
|
@ -3,6 +3,7 @@
|
||||||
|
|
||||||
from dal import autocomplete
|
from dal import autocomplete
|
||||||
from django import forms
|
from django import forms
|
||||||
|
from django.utils.translation import gettext_lazy as _
|
||||||
|
|
||||||
from .models import Transaction, TransactionTemplate, TemplateTransaction
|
from .models import Transaction, TransactionTemplate, TemplateTransaction
|
||||||
|
|
||||||
|
@ -33,6 +34,23 @@ class TransactionForm(forms.ModelForm):
|
||||||
def save(self, commit=True):
|
def save(self, commit=True):
|
||||||
super().save(commit)
|
super().save(commit)
|
||||||
|
|
||||||
|
|
||||||
|
def clean(self):
|
||||||
|
"""
|
||||||
|
If the user has no right to transfer funds, then it will be the source of the transfer by default.
|
||||||
|
Transactions between a note and the same note are not authorized.
|
||||||
|
"""
|
||||||
|
|
||||||
|
cleaned_data = super().clean()
|
||||||
|
if not "source" in cleaned_data: # TODO Replace it with "if %user has no right to transfer funds"
|
||||||
|
cleaned_data["source"] = self.user.note
|
||||||
|
|
||||||
|
if cleaned_data["source"].pk == cleaned_data["destination"].pk:
|
||||||
|
self.add_error("destination", _("Source and destination must be different."))
|
||||||
|
|
||||||
|
return cleaned_data
|
||||||
|
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = Transaction
|
model = Transaction
|
||||||
fields = (
|
fields = (
|
||||||
|
|
|
@ -84,8 +84,6 @@ class Transaction(PolymorphicModel):
|
||||||
|
|
||||||
amount is store in centimes of currency, making it a positive integer
|
amount is store in centimes of currency, making it a positive integer
|
||||||
value. (from someone to someone else)
|
value. (from someone to someone else)
|
||||||
|
|
||||||
TODO: Ensure source != destination.
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
source = models.ForeignKey(
|
source = models.ForeignKey(
|
||||||
|
@ -126,6 +124,11 @@ class Transaction(PolymorphicModel):
|
||||||
"""
|
"""
|
||||||
When saving, also transfer money between two notes
|
When saving, also transfer money between two notes
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
if self.source.pk == self.destination.pk:
|
||||||
|
# When source == destination, no money is transfered
|
||||||
|
return
|
||||||
|
|
||||||
created = self.pk is None
|
created = self.pk is None
|
||||||
to_transfer = self.amount * self.quantity
|
to_transfer = self.amount * self.quantity
|
||||||
if not created:
|
if not created:
|
||||||
|
|
|
@ -41,17 +41,12 @@ class TransactionCreate(LoginRequiredMixin, CreateView):
|
||||||
|
|
||||||
if False: # TODO: fix it with "if %user has no right to transfer funds"
|
if False: # TODO: fix it with "if %user has no right to transfer funds"
|
||||||
del form.fields['source']
|
del form.fields['source']
|
||||||
|
form.user = self.request.user
|
||||||
|
|
||||||
return form
|
return form
|
||||||
|
|
||||||
def form_valid(self, form):
|
def get_success_url(self):
|
||||||
"""
|
return reverse('note:transfer')
|
||||||
If the user has no right to transfer funds, then it will be the source of the transfer by default.
|
|
||||||
"""
|
|
||||||
if False: # TODO: fix it with "if %user has no right to transfer funds"
|
|
||||||
form.instance.source = self.request.user.note
|
|
||||||
|
|
||||||
return super().form_valid(form)
|
|
||||||
|
|
||||||
|
|
||||||
class NoteAutocomplete(autocomplete.Select2QuerySetView):
|
class NoteAutocomplete(autocomplete.Select2QuerySetView):
|
||||||
|
|
Loading…
Reference in New Issue