MODEL from django db import models class Company models Model user mod

  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
############################ MODEL
from django.db import 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)
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.CharField(max_length=200)
asset = models.CharField(max_length=200)
capital_reserve = models.CharField(max_length=200)
############################ VIEW
from models import Company, Company_Finance
from time import strftime
def current_quarter():
lang = {}
lang[1] = u'I кв.'
lang[2] = u'II кв.'
lang[3] = u'III кв.'
lang[4] = u'IV кв.'
current_day = int(strftime('%j'))
current_year = int(strftime('%Y'))
if current_day <= 90:
p = 4
if 90 < current_day <= 180:
p = 1
if 180 < current_day <= 270:
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('/new/')
period_data = current_quarter()
CompanyFinanceSet = modelformset_factory(Company_Finance, extra=4, max_num=4, fields = ('receipt', 'asset', 'capital_reserve', ))
if request.method == 'POST':
formset = CompanyFinanceSet(request.POST, queryset=Company_Finance.objects.filter(company=id))
if formset.is_valid():
Company_Finance.objects.filter(company=id).delete()
period_data['def'].reverse()
for form in formset.forms:
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()
return HttpResponseRedirect('/app/create/%s/' % id)
else:
return HttpResponseRedirect('/')
formset = CompanyFinanceSet(queryset=Company_Finance.objects.filter(period_data['db']).filter(company=id))
return render_to_response('finance.html', {'formset': formset, 'period': period_data['web']})
############################ TEMPLATE
<form action="" method="post">
{{ formset.management_form }}
<div id="table-style">
<table id="content-table-nowidth" colspan="0" cellspacing="0" width="1">
<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>