class TargetAllocationsForm(forms.Form):
def __init__(self, cliente, *args, **kwargs):
super(TargetAllocationsForm, self).__init__(*args, **kwargs)
for cat in CategoriaActivo.objects.all():
self.fields[cat] = forms.FloatField(label=cat.nombre, min_value=0, max_value=1)
try:
currentAlloc = cliente.targetCategoriaAllocations.filter(categoria=cat).latest("fecha")
self.fields[cat].initial = currentAlloc.allocation
except:
self.fields[cat].initial = cat.default_allocation
for sca in SubcategoriaActivo.objects.all():
self.fields[sca] = forms.FloatField(label=sca.nombre, min_value=0, max_value=1)
try:
currentAlloc = cliente.targetSubcategoriaAllocations.filter(subcategoria=sca).latest("fecha")
self.fields[sca].initial = currentAlloc.allocation
except:
self.fields[sca].initial = sca.default_allocation
def clean(self):
cleaned_data = super(TargetAllocationsForm, self).clean() # Here is where I think the error is being raised
allocations = {}
for k in self.fields.keys():
if isinstance(k, CategoriaActivo):
if k not in allocations:
allocations[k] = {"subcategorias":{}}
allocations[k]["alloc"] = cleaned_data.get(k)
elif isinstance(k, SubcategoriaActivo):
if k.categoria not in allocations:
allocations[k] = {"subcategorias":{}}
allocations[k.categoria]["subcategorias"][k] = cleaned_data.get(k)
total_allocations_cat = 0
for cat in allocations:
total_allocations_cat += allocations[cat]["alloc"]
total_alloc_subcat = 0
for subcat in allocations[cat]["subcategorias"]:
total_alloc_subcat += allocations[cat]["subcategorias"][subcat]
if total_alloc_subcat > 1:
raise forms.ValidationError("Total de allocations en subcategorías de %s supera el 100%" % str(cat))
if total_allocations_cat > 1:
raise forms.ValidationError("Total de allocations en categorías supera el 100%")
def perfilInversionCliente(request, cliente_pk):
objetos = {}
cliente = get_object_or_404(Cliente, pk=cliente_pk)
targetAllocationsForm = TargetAllocationsForm(cliente=cliente)
if request.method == "POST":
targetAllocationsForm = TargetAllocationsForm(request.POST)
if targetAllocationsForm.is_valid():
data = targetAllocationsForm.cleaned_data
for k in data:
editar = False
if isinstance(k, CategoriaActivo):
try:
currentAlloc = cliente.targetCategoriaAllocations.filter(categoria=k).latest("fecha")
if currentAlloc.allocation != data[k]:
editar = True
except DoesNotExist:
editar = True
if editar:
newAlloc = TargetCategoriaAllocation(cliente=cliente, categoria=k, allocation=data[k], fecha=datetime.datetime.now(), editor=request.user)
newAlloc.save()
if isinstance(k, SubcategoriaActivo):
try:
currentAlloc = cliente.targetSubcategoriaAllocations.filter(subcategoria=k).latest("fecha")
if currentAlloc.allocation != data[k]:
editar = True
except DoesNotExist:
editar = True
if editar:
newAlloc = TargetSubcategoriaAllocation(cliente=cliente, subcategoria=k, allocation=data[k], fecha=datetime.datetime.now(), editor=request.user)
newAlloc.save()
else:
pdb.set_trace()
objetos["targetAllocationsForm"] = targetAllocationsForm
categorias = {}
for cat in CategoriaActivo.objects.all():
categorias[cat] = cat.subcategorias.all()
objetos["categorias"] = categorias
objetos["cliente"] = cliente
template = loader.get_template('carteras/perfilInversionista.html')
context = RequestContext(request, objetos)
return HttpResponse(template.render(context))