Beispiel #1
0
def test_importcsv_no_drop(cli_runner, db, request):
    collection = "actionCards"
    csv_file = "{}/ressources/csv/action_cards.csv.example".format(BASE_DIR)

    # Inserting an action card beforehand
    action_card4 = ActionCardModel(
        cardNumber=4,
        name="action_card_name_4",
        category="action_card_category_4",
        type="action_card_type_4",
        key="action_card_key_4",
        sector="action_card_sector_4",
        cost=4,
    )
    action_card4.save()

    count = 0
    with open(csv_file, "r") as fr:
        reader = DictReader(fr)
        for _ in reader:
            count += 1

    result = cli_runner.invoke(importcsv, [collection, csv_file])
    assert ("Successfully inserted {} objects in collection {}".format(
        count, collection) in result.output)
    assert len(ActionCardModel.find_all()) == count + 1

    def teardown():
        ActionCardModel.drop_collection()

    request.addfinalizer(teardown)
def get_workshop(workshop_id) -> (dict, int):
    workshop = WorkshopModel.find_by_id(workshop_id=workshop_id)

    # Check if given workshop_id exists in DB
    if workshop is None:
        raise EntityNotFoundError

    # Append action cards to field model
    action_cards = ActionCardModel.find_all()
    workshop.model.actionCards = action_cards

    # Append action card batches from creator to field model
    action_cards_batches = ActionCardBatchModel.find_action_card_batches_by_coach(
        coach_id=workshop.coachId
    )
    workshop.model.actionCardBatches = action_cards_batches

    # Append carbon form answers
    carbon_form_answers = CarbonFormAnswersModel.find_all_by_workshop_id(workshop_id)
    carbon_form_answers = {
        cfa.participant.pk: cfa.answers for cfa in carbon_form_answers
    }  # Convert into dict
    for wp in workshop.participants:
        if wp.user.id in carbon_form_answers:
            wp.surveyVariables = carbon_form_answers[wp.user.id]

    return WorkshopDetailSchema().dump(workshop), 200
    def check_action_card_ids(self, data, many, **kwargs):
        existing_action_cards = {
            k.id: k.type for k in ActionCardModel.objects().only("actionCardId", "type")
        }

        for d in data:
            # Check that all ids are valid id
            for action_card_id in d["actionCardIds"]:
                if action_card_id not in existing_action_cards:
                    raise ValidationError(
                        "actionCardId {} does not exist. (Batch {})".format(
                            action_card_id, d["name"]
                        ),
                        "actionCardIds",
                    )
            # Check that each batch always are the same type
            if (
                len(
                    set(
                        [
                            existing_action_cards[action_card_id]
                            for action_card_id in d["actionCardIds"]
                        ]
                    )
                )
                > 1
            ):
                raise ValidationError(
                    "Action cards within the same batch must be of the same type. "
                    "(Batch {})".format(d["name"]),
                    "actionCardIds",
                )

        # Check that all the action ids are present
        missing_action_card_ids = set(
            [k for k in existing_action_cards.keys()]
        ) - set().union(*[d["actionCardIds"] for d in data])
        if len(missing_action_card_ids) > 0:
            raise ValidationError(
                "All actions cards need to be present. Missing {}".format(
                    ", ".join(missing_action_card_ids)
                ),
                "actionCardIds",
            )

        return data
Beispiel #4
0
def action_cards(db, request):
    action_card1 = ActionCardModel(
        cardNumber=1,
        name="action_card_name_1",
        category=ActionCardCategory.AWARENESS.value,
        type=ActionCardType.INDIVIDUAL.value,
        key="action_card_key_1",
        sector="action_card_sector_1",
        cost=1,
    )

    action_card2 = ActionCardModel(
        cardNumber=2,
        name="action_card_name_2",
        category=ActionCardCategory.ECOFRIENDLY_ACTION.value,
        type=ActionCardType.INDIVIDUAL.value,
        key="action_card_key_2",
        sector="action_card_sector_2",
        cost=2,
    )
    action_card3 = ActionCardModel(
        cardNumber=3,
        name="action_card_name_3",
        category=ActionCardCategory.SYSTEM.value,
        type=ActionCardType.COLLECTIVE.value,
        key="action_card_key_3",
        sector="action_card_sector_3",
        cost=3,
    )

    action_card1.save()
    action_card2.save()
    action_card3.save()

    def teardown():
        action_card1.delete()
        action_card2.delete()
        action_card3.delete()

    request.addfinalizer(teardown)

    return [action_card1, action_card2, action_card3]
Beispiel #5
0
 def teardown():
     ActionCardModel.drop_collection()
Beispiel #6
0
def get_all_action_cards() -> (dict, int):
    action_cards = ActionCardModel.find_all()
    return ActionCardSchema(many=True).dump(action_cards), 200