Hello, so i'm writing a program that uploads invoices to an accounting 
webapp based on order info from Wordpress. 

At one point, the code takes an order number, fetches order data using the 
WP REST API and is supposed to write a .txt file in a specified location 
containing mentioned order data.

All of the files are stored in the same app directory. 

app>
    ikros_faktury (folder)>
                              orders.txt
    all of the .py files

    

*After submitting the form data i get an error:* 

FileNotFoundError at /faktury/ [Errno 2] 
No such file or directory: 'ikros_faktury/7079.txt'

You can see more of the error message* here 
<https://drive.google.com/file/d/1qQ5Xmkd_YutFs5XeW1hXAvO_T-eO3jna/view?usp=sharing>*

*Here are my files:*

*views.py*
def faktury(request):
if request.method == 'POST':
form = InvoiceForm(request.POST)
if form.is_valid():
invoices_to_do = form.cleaned_data.get('orders')
do_done = form.cleaned_data.get('Zmeniť_stav_objednávky_na_Vybavené')

invoices_to_do_parsed = invoices_to_do.split(', ')
errors = []

for i in invoices_to_do_parsed:
add_invoice(i)
else:
form = InvoiceForm()
return render(request, 'invoices/faktury.html', 
{'title':'FAKTÚRY','form':form})

*forms.py*
class InvoiceForm(forms.Form):
orders = forms.CharField()
Zmeniť_stav_objednávky_na_Vybavené = forms.BooleanField(required=False)

class Meta:
fields = ['orders', 'mark_as_done']

*urls.py*
urlpatterns = [
path('', views.faktury, name='invoices')
]


*here is the function where it should handle all the data:*
def add_invoice(cislo_objednavky):
#definuje casy pre fakturu
now = datetime.now()
after_seven_days = datetime.now() + timedelta(days=14) 
completion_date = now.strftime("%Y-%m-%dT%H:%M:%S")
end_date = after_seven_days.strftime("%Y-%m-%dT%H:%M:%S")

#defining directories
direct = "ikros_faktury/"
direct_produkty = "ikros_faktury/produkty/"

#fetches wordpress data
kniznica = 
requests.get('http://cryformercy.eu/wp-json/wc/v3/orders/'+str(cislo_objednavky),
 
auth=HTTPBasicAuth('username', 'password'))
data = kniznica.json()

obj_c = cislo_objednavky
sposob_platby = ''
produkty = data['line_items']

#checkes payment method
if data['payment_method'] == 'cod':
sposob_platby= 'paymentType_Dobierka\nclosingText_'
else:
sposob_platby= 'paymentType_Bankový prevod\nclosingText_Faktúra uhradená.'

#checks country
if data['shipping']['country'] == 'SK':
stat_na_fakture = 'Slovensko'
mena = 'EUR'
suma_var = data['shipping_total']
elif data['shipping']['country'] == 'CZ':
stat_na_fakture = 'Česká republika'
mena = 'EUR'

#defines tottal COD
suma_var = data['shipping_total']
suma_dobierky = {'name':'Dobierka',
'totalPriceWithVat':suma_var,
'unitPriceWithVat':suma_var,
'measureType':'ks',
'vat':'0'}

#looks for product variables
hladam_farbu = data['line_items'][0]['meta_data']
ma_farbu = ''
for i in hladam_farbu:
if i['key'] == 'farba':
ma_farbu = ', ' + i['value']
else:
pass

#creates files 'ordernum.txt' -> uses the same keys as acounting app
with open(direct+f"{obj_c}.txt", "w") as f:   *<---------* *this is where 
is is supposed to create the and write the file, but doesn't*
f.write('documentNumber_\n'
'clientName_'+ data['shipping']['first_name'] + ' ' + 
data['shipping']['last_name'] + '\n' 
'clientStreet_'+ data['shipping']['address_1'] + '\n'
'clientPostCode_'+ str(data['shipping']['postcode']) + '\n'
'clientTown_'+ data['shipping']['city'] + '\n'
'clientCountry_'+ stat_na_fakture + '\n'
'orderNumber_'+ str(cislo_objednavky) + '\n'
'totalPrice_'+ str(data['total']) + '\n'
'totalPriceWithVat_' + str(data['total']) + '\n'
'createDate_'+ str(now) + '\n'
'dueDate_' + str(end_date) + '\n'
'completionDate_' + str(now) + '\n'
'currency_' + mena + '\n'
'deliveryType_Dobierka\n'
f'{sposob_platby}'
)


Any help would be greatly appreciated. 
Thanks alot,
Mike.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/4013af00-c8c4-4ed6-b734-5b74d6191454%40googlegroups.com.

Reply via email to