def upload_fixture_api(request, domain, **kwargs): """ Use following curl-command to test. > curl -v --digest http://127.0.0.1:8000/a/gsid/fixtures/fixapi/ -u [email protected]:password -F "file-to-upload=@hqtest_fixtures.xlsx" -F "replace=true" """ response_codes = {"fail": 405, "warning": 402, "success": 200} error_messages = { "invalid_post_req": "Invalid post request. Submit the form with field 'file-to-upload' and POST parameter 'replace'", "has_no_permission": "User {attr} doesn't have permission to upload fixtures", "invalid_file": "Error processing your file. Submit a valid (.xlsx) file", "has_no_sheet": "Workbook does not have a sheet called {attr}", "unknown_fail": "Fixture upload couldn't succeed due to the following error: {attr}", } def _return_response(code, message): resp_json = {} resp_json["code"] = code resp_json["message"] = message return HttpResponse(json.dumps(resp_json), mimetype="application/json") try: upload_file = request.FILES["file-to-upload"] replace = request.POST["replace"] if replace.lower() == "true": replace = True elif replace.lower() == "false": replace = False except Exception: return _return_response(response_codes["fail"], error_messages["invalid_post_req"]) if not request.couch_user.has_permission(domain, Permissions.edit_data.name): error_message = error_messages["has_no_permission"].format( attr=request.couch_user.username) return _return_response(response_codes["fail"], error_message) try: workbook = get_workbook(upload_file) except Exception: return _return_response(response_codes["fail"], error_messages["invalid_file"]) try: upload_resp = run_upload( domain, workbook, replace=replace) # error handle for other files except WorksheetNotFound as e: error_message = error_messages["has_no_sheet"].format(attr=e.title) return _return_response(response_codes["fail"], error_message) except ExcelMalformatException as e: return _return_response(response_codes["fail"], str(e)) except DuplicateFixtureTagException as e: return _return_response(response_codes["fail"], str(e)) except FixtureAPIException as e: return _return_response(response_codes["fail"], str(e)) except Exception as e: error_message = error_messages["unknown_fail"].format(attr=e) return _return_response(response_codes["fail"], error_message) num_unknown_groups = len(upload_resp.unknown_groups) num_unknown_users = len(upload_resp.unknown_users) resp_json = {} if not num_unknown_users and not num_unknown_groups: num_uploads = upload_resp.number_of_fixtures success_message = "Successfully uploaded %d fixture%s." % ( num_uploads, 's' if num_uploads > 1 else '') return _return_response(response_codes["success"], success_message) else: resp_json["code"] = response_codes["warning"] warn_groups = "%d group%s unknown" % (num_unknown_groups, 's are' if num_unknown_groups > 1 else ' is') warn_users = "%d user%s unknown" % (num_unknown_users, 's are' if num_unknown_users > 1 else ' is') resp_json["message"] = "Fixtures have been uploaded. But following " if num_unknown_groups: resp_json["message"] += "%s %s" % (warn_groups, upload_resp.unknown_groups) if num_unknown_users: resp_json["message"] += "%s%s%s" % ( ("and following " if num_unknown_groups else ""), warn_users, upload_resp.unknown_users) return HttpResponse(json.dumps(resp_json), mimetype="application/json")
def upload_fixture_api(request, domain, **kwargs): """ Use following curl-command to test. > curl -v --digest http://127.0.0.1:8000/a/gsid/fixtures/fixapi/ -u [email protected]:password -F "file-to-upload=@hqtest_fixtures.xlsx" -F "replace=true" """ response_codes = {"fail": 405, "warning": 402, "success": 200} error_messages = { "invalid_post_req": "Invalid post request. Submit the form with field 'file-to-upload' and POST parameter 'replace'", "has_no_permission": "User {attr} doesn't have permission to upload fixtures", "invalid_file": "Error processing your file. Submit a valid (.xlsx) file", "has_no_sheet": "Workbook does not have a sheet called {attr}", "unknown_fail": "Fixture upload couldn't succeed due to the following error: {attr}", } def _return_response(code, message): resp_json = {} resp_json["code"] = code resp_json["message"] = message return HttpResponse(json.dumps(resp_json), mimetype="application/json") try: upload_file = request.FILES["file-to-upload"] replace = request.POST["replace"] if replace.lower() == "true": replace = True elif replace.lower() == "false": replace = False except Exception: return _return_response(response_codes["fail"], error_messages["invalid_post_req"]) if not request.couch_user.has_permission(domain, Permissions.edit_data.name): error_message = error_messages["has_no_permission"].format(attr=request.couch_user.username) return _return_response(response_codes["fail"], error_message) try: workbook = get_workbook(upload_file) except Exception: return _return_response(response_codes["fail"], error_messages["invalid_file"]) try: upload_resp = run_upload(domain, workbook, replace=replace) # error handle for other files except WorksheetNotFound as e: error_message = error_messages["has_no_sheet"].format(attr=e.title) return _return_response(response_codes["fail"], error_message) except ExcelMalformatException as e: return _return_response(response_codes["fail"], str(e)) except DuplicateFixtureTagException as e: return _return_response(response_codes["fail"], str(e)) except FixtureAPIException as e: return _return_response(response_codes["fail"], str(e)) except Exception as e: error_message = error_messages["unknown_fail"].format(attr=e) return _return_response(response_codes["fail"], error_message) num_unknown_groups = len(upload_resp.unknown_groups) num_unknown_users = len(upload_resp.unknown_users) resp_json = {} if not num_unknown_users and not num_unknown_groups: num_uploads = upload_resp.number_of_fixtures success_message = "Successfully uploaded %d fixture%s." % (num_uploads, 's' if num_uploads > 1 else '') return _return_response(response_codes["success"], success_message) else: resp_json["code"] = response_codes["warning"] warn_groups = "%d group%s unknown" % (num_unknown_groups, 's are' if num_unknown_groups > 1 else ' is') warn_users = "%d user%s unknown" % (num_unknown_users, 's are' if num_unknown_users > 1 else ' is') resp_json["message"] = "Fixtures have been uploaded. But following " if num_unknown_groups: resp_json["message"] += "%s %s" % (warn_groups, upload_resp.unknown_groups) if num_unknown_users: resp_json["message"] += "%s%s%s" % (("and following " if num_unknown_groups else ""), warn_users, upload_resp.unknown_users) return HttpResponse(json.dumps(resp_json), mimetype="application/json")