def create_player(cid, uid): """Create a player character entity.""" form = EntityForm() form["csrf_token"].data = request.cookies["csrf_token"] if form.validate_on_submit(): image_filename = upload_file(form["image"].data) entity = Entity( chronicle_id=cid, type="character", category="player", title=form["title"].data, description=form["description"].data, color=form["color"].data, icon=form["icon"].data, image=image_filename, ) # TODO Option to add entity assets, meters, conditions? db.session.add(entity) # current_user.p_characters.append(entity) db.session.commit() # print("\n\nPC PC PC PC PC") # pprint(entity.to_dict()) return entity.to_dict() else: return {"errors": validation_errors_to_messages(form.errors)}, 401
def create_asset(): """Create a new asset""" form = AssetForm() form["csrf_token"].data = request.cookies["csrf_token"] if form.validate_on_submit(): image_filename = upload_file(form["image"].data) asset = Asset( user=current_user, category=form["category"].data, title=form["title"].data, description=form["description"].data, color=form["color"].data, icon=form["icon"].data, image=image_filename, value=form["value"].data, max=form["max"].data, is_allowed_multiple=form["is_allowed_multiple"].data, ) # TODO Option to add asset assets, meters, conditions? db.session.add(asset) db.session.commit() return asset.to_dict() else: return {"errors": validation_errors_to_messages(form.errors)}, 401
def create_status(): """Create a new status""" form = StatusForm() form["csrf_token"].data = request.cookies["csrf_token"] if form.validate_on_submit(): image_filename = upload_file(form["image"].data) # duration = form["duration"].data status = Status( user=current_user, category=form["category"].data, title=form["title"].data, description=form["description"].data, color=form["color"].data, icon=form["icon"].data, image=image_filename, # duration=duration, ) # TODO Option to add status assets, meters, conditions? db.session.add(status) db.session.commit() return status.to_dict() else: return {"errors": validation_errors_to_messages(form.errors)}, 401
def login(): """Logs a user in""" print("\nPRINT: request.get_json()", request.get_json()) form = LoginForm() # Get the csrf_token from the request cookie and put it into the # form manually to validate_on_submit can be used form['csrf_token'].data = request.cookies['csrf_token'] if form.validate_on_submit(): # Add the user to the session, we are logged in! user = User.query.filter(User.username == form.data['username']).first() login_user(user) user, pcs, chronicles, tales, threads, choices, entities, assets, statuses, meters = get_and_normalize_all_data_for_user(user) return jsonify( user=user, pcs=pcs, chronicles=chronicles, tales=tales, threads=threads, choices=choices, entities=entities, assets=assets, statuses=statuses, meters=meters, msg="login successful") return {'errors': validation_errors_to_messages(form.errors)}, 401
def create_thread(tid): """Create a new thread.""" form = ThreadForm() form["csrf_token"].data = request.cookies["csrf_token"] if form.validate_on_submit(): print("form", form.data) image_filename = upload_file(form["image"].data) thread = Thread( tale_id=tid, title=form["title"].data, description=form["description"].data, color=form["color"].data, icon=form["icon"].data, image=image_filename, ) db.session.add(thread) db.session.commit() # Creates effects for thread in database # effects = createEffects(request.json["effects"], thread) # Creates choices for thread in database choices = createChoicesFromString(form["choices"].data, thread) # Create locks for choices in database # locks = createLocks(request.json["locks"], thread) return jsonify(thread=thread.to_dict(), choices=choices) else: return {"errors": validation_errors_to_messages(form.errors)}, 401
def create_thread_node(tid): """Create a new thread.""" form = ThreadForm() form["csrf_token"].data = request.cookies["csrf_token"] if form.validate_on_submit(): thread = Thread( tale_id=tid, title="Untitled", color="#f9fbefff", icon="feather-alt", ) db.session.add(thread) db.session.commit() return thread.to_dict() else: return {"errors": validation_errors_to_messages(form.errors)}, 401
def edit_tale(tid): """Edit a tale.""" form = TaleForm() form["csrf_token"].data = request.cookies["csrf_token"] if form.validate_on_submit(): image_filename = upload_file(form["image"].data) tale = Tale.query.get(tid) tale.title = form["title"].data tale.description = form["description"].data tale.color = form["color"].data tale.icon = form["icon"].data tale.image = image_filename db.session.commit() return tale.to_dict() else: return {"errors": validation_errors_to_messages(form.errors)}, 401
def edit_choice(chid): """Edit a choice.""" form = ThreadForm() form["csrf_token"].data = request.cookies["csrf_token"] if form.validate_on_submit(): image_filename = upload_file(form["image"].data) choice = Choice.query.get(chid) choice.title = form["title"].data choice.prev_thread_id = form["prev_thread"].data choice.next_thread_id = form["next_thread"].data choice.color = form["color"].data choice.icon = form["icon"].data choice.image = image_filename db.session.commit() return choice.to_dict() else: return {"errors": validation_errors_to_messages(form.errors)}, 401
def edit_entity(eid): """Edit an entity.""" form = EntityForm() form["csrf_token"].data = request.cookies["csrf_token"] if form.validate_on_submit(): image_filename = upload_file(form["image"].data) entity = Entity.query.get(eid) entity.category = form["category"].data, entity.title = form["title"].data entity.description = form["description"].data entity.color = form["color"].data entity.icon = form["icon"].data entity.image = image_filename db.session.commit() return entity.to_dict() else: return {"errors": validation_errors_to_messages(form.errors)}, 401
def create_choice(thid): """Create a choice.""" form = ThreadForm() form["csrf_token"].data = request.cookies["csrf_token"] if form.validate_on_submit(): image_filename = upload_file(form["image"].data) choice = Choice( title=form["title"].data, prev_thread_id=form["prev_thread"].data, next_thread_id=form["next_thread"].data, color=form["color"].data, icon=form["icon"].data, image=image_filename ) db.session.add(choice) db.session.commit() return choice.to_dict() else: return {"errors": validation_errors_to_messages(form.errors)}, 401
def create_tale(cid): """Create a new tale that belongs to a chronicle""" form = TaleForm() form["csrf_token"].data = request.cookies["csrf_token"] if form.validate_on_submit(): image_filename = upload_file(form["image"].data) tale = Tale( chronicle_id=cid, title=form["title"].data, description=form["description"].data, color=form["color"].data, icon=form["icon"].data, image=image_filename, ) db.session.add(tale) db.session.commit() return tale.to_dict() else: return {"errors": validation_errors_to_messages(form.errors)}, 401
def edit_meter(plid): """Edit a meter.""" form = MeterForm() form["csrf_token"].data = request.cookies["csrf_token"] if form.validate_on_submit(): image_filename = upload_file(form["image"].data) meter = Meter.query.get(plid) meter.title = form["title"].data meter.description = form["description"].data meter.color = form["color"].data meter.image = image_filename meter.min = form["min"].data meter.max = form["max"].data meter.base = form["base"].data meter.mod = form["mod"].data meter.algo = form["algo"].data db.session.commit() return meter.to_dict() else: return {"errors": validation_errors_to_messages(form.errors)}, 401
def edit_status(sid): """Edit an status.""" form = StatusForm() form["csrf_token"].data = request.cookies["csrf_token"] if form.validate_on_submit(): image_filename = upload_file(form["image"].data) # duration = form["duration"].data status = Status.query.get(sid) status.category = form["category"].data, status.title = form["title"].data status.description = form["description"].data status.color = form["color"].data status.icon = form["icon"].data status.image = image_filename # status.duration = duration db.session.commit() return status.to_dict() else: return {"errors": validation_errors_to_messages(form.errors)}, 401
def edit_asset(aid): """Edit an asset.""" form = AssetForm() form["csrf_token"].data = request.cookies["csrf_token"] if form.validate_on_submit(): image_filename = upload_file(form["image"].data) asset = Asset.query.get(aid) asset.category = form["category"].data, asset.title = form["title"].data asset.description = form["description"].data asset.color = form["color"].data asset.icon = form["icon"].data asset.value = form["value"].data asset.max = form["max"].data asset.is_allowed_multiple = form["is_allowed_multiple"].data asset.image = image_filename db.session.commit() return asset.to_dict() else: return {"errors": validation_errors_to_messages(form.errors)}, 401
def edit_thread(thid): """Edit a thread.""" form = ThreadForm() form["csrf_token"].data = request.cookies["csrf_token"] if form.validate_on_submit(): image_filename = upload_file(form["image"].data) thread = Thread.query.get(thid) thread.title = form["title"].data thread.description = form["description"].data thread.color = form["color"].data thread.icon = form["icon"].data thread.image = image_filename # updating choices existing_choices = Choice.query.filter(Choice.prev_thread_id == thread.id).all() existing_choice_ids = [c.id for c in existing_choices] editted_choice_ids = [c.id for c in request.json["choices"]] for choice in existing_choices: print("\n\nDELETE", choice) if choice.id not in editted_choice_ids: # deleting removed choices db.session.delete(choice) else: print("\n\nUPDATE", choice) # updating existing choices [matching_choice] = [c for c in request.json["choices"] if c["id"] and c["id"] == choice.id] choice.title = matching_choice["title"] choice.color = matching_choice["color"] choice.icon = matching_choice["icon"] choice.image = matching_choice["image"] request.json["choices"].remove(matching_choice) # adding new choices choices = createChoices(request.json["choices"], thread) print("\n\nCHOICES?", choices) db.session.commit() return jsonify(thread=thread.to_dict(), choices=choices) else: return {"errors": validation_errors_to_messages(form.errors)}, 401
def create_entity(entity): """Create a new entity""" form = EntityForm() form["csrf_token"].data = request.cookies["csrf_token"] if form.validate_on_submit(): image_filename = upload_file(form["image"].data) entity = Entity( user=current_user, type=entity, category=form["category"].data, title=form["title"].data, description=form["description"].data, color=form["color"].data, icon=form["icon"].data, image=image_filename, ) # TODO Option to add entity assets, meters, conditions? db.session.add(entity) db.session.commit() return entity.to_dict() else: return {"errors": validation_errors_to_messages(form.errors)}, 401
def create_meter(cid): """Create a new meter that belongs to a chronicle""" form = MeterForm() form["csrf_token"].data = request.cookies["csrf_token"] if form.validate_on_submit(): image_filename = upload_file(form["image"].data) meter = Meter( title=form["title"].data, description=form["description"].data, color=form["color"].data, icon=form["icon"].data, image=image_filename, min=form["min"].data, max=form["max"].data, base=form["base"].data, mod=form["mod"].data, algorithm=form["algo"].data, ) db.session.add(meter) db.session.commit() return meter.to_dict() else: return {"errors": validation_errors_to_messages(form.errors)}, 401
def sign_up(): """Creates a new user and logs them in""" form = SignUpForm() form['csrf_token'].data = request.cookies['csrf_token'] if form.validate_on_submit(): # TODO Idea: Populate an entire sample demo for new users? user = User( username=form['username'].data, email=form['email'].data, password=form['password'].data) # chronicle = Chronicle( # user=user, # title=f"{user.username}'s Chronicles", # description="Record the details of your story in this Chronicle, including characters, items, locations, and events. Start new Tales and weave them together by making and connecting Threads.") # tale = Tale( # chronicle=chronicle, # title=f"{user.username}'s First Tale", # description="Start your very first Tale here. Make a Thread, then add more and start weaving them together.") # thread = Thread( # tale=tale, # title="Start Here!") db.session.add(user) # db.session.add(chronicle) # db.session.add(tale) # db.session.add(thread) # db.session.commit() # tale.first_thread_id = thread.id db.session.commit() login_user(user) norm_user = normalize_user_data(user) return jsonify(user=norm_user, msg="signup successful") return {'errors': validation_errors_to_messages(form.errors)}
def create_chronicle(): """Create a new chronicle for a user.""" form = ChronicleForm() form["csrf_token"].data = request.cookies["csrf_token"] if form.validate_on_submit(): image_filename = upload_file(form["image"].data) chronicle = Chronicle( user_id=current_user.id, title=form["title"].data, description=form["description"].data, color=form["color"].data, icon=form["icon"].data, image=image_filename, ) tale = Tale(chronicle=chronicle, title=f"{chronicle.title}'s First Tale") thread = Thread( tale=tale, title=f"Untitled", description="Edit this scene and link threads with choices!") db.session.add(chronicle) db.session.add(tale) db.session.add(thread) db.session.commit() tale.first_thread_id = thread.id db.session.commit() return chronicle.to_dict() # jsonify( # chronicle={chronicle.id: chronicle.to_dict()}, # tale={tale.id: tale.to_dict()}, # thread={thread.id: thread.to_dict()} # ) else: return {"errors": validation_errors_to_messages(form.errors)}, 401