def update_content_info(resource_path, new_info, state=None): """ Update game info based on changes - create patch changes using JSON patch (RFC 6902) - store patch in an history array within the JSON file - replace original info content with updated content """ original_info = read_content(resource_path) # get current user user = None if current_user.is_authenticated : user = current_user.email # create patch if state: event = make_update_state_event(original_info, state, user=user) else : event = make_update_event(new_info, original_info, user=user) if event is None : return original_info new_info_with_history = add_event_to_history(original_info, event) # write updated game to file write_info_json(new_info_with_history, resource_path) return new_info_with_history
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 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)
def test_read_content_validation(self): """Wrong game should raises validation error""" wrong_content = self.borgia_info_content.copy() wrong_content["title"] = 345 # create a bad record if needed wrong_game_path = os.path.join(TEST_DATA_DIR, "wrong-game") os.makedirs(wrong_game_path) write_info_json(wrong_content, wrong_game_path) with_errors = read_content(wrong_game_path) self.assertIs(type(with_errors["errors"]), list) self.assertIs(len(with_errors["errors"]), 1) self.assertIs(type(with_errors["errors"][0]), dict) self.assertIn("title", with_errors["errors"][0]["path"]) self.assertIn("345", with_errors["errors"][0]["message"])
if confirm_choice(): # update the actual data for game_path in needs_update: print game_path info = read_content(game_path) print "-" * 10 print "%s..." % info["title"] # remove history info.pop('history', None) # make a 'create' event = make_create_event(info.copy()) # add it to history info["history"] = [event] # reset state info["state"] = "needs_review" # validate_content(info) write_info_json(info, game_path) print "updated." print print "OK ! done."