I'm trying to collect data of Investment Funds in a API, there's a list of Fund Codes and a List of days in format that I want.
I create a routine in a For by day, and after a For by fund. But to complete all data frame, this take me 30 minutes.
I would like to know how to optmize it
Here's the part of code:
dataPosicao_list is a list of days, and fundo_id_list is a list o funds by ID that API recognize
df_geral = pd.DataFrame()
df_day = pd.DataFrame()
for day in dataPosicao_list:
#df_day = pd.DataFrame()
for fundo_id in fundo_id_list:
try:
url_api = f'https://modal-pp.totvs.amplis.com.br/amplisapi/ws/v2/rendavariavel/avista/movimentos/carteira/{fundo_id}/data/{day}?pagina=0&tamanhoPagina=100'
session_api = requests.Session()
response = session_api.get(url_api, verify=False)
payload_api = ''
headers_api = {'Content-Type': 'application/json',
'charset': 'UTF-8',
'Authorization': token }
posicoes_api = session_api.request('GET', url_api, headers = headers_api, data = payload_api)
posicoes = json.loads(posicoes_api.text)
df_total = pd.DataFrame()
for linha in posicoes['conteudo']:
try:
bolsa = linha['bolsa']['codigo']
except:
bolsa = "None"
try:
ativo = linha['ativo']['codigo']
except:
ativo = "None"
try:
bolsa_internacional = linha['bolsa']['bolsaInternacional']
except:
bolsa_internacional = "None"
try:
localidade = linha['bolsa']['localidade']['codigo']
except:
localidade = "None"
try:
moeda = linha['bolsa']['moeda']['internationalCode']
except:
moeda = "None"
try:
fundo = linha['carteira']['administrador']['codigoB3']
except:
fundo = "None"
try:
Fundo_ou_Clube = linha['carteira']['administrador']['administradorClubeInvestimento']
except:
Fundo_ou_Clube = "None"
try:
carteira = linha['carteira']['administrador']['codigo']
except:
carteira = "None"
try:
class_fundo = linha['carteira']['classificacaoFundo']['classificacao']
except:
class_fundo = "None"
try:
cota_fidic = linha['carteira']['classificacaoFundo']['tipoCotaFIDC']
except:
cota_fidic = "None"
try:
mov_cetip = linha['carteira']['codAtivoMnemonicoCetip']
except:
mov_cetip = "None"
try:
controlador = linha['carteira']['controlador']['codigo']
except:
controlador = linha['carteira']['controlador']
try:
custodiante = linha['carteira']['custodiante']['nome']
except:
custodiante = "None"
try:
data = linha['data']
except:
data = "None"
try:
nome_fundo = fundo_id
except:
nome_fundo = "None"
try:
taxa_performance = linha['carteira']['taxaPerformance']
except:
taxa_performance = "None"
try:
mercado = linha['mercado']['descricao']
except:
mercado = "None"
try:
calculo_corretagem_emulamentos = linha['corretagem']['calculaCorretagemEmolumentos']
except:
calculo_corretagem_emulamento = "None"
try:
porcentagemDevolucaoCorretagemCorretora = linha['corretagem']['porcentagemDevolucaoCorretagemCorretora']
except:
porcentagemDevolucaoCorretagemCorretora = "None"
try:
modalidade = linha['modalidadeMovimento']['descricao']
except:
modalidade = "None"
try:
preco = linha['preco']
except:
preco = "None"
try:
quantidade = linha['quantidade']
except:
quantidade = "None"
try:
valor_bruto = linha['valorBruto']
except:
valor_bruto = "None"
try:
id = linha['id']
except:
id = 'None'
serie = {'id' : id, 'controlador': controlador,'taxa_performance': taxa_performance, 'nome_fundo': nome_fundo, 'modalidade':modalidade, 'mercado': mercado, 'preco': preco, 'quantidade':quantidade, 'valor_bruto': valor_bruto, 'cota_fidic': cota_fidic,'porcentagemDevolucaoCorretagemCorretora': porcentagemDevolucaoCorretagemCorretora, 'calculo_corretagem_emulamentos': calculo_corretagem_emulamentos, 'data': data, 'custodiante':custodiante, 'controlador': controlador, 'mov_cetip': mov_cetip, 'bolsa': bolsa, 'ativo' : ativo, 'bolsa_internacional': bolsa_internacional, 'localidade': localidade , 'moeda': moeda, 'fundo': fundo, 'Fundo_ou_Clube': Fundo_ou_Clube, 'carteira': carteira, ' class_fundo': class_fundo }
serie = pd.Series(serie)
df_total = df_total.append(serie, ignore_index=True)
#df_geral = df_geral.append(df_total, ignore_index=True)
df_day = df_day.append(df_total, ignore_index=True)
except:
continue
df_geral = df_geral.append(df_day, ignore_index=True)
df_geral = df_geral.drop_duplicates(subset=['id'])
df_geral.head()
print(df_geral)
Related
Please, can anyone help me out. I am trying to call a function I defined in my views.py under food_app into another application in my project. after calling it, it works but I have a problem in reading out the function objects on my html template. it usually displays "₦<HttpResponse status_code=200, "text/html; charset=utf-8">"
But the objects prints out on the terminal after when printed.
This is the first function defined
views.py #food_app
def price_in_packs(request):
total_price1 = ""
total_price2 = ""
total_price3 = ""
total_price4 = ""
total_price5 = ""
total_price6 = ""
total_price7 = ""
total_price8 = ""
total_price9 = ""
total_price10 = ""
total_price11 = ""
total_price12 = ""
price_pack_box1 = ""
price_pack_box2 = ""
price_pack_box3 = ""
price_pack_box4 = ""
price_pack_box5 = ""
price_pack_box6 = ""
price_pack_box7 = ""
price_pack_box8 = ""
price_pack_box9 = ""
price_pack_box10 = ""
price_pack_box11 = ""
price_pack_box12 = ""
food = ""
food_price = ""
#try and except is used here to avoid a null or empty value input by the user. A user can forget to input a price in pack value, which can break the code.
if request.method == "POST":
if request.POST["form"] == "form1":
try:
price_pack_box1 = int(request.POST.get("price1"))
food = Food.objects.get(pk=1)
food_price = food.food_price
total_price1 = price_pack_box1*food_price
print(total_price1)
except ValueError:
return render(request,'food_app/404.html')
elif request.POST["form"] == "form2":
try:
price_pack_box2 = int(request.POST.get("price2"))
food = Food.objects.get(pk=2)
food_price = food.food_price
total_price2 = price_pack_box2*food_price
print(total_price2)
except ValueError:
return render(request,'food_app/404.html')
elif request.POST["form"] == "form3":
try:
price_pack_box3 = int(request.POST.get("price3"))
food = Food.objects.get(pk=3)
food_price = food.food_price
total_price3 = price_pack_box3*food_price
print(total_price3)
except ValueError:
return render(request,'food_app/404.html')
elif request.POST["form"] == "form4":
try:
price_pack_box4 = int(request.POST.get("price4"))
food = Food.objects.get(pk=4)
food_price = food.food_price
total_price4 = price_pack_box4*food_price
print(total_price4)
except ValueError:
return render(request,'food_app/404.html')
elif request.POST["form"] == "form5":
try:
price_pack_box5 = int(request.POST.get("price5"))
food = Food.objects.get(pk=5)
food_price = food.food_price
total_price5 = price_pack_box5*food_price
print(total_price5)
except ValueError:
return render(request,'food_app/404.html')
elif request.POST["form"] == "form6":
try:
price_pack_box6 = int(request.POST.get("price6"))
food = Food.objects.get(pk=6)
food_price = food.food_price
total_price6 = price_pack_box6*food_price
print(total_price6)
except ValueError:
return render(request,'food_app/404.html')
elif request.POST["form"] == "form7":
try:
price_pack_box7 = int(request.POST.get("price7"))
food = Food.objects.get(pk=7)
food_price = food.food_price
total_price7 = price_pack_box7*food_price
print(total_price7)
except ValueError:
return render(request,'food_app/404.html')
elif request.POST["form"] == "form8":
try:
price_pack_box8 = int(request.POST.get("price8"))
food = Food.objects.get(pk=8)
food_price = food.food_price
total_price8 = price_pack_box8*food_price
print(total_price8)
except ValueError:
return render(request,'food_app/404.html')
elif request.POST["form"] == "form9":
try:
price_pack_box9 = int(request.POST.get("price9"))
food = Food.objects.get(pk=9)
food_price = food.food_price
total_price9 = price_pack_box9*food_price
print(total_price9)
except ValueError:
return render(request,'food_app/404.html')
elif request.POST["form"] == "form10":
try:
price_pack_box10 = int(request.POST.get("price10"))
food = Food.objects.get(pk=10)
food_price = food.food_price
total_price10 = price_pack_box10*food_price
print(total_price10)
except ValueError:
return render(request,'food_app/404.html')
elif request.POST["form"] == "form11":
try:
price_pack_box11 = int(request.POST.get("price11"))
food = Food.objects.get(pk=11)
food_price = food.food_price
total_price11 = price_pack_box11*food_price
print(total_price11)
except ValueError:
return render(request,'food_app/404.html')
elif request.POST["form"] == "form12":
try:
price_pack_box12 = int(request.POST.get("price12"))
food = Food.objects.get(pk=12)
food_price = food.food_price
total_price12 = price_pack_box12*food_price
print(total_price12)
except ValueError:
return render(request,'food_app/404.html')
#Total price represents the nummber of the number input of price in packs
#while price_in_pack_box is the amount of the amount of the product in packs
my_dict = {'new_price1':[total_price1,price_pack_box1],'new_price2':[total_price2,price_pack_box2],
'new_price3':[total_price3,price_pack_box3],'new_price4':[total_price4,price_pack_box4],
'new_price5':[total_price5,price_pack_box5],'new_price6':[total_price6,price_pack_box6],
'new_price7':[total_price7,price_pack_box7],'new_price8':[total_price8,price_pack_box8],
'new_price9':[total_price9,price_pack_box9],'new_price10':[total_price10,price_pack_box10],
'new_price11':[total_price11,price_pack_box11],'new_price12':[total_price12,price_pack_box12]}
return render(request, 'food_app/price.html',context=my_dict)
I need to use the price_in_packs function in this view
views.py #payment app
def payment(request, pk):
item = ""
if request.method == "POST":
if request.POST.get("form") == "form1":
seerbit_pay = request.POST.get("ewa")
return render(request,'payments/pay.html',{'pk':pk,'all_pk':all_pk,'soup':soup_box,'price_in_pack':packs(request)})
After calling the price_in_pack key in my template view, it displace this "₦<HttpResponse status_code=200, "text/html; charset=utf-8">" but I want it to display the object instead.
**QPainter::begin(): Returned false============================] 100%
Error: Unable to write to destination
Exit with code 1, due to unknown error.**
def view_entry_pdf(request,id):
standard_fields = ['user_username','user_email', 'form_id', 'entry_id', 'date_dmy','user_full_name']
try:
entry = Entries.objects.get(pk=id)
cert = Certificate.objects.filter(form_id=entry.form.id, is_active=1).first()
get_cert = request.GET.get('cert_id','')
if get_cert:
cert = Certificate.objects.get(id=get_cert)
if not cert:
messages.warning(request, 'PDF template not found.')
return HttpResponseRedirect(request.META.get('HTTP_REFERER'))
valid_rules = validate_rules(entry,cert, cert.rules.all())
if valid_rules:
pass
else:
messages.warning(request, 'Certificate rules not matched.')
return HttpResponseRedirect(request.META.get('HTTP_REFERER'))
entry.is_read = True
entry.save()
file_name = 'entry-certificate.pdf'
if cert.file_name:
file_name = cert.file_name
res = re.findall(r'(\{\{[^}]+}\})', file_name)
placeholders = {}
standard_placeholders = {}
#standard_fields
for sf in standard_fields:
sfv = ''
if sf == 'user_username':
sfv = entry.user.username
elif sf == 'user_email':
sfv = entry.user.email
elif sf == 'form_id':
sfv = str(entry.form.id)
elif sf == 'entry_id':
sfv = str(entry.id)
elif sf == 'date_dmy':
today = datetime.now().date()
sfv = today.strftime("%d-%m-%Y")
elif sf == 'user_full_name':
sfv = f"{entry.user.first_name} {entry.user.last_name}"
standard_placeholders['{{'+sf+'}}'] = sfv
standard_placeholders = account_placeholders(request, standard_placeholders)
if res:
fields_all = entry.form.fields.all().order_by('sort_order')
for f in fields_all:
key = '{{'+f.label_name.replace(" ", "_").lower()+'}}'
placeholders[key] = f.id
for p in res:
f_id = placeholders.get(p)
if p and f_id:
en_data = entry.columns.filter(field_id=f_id)
val = ''
if en_data.count() and en_data[0].value:
val = en_data[0].value
file_name = file_name.replace(p, val)
elif standard_placeholders.get(p):
file_name = file_name.replace(p, standard_placeholders[p])
cert_path = '{}/form/certificates/{}'.format(settings.MEDIA_ROOT,file_name)
#cert_path = 'media/form/certificates/{file_name}'
url = '{}://{}/admin/forms/entry-pdf/{}?system_run=1&cert={}'.format(request.scheme, request.get_host(), entry.id, cert.id)
print("reached here------------------------------")
options = {
'dpi': 365,
'page-size':cert.page_type,
'orientation':cert.orientation
}
pdfkit.from_url(url,cert_path,options=options)
#set pdf permissions
setPdfPermissions(cert, cert_path)
return FileResponse(open(cert_path, 'rb'), content_type='application/pdf')
except FileNotFoundError:
raise Http404()
I am converting html to pdf. This code worked fine before. I dont know what kind of problem is arrising now. I am not aware of this tool htmltopdf. If i am doing anything wrong please help me
TypeError("'%s' instance expected, got %r" % (
TypeError: 'CartItem' instance expected, got
I am getting this error when I tried to add or remove the product obj from cartItem. Help me to overcome this error.
CartUpdate view
def cart_update(request):
product_id = request.POST.get('product_id')
print(product_id)
try:
qty = request.POST.get('qty')
update_qty = True
except:
qty = None
update_qty = False
if product_id is not None:
try:
product_obj = Product.objects.get(id=product_id)
except Product.DoesNotExist:
print("Show message to user, product is gone?")
return redirect("cart:cart")
cart_obj, new_obj = Cart.objects.new_or_get(request)
cart_item, created = CartItem.objects.get_or_create(cart=cart_obj, product=product_obj)
if created:
print("created")
if update_qty and qty:
if int(qty) == 0:
cart_item.delete()
else:
cart_item.quantity = qty
cart_item.save()
else:
pass
if product_obj in cart_obj.cartitem_set.all():
cart_obj.cartitem_set.remove(product_obj)
added = False
else:
cart_obj.cartitem_set.add(product_obj)
added = True
new_total = 0.0
for x in cart_obj.cartitem_set.all():
line_item = float(x.product.price) * x.quantity
new_total += line_item
request.session['cart_items'] = cart_obj.cartitem_set.count()
cart_obj.subtotal = new_total
if cart_obj.subtotal > 0:
cart_obj.total = Decimal(cart_obj.subtotal) * Decimal(1.08)
else :
cart_obj.total = 0.00
cart_obj.save()
i have an query_name field in database. i want that every value should be unique so i changed it constraint and add unique= true.
now i want that if user enter the duplicate value then error duplicate value show to the user. currently error is showing only in backend
here is my code in python
def save_report(request):
if request.method == 'POST':
print(request.POST.dict())
data_dict = request.POST.dict()
query_json = {}
#query json data
query_json['data_src_name'] = data_dict['data_src_name']
query_json['fields'] = data_dict['fields']
query_json['group_by'] = data_dict['group_by']
query_json['order_by'] = data_dict['order_by']
query_json['where'] = data_dict['where']
query_json['limit'] = data_dict['limit']
query_json = json.dumps(query_json)
report_creation_obj = ReportCreationData.objects.create(
query_json = query_json,
data_source_name = data_dict['data_src_name'],
query_name = data_dict['query_name'],
mail_body = data_dict['mail_body'])
report_creation_obj.save()
return HttpResponse('success')
else:
return render(request, 'home/report_creation.html', context = None)
database :
query_name = models.CharField(max_length=100,unique= True, default= True)
code 2 :
def save_report(request):
if request.method == 'POST':
print(request.POST.dict())
querydata = ReportCreationData.objects.all()
querydata_list = []
querydata_dict = {'query_name':''}
for data in querydata:
querydata_dict['query_name'] = data.query_name
print ('querydata_dict', querydata_dict)
data_dict = request.POST.dict()
query_name = data_dict['query_name'],
print ('query_name', query_name)
query_json = {}
#query json data
query_json['data_src_name'] = data_dict['data_src_name']
query_json['fields'] = data_dict['fields']
query_json['group_by'] = data_dict['group_by']
query_json['order_by'] = data_dict['order_by']
query_json['where'] = data_dict['where']
query_json['limit'] = data_dict['limit']
query_json = json.dumps(query_json)
report_creation_obj = ReportCreationData.objects.create(
query_json = query_json,
data_source_name = data_dict['data_src_name'],
query_name = data_dict['query_name'],
mail_body = data_dict['mail_body'])
if (query_name == querydata_dict).exists():
raise ('already exists')
else:
report_creation_obj.save()
return HttpResponse('success')
else:
return render(request, 'home/report_creation.html')
with code 2 getting error:
AttributeError: 'bool' object has no attribute 'exists'
Please help
Thanks
You can try before inserting the data,run a select query from database and apply where clause on your query_name with your current value.So by this you can get duplicate records.
I am working on a CRUD for a medical company, im trying to display the specialties form a certain physician but when they have more than one i am trying to get the object as a list (and i got it)
but when i try to pass it to the template, it fails, what can i do?
this is my view
def UpdatePhysician(request,id):
physician = get_object_or_404(Physician, id=id)
employee = get_object_or_404(Employee, id=physician.employee.id)
person = get_object_or_404(Person, id=employee.person.id)
try:
address = get_object_or_404(Address, id=return_id(str(person.addresses.values('id'))))
except:
address = None
try:
email = get_object_or_404(Email, id=return_id(str(person.emails.values('id'))))
except:
email = None
try:
phone = get_object_or_404(PhoneNumber, id=return_id(str(person.phone_numbers.values('id'))))
except:
phone = None
try:
academic_degree = get_object_or_404(AcademicDegree, id=return_id(str(employee.academic_degrees.values('id'))))
university = get_object_or_404(University, id=return_id(str(employee.academic_degrees.values('university'))))
except:
academic_degree = None
university = None
physician_specialties = get_list_or_404(PhysicianSpecialties, employee_academic_degree__employee__pk=physician.employee.id)
for item in physician_specialties:
print(unicode(item.specialty))
physician_specialties_form = PhysicianSpecialtiesForm(request.POST or None, instance=physician_specialties)
specialties = Specialty.objects.all()
specialty = PhysicianSpecialties.objects.filter(employee_academic_degree__employee__physician__pk=id)
academic_degree = EmployeeAcademicDegree.objects.filter(employee__physician__pk=id)
person_form = PersonForm(request.POST or None, instance=person)
employee_form = EmployeeForm(request.POST or None, instance=employee)
physician_form = PhysicianForm(request.POST or None, instance=physician)
email_form = EmailForm(request.POST or None, instance=email)
address_form = AddressForm(request.POST or None, instance=address)
phone_form = PhoneForm(request.POST or None, instance=phone)
academic_degree_form = AcademicDegreeForm(request.POST or None, instance= academic_degree)
university_form = UniversityForm(request.POST or None, instance=university)
if (person_form.is_valid() and employee_form.is_valid() and physician_form.is_valid() and email_form.is_valid() and
address_form.is_valid() and phone_form.is_valid and physician_form.is_valid() and university_form.is_valid and
academic_degree_form.is_valid()):
person_form.save()
physician_form.save()
academic_degree_form.save()
new_address = address_form.save()
new_email = email_form.save()
new_phone = phone_form.save()
university_form.save()
if address == None:
PersonAddress.objects.create(person=person, address=new_address)
if email == None:
PersonEmail.object.create(person=person, email=new_email)
if phone == None:
PersonPhoneNumber.objects.create(person=person, hone_number=new_phone)
return HttpResponse('yesh')
return render(request, 'UpdatePhysician.html', {'person_form': person_form,
'employee_form': employee_form,
'email_form': email_form,
'phone_form': phone_form,
'address_form': address_form,
'physician_form': physician_form,
'specialty': specialty,
'academic_degree_form': academic_degree_form,
'academic_degree': academic_degree,
'university_form':university_form,
'specialties': specialties,
'physician_specialties_form': physician_specialties_form,
})
the problem lives in
physician_specialties = get_list_or_404(PhysicianSpecialties, employee_academic_degree__employee__pk=physician.employee.id)
for item in physician_specialties:
print(unicode(item.specialty))
physician_specialties_form = PhysicianSpecialtiesForm(request.POST or None, instance=physician_specialties)