VIEWS PY def current_quarter lang lang Первый квартал lang Второй квар

 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
##################### VIEWS.PY
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 (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):
# print i
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', ))
if request.method == 'POST':
formset = CompanyFinanceSet(request.POST)
if formset.is_valid():
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('/')
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']})
#######################