def render(self, context): objects = Variable(self.variable).resolve(context) objects = ufs_sorted(list(objects)) context[self.variable] = objects return ''
def new_edit_transaction(request, group, transaction=None, is_admin=False): """Admin view for creating transactions""" if transaction is None: transaction = Transaction(group=group) elif not transaction.is_editable(): messages.error( request, _('Transaction %d can\'t be changed.' % transaction.id)) db_transaction.commit() url = reverse('group-summary', args=(group.slug,)) return HttpResponseRedirect(url) if request.method == 'POST': data = request.POST elif transaction.id: data = {} # Load "fake" post data if we are editing a transaction for e in transaction.entry_set.all(): if e.debit > 0: data['%d-debit' % e.account.id] = e.debit if e.credit > 0: data['%d-credit' % e.account.id] = e.credit data['settlement-settlement'] = transaction.settlement_id else: data = None # Init forms settlement_form = TransactionSettlementForm( data, prefix='settlement', instance=transaction) user_forms = [] group_forms = [] for account in ufs_sorted(group.user_account_set.filter(active=True)): user_forms.append((account, EntryForm(data, prefix=account.id))) for account in ufs_sorted(group.group_account_set.filter(active=True)): group_forms.append((account, EntryForm(data, prefix=account.id))) errors = [] if request.method == 'POST' and settlement_form.is_valid(): entries = {} if transaction.id is None: transaction.save() else: for e in transaction.entry_set.all(): entries[e.account.id] = e transaction.settlement = settlement_form.cleaned_data['settlement'] try: for forms in [group_forms, user_forms]: for account, form in forms: if not form.is_valid(): raise InvalidTransaction( 'Form was not valid, id: %d' % account.id) else: credit = form.cleaned_data['credit'] debit = form.cleaned_data['debit'] if account.id in entries: entry = entries[account.id] else: entry = TransactionEntry( account=account, transaction=transaction) if credit > 0 or debit > 0: entry.credit = credit or 0 entry.debit = debit or 0 entry.save() elif entry.id: entry.delete() details = settlement_form.cleaned_data['details'] transaction.save() transaction.set_pending(user=request.user, message=details) messages.success(request, _('Your transaction has been added.')) except InvalidTransaction, e: errors.append(e) db_transaction.rollback() else: db_transaction.commit() url = reverse('group-summary', args=(group.slug,)) return HttpResponseRedirect(url)
def new_edit_transaction(request, group, transaction=None, is_admin=False): """Admin view for creating transactions""" savepoint_id = db_transaction.savepoint() if transaction is None: transaction = Transaction(group=group) elif not transaction.is_editable(): messages.error(request, _("Transaction %d can't be changed." % transaction.id)) db_transaction.savepoint_rollback(savepoint_id) url = reverse("group-summary", args=(group.slug, )) return HttpResponseRedirect(url) if request.method == "POST": data = request.POST elif transaction.id: data = {} # Load "fake" post data if we are editing a transaction for e in transaction.entry_set.all(): if e.debit > 0: data["%d-debit" % e.account.id] = e.debit if e.credit > 0: data["%d-credit" % e.account.id] = e.credit data["settlement-settlement"] = transaction.settlement_id else: data = None # Init forms settlement_form = TransactionSettlementForm(data, prefix="settlement", instance=transaction) user_forms = [] group_forms = [] for account in ufs_sorted(group.user_account_set.filter(active=True)): user_forms.append((account, EntryForm(data, prefix=account.id))) for account in ufs_sorted(group.group_account_set.filter(active=True)): group_forms.append((account, EntryForm(data, prefix=account.id))) errors = [] if request.method == "POST" and settlement_form.is_valid(): entries = {} if transaction.id is None: transaction.save() else: for e in transaction.entry_set.all(): entries[e.account.id] = e transaction.settlement = settlement_form.cleaned_data["settlement"] try: for forms in [group_forms, user_forms]: for account, form in forms: if not form.is_valid(): raise InvalidTransaction("Form was not valid, id: %d" % account.id) else: credit = form.cleaned_data["credit"] or 0 debit = form.cleaned_data["debit"] or 0 if account.id in entries: entry = entries[account.id] else: entry = TransactionEntry(account=account, transaction=transaction) if credit > 0 or debit > 0: entry.credit = credit or 0 entry.debit = debit or 0 entry.save() elif entry.id: entry.delete() details = settlement_form.cleaned_data["details"] transaction.save() transaction.set_pending(user=request.user, message=details) messages.success(request, _("Your transaction has been added.")) except InvalidTransaction as e: errors.append(e) db_transaction.savepoint_rollback(savepoint_id) else: db_transaction.savepoint_commit(savepoint_id) url = reverse("group-summary", args=(group.slug, )) return HttpResponseRedirect(url) db_transaction.savepoint_rollback(savepoint_id) return render( request, "accounting/transaction_form.html", { "is_admin": is_admin, "group": group, "settlement_form": settlement_form, "group_forms": group_forms, "user_forms": user_forms, "errors": errors, "transaction": transaction, }, )