def view(id, character, editable): subskillform = SubskillForm(prefix="subskillform") if editable and subskillform.data and subskillform.validate_on_submit(): character.add_subskill(subskillform.name.data, subskillform.parent.data) logentry = LogEntry( character, f"add subskill {subskillform.name.data} under {subskillform.parent.data}", user_id=current_user.id) session.add(logentry) character.store_data() session.commit() return redirect(url_for('character.view', id=id)) skillform = SkillForm(prefix="skillform") if editable and skillform.data and skillform.validate_on_submit(): skills = character.skills() for skill in skills: if skillform.name.data == skill['name']: flash("Skill already exists") return redirect(url_for('character.view', id=id)) character.add_skill(skillform.name.data) character.store_data() logentry = LogEntry(character, f"add skill {subskillform.name.data}", user_id=current_user.id) session.add(logentry) session.commit() return redirect(url_for('character.view', id=id)) typeheader = "1920s Era Investigator" if character.game and character.game[1] == "Modern": typeheader = "Modern Era" shared = Invite.query_for(character).count() return render_template('character/coc7e/sheet.html.jinja', shared=shared, character=character, typeheader=typeheader, editable=editable, skillform=skillform, subskillform=subskillform)
def share(id: int): """Share a character.""" character = get_character(id, check_author=True) logger.debug("Finding previous invite") invite = Invite.query_for(character).first() logger.debug(f"Invites found {invite}") if not invite: logger.debug(f"Creating an invite for character {character.id}") invite = Invite(character) invite.owner_id = character.user_id session.add(invite) session.commit() share_url = url_for('character.shared', code=invite.id, _external=True) form = None html_response = render_template('character/api_share.html.jinja', form=form, url=share_url, code=invite.id) return jsonify({'url': share_url, 'html': html_response})
def view(id: int): invites = None campaign: Campaign = Campaign.query.get(id) # Set up forms inviteform = InvitePlayerForm(prefix="inviteform") createinviteform = CreateInviteForm(prefix="createinviteform") characterform = AddCharacterForm(prefix="characterform") npcform = AddNPCForm(prefix="npcform") messageform = MessagePlayerForm(players=[ (campaign.user_id, "GM"), ] + [(p.id, p.user.username) for p in campaign.players], campaign_id=campaign.id, from_id=current_user.profile.id) is_player = current_user.profile in campaign.players is_owner = current_user and current_user.profile.id == campaign.user_id logger.debug(f"Viewing campagin {campaign.title}") logger.debug(f"There are {campaign.players.count()} players") logger.debug(f"There are {campaign.characters.count()} characters") if not is_player and not is_owner: abort(404, "This is not the campaign your are looking for.") if is_owner: if createinviteform.submit.data and \ createinviteform.validate_on_submit(): invite = Invite(campaign) invite.owner_id = campaign.user_id session.add(invite) session.commit() if inviteform.submit.data and inviteform.validate_on_submit(): player = (User.query.filter_by( email=inviteform.email.data).first().profile) campaign.players.append(player) session.commit() return redirect(url_for('campaign.view', id=id)) invites = Invite.query_for(campaign) if npcform.submit.data and npcform.validate_on_submit(): print("Adding NPC") character = npcform.character.data npc = NPC(character=character, campaign=campaign) session.add(npc) session.commit() return redirect(url_for('campaign.view', id=id)) if characterform.submit.data and characterform.validate_on_submit(): print("Adding character") character = characterform.character.data if (character not in campaign.characters and character.user_id == current_user.profile.id): campaign.characters.append(character) session.commit() else: flash("Character is already added to campaign") return redirect(url_for('campaign.view', id=id)) createinviteform.submit.label.text = "Create share link." handouts = campaign.handouts.filter_by(status=HandoutStatus.visible) messages = campaign.messages.filter( or_(Message.from_id == current_user.profile.id, Message.to_id == current_user.profile.id, Message.to_id.is_(None))) added_npc_ids = [c.character_id for c in campaign.NPCs] npcform.character.query = current_user.profile.characters.filter( Character.id.notin_(added_npc_ids)).\ order_by( Character.folder_id.__eq__(campaign.folder_id).desc()).\ order_by('title') added_character_ids = [c.id for c in campaign.characters] characterform.character.query = current_user.profile.characters.\ filter(Character.id.notin_(added_character_ids)).\ order_by(Character.folder_id.__eq__(campaign.folder_id).desc()).\ order_by('title') return render_template('campaign/campaign.html.jinja', campaign=campaign, handouts=handouts, invites=invites, inviteform=inviteform, createinviteform=createinviteform, characterform=characterform, messages=messages, npcform=npcform, editable=is_owner, messageform=messageform)