Esempio n. 1
0
def available_traits (request, id):
    character = session.query(Character).get(int(id))

    response = storyteller_or_editing_owner(request, character, 'edit')
    if response:
        return response

    chronicle_ids = [c.id for c in character.chronicle.all_chronicles]
    availabilities = calculate_availabilities(
        character,
        session.query(Trait).filter(
            Trait.chronicle_id.in_(chronicle_ids)
        )
    )

    return JsonResponse(
        {
            'availabilities': availabilities,
        },
        encoder = ModelEncoder
    )
Esempio n. 2
0
    def recalculate_access (self, trait):
        affected_traits = set()
        chronicle_ids = [c.id for c in self.character.chronicle.all_chronicles]

        # Rule: character-has-trait
        for rule in session.query(CharacterHasTrait).filter(
            CharacterHasTrait.chronicle_id.in_(chronicle_ids),
            CharacterHasTrait.other_trait_id == trait.id
        ):
            affected_traits.add(rule.trait)

        # Rule: character-does-not-have-trait
        for rule in session.query(CharacterDoesNotHaveTrait).filter(
            CharacterDoesNotHaveTrait.chronicle_id.in_(chronicle_ids),
            CharacterDoesNotHaveTrait.other_trait_id == trait.id
        ):
            affected_traits.add(rule.trait)

        # Recalculate availabilities for affected traits.
        availabilities = calculate_availabilities(self.character, affected_traits)

        return availabilities
Esempio n. 3
0
def update_character_summary (request, id):
    character = session.query(Character).get(int(id))

    response = storyteller_or_editing_owner(request, character, 'edit')
    if response:
        return response

    chronicle_ids = [c.id for c in character.chronicle.all_chronicles]
    affected_traits = set()
    data = json.loads(request.body.decode('ascii'))

    for key, value in data.items():
        column = Character.__table__.columns[key]

        if len(column.foreign_keys) == 0:
            old_value = getattr(character, key)
            new_value = value

            # TODO (Emery): Check rules for character_summary attributes. (Name, last_edited, ...)

        else:
            TraitType = get_referenced_model(column)

            key = key[:-3]  # assumes key in format "<key>_id"
            old_value = getattr(character, key)
            new_value = None if value is None else session.query(TraitType).get(value)

            if old_value:
                changing_trait_ids = (old_value.id,)
            else:
                changing_trait_ids = tuple()
            if new_value:
                changing_trait_ids += (new_value.id,)

            # Rule: character-has-trait
            for rule in session.query(CharacterHasTrait).filter(
                CharacterHasTrait.chronicle_id.in_(chronicle_ids),
                CharacterHasTrait.other_trait_id.in_(changing_trait_ids)
            ):
                affected_traits.add(rule.trait)

            # Rule: character-does-not-have-trait
            for rule in session.query(CharacterDoesNotHaveTrait).filter(
                CharacterDoesNotHaveTrait.chronicle_id.in_(chronicle_ids),
                CharacterDoesNotHaveTrait.other_trait_id.in_(changing_trait_ids)
            ):
                affected_traits.add(rule.trait)

        # Set attribute on character.
        setattr(character, key, new_value)

    session.flush()

    # Recalculate availabilities for affected traits.
    availabilities = calculate_availabilities(character, affected_traits)

    return JsonResponse(
        {
            'character': character,
            'availabilities': availabilities,
        },
        encoder = ModelEncoder
    )