def test_ok(self): test_json = {'username': '******', 'password': '******'} response = ok(test_json) self.assertEqual(response[0].json, test_json) self.assertEqual(response[1], 200)
def get_directory_list_for_path(): try: path = _get_base_directory() + '/' + request.args.get('path') directory_list = DirectoryContentParser.get_directory_list(path) return ok([directory.to_json() for directory in directory_list]) except Exception as e: trace = traceback.format_exc() return internal_server_error(e, trace)
def get_file_metadata_list_for_path(): try: path = _get_base_directory() + '/' + request.args.get('path') file_metadata_list = DirectoryContentParser.parse_directory_content( path) return ok([metadata.to_json() for metadata in file_metadata_list]) except Exception as e: trace = traceback.format_exc() return internal_server_error(e, trace)
def logout() -> Dict: """ Logs the user out. :return: a generic success message wrapped in JSON """ try: logout_user() return ok({'message': 'You have been logged out.'}) except Exception as e: trace = traceback.format_exc() return internal_server_error(e, trace)
def create_new_directory(): try: data = request.get_json() directory_name = data['name'] path = data['path'] directory_path = path + '/' + directory_name absolute_path = _get_base_directory() + '/' + directory_path if not os.path.exists(absolute_path): os.mkdir(absolute_path) return ok({'name': directory_name, 'path': path}) except Exception as e: trace = traceback.format_exc() return internal_server_error(e, trace)
def rename_file(): try: data = request.get_json() old_filename = data['oldFilename'] new_filename = data['newFilename'] path = data['path'] absolute_path_old_filename = _get_base_directory( ) + '/' + path + '/' + old_filename absolute_path_new_filename = _get_base_directory( ) + '/' + path + '/' + new_filename os.rename(absolute_path_old_filename, absolute_path_new_filename) return ok(new_filename) except Exception as e: trace = traceback.format_exc() return internal_server_error(e, trace)
def move_files(): try: data = request.get_json() for d in data: source_path = _get_base_directory() + '/' + d['sourcePath'] destination_path = _get_base_directory( ) + '/' + d['destinationPath'] if os.path.exists(destination_path) and os.path.isdir( destination_path): shutil.move(source_path, destination_path) else: raise Exception('The destination path ' + destination_path + ' does not exist.') return ok() except Exception as e: trace = traceback.format_exc() return internal_server_error(e, trace)
def upload_file(): try: file_to_upload = next(iter(request.files.values())) current_directory = request.headers.get('Current-Directory') filename = secure_filename(file_to_upload.filename) directory_path = _get_base_directory() + '/' + current_directory file_path = os.path.join(directory_path, filename) if not os.path.exists(directory_path): os.makedirs(directory_path) file_to_upload.save(file_path) file_to_upload.close() file_metadata = DirectoryContentParser.get_file(file_path) return ok(file_metadata.to_json()) except Exception as e: trace = traceback.format_exc() return internal_server_error(e, trace)
def delete_files(): try: data = request.get_json() for d in data: filename = d['filename'] path = d['path'] is_directory = d['isDirectory'] absolute_path_file_to_delete = _get_base_directory( ) + '/' + path + '/' + filename if is_directory: shutil.rmtree(absolute_path_file_to_delete) else: os.remove(absolute_path_file_to_delete) return ok() except Exception as e: trace = traceback.format_exc() return internal_server_error(e, trace)
def login() -> PiFillingUser: """ Logs the user in. :return: the logged in user """ try: data = request.get_json() username = data['username'] password = data['password'] user = PiFillingUser.query.filter_by(username=username).first() if user and check_password_hash(user.password_hash, password): login_user(user, remember=False) return ok(user.to_json()) return unauthorized() except Exception as e: trace = traceback.format_exc() return internal_server_error(e, trace)
def create_user() -> Dict: """ Creates a new user. The user who accesses this endpoint must have admin privileges. :return: a generic success message wrapped in JSON """ try: logged_in_user = PiFillingUser.query.filter_by(id=current_user.id).first() if not logged_in_user or not logged_in_user.is_admin: return unauthorized() data = request.get_json() hashed_password = generate_password_hash(data['password'], method='pbkdf2:sha256:200000', salt_length=32) new_user = PiFillingUser(username=data['username'], password_hash=hashed_password, is_admin=False) db.session.add(new_user) db.session.commit() return ok(new_user.to_json()) except Exception as e: trace = traceback.format_exc() return internal_server_error(e, trace)