def character_from_post(user, POST): char_form = make_character_form(user)(POST) if char_form.is_valid(): new_character = char_form.save(commit=False) new_character.pub_date = timezone.now() new_character.edit_date = timezone.now() if user.is_authenticated: new_character.player = user new_character.save() __save_stats_diff_from_post(POST=POST, new_character=new_character, user=user) new_character.regen_stats_snapshot() if user.is_authenticated: cell = char_form.cleaned_data['cell'] if cell != "free": new_character.cell = cell new_character.save() charon_coin_form = __get_charon_coin_form(user, None, POST=POST) if charon_coin_form and charon_coin_form.is_valid(): if charon_coin_form.cleaned_data["spend_coin"]: new_character.use_charon_coin() return new_character else: logger.error('Bad Character form: %s', str(char_form.errors)) raise ValueError("Invalid char_form")
def update_character_from_post(user, POST, existing_character): char_form = make_character_form(user, existing_character)( POST, instance=existing_character) if char_form.is_valid(): char_form.save(commit=False) existing_character.edit_date = timezone.now() if user.is_authenticated and char_form.cleaned_data['cell']: existing_character.cell = char_form.cleaned_data['cell'] existing_character.save() if existing_character.private != char_form.cleaned_data['private']: for power_full in existing_character.power_full_set.all(): power_full.set_self_and_children_privacy( is_private=char_form.cleaned_data['private']) if existing_character.stats_snapshot: __save_stats_diff_from_post(POST=POST, existing_character=existing_character, user=user) else: __save_stats_diff_from_post(POST=POST, new_character=existing_character, user=user) existing_character.grant_initial_source_if_required() existing_character.regen_stats_snapshot() charon_coin_form = __get_charon_coin_form(user, existing_character, POST=POST) if charon_coin_form and charon_coin_form.is_valid(): if charon_coin_form.cleaned_data["spend_coin"]: existing_character.use_charon_coin() else: existing_character.refund_coin() else: logger.error('Bad Character form: %s', str(char_form.errors)) raise ValueError("invalid edit char_form")
def get_edit_context(user, existing_character=None, secret_key=None): char_form = make_character_form( user, existing_character)(instance=existing_character) AttributeFormSet = formset_factory(AttributeForm, extra=0) AbilityFormSet = formset_factory(AbilityForm, extra=1) attributes = Attribute.objects.order_by('name') tutorial = get_object_or_404(CharacterTutorial) if existing_character and existing_character.stats_snapshot: asset_formsets = __get_asset_formsets(existing_character) liability_formsets = __get_liability_formsets(existing_character) ability_formset = __get_ability_formset_for_edit( existing_character, AbilityFormSet) attribute_formset = __get_attribute_formset_for_edit( existing_character, AttributeFormSet) limit_formset = __get_limit_formset_for_edit(existing_character) source_formset = __get_source_formset_for_edit(existing_character) show_tutorial = False else: asset_formsets = __get_asset_formsets() liability_formsets = __get_liability_formsets() attribute_formset = AttributeFormSet(initial=[{ 'attribute_id': x.id, 'value': 1, 'attribute': x } for x in attributes], prefix="attributes") default_abilities = Ability.objects.filter( is_primary=True).order_by('name') ability_formset = AbilityFormSet(initial=[{ 'ability_id': x.id, 'value': 0, 'ability': x } for x in default_abilities], prefix="abilities") primary_limits = Limit.objects.filter(is_primary=True).order_by( '-is_default', 'name') LimitFormSet = formset_factory(LimitForm, extra=3) limit_formset = LimitFormSet(initial=[{ 'limit': x, 'selected': x.is_default } for x in primary_limits], prefix="limits") source_formset = () show_tutorial = False if user.is_authenticated and user.character_set.all( ) else True charon_coin_form = None if user.is_authenticated: charon_coin_form = __get_charon_coin_form(user, existing_character, POST=None) context = { 'char_form': char_form, 'attribute_formset': attribute_formset, 'ability_formset': ability_formset, 'asset_formsets': asset_formsets, 'liability_formsets': liability_formsets, 'limit_formset': limit_formset, 'charon_coin_form': charon_coin_form, 'tutorial': tutorial, 'character': existing_character, 'source_formset': source_formset, 'unspent_experience': existing_character.unspent_experience() \ if existing_character and existing_character.stats_snapshot and existing_character.stats_snapshot.exp_cost \ else EXP_NEW_CHAR, 'exp_costs': {"EXP_ADV_COST_ATTR_MULTIPLIER": EXP_ADV_COST_ATTR_MULTIPLIER, "EXP_ADV_COST_SKILL_MULTIPLIER": EXP_ADV_COST_SKILL_MULTIPLIER, "EXP_COST_QUIRK_MULTIPLIER": EXP_COST_QUIRK_MULTIPLIER, "EXP_ADV_COST_SOURCE_MULTIPLIER": EXP_ADV_COST_SOURCE_MULTIPLIER,}, 'secret_key': secret_key if secret_key else "", 'show_tutorial': show_tutorial, } return context
def get_edit_context(user, existing_character=None, secret_key=None, cell=None): char_form = make_character_form(user, existing_character, supplied_cell=cell)(instance=existing_character) AttributeFormSet = formset_factory(AttributeForm, extra=0) tutorial = get_object_or_404(CharacterTutorial) max_ability_score = existing_character.ability_maximum() if existing_character and existing_character.stats_snapshot else 5 AbilityFormSet = formset_factory(get_ability_form(max_ability_score), extra=1) if existing_character and existing_character.stats_snapshot: asset_formsets = __get_asset_formsets(existing_character) liability_formsets = __get_liability_formsets(existing_character) ability_formset = __get_ability_formset_for_edit(existing_character, AbilityFormSet) attribute_formset = __get_attribute_formset_for_edit(existing_character, AttributeFormSet) limit_formset = __get_limit_formset_for_edit(existing_character) source_formset = __get_source_formset_for_edit(existing_character) show_tutorial = False else: attributes = Attribute.objects.filter(is_deprecated=False).order_by('name') asset_formsets = __get_asset_formsets() liability_formsets = __get_liability_formsets() attribute_formset = AttributeFormSet( initial=[{'attribute_id': x.id, 'value': 1, 'attribute': x} for x in attributes], prefix="attributes") default_abilities = Ability.objects.filter(is_primary=True).order_by('name') ability_formset = AbilityFormSet( initial=[{'ability_id': x.id, 'value': 0, 'ability': x} for x in default_abilities], prefix="abilities") primary_limits = Limit.objects.filter(is_primary=True).order_by('-is_default', 'name') LimitFormSet = formset_factory(LimitForm, extra=3) limit_formset = LimitFormSet( initial=[{'limit': x, 'selected': x.is_default} for x in primary_limits], prefix="limits" ) source_formset = () show_tutorial = False if user.is_authenticated and user.character_set.all() else True charon_coin_form = None ported_character_form = None if user.is_authenticated: charon_coin_form = __get_charon_coin_form(user, existing_character, POST=None) ported_character_form = __get_ported_character_form(user, existing_character, POST=None) cell_info = {} for cell1 in char_form.fields["cell"].queryset.all(): sheet_blurb = cell1.setting_create_char_info if cell1.setting_create_char_info \ else linebreaks(cell1.setting_summary) if cell1.setting_summary else None cell_info[cell1.pk] = [cell1.setting_sheet_blurb] if sheet_blurb: cell_info[cell1.pk].append(sheet_blurb) context = { 'char_form': char_form, 'attribute_formset': attribute_formset, 'ability_formset': ability_formset, 'max_ability_score': max_ability_score, 'asset_formsets': asset_formsets, 'liability_formsets': liability_formsets, 'limit_formset': limit_formset, 'charon_coin_form': charon_coin_form, 'ported_character_form': ported_character_form, 'tutorial': tutorial, 'character': existing_character, 'source_formset': source_formset, 'unspent_experience': existing_character.unspent_experience() \ if existing_character and existing_character.stats_snapshot and existing_character.stats_snapshot.exp_cost \ else EXP_NEW_CHAR, 'exp_costs': {"EXP_ADV_COST_ATTR_MULTIPLIER": EXP_ADV_COST_ATTR_MULTIPLIER, "EXP_ADV_COST_SKILL_MULTIPLIER": EXP_ADV_COST_SKILL_MULTIPLIER, "EXP_COST_QUIRK_MULTIPLIER": EXP_COST_QUIRK_MULTIPLIER, "EXP_ADV_COST_SOURCE_MULTIPLIER": EXP_ADV_COST_SOURCE_MULTIPLIER,}, 'secret_key': secret_key if secret_key else "", 'show_tutorial': show_tutorial, 'cell': cell, 'cell_info': cell_info, } return context