def requestMaterialExport(material, export_format): """ Request export material with given format """ res_pending = "PENDING" res_success = "SUCCESS" ready = False # get export format exp_format = models.ExportFormat.objects.get(name=export_format) # check export status export_obj = MaterialExport.objects.filter( material_id=material.material_id, export_format=exp_format, version=material.version ) # in case of nothing have been saved if export_obj.count() == 0: res = postMaterialZip(material, export_format) try: values = json.loads(res.content) new_export = MaterialExport( material_id=material.material_id, export_format=exp_format, version=material.version ) if values["status"] == res_pending: new_export.path = TASK_ID_PREFIX + values["task_id"] new_export.name = datetime.now().strftime(EXPORT_TIME_FORMAT) new_export.save() elif values["status"] == res_success: storeMaterialExport(values["url"], export_obj) ready = True else: # failure, delete the export object export_obj.delete() except ValueError: pass else: # check for the status if existed export_obj = export_obj[0] if isExportProcessing(export_obj): # conversion not completed, ask again task_id = export_obj.path[3:] print "task id:", task_id res = requests.get(EXPORT_URL + "?task_id=" + task_id) try: values = json.loads(res.content) except ValueError: # non-json content returned export_obj.delete() else: # download the PDF if done already if values["status"] == res_success: storeMaterialExport(values["url"], export_obj) ready = True elif values["status"] == res_pending: # clear the object, prepare for retrying download if isExportExpired(export_obj.name): export_obj.delete() elif values["status"] != res_pending: # failure, delete the export object export_obj.delete() elif export_obj.path: ready = True return ready
def requestMaterialPDF(material): """ Create the zip package and post it to vpt in order to receive the PDF genereated. After receiving the file exported, an entry of export material will be created (as MaterialExport). This returns: True: Export file is ready to get. False: Export file is not ready. Try again later. """ res_pending = 'PENDING' res_success = 'SUCCESS' ready = False # check export status export_obj = MaterialExport.objects.filter( material_id=material.material_id, version=material.version) # in case of nothing have been saved if export_obj.count() == 0: res = postMaterialZip(material) try: values = json.loads(res.content) new_export = MaterialExport( material_id=material.material_id, version=material.version) if values['status'] == res_pending: new_export.path = TASK_ID_PREFIX + values['task_id'] new_export.name = datetime.now().strftime(EXPORT_TIME_FORMAT) new_export.save() elif values['status'] == res_success: storeMaterialExport(values['url'], export_obj) ready = True else: # failure, delete the export object export_obj.delete() except ValueError: pass else: # check for the status if existed export_obj = export_obj[0] if isExportProcessing(export_obj): # conversion not completed, ask again task_id = export_obj.path[3:] res = requests.get(EXPORT_URL+'?task_id='+task_id) try: values = json.loads(res.content) except ValueError: # non-json content returned export_obj.delete() else: # download the PDF if done already if values['status'] == res_success: storeMaterialExport(values['url'], export_obj) ready = True elif values['status'] == res_pending: # clear the object, prepare for retrying download if isExportExpired(export_obj.name): export_obj.delete() elif values['status'] != res_pending: # failure, delete the export object export_obj.delete() elif export_obj.path: ready = True return ready