Example #1
0
def test_save_property_set_value_if_property_and_user_property_exist(app):
    # Given
    obj_to_save = []
    user = create_user(email='*****@*****.**',
                       username='******',
                       password='******')
    obj_to_save.append(user)
    question = Question()
    question.question_name = 'question_1'
    obj_to_save.append(question)
    BaseObject.check_and_save(*obj_to_save)

    user_property_obj = UserProperty()
    user_property_obj.user_id = user.id
    user_property_obj.question_id = question.id
    user_property_obj.value = float(12)
    BaseObject.check_and_save(user_property_obj)

    data = dict()
    data['question_1'] = "0.5"

    # When
    property_request = req_with_auth(email='*****@*****.**', password='******') \
        .post(API_URL + '/property', json=data)

    # Then
    assert property_request.status_code == 200

    user_property_obj = UserProperty.query. \
        filter_by(user_id=user.id). \
        filter_by(question_id=question.id). \
        first()

    assert user_property_obj is not None
    assert user_property_obj.value == 0.5
Example #2
0
def test_get_property_should_return_value_for_property_if_already_answered(
        app):
    # Given
    obj_to_save = []
    user = create_user(email='*****@*****.**',
                       username='******',
                       password='******')
    obj_to_save.append(user)
    question = Question()
    question.question_name = 'question_1'
    obj_to_save.append(question)
    BaseObject.check_and_save(*obj_to_save)
    user_property_obj = UserProperty()
    user_property_obj.user_id = user.id
    user_property_obj.question_id = question.id
    user_property_obj.value = float(0.5)
    BaseObject.check_and_save(user_property_obj)

    # When
    property_request = req_with_auth(email='*****@*****.**', password='******') \
        .get(API_URL + '/property')

    # Then
    assert property_request.status_code == 200
    content = property_request.json()
    assert 'question_1' in content
    assert content['question_1'] == 0.5
Example #3
0
def signup():
    data = request.json

    new_user = User(from_dict=request.json)
    new_user.id = None

    footprints = data.get('footprints')[0]
    BaseObject.check_and_save(new_user)

    objects_to_save = []
    for footprint in footprints.get('footprints'):
        footprint_obj = Footprint(from_dict=footprint)
        footprint_obj.user_id = int(new_user.get_id())
        objects_to_save.append(footprint_obj)

    # TODO: c'est pas beau mais c'était plus rapide :(
    answers = footprints.get('answers')
    for key, value in answers.items():
        property_obj = Property.query.filter_by(property_name=key).first()
        answer_obj = UserProperty()
        answer_obj.user_id = int(new_user.get_id())
        answer_obj.property_id = property_obj.id
        answer_obj.value = float(value)
        objects_to_save.append(answer_obj)

    BaseObject.check_and_save(*objects_to_save)

    login_user(new_user)

    return jsonify(new_user._asdict(include=USER_INCLUDES)), 201
Example #4
0
def signup():
    data = request.json

    new_user = User(from_dict=request.json)
    new_user.id = None

    footprints = data.get('footprints')[0]
    BaseObject.check_and_save(new_user)

    objects_to_save = []
    for footprint in footprints.get('footprints'):
        if footprint.get('type') == 'home_mates':
            new_user.home_mates = int(footprint.get('value'))
            BaseObject.check_and_save(new_user)
        else:
            footprint_obj = Footprint(from_dict=footprint)
            footprint_obj.user_id = int(new_user.get_id())
            objects_to_save.append(footprint_obj)

    answers = footprints.get('answers')
    details = footprints.get('details')

    for footprint_detail in details:
        logger.info(footprint_detail)
        footprint_details = FootprintDetails()
        footprint_details.user_id = int(new_user.get_id())
        footprint_details.category = footprint_detail['category']
        footprint_details.value = float(footprint_detail['value'])
        footprint_details.type = FootprintType({'label': footprint_detail['type']})
        objects_to_save.append(footprint_details)

    for key, value in answers.items():
        property_value = Answer.query.filter_by(answer_name=key).first()
        if property_value is None:
            logger.info("Form seems to be broken: %s" % key)
            continue

        answer_obj = UserProperty()
        answer_obj.user_id = int(new_user.get_id())
        answer_obj.answer_id = int(property_value.id)
        objects_to_save.append(answer_obj)

    BaseObject.check_and_save(*objects_to_save)
    login_user(new_user)

    return jsonify(new_user._asdict(include=USER_INCLUDES)), 201
Example #5
0
    def execute(self, data: dict, user_id: int):
        object_to_save = []
        for key, value in data.items():
            property_obj = Property.query.filter_by(property_name=key).first()
            if property_obj is not None\
                    and value != "":
                user_property_obj = UserProperty.query.\
                    filter_by(user_id=user_id).\
                    filter_by(property_id=property_obj.id).\
                    first()
                if user_property_obj is None:
                    user_property = UserProperty()
                    user_property.user_id = user_id
                    user_property.property_id = property_obj.id
                    user_property.value = float(value)
                    object_to_save.append(user_property)
                else:
                    user_property_obj.value = float(value)
                    object_to_save.append(user_property_obj)

        BaseObject.check_and_save(*object_to_save)
Example #6
0
    def execute(self, data: dict, user_id: int):
        object_to_save = []
        for key, value in data.items():
            question = Question.query.filter_by(question_name=key).first()
            if question is not None \
                    and value != "":
                user_property = UserProperty.query. \
                    filter_by(user_id=user_id). \
                    filter_by(question_id=question.id). \
                    first()
                if user_property is None:
                    user_property = UserProperty()
                    user_property.user_id = user_id
                    user_property.question_id = question.id
                    user_property.value = float(value)
                    object_to_save.append(user_property)
                else:
                    user_property.value = float(value)
                    object_to_save.append(user_property)

        BaseObject.check_and_save(*object_to_save)
        for user_property in object_to_save:
            HistoryUserProperties().execute(user_property.id,
                                            user_property.value)