def validate_token(self, _, token): """ Validate token properties """ if not token: raise exceptions.DataValidationException("token can't be empty") if len(token) > MAX_STRING_LENGTH: raise exceptions.DataValidationException("Invalid length of token") return token
def validate_file_name(self, _, file_name): """ Validate file_name properties """ if not file_name: raise exceptions.DataValidationException("file_name can't be empty") if len(file_name) > MAX_STRING_LENGTH: raise exceptions.DataValidationException("Invalid length of file name") return file_name
def validate_user_name(self, _, user_name): """ Validate user_name properties """ if not user_name: raise exceptions.DataValidationException("user_name can't be empty") if len(user_name) > MAX_STRING_LENGTH: raise exceptions.DataValidationException( "Invalid length of user_name name") return user_name
def add_content_to_file(token, contents): """ Append the content to file for a given token. Function first validates token is valid and append the content to the file. Args: token(str): token value for which content to be added. contents(str): contents to append to the file. Returns: Boolean status True or False """ filters = (("token", token), ) file_record = None with DB.session.begin(): file_record = get_files_by_filter((filters)) if not file_record: raise exceptions.DataValidationException( "No record exists with token '%s'" % token) file_record = file_record[0] try: LOCK.acquire() if contents: with codecs.open(file_record.file_path, "a", encoding="utf-8") as file_handle: file_handle.write(contents) except OSError: LOG.error("Error occurred for updating content", exc_info=True) return False finally: LOCK.release() return True
def get_file_content(token): """ Get the file content for a given token. Function first validates token is valid and return the content of the file. Args: token(str): token value for which content to be retrieved. Returns: Content of the file identified by the token """ filters = (("token", token), ) file_record = None with DB.session.begin(): file_record = get_files_by_filter((filters)) if not file_record: raise exceptions.DataValidationException( "No record exists with token '%s'" % token) file_record = file_record[0] try: with codecs.open(file_record.file_path, "r", encoding="utf-8") as file_handle: return file_handle.read() except OSError as oe: LOG.error("Error occurred for updating content", exc_info=True) raise oe except IOError: # File entry exists, but file not created yet LOG.warning("File '%s' not created yet") return ""
def add_new_file(fields): """ Create a new file entry with user name and token """ filters = (("file_name", fields["file_name"]), ("user_name", fields["user_name"])) new_file_obj = None with DB.session.begin(): existing_object = get_files_by_filter((filters)) if existing_object: raise exceptions.DataValidationException( "Record with given file_name and user_name already exists") fields["token"] = utils.generate_token() fields["file_path"] = utils.get_file_path( "%s_%s" % (fields["file_name"], fields["user_name"])) new_file_obj = Files(**fields) DB.session.add(new_file_obj) return new_file_obj
def sync_file_contents(token): """ Rest point to sync file content from client to server """ try: content = request.json["contents"] # Validating content assuming only append is allowed, not replacing the # content of files to make it empty if not content: raise exceptions.DataValidationException( "Nothing to append to file") except exceptions.DataValidationException as dve: return Response(response=json.dumps({ "error": dve.message, "error_code": exceptions.INVALID_INPUT }), status=500, mimetype='application/json') except KeyError as ke: return Response(response=json.dumps({ "error": "Missing required input %s" % ke, "error_code": exceptions.MISSING_REQUIRED_INPUT }), status=500, mimetype='application/json') except Exception as ex: return Response(response=json.dumps({ "error": "Invalid Input", "error_code": exceptions.INVALID_INPUT }), status=500, mimetype='application/json') try: status = api.add_content_to_file(token, content) except exceptions.DataValidationException as dve: return Response(response=json.dumps({ "error": dve.message, "error_code": exceptions.INVALID_TOKEN }), status=500, mimetype='application/json') except Exception as ex: LOG.error("Error occurred for syncing file contents: %s" % str(ex), exc_info=True) return Response(response=json.dumps({ "error": "Unknown error occurred.", "error_code": exceptions.UNKNOWN_ERROR }), status=500, mimetype='application/json') return Response(response=json.dumps({"status": status}), mimetype='application/json')