def test_delete_resource_folder(self): create_resource_folder(self.path) self.assertTrue(os.path.isdir(self.path)) delete_resource_folder(self.path) self.assertTrue(not os.path.isdir(self.path))
def test_create_resource_folder(self): create_resource_folder(self.path) self.assertTrue(os.path.isdir(self.path)) # impossible to write if already exists self.assertRaises(LudoboxError, lambda: create_resource_folder(self.path))
def create_content(info, attachments, data_dir): """ Write a JSON file description of a game according to the provided data and attachment files in a dedicated directory named after the game. Arguments: info -- a dictionnary storing all the information about the content. It exactly mimic the structure of the disired JSON file. The data["title"] must exist, no other verification is made to check it contains coherent structure or data. attachments -- list of files attached to the content (images, PDF...). All files will be stored in a '/files' folder data_dir -- directory where all the game directories are stored i.e. where a directory named after the content will be created to store the JSON file. It must exist. Returns the path to the directory created after the name of the slugified title of the content. """ # get slug and add name slugified_name = get_resource_slug(info) info["slug"] = slugified_name # if info about files, remove it info.pop('files', None) # validate game data validate_content(info) # add default state info["state"] = "needs_review" # check attachments if attachments: check_attachments(attachments) # Create a directory after the cleaned name of the content content_path = os.path.join(data_dir, slugified_name) create_resource_folder(content_path) # get current user user = None if current_user.is_authenticated : user = current_user.email # create event and add to history record event = make_create_event(info, user=user) info_with_updated_history = add_event_to_history(info, event) # create the JSON file write_info_json(info_with_updated_history, content_path) # Write the attached files if attachments: write_attachments(attachments, content_path) return content_path
def api_clone_resource(): """ This function clone a resource it takes a valid JSON description of the game and will then proceed to the download the files. """ # TODO : convert to decorator # make sure unauthorized boxes can not create new games if current_app.config["UPLOAD_ALLOWED"] is False: response = jsonify({'message': 'Upload not allowed'}) return response, 401 data = request.get_json() info = data["info"] files_list = data["files"] slug = data["slug"] resource_path = os.path.join(app.config["DATA_DIR"], slug) if not os.path.exists(resource_path): create_resource_folder(resource_path) socket.emit("downloadEvent", { "slug": slug, "message": "Game path created." }) # clone the JSON info write_info_json(info, resource_path) socket.emit("downloadEvent", { "slug": slug, "message": "Game info copied." }) # make sub-rep to store files files_path = os.path.join(resource_path, "files") if not os.path.exists(files_path): os.makedirs(files_path) # download files from server for i, f in enumerate(files_list): socket.emit( "downloadEvent", { "slug": slug, "message": "File %s/%s downloading..." % (i, len(files_list)) }) download_from_server(f["url"], files_path, f["filename"]) socket.emit( "downloadEvent", { "slug": slug, "message": "File %s/%s downloaded." % (i, len(files_list)) }) # return original JSON return jsonify({"path": content_path}), 201
def test_read_info_json(self): path = "/tmp/my-game" create_resource_folder(path) self.assertTrue(os.path.isdir(path)) info = {"title": "some game", "description": "some stuff"} write_info_json(info, self.path) json_path = os.path.join(self.path, "info.json") self.assertTrue(os.path.exists(json_path)) data = read_info_json(self.path) self.assertDictEqual(data, info)
def test_write_info_json(self): path = "/tmp/my-game" create_resource_folder(path) self.assertTrue(os.path.isdir(path)) info = {"title": "some game", "description": "some stuff"} write_info_json(info, self.path) json_path = os.path.join(self.path, "info.json") self.assertTrue(os.path.exists(json_path)) # make sure data is written properly with open(json_path, 'r') as json_file: data = json.load(json_file) self.assertDictEqual(data, info)