Replaced the function-based txn_form and txn_store view with the class-based TransactionFormView in the accounting application.
This commit is contained in:
parent
60b3dfa33c
commit
0d38cdd403
@ -152,6 +152,8 @@ class Transaction(DirtyFieldsMixin, models.Model):
|
||||
self._records = None
|
||||
self._is_balanced = None
|
||||
self._has_order_hole = None
|
||||
self.old_date = None
|
||||
self.current_user = None
|
||||
|
||||
def __str__(self):
|
||||
"""Returns the string representation of this accounting
|
||||
@ -190,17 +192,16 @@ class Transaction(DirtyFieldsMixin, models.Model):
|
||||
return True
|
||||
return False
|
||||
|
||||
def save(self, current_user=None, old_date: datetime.date = None,
|
||||
force_insert=False, force_update=False, using=None,
|
||||
def save(self, force_insert=False, force_update=False, using=None,
|
||||
update_fields=None):
|
||||
# When the date is changed, the orders of the transactions in the same
|
||||
# day need to be reordered
|
||||
txn_to_sort = []
|
||||
if self.date != old_date:
|
||||
if old_date is not None:
|
||||
if self.date != self.old_date:
|
||||
if self.old_date is not None:
|
||||
txn_same_day = list(
|
||||
Transaction.objects
|
||||
.filter(Q(date=old_date), ~Q(pk=self.pk))
|
||||
.filter(Q(date=self.old_date), ~Q(pk=self.pk))
|
||||
.order_by("ord"))
|
||||
for i in range(len(txn_same_day)):
|
||||
if txn_same_day[i].ord != i + 1:
|
||||
@ -215,18 +216,18 @@ class Transaction(DirtyFieldsMixin, models.Model):
|
||||
# Applies the created by and updated by
|
||||
if self.pk is None:
|
||||
self.pk = new_pk(Transaction)
|
||||
if current_user is not None:
|
||||
self.created_by = current_user
|
||||
if current_user is not None:
|
||||
self.updated_by = current_user
|
||||
if self.current_user is not None:
|
||||
self.created_by = self.current_user
|
||||
if self.current_user is not None:
|
||||
self.updated_by = self.current_user
|
||||
to_save = [x for x in self.records if x.is_dirty()]
|
||||
for record in to_save:
|
||||
if record.pk is None:
|
||||
record.pk = new_pk(Record)
|
||||
if current_user is not None:
|
||||
record.created_by = current_user
|
||||
if current_user is not None:
|
||||
record.updated_by = current_user
|
||||
if self.current_user is not None:
|
||||
record.created_by = self.current_user
|
||||
if self.current_user is not None:
|
||||
record.updated_by = self.current_user
|
||||
# Runs the update
|
||||
with transaction.atomic():
|
||||
super().save(force_insert=force_insert, force_update=force_update,
|
||||
|
@ -72,7 +72,7 @@ First written: 2020/7/23
|
||||
<i class="fas fa-chevron-circle-left"></i>
|
||||
{{ _("Back")|force_escape }}
|
||||
</a>
|
||||
<a class="btn btn-primary" role="button" href="{% url "accounting:transactions.edit" "expense" txn as url %}{% url_keep_return url %}">
|
||||
<a class="btn btn-primary" role="button" href="{% url "accounting:transactions.update" "expense" txn as url %}{% url_keep_return url %}">
|
||||
<i class="fas fa-edit"></i>
|
||||
{{ _("Edit")|force_escape }}
|
||||
</a>
|
||||
|
@ -72,7 +72,7 @@ First written: 2020/7/23
|
||||
<i class="fas fa-chevron-circle-left"></i>
|
||||
{{ _("Back")|force_escape }}
|
||||
</a>
|
||||
<a class="btn btn-primary" role="button" href="{% url "accounting:transactions.edit" "income" txn as url %}{% url_keep_return url %}">
|
||||
<a class="btn btn-primary" role="button" href="{% url "accounting:transactions.update" "income" txn as url %}{% url_keep_return url %}">
|
||||
<i class="fas fa-edit"></i>
|
||||
{{ _("Edit")|force_escape }}
|
||||
</a>
|
||||
|
@ -72,7 +72,7 @@ First written: 2020/7/23
|
||||
<i class="fas fa-chevron-circle-left"></i>
|
||||
{{ _("Back")|force_escape }}
|
||||
</a>
|
||||
<a class="btn btn-primary" role="button" href="{% url "accounting:transactions.edit" "transfer" txn as url %}{% url_keep_return url %}">
|
||||
<a class="btn btn-primary" role="button" href="{% url "accounting:transactions.update" "transfer" txn as url %}{% url_keep_return url %}">
|
||||
<i class="fas fa-edit"></i>
|
||||
{{ _("Edit")|force_escape }}
|
||||
</a>
|
||||
|
@ -56,7 +56,7 @@ First written: 2020/7/23
|
||||
<input id="account-option-url" type="hidden" value="{% url "accounting:api.accounts.options" %}" />
|
||||
<input id="summary-categories" type="hidden" value="{{ summary_categories }}" />
|
||||
<input id="new-record-template" type="hidden" value="{{ new_record_template }}" />
|
||||
<form id="txn-form" action="{% if form.transaction %}{% url "accounting:transactions.update" "expense" form.transaction as url %}{% url_keep_return url %}{% else %}{% url "accounting:transactions.store" "expense" as url %}{% url_keep_return url %}{% endif %}" method="post">
|
||||
<form id="txn-form" action="{% if form.transaction %}{% url "accounting:transactions.update" "expense" form.transaction as url %}{% url_keep_return url %}{% else %}{% url "accounting:transactions.create" "expense" as url %}{% url_keep_return url %}{% endif %}" method="post">
|
||||
{% csrf_token %}
|
||||
<div class="row form-group">
|
||||
<div class="col-sm-2">
|
||||
|
@ -56,7 +56,7 @@ First written: 2020/7/23
|
||||
<input id="account-option-url" type="hidden" value="{% url "accounting:api.accounts.options" %}" />
|
||||
<input id="summary-categories" type="hidden" value="{{ summary_categories }}" />
|
||||
<input id="new-record-template" type="hidden" value="{{ new_record_template }}" />
|
||||
<form id="txn-form" action="{% if form.transaction %}{% url "accounting:transactions.update" "income" form.transaction as url %}{% url_keep_return url %}{% else %}{% url "accounting:transactions.store" "income" as url %}{% url_keep_return url %}{% endif %}" method="post">
|
||||
<form id="txn-form" action="{% if form.transaction %}{% url "accounting:transactions.update" "income" form.transaction as url %}{% url_keep_return url %}{% else %}{% url "accounting:transactions.create" "income" as url %}{% url_keep_return url %}{% endif %}" method="post">
|
||||
{% csrf_token %}
|
||||
<div class="row form-group">
|
||||
<div class="col-sm-2">
|
||||
|
@ -56,7 +56,7 @@ First written: 2020/7/23
|
||||
<input id="account-option-url" type="hidden" value="{% url "accounting:api.accounts.options" %}" />
|
||||
<input id="summary-categories" type="hidden" value="{{ summary_categories }}" />
|
||||
<input id="new-record-template" type="hidden" value="{{ new_record_template }}" />
|
||||
<form id="txn-form" action="{% if form.transaction %}{% url "accounting:transactions.update" "transfer" form.transaction as url %}{% url_keep_return url %}{% else %}{% url "accounting:transactions.store" "transfer" as url %}{% url_keep_return url %}{% endif %}" method="post">
|
||||
<form id="txn-form" action="{% if form.transaction %}{% url "accounting:transactions.update" "transfer" form.transaction as url %}{% url_keep_return url %}{% else %}{% url "accounting:transactions.create" "transfer" as url %}{% url_keep_return url %}{% endif %}" method="post">
|
||||
{% csrf_token %}
|
||||
<div class="row form-group">
|
||||
<div class="col-sm-2">
|
||||
|
@ -76,15 +76,11 @@ urlpatterns = [
|
||||
path("search",
|
||||
views.search, name="search"),
|
||||
path("transactions/<txn-type:txn_type>/create",
|
||||
views.txn_form, name="transactions.create"),
|
||||
path("transactions/<txn-type:txn_type>/store",
|
||||
views.txn_store, name="transactions.store"),
|
||||
views.TransactionFormView.as_view(), name="transactions.create"),
|
||||
path("transactions/<txn-type:txn_type>/<txn:txn>",
|
||||
views.TransactionView.as_view(), name="transactions.detail"),
|
||||
path("transactions/<txn-type:txn_type>/<txn:txn>/edit",
|
||||
views.txn_form, name="transactions.edit"),
|
||||
path("transactions/<txn-type:txn_type>/<txn:txn>/update",
|
||||
views.txn_store, name="transactions.update"),
|
||||
views.TransactionFormView.as_view(), name="transactions.update"),
|
||||
path("transactions/<txn:txn>/delete",
|
||||
views.TransactionDeleteView.as_view(), name="transactions.delete"),
|
||||
path("transactions/sort/<date:date>",
|
||||
|
@ -812,84 +812,60 @@ class TransactionView(DetailView):
|
||||
% (self.kwargs["txn_type"],)]
|
||||
|
||||
|
||||
@require_GET
|
||||
@login_required
|
||||
def txn_form(request: HttpRequest, txn_type: str,
|
||||
txn: Transaction = None) -> HttpResponse:
|
||||
"""The view to edit an accounting transaction.
|
||||
@method_decorator(login_required, name="dispatch")
|
||||
class TransactionFormView(FormView):
|
||||
"""The form to create or update an accounting transaction."""
|
||||
model = Transaction
|
||||
form_class = TransactionForm
|
||||
not_modified_message = gettext_noop("This transaction was not modified.")
|
||||
success_message = gettext_noop("This transaction was saved successfully.")
|
||||
|
||||
Args:
|
||||
request: The request.
|
||||
txn_type: The transaction type.
|
||||
txn: The transaction.
|
||||
def get_context_data(self, **kwargs):
|
||||
"""Returns the context data for the template."""
|
||||
context = super().get_context_data(**kwargs)
|
||||
context["summary_categories"] = utils.get_summary_categories()
|
||||
context["new_record_template"] = self._get_new_record_template_json()
|
||||
return context
|
||||
|
||||
Returns:
|
||||
The response.
|
||||
"""
|
||||
previous_post = stored_post.get_previous_post(request)
|
||||
if previous_post is not None:
|
||||
form = TransactionForm(previous_post)
|
||||
elif txn is not None:
|
||||
form = utils.make_txn_form_from_model(txn_type, txn)
|
||||
else:
|
||||
form = TransactionForm()
|
||||
form.transaction = txn
|
||||
form.txn_type = txn_type
|
||||
new_record_context = {"record_type": "TTT", "no": "NNN"}
|
||||
if txn_type == "transfer":
|
||||
new_record_template = json.dumps(render_to_string(
|
||||
"accounting/include/record_form-transfer.html",
|
||||
new_record_context))
|
||||
else:
|
||||
new_record_template = json.dumps(render_to_string(
|
||||
"accounting/include/record_form-non-transfer.html",
|
||||
new_record_context))
|
||||
return render(request, F"accounting/transaction_form-{txn_type}.html", {
|
||||
"form": form,
|
||||
"summary_categories": utils.get_summary_categories,
|
||||
"new_record_template": new_record_template,
|
||||
})
|
||||
def get_template_name(self) -> str:
|
||||
"""Returns the name of the template."""
|
||||
namespace = self.request.resolver_match.namespace
|
||||
model_name = self.model.__name__.lower()
|
||||
return F"{namespace}/{model_name}_form-{self.txn_type}.html"
|
||||
|
||||
def _get_new_record_template_json(self) -> str:
|
||||
context = {"record_type": "TTT", "no": "NNN"}
|
||||
template_name = "accounting/include/record_form-transfer.html"\
|
||||
if self.txn_type == "transfer"\
|
||||
else "accounting/include/record_form-non-transfer.html"
|
||||
return json.dumps(render_to_string(template_name, context))
|
||||
|
||||
@require_POST
|
||||
@login_required
|
||||
def txn_store(request: HttpRequest, txn_type: str,
|
||||
txn: Transaction = None) -> HttpResponseRedirect:
|
||||
"""The view to store an accounting transaction.
|
||||
def make_form_from_post(self, post: Dict[str, str]) -> TransactionForm:
|
||||
"""Creates and returns the form from the POST data."""
|
||||
form = TransactionForm(post)
|
||||
form.txn_type = self.txn_type
|
||||
form.transaction = self.get_object()
|
||||
return form
|
||||
|
||||
Args:
|
||||
request: The request.
|
||||
txn_type: The transaction type.
|
||||
txn: The transaction.
|
||||
def make_form_from_model(self, obj: Transaction) -> TransactionForm:
|
||||
"""Creates and returns the form from a data model."""
|
||||
return utils.make_txn_form_from_model(self.txn_type, obj)
|
||||
|
||||
Returns:
|
||||
The response.
|
||||
"""
|
||||
post = request.POST.dict()
|
||||
strip_post(post)
|
||||
utils.sort_post_txn_records(post)
|
||||
form = TransactionForm(post)
|
||||
form.transaction = txn
|
||||
form.txn_type = txn_type
|
||||
if not form.is_valid():
|
||||
if txn is None:
|
||||
url = reverse("accounting:transactions.create", args=(txn_type,))
|
||||
else:
|
||||
url = reverse("accounting:transactions.edit", args=(txn_type, txn))
|
||||
url = str(UrlBuilder(url).query(r=request.GET.get("r")))
|
||||
return stored_post.error_redirect(request, url, post)
|
||||
if txn is None:
|
||||
txn = Transaction()
|
||||
old_date = txn.date
|
||||
utils.fill_txn_from_post(txn_type, txn, post)
|
||||
if not txn.is_dirty(check_relationship=True):
|
||||
message = gettext_noop("This transaction was not modified.")
|
||||
else:
|
||||
txn.save(current_user=request.user, old_date=old_date)
|
||||
message = gettext_noop("This transaction was saved successfully.")
|
||||
messages.success(request, message)
|
||||
url = reverse("accounting:transactions.detail", args=(txn_type, txn))
|
||||
return redirect(str(UrlBuilder(url).query(r=request.GET.get("r"))))
|
||||
def fill_model_from_form(self, obj: Transaction,
|
||||
form: TransactionForm) -> None:
|
||||
"""Fills in the data model from the form."""
|
||||
obj.old_date = obj.date
|
||||
utils.fill_txn_from_post(self.txn_type, obj, form.data)
|
||||
obj.current_user = self.request.user
|
||||
|
||||
def get_object(self) -> Optional[Account]:
|
||||
"""Returns the current object, or None on a create form."""
|
||||
return self.kwargs.get("txn")
|
||||
|
||||
@property
|
||||
def txn_type(self) -> str:
|
||||
"""Returns the transaction type of this form"""
|
||||
return self.kwargs["txn_type"]
|
||||
|
||||
|
||||
@method_decorator(require_POST, name="dispatch")
|
||||
|
Loading…
Reference in New Issue
Block a user