def create(): form = CampaignCreateForm() form.dm.choices = gen_dm_choices() form.associated_parties.choices = gen_party_choices() form.default_participants.choices = gen_participant_choices() if form.validate_on_submit(): associated_parties = Party.query.filter(Party.id.in_(form.associated_parties.data)).all() default_members = Character.query.filter(Character.id.in_(form.default_participants.data)).all() new_campaign = Campaign(name=form.name.data, dm_id=form.dm.data, description=form.description.data, associated_parties=associated_parties, default_participants=default_members, color=stretch_color(form.color.data.hex)) success = True if form.profile_picture.data: success, filename = upload_profile_picture(form.profile_picture.data) new_campaign.profile_picture = filename if success is False: flash("Error while creating campaign.", "error") else: db.session.add(new_campaign) db.session.commit() flash("Campaign was created.", "success") return redirect(url_for("campaign.index")) return render_template("campaign/create.html", form=form, title=page_title("Add Campaign"))
def create(): form = CreateCharacterForm() if form.validate_on_submit(): char = Character(name=form.name.data, race=form.race.data, class_=form.class_.data, description=form.description.data, private_notes=form.private_notes.data, user_id=current_user.id, is_visible=form.is_visible.data) success = True if form.profile_picture.data: success, filename = upload_profile_picture( form.profile_picture.data) char.profile_picture = filename if success is False: flash("Error while creating character.", "error") else: db.session.add(char) db.session.commit() flash("Character was created.", "success") return redirect(char.view_url()) return render_template("character/create.html", form=form, title=page_title("Add Character"))
def edit(id, name=None): # noqa: C901 party = Party.query.filter_by(id=id).first_or_404() if not party.is_editable_by_user(): return deny_access(no_perm_url) is_admin = current_user.is_admin() form = PartyForm() form.submit.label.text = "Save Party" if is_admin: form.members.choices = gen_party_members_choices(ensure=party.members) else: del form.members if form.validate_on_submit(): party.name = form.name.data party.description = form.description.data if is_admin: members = Character.query.filter(Character.id.in_(form.members.data)).all() party.members = members success = True if form.profile_picture.data: success, filename = upload_profile_picture(form.profile_picture.data) if success and party.profile_picture: delete_profile_picture(party.profile_picture) party.profile_picture = filename if success is False: flash("Error while editing party.", "error") else: db.session.commit() flash("Party was edited.", "success") return redirect(party.view_url()) elif request.method == "GET": form.name.data = party.name form.description.data = party.description if is_admin: members = [] for m in party.members: members.append(m.id) form.members.data = members return render_template("party/edit.html", form=form, party=party, title=page_title(f"Edit Party '{party.name}'"))
def test_upload_profile_picture(self, app, client): from app.helpers import upload_profile_picture from flask import current_app img = FakeFile(filename="test.jpg") with tempfile.TemporaryDirectory() as tmpdir: current_app.config["PROFILE_PICTURE_DIR"] = tmpdir os.mkdir(os.path.join(tmpdir, "thumbnails")) success, filename = upload_profile_picture(img) self.assertTrue(success) self.assertTrue(os.path.exists(os.path.join(tmpdir, filename))) self.assertTrue(os.path.exists(os.path.join(tmpdir, "thumbnails", filename))) success, filename = upload_profile_picture(img, filename="fix.jpg") self.assertTrue(success) self.assertEqual(filename, "fix.jpg") self.assertTrue(os.path.exists(os.path.join(tmpdir, filename))) self.assertTrue(os.path.exists(os.path.join(tmpdir, "thumbnails", filename)))
def create(): form = PartyForm() form.submit.label.text = "Create Party" form.members.choices = gen_party_members_choices() if form.validate_on_submit(): members = Character.query.filter(Character.id.in_(form.members.data)).all() new_party = Party(name=form.name.data, description=form.description.data, members=members) success = True if form.profile_picture.data: success, filename = upload_profile_picture(form.profile_picture.data) new_party.profile_picture = filename if success is False: flash("Error while creating party.", "error") else: db.session.add(new_party) db.session.commit() flash("Party was created.", "success") return redirect(new_party.view_url()) return render_template("party/create.html", form=form, title=page_title("Add Party"))
def edit(id, name=None): # noqa: C901 campaign = Campaign.query.filter_by(id=id).first_or_404() if not campaign.is_editable_by_user(): return deny_access(no_perm_url) is_admin = current_user.is_admin() is_dm = current_user.is_dm_of(campaign) form = CampaignEditForm() form.associated_parties.choices = gen_party_choices() form.default_participants.choices = gen_participant_choices(ensure=campaign.default_participants) if not is_admin: del form.dm else: form.dm.choices = gen_dm_choices() if not is_dm: del form.dm_notes if form.validate_on_submit(): campaign.name = form.name.data campaign.description = form.description.data campaign.associated_parties = Party.query.filter(Party.id.in_(form.associated_parties.data)).all() campaign.default_participants = Character.query.filter(Character.id.in_(form.default_participants.data)).all() campaign.color = stretch_color(form.color.data.hex) if is_admin: campaign.dm_id = form.dm.data if is_dm: campaign.dm_notes = form.dm_notes.data success = True if form.profile_picture.data: success, filename = upload_profile_picture(form.profile_picture.data) if success and campaign.profile_picture: delete_profile_picture(campaign.profile_picture) campaign.profile_picture = filename if success is False: flash("Error while editing campaign.", "error") else: db.session.commit() flash("Campaign was edited.", "success") return redirect(campaign.view_url()) elif request.method == "GET": form.name.data = campaign.name form.description.data = campaign.description form.color.data = campaign.color form.associated_parties.data = [p.id for p in campaign.associated_parties] participants = [] for p in campaign.default_participants: participants.append(p.id) form.default_participants.data = participants if is_admin: form.dm.data = campaign.dm_id if is_dm: form.dm_notes.data = campaign.dm_notes return render_template("campaign/edit.html", form=form, campaign=campaign, title=page_title(f"Edit Campaign '{campaign.name}'"))
def edit(id, name=None): # noqa: C901 char = Character.query.filter_by(id=id).first_or_404() if not char.is_editable_by_user(): return deny_access(no_perm_url) form = EditCharacterForm() if not char.is_owned_by_user(): del form.private_notes if not char.is_hideable_by_user(): del form.is_visible if form.validate_on_submit(): char.name = form.name.data char.race = form.race.data char.class_ = form.class_.data char.description = form.description.data char.edited = datetime.utcnow() if char.is_owned_by_user(): char.private_notes = form.private_notes.data if char.is_hideable_by_user(): char.is_visible = form.is_visible.data success = True if form.profile_picture.data: success, filename = upload_profile_picture( form.profile_picture.data) if success and char.profile_picture: delete_profile_picture(char.profile_picture) char.profile_picture = filename if success is False: flash("Error while editing character.", "error") else: flash("Character was edited.", "success") db.session.commit() return redirect(char.view_url()) else: form.name.data = char.name form.race.data = char.race form.class_.data = char.class_ form.description.data = char.description if char.is_owned_by_user(): form.private_notes.data = char.private_notes if char.is_hideable_by_user(): form.is_visible.data = char.is_visible return render_template( "character/edit.html", form=form, char=char, title=page_title(f"Edit character '{char.name}'"))