Fix operation create view

This commit is contained in:
Skia 2016-10-05 20:17:37 +02:00
parent 63217b2ead
commit 0acc97fa90
3 changed files with 14 additions and 7 deletions

View File

@ -13,12 +13,14 @@
</p>
<hr>
<h2>{% trans %}General journal:{% endtrans %} {{ object.name }}</h2>
<p><a href="{{ url('accounting:label_new') }}?parent={{ object.club_account.id }}">{% trans %}New label{% endtrans %}</a></p>
<p><a href="{{ url('accounting:label_list', clubaccount_id=object.club_account.id) }}">{% trans %}Label list{% endtrans %}</a></p>
<p><strong>{% trans %}Amount: {% endtrans %}</strong>{{ object.amount }} € -
<strong>{% trans %}Effective amount: {% endtrans %}</strong>{{ object.effective_amount }} €</p>
{% if object.closed %}
<p>{% trans %}Journal is closed, you can not create operation{% endtrans %}</p>
{% else %}
<p><a href="{{ url('accounting:op_new') }}?parent={{ object.id }}">{% trans %}New operation{% endtrans %}</a></p>
<p><a href="{{ url('accounting:op_new', j_id=object.id) }}">{% trans %}New operation{% endtrans %}</a></p>
{% endif %}
<table>
<thead>

View File

@ -27,7 +27,7 @@ urlpatterns = [
url(r'^journal/(?P<j_id>[0-9]+)$', JournalDetailView.as_view(), name='journal_details'),
url(r'^journal/(?P<j_id>[0-9]+)/edit$', JournalEditView.as_view(), name='journal_edit'),
# Operations
url(r'^operation/create$', OperationCreateView.as_view(), name='op_new'),
url(r'^operation/create/(?P<j_id>[0-9]+)$', OperationCreateView.as_view(), name='op_new'),
url(r'^operation/(?P<op_id>[0-9]+)$', OperationEditView.as_view(), name='op_edit'),
# Companies
url(r'^company/create$', CompanyCreateView.as_view(), name='co_new'),

View File

@ -209,8 +209,10 @@ class OperationForm(forms.ModelForm):
company = AutoCompleteSelectField('companies', help_text=None, required=False)
def __init__(self, *args, **kwargs):
club_account = kwargs.pop('club_account', None)
super(OperationForm, self).__init__(*args, **kwargs)
self.fields['label'].queryset = self.instance.journal.club_account.labels.order_by('name').all()
if club_account:
self.fields['label'].queryset = club_account.labels.order_by('name').all()
if self.instance.target_type == "USER":
self.fields['user'].initial = self.instance.target_id
elif self.instance.target_type == "ACCOUNT":
@ -268,10 +270,13 @@ class OperationCreateView(CanCreateMixin, CreateView):
form_class = OperationForm
template_name = 'accounting/operation_edit.jinja'
def get_form(self, form_class=None):
self.journal = GeneralJournal.objects.filter(id=self.kwargs['j_id']).first()
ca = self.journal.club_account if self.journal else None
return self.form_class(club_account=ca, **self.get_form_kwargs())
def get_initial(self):
ret = super(OperationCreateView, self).get_initial()
if 'parent' in self.request.GET.keys():
self.journal = GeneralJournal.objects.filter(id=int(self.request.GET['parent'])).first()
if self.journal is not None:
ret['journal'] = self.journal.id
return ret