Esempio n. 1
0
    def test_gen_participant_choices(self, app, client):
        from app.session.helpers import gen_participant_choices

        choices = gen_participant_choices()
        self.assertEqual(len(choices), 2)
        self.assertEqual(len(choices[0][1]), 1)  # Party 1
        self.assertEqual(len(choices[1][1]), 2)  # Party 2

        choices2 = gen_participant_choices(ensure=[self.char_admin])
        self.assertEqual(
            choices, choices2)  # ensure with character that is already visible

        choices = gen_participant_choices(ensure=[self.char_admin_priv])
        self.assertEqual(len(choices), 3)
        self.assertEqual(len(choices[0][1]), 1)  # Party 1
        self.assertEqual(len(choices[1][1]), 2)  # Party 2
        self.assertEqual(len(choices[2][1]), 1)  # No Party

        choices = gen_participant_choices(ensure=[
            self.char_admin_priv, self.char_moderator_priv, self.char_user_priv
        ])
        self.assertEqual(len(choices), 4)
        self.assertEqual(len(choices[0][1]), 2)  # Party 1
        self.assertEqual(len(choices[1][1]), 2)  # Party 2
        self.assertEqual(len(choices[2][1]), 1)  # Party 3
        self.assertEqual(len(choices[3][1]), 1)  # No Party
Esempio n. 2
0
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"))
Esempio n. 3
0
def create_with_campaign(id):
    form = SessionForm()
    form.submit.label.text = "Create Session"
    form.participants.choices = gen_participant_choices()

    campaign = Campaign.query.filter_by(id=id).first_or_404()

    if not current_user.is_dm_of(campaign):
        del form.dm_notes

    if form.validate_on_submit():
        participants = Character.query.filter(Character.id.in_(form.participants.data)).all()

        dm_notes = None
        if current_user.is_dm_of(campaign):
            dm_notes = form.dm_notes.data

        new_session = Session(title=form.title.data, campaign_id=form.campaign.data, summary=form.summary.data, dm_notes=dm_notes, date=form.date.data, participants=participants)

        db.session.add(new_session)
        db.session.commit()

        flash("Session was created.", "success")
        return redirect(url_for("session.view", id=new_session.id))
    elif request.method == "GET":
        participants = []

        for p in campaign.default_participants:
            participants.append(p.id)

        form.participants.data = participants

        form.campaign.data = id

    return render_template("session/create.html", form=form, campaign=campaign, title=page_title("Add Session"))
Esempio n. 4
0
def edit(id):
    session = Session.query.filter_by(id=id).first_or_404()
    is_admin = current_user.has_admin_role()
    is_dm = current_user.is_dm_of(session.campaign)

    form = SessionForm()
    form.submit.label.text = "Save Session"

    if is_admin or is_dm:
        form.participants.choices = gen_participant_choices()
    else:
        del form.participants
        del form.date

    if not is_dm:
        del form.dm_notes

    del form.campaign

    if form.validate_on_submit():
        session.title = form.title.data
        session.summary = form.summary.data

        if is_admin or is_dm:
            session.date = form.date.data

            participants = Character.query.filter(Character.id.in_(form.participants.data)).all()
            session.participants = participants

        if is_dm:
            session.dm_notes = form.dm_notes.data

        db.session.commit()
        flash("Session was changed.", "success")
        return redirect(url_for("session.view", id=id))
    elif request.method == "GET":
        form.title.data = session.title
        form.summary.data = session.summary

        if is_admin or is_dm:
            form.date.data = session.date

            participants = []

            for p in session.participants:
                participants.append(p.id)

            form.participants.data = participants

        if is_dm:
            form.dm_notes.data = session.dm_notes

    return render_template("session/edit.html", form=form, title=page_title("Edit Session '%s'" % session.title))
Esempio n. 5
0
def edit(id):
    campaign = Campaign.query.filter_by(id=id).first_or_404()
    is_admin = current_user.has_admin_role()
    is_dm = current_user.is_dm_of(campaign)

    form = CampaignEditForm()
    form.default_participants.choices = gen_participant_choices()

    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.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

        db.session.commit()
        flash("Campaign was changed.", "success")
        return redirect(url_for("campaign.view", id=id))

    elif request.method == "GET":
        form.name.data = campaign.name
        form.description.data = campaign.description
        form.color.data = campaign.color

        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("Edit Campaign '%s'" % campaign.name))
Esempio n. 6
0
def create():
    form = CampaignCreateForm()
    form.dm.choices = gen_dm_choices()
    form.default_participants.choices = gen_participant_choices()

    if form.validate_on_submit():
        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, default_participants=default_members, color=stretch_color(form.color.data.hex))

        db.session.add(new_campaign)
        db.session.commit()

        flash("Campaign was created.", "success")
        #return redirect(url_for("campaign.view", id=new_campaign.id))
        return redirect(url_for("campaign.index"))

    return render_template("campaign/create.html", form=form, title=page_title("Add Campaign"))
Esempio n. 7
0
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}'"))
Esempio n. 8
0
def edit(id, name=None):  # noqa: C901
    session = Session.query.filter_by(id=id).first_or_404()

    if not session.is_editable_by_user():
        return deny_access(no_perm_url)

    is_dm = current_user.is_dm_of(session.campaign)
    is_admin = current_user.is_admin()

    form = SessionForm()
    form.submit.label.text = "Save Session"

    if is_dm or is_admin:
        form.participants.choices = gen_participant_choices(
            ensure=session.participants)
    else:
        del form.participants
        del form.date

    if not is_dm:
        del form.dm_notes

    del form.campaign

    if form.validate_on_submit():
        session.title = form.title.data
        session.summary = form.summary.data

        if is_dm or is_admin:
            session.date = form.date.data

            participants = Character.query.filter(
                Character.id.in_(form.participants.data)).all()
            session.participants = participants

        if is_dm:
            session.dm_notes = form.dm_notes.data

        db.session.commit()

        recalc_session_numbers(session.campaign, db)

        flash("Session was changed.", "success")
        return redirect(session.view_url())
    elif request.method == "GET":
        form.title.data = session.title
        form.summary.data = session.summary

        if is_dm or is_admin:
            form.date.data = session.date

            participants = []

            for p in session.participants:
                participants.append(p.id)

            form.participants.data = participants

        if is_dm:
            form.dm_notes.data = session.dm_notes

    return render_template("session/edit.html",
                           form=form,
                           campaign=session.campaign,
                           title=page_title(f"Edit Session '{session.title}'"))