Шаблон form action method post formset management_form div id table-st

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
###################################################Шаблон
<form action="" method="post">
{{ formset.management_form }}
<div id="table-style">
<table id="content-table" colspan="0" cellspacing="0">
<tr>
<th>&nbsp;</th>
{% for item in period %}
<th>{{ item.Period }} {{ item.Period_Year}} года</th>
{% endfor %}
</tr>
<tr>
<th>Выручка</th>
<td align="center">{{ formset.forms.0.receipt }}<br>{% for error in formset.forms.0.receipt.errors %}{{ error }}{% endfor %}</td>
<td align="center">{{ formset.forms.1.receipt }}<br>{% for error in formset.forms.1.receipt.errors %}{{ error }}{% endfor %}</td>
<td align="center">{{ formset.forms.2.receipt }}<br>{% for error in formset.forms.2.receipt.errors %}{{ error }}{% endfor %}</td>
<td align="center">{{ formset.forms.3.receipt }}<br>{% for error in formset.forms.3.receipt.errors %}{{ error }}{% endfor %}</td>
</tr>
<tr>
<th>Активы</th>
<td align="center">{{ formset.forms.0.asset }}<br>{% for error in formset.forms.0.asset.errors %}{{ error }}{% endfor %}</td>
<td align="center">{{ formset.forms.1.asset }}<br>{% for error in formset.forms.1.asset.errors %}{{ error }}{% endfor %}</td>
<td align="center">{{ formset.forms.2.asset }}<br>{% for error in formset.forms.2.asset.errors %}{{ error }}{% endfor %}</td>
<td align="center">{{ formset.forms.3.asset }}<br>{% for error in formset.forms.3.asset.errors %}{{ error }}{% endfor %}</td>
</tr>
<tr>
<th>Капитал резерва</th>
<td align="center">{{ formset.forms.0.capital_reserve }}<br>{% for error in formset.forms.0.capital_reserve.errors %}{{ error }}{% endfor %}</td>
<td align="center">{{ formset.forms.1.capital_reserve }}<br>{% for error in formset.forms.1.capital_reserve.errors %}{{ error }}{% endfor %}</td>
<td align="center">{{ formset.forms.2.capital_reserve }}<br>{% for error in formset.forms.2.capital_reserve.errors %}{{ error }}{% endfor %}</td>
<td align="center">{{ formset.forms.3.capital_reserve }}<br>{% for error in formset.forms.3.capital_reserve.errors %}{{ error }}{% endfor %}</td>
</tr>
<tr>
<th colspan="5"><button>Продолжить</button></td>
</tr>
</table>
</div>
</form>
</div>
{% endblock %}
################################################### Views
def current_quarter():
lang = {}
lang[1] = 'Первый квартал (I)'
lang[2] = 'Второй квартал (II)'
lang[3] = 'Третий квартал (III)'
lang[4] = 'Четвертый квартал (IV)'
current_day = int(strftime('%j'))
current_year = int(strftime('%Y'))
if current_day <= 90:
p = 4
if (current_day <= 180) and (current_day > 90):
p = 1
if (current_day <= 270) and (current_day > 180):
p = 2
if current_day > 270:
p = 3
for_web = []
for_def = []
for_db = Q()
for i in range(4):
if p == 0:
p = 4
current_year -= 1
for_web.append({'Period': lang[p], 'Period_Year': current_year})
for_def.append({'Period': p, 'Period_Year': current_year})
for_db = for_db | (Q(period=p) & Q(period_year=current_year))
p -= 1
return {'db':for_db, 'web':for_web, 'def':for_def}
def finance(request, id):
try:
company_data = Company.objects.get(id=id, user=request.user)
except Company.DoesNotExist:
return HttpResponseRedirect('/company/new/')
period_data = current_quarter()
CompanyFinanceSet = modelformset_factory(Company_Finance, extra=4, max_num=4, fields = ('receipt', 'asset', 'capital_reserve', ))
formset = CompanyFinanceSet(queryset=Company_Finance.objects.filter(period_data['db']).filter(company=id))
if request.method == 'POST':
formset = CompanyFinanceSet(request.POST)
if formset.is_valid():
Company_Finance.objects.filter(company=id).delete()
period_data['def'].reverse()
for form in formset.forms:
try:
per_data = period_data['def'].pop()
instance = form.save(commit=False)
instance.company = company_data
instance.period = per_data['Period']
instance.period_year = per_data['Period_Year']
instance.save()
except:
return render_to_response('finance.html', {'formset': formset, 'period': period_data['web']})
return HttpResponseRedirect('/')
return render_to_response('finance.html', {'formset': formset, 'period': period_data['web']})
################################################### Models
class Company(models.Model):
user = models.ForeignKey(User)
organizational_form = models.CharField(max_length=2, choices=ORGANIZATIONAL_FORM_CHOICES)
company_name = models.CharField(max_length=200)
date_of_create = models.DateTimeField(auto_now_add=True)
class Company_Finance(models.Model):
company = models.ForeignKey(Company)
date_of_create = models.DateTimeField(auto_now_add=True)
period = models.IntegerField()
period_year = models.IntegerField()
receipt = models.DecimalField(max_digits=50, decimal_places=2)
asset = models.DecimalField(max_digits=50, decimal_places=2)
capital_reserve = models.DecimalField(max_digits=50, decimal_places=2)