def home(): """Default home route with conversion form""" form = CurrencyForm() if form.validate_on_submit(): session.clear() convert_from = form.convert_from.data.upper() convert_to = form.convert_to.data.upper() amount = form.amount.data cc = CurrencyCodes() for curr in (convert_from, convert_to): if not cc.get_currency_name(curr): flash(f'Incorrect currency code: {curr}', 'error') return redirect(url_for('home')) if amount < 1: flash('Not a valid amount', 'error') return redirect(url_for('home')) symbol = cc.get_symbol(convert_to) cr = CurrencyRates() session['symbol'] = symbol session[ 'amount'] = f"{cr.convert(convert_from, convert_to, amount):.2f}" return redirect(url_for('converted')) else: return render_template('home.html', form=form)
def currency_add(request): if request.method == 'POST': form = CurrencyForm(request.POST) p_sn = generate_sn(prefix = 'CR') if form.is_valid(): p = form.save(commit=False) p.sn = p_sn p.save() serializer = JSONSimpleSerializer() return HttpResponse(serializer.serialize([p,], use_natural_foreign_keys=True)) else: form = CurrencyForm() return render_to_response('modal/main_form.html',{'action':'Add', 'module':module, 'form': form},context_instance=RequestContext(request))
def currency_update(request): if request.method == 'POST': postdata = request.POST.copy() try: p = Currency.objects.get(sn=postdata.pop('sn')[0]) except Currency.DoesNotExist: raise Http404(module + ' not found') form = CurrencyForm(postdata, instance=p) print form.errors if form.is_valid(): p_saved = form.save() serializer = JSONSimpleSerializer() return HttpResponse(serializer.serialize([p_saved,])) else: return HttpResponse('Invalid request.')
def index(request): form = CurrencyForm() context = {'form': form} return render(request, 'exchanger/index.html', context)