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"))
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))
def __call__(self, form, field): campaign_id = form._fields.get(self.campaign_field).data campaign = Campaign.query.filter_by(id=campaign_id).first() if campaign == None: raise ValidationError("Unknown campaign.") if not current_user.is_dm_of( campaign) and not current_user.has_admin_role(): raise ValidationError( "You are not the DM of the selected campaign.")
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))
def decorated_function(*args, **kwargs): if not 'id' in kwargs: flash( "@admin_or_dm_required was used incorrectly, contact the administrator", "danger") return redirect(url_for(url)) campaign = Campaign.query.filter_by(id=kwargs['id']).first_or_404() if not current_user.has_admin_role() and not current_user.is_dm_of( campaign): flash( "You need to be admin or dm for this campaign to perform this action.", "danger") return redirect(url_for(url)) return f(*args, **kwargs)
def is_deletable_by_user(self): return current_user.is_admin() or current_user.is_dm_of(self.campaign)
def is_editable_by_user(self): return current_user.is_admin() or current_user.is_dm_of( self.campaign) or current_user.has_char_in_session(self)
def is_editable_by_user(self): return current_user.is_dm_of(self) or current_user.is_admin()
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 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}'"))