def post_equipment(request, character_id, secret_key = None): if request.is_ajax and request.method == "POST": character = get_object_or_404(Character, id=character_id) form = EquipmentForm(request.POST) __check_edit_perms(request, character, secret_key) if form.is_valid(): character.equipment=form.cleaned_data['equipment'] with transaction.atomic(): character.save() return JsonResponse({"equipment": form.cleaned_data['equipment']}, status=200) else: return JsonResponse({"error": form.errors}, status=400) return JsonResponse({"error": ""}, status=400)
def view_character(request, character_id, secret_key = None): character = get_object_or_404(Character, id=character_id) if character.player and secret_key: return HttpResponseRedirect(reverse('characters:characters_view', args=(character_id,))) if not character.player_can_view(request.user): raise PermissionDenied("You do not have permission to view this Character") if request.user.is_authenticated and not request.user.profile.confirmed_agreements: return HttpResponseRedirect(reverse('profiles:profiles_terms')) secret_key_valid = False if secret_key: secret_key_valid = character.is_editable_with_key(secret_key) else: secret_key = "" user_can_edit = (request.user.is_authenticated and character.player_can_edit(request.user)) or secret_key_valid if not character.stats_snapshot: context={"character": character, "user_can_edit": user_can_edit} return render(request, 'characters/legacy_character.html', context) completed_games = [(x.relevant_game.end_time, "game", x) for x in character.completed_games()] # completed_games() does ordering character_edit_history = [(x.created_time, "edit", x) for x in character.contractstats_set.filter(is_snapshot=False).order_by("created_time").all()[1:]] exp_rewards = [(x.created_time, "exp_reward", x) for x in character.experiencereward_set.order_by("created_time").all()] events_by_date = list(merge(completed_games, character_edit_history, exp_rewards)) timeline = defaultdict(list) for event in events_by_date: timeline[event[0].strftime("%d %b %Y")].append((event[1], event[2])) char_ability_values = character.stats_snapshot.abilityvalue_set.order_by("relevant_ability__name").all() char_value_ids = [x.relevant_ability.id for x in char_ability_values] primary_zero_values = [(x.name, x, 0) for x in Ability.objects.filter(is_primary=True).order_by("name").all() if x.id not in char_value_ids] all_ability_values =[(x.relevant_ability.name, x.relevant_ability, x.value) for x in char_ability_values] ability_value_by_name = list(merge(primary_zero_values, all_ability_values)) unspent_experience = character.unspent_experience() exp_earned = character.exp_earned() exp_cost = character.exp_cost() equipment_form = EquipmentForm() context = { 'character': character, 'user_can_edit': user_can_edit, 'health_display': character.get_health_display(), 'ability_value_by_name': ability_value_by_name, 'physical_attributes': character.get_attributes(is_physical=True), 'mental_attributes': character.get_attributes(is_physical=False), 'timeline': dict(timeline), 'tutorial': get_object_or_404(CharacterTutorial), 'battle_scar_form': BattleScarForm(), 'trauma_form': TraumaForm(prefix="trauma"), 'injury_form': InjuryForm(request.POST, prefix="injury"), 'exp_cost': exp_cost, 'exp_earned': exp_earned, 'unspent_experience': unspent_experience, 'equipment_form': equipment_form, 'secret_key': secret_key, 'secret_key_valid': secret_key_valid, } return render(request, 'characters/view_pages/view_character.html', context)
def view_character(request, character_id, secret_key=None): character = get_object_or_404(Character, id=character_id) if character.player and secret_key: return HttpResponseRedirect( reverse('characters:characters_view', args=(character_id, ))) if not character.player_can_view(request.user): raise PermissionDenied( "You do not have permission to view this Character") if request.user.is_authenticated and not request.user.profile.confirmed_agreements: return HttpResponseRedirect(reverse('profiles:profiles_terms')) secret_key_valid = False if secret_key: secret_key_valid = character.is_editable_with_key(secret_key) else: secret_key = "" user_can_edit = (request.user.is_authenticated and character.player_can_edit( request.user)) or secret_key_valid if not character.stats_snapshot: context = {"character": character, "user_can_edit": user_can_edit} return render(request, 'characters/legacy_character.html', context) completed_games = [(x.relevant_game.end_time, "game", x) for x in character.completed_games() ] # completed_games() does ordering character_edit_history = [ (x.created_time, "edit", x) for x in character.contractstats_set.filter( is_snapshot=False).order_by("created_time").all()[1:] ] exp_rewards = [(x.created_time, "exp_reward", x) for x in character.experiencereward_set.filter( is_void=False).order_by("created_time").all()] events_by_date = list( merge(completed_games, character_edit_history, exp_rewards)) timeline = defaultdict(list) for event in events_by_date: if event[1] == "edit": phrases = event[2].get_change_phrases() if len(phrases): timeline[event[0].strftime("%d %b %Y")].append( (event[1], phrases)) else: timeline[event[0].strftime("%d %b %Y")].append( (event[1], event[2])) char_ability_values = character.stats_snapshot.abilityvalue_set.order_by( "relevant_ability__name").all() ability_value_by_id = {} char_value_ids = [x.relevant_ability.id for x in char_ability_values] primary_zero_values = [(x.name, x, 0) for x in Ability.objects.filter( is_primary=True).order_by("name").all() if x.id not in char_value_ids] all_ability_values = [] for x in char_ability_values: all_ability_values.append( (x.relevant_ability.name, x.relevant_ability, x.value)) ability_value_by_id[x.relevant_ability.id] = x.value ability_value_by_name = list(merge(primary_zero_values, all_ability_values)) unspent_experience = character.unspent_experience() exp_earned = character.exp_earned() exp_cost = character.exp_cost() equipment_form = EquipmentForm() bio_form = BioForm() num_journal_entries = character.num_journals if character.num_journals else 0 latest_journals = [] if num_journal_entries > 0: journal_query = Journal.objects.filter( game_attendance__attending_character=character.id).order_by( '-created_date') if request.user.is_anonymous or not request.user.profile.view_adult_content: journal_query = journal_query.exclude(is_nsfw=True) journals = journal_query.all() for journal in journals: if len(latest_journals) > 2: break if journal.player_can_view(request.user): latest_journals.append(journal) journal_cover = get_object_or_none(JournalCover, character=character.id) next_entry = get_characters_next_journal_credit( character) if user_can_edit else None show_more_home_games_warning = character.number_completed_games() > 3 \ and (character.number_completed_games_in_home_cell() < character.number_completed_games_out_of_home_cell()) available_gift = character.unspent_rewards().count() > 0 circumstance_form = None condition_form = None artifact_form = None world_element_initial_cell = character.world_element_initial_cell() world_element_cell_choices = None if user_can_edit: # We only need these choices if the user can edit, both for forms and for char sheet. world_element_cell_choices = character.world_element_cell_choices() circumstance_form = make_world_element_form( world_element_cell_choices, world_element_initial_cell) condition_form = make_world_element_form(world_element_cell_choices, world_element_initial_cell) artifact_form = make_world_element_form(world_element_cell_choices, world_element_initial_cell) artifacts = get_world_element_default_dict(world_element_cell_choices) for artifact in character.artifact_set.all(): artifacts[artifact.cell].append(artifact) artifacts = dict(artifacts) circumstances = get_world_element_default_dict(world_element_cell_choices) for circumstance in character.circumstance_set.all(): circumstances[circumstance.cell].append(circumstance) circumstances = dict(circumstances) conditions = get_world_element_default_dict(world_element_cell_choices) for condition in character.condition_set.all(): conditions[condition.cell].append(condition) conditions = dict(conditions) assets = character.stats_snapshot.assetdetails_set.all() liabilities = character.stats_snapshot.liabilitydetails_set.all() physical_attributes = character.get_attributes(is_physical=True) mental_attributes = character.get_attributes(is_physical=False) attribute_value_by_id = {} for attr in physical_attributes: attribute_value_by_id[ attr.relevant_attribute.id] = attr.val_with_bonuses() for attr in mental_attributes: attribute_value_by_id[ attr.relevant_attribute.id] = attr.val_with_bonuses() context = { 'character': character, 'user_can_edit': user_can_edit, 'health_display': character.get_health_display(), 'ability_value_by_name': ability_value_by_name, 'ability_value_by_id': ability_value_by_id, 'physical_attributes': physical_attributes, 'mental_attributes': mental_attributes, 'attribute_value_by_id': attribute_value_by_id, 'timeline': dict(timeline), 'tutorial': get_object_or_404(CharacterTutorial), 'battle_scar_form': BattleScarForm(), 'trauma_form': TraumaForm(prefix="trauma"), 'injury_form': InjuryForm(request.POST, prefix="injury"), 'exp_cost': exp_cost, 'exp_earned': exp_earned, 'unspent_experience': unspent_experience, 'equipment_form': equipment_form, 'bio_form': bio_form, 'secret_key': secret_key, 'secret_key_valid': secret_key_valid, 'num_journal_entries': num_journal_entries, 'journal_cover': journal_cover, 'next_entry': next_entry, 'latest_journals': latest_journals, 'show_more_home_games_warning': show_more_home_games_warning, 'available_gift': available_gift, 'circumstance_form': circumstance_form, 'condition_form': condition_form, 'artifact_form': artifact_form, 'artifacts_by_cell': artifacts, 'conditions_by_cell': conditions, 'circumstances_by_cell': circumstances, 'initial_cell': world_element_initial_cell, 'assets': assets, 'liabilities': liabilities, } return render(request, 'characters/view_pages/view_character.html', context)