Discussion:
Django can not save the model inside restless Endpoint
Chasan KIOUTSOUK MOUSTAFA
2018-11-17 12:04:55 UTC
Permalink
Hi everyone,

I'm facing with a very strange issue. Basically I've written a JSON
interface which a remote server would feed it with log data about a batch.
I'm adding each JSON data to BatchStatusLog table, and last log contains
extra information about the batch and I'm trying to update the batch with
last request.

Problem starts here; There are no exception on logs both on local and
remote server, everything seems fine. Even I see the log lines after
batch.save() but batch could not be updated successfully on database. The
strangest one, I've written a small script to mimic server, and it could
update the batch!! There is something that I can not see.

from restless.views import Endpointfrom restless.auth import
BasicHttpAuthMixin, login_requiredfrom api.authentications import
CustomAuthMixinfrom api.utils import date_with_tz_from_timestampfrom
splitter.models import Batch, BatchStatusLog

class BatchStatusLogEndpoint(Endpoint, BasicHttpAuthMixin):
def post(self, request):
if request.data is None:
return {'is_valid': False, 'message': 'No data'}
try:
data = request.data['data']
entry = data['status_entry']
batch_id = entry["batch_id"]
message = entry["text"]
state = entry["state"]
item = entry["item"]
total_items = entry["numOfItems"]
timestamp = entry["timestamp"]
except Exception as e:
print('EXCEPTION: ' + str(e))
return {'is_valid': False, 'message': 'key {0} is
required'.format(str(e))}

try:
batch = Batch.objects.get(id=batch_id)
except Batch.DoesNotExist as e:
print('EXCEPTION: ' + str(e))
return {'is_valid': False, 'message': str(e)}

try:
batch_log = BatchStatusLog()
batch_log.batch = batch
batch_log.message = message
batch_log.state = state
if int(total_items) != 0:
batch_log.progress = int(100 * float(item) / float(total_items))
else:
batch_log.progress = 100
batch_log.remote_date = date_with_tz_from_timestamp(int(timestamp))
batch_log.save()
if batch_log.state != '99':
return {'is_valid': True, 'message': str(batch_log.id)}
except Exception as e:
print('EXCEPTION: ' + str(e))
return {'is_valid': False, 'message': str(e)}

try:
pid = entry["pid"]
total_good = int(entry["TotalGood"])
total_bad = int(entry["TotalBad"])
total_belege = int(entry["TotalBelege"])
total_invoices = int(entry["TotalInvoices"])
num_of_items = int(entry["numOfItems"])
month = int(entry["month"])
year = int(entry["year"])
zip_filename = entry["zipFile"]
elapsed = entry["Elapsed"]
except Exception as e:
print('EXCEPTION: ' + str(e))
return {'is_valid': False, 'message': 'key {0} is
required'.format(str(e))}

try:
batch.refresh_from_db()
batch.pid = pid
batch.total_good = total_good
batch.total_bad = total_bad
batch.total_belege = total_belege
batch.total_invoices = total_invoices
batch.num_of_items = num_of_items
batch.month = month
batch.year = year
batch.zip_filename = zip_filename
batch.elapsed = elapsed
first_log = BatchStatusLog.objects.filter(batch_id=batch.id).first()
delta = batch_log.remote_date - first_log.remote_date
batch.delta = int(delta.seconds)
batch.logi_start_date = first_log.remote_date
batch.logi_end_date = batch_log.remote_date
batch.save()
print('---------------SAVE----------')
print('Batch ID : ' + str(batch.id))
print('Batch PID :' + str(batch.pid))
print('Batch Total Good :' + str(batch.total_good))
print('Batch Total Bad :' + str(batch.total_bad))
print('Batch Total Belege :' + str(batch.total_belege))
print('Batch Total Invoices :' + str(batch.total_invoices))
print('Batch Number Of Items :' + str(batch.num_of_items))
print('Batch Month :' + str(batch.month))
print('Batch Year :' + str(batch.year))
print('Batch Zip Filename :' + str(batch.zip_filename))
print('Batch Elapsed :' + str(batch.elapsed))
print('Batch Year :' + str(batch.year))
print('Batch Delta :' + str(batch.delta))
print('Batch Logi Start Date :' + str(batch.logi_start_date))
print('Batch Logi End Date :' + str(batch.logi_end_date))
return {'is_valid': True, 'message': 'Batch Updated'}
except Exception as e:
print('EXCEPTION: ' + str(e))
return {'is_valid': False, 'message': str(e)}

And here is my test script to mimic remote server (which actually can
update the batch!):

import requests

data = {
"files":[
],
"data":{
"status_entry":{
"numOfItems":"5",
"item":"5",
"year":2018,
"TotalInvoices":"5",
"TotalGood":"5",
"month":"11",
"TotalBelege":"5",
"id":"15725",
"mandant":"1",
"Elapsed":"00:00:07",
"timestamp":1542381730,
"pid":42081,
"TotalBad":"0",
"status_file":"",
"text":"filename.zip;5;0;00:00:07",
"buchsymbol":"AR",
"batch_id":33,
"client":"1111",
"zipFile":"filename.zip",
"state":"99"
}
},
"function":"writeStatusDetail"}

def post_data():
url = 'http://xxx.xxx.xxx.xxx:yyyy/api/some-url/'
headers = {'Content-Type': 'application/json', 'Accept':
'application/json', 'charset': 'UTF-8'}
try:
req = requests.post(url=url, headers=headers, json=data)
if req.status_code == 200:
print(req.json())
else:
print(req.text)
except Exception as e:
print(str(e))

if __name__ == '__main__':
post_data()
--
Kind Regards.
--------------------------------------------------
Chasan KIOUTSOUK MOUSTAFA,
Freelance Software Engineer
www.chasank.com
--------------------------------------------------
--
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+***@googlegroups.com.
To post to this group, send email to django-***@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAOhBaNBmW3mKy%3Dbq%2Be2%3DYpAEv2y%3Dx1dtA%3DLK604TvRA9fadveQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Loading...