예제 #1
0
def create_exercise(request, series_id=None):
    request_body = prepare_exercise(dict(request.data))

    query_params = request.query_params
    if 'session_id' not in query_params:
        raise UnauthorizedException("You need to be logged to create training")

    if series_id is None:
        raise BadRequestException("No series id provided")
    user = perform_get(User.objects.get, session_id=query_params['session_id'])
    series = perform_get(Series.objects.get, series_id=series_id)

    if user is None:
        raise UnauthorizedException("You need to be logged to create training")

    if series is None:
        raise BadRequestException("Invalid series id")

    training = perform_get(Training.objects.get, training_id=series.training_id.training_id)

    if training.created_by.user_id != user.user_id:
        raise ForbiddenException("You don`t have permission to modify that training")

    request_body['series_id'] = series_id
    if 'photo' not in request_body or request_body['photo'] == '':
        request_body['photo'] = None

    created_exercise = create_new_exercise(request_body)

    if created_exercise is None:
        raise BadRequestException("Invalid exercise format")

    return Response(created_exercise, status.HTTP_200_OK)
예제 #2
0
def create_series(request, training_id=None):
    session_id = request.query_params['session_id']
    training = perform_get(Training.objects.get, training_id=training_id)
    request_body = dict(request.data)

    if session_id is None:
        raise UnauthorizedException("You need to be logged")

    if training is None:
        raise BadRequestException("Invalid training id")

    user = perform_get(User.objects.get, session_id=session_id)

    if user is None:
        raise UnauthorizedException("You need to be logged")

    if user.user_id != training.created_by.user_id:
        raise ForbiddenException(
            "You don`t have permission to modify that training")

    request_body['training_id'] = training_id
    created_series = create_new_series(request_body)

    if created_series is None:
        raise BadRequestException("Invalid exercise format")

    return Response(created_series, status.HTTP_200_OK)
예제 #3
0
def get_training_by_args(**kwargs):
    training = perform_get(Training.objects.get, **kwargs)

    if training is None:
        return None

    serializer = TrainingSerializer(training)
    return serializer.data
예제 #4
0
def logout_user_by_id(session_id):
    user = perform_get(User.objects.get, session_id=session_id)

    if user is None:
        return

    user.session_id = None
    user.save(update_fields=['session_id'])
예제 #5
0
def get_raw_user_by_args(**kwargs):
    user = perform_get(User.objects.get, **kwargs)

    if user is None:
        return None

    serializer = UserRawSerializer(user, many=False)
    return serializer.data
예제 #6
0
def delete_user_by_id(user_id):
    user = perform_get(User.objects.get, user_id=user_id)

    if user is None:
        return False

    user.delete()

    return True
예제 #7
0
def update_user_photo(session_id, avatar):
    user = perform_get(User.objects.get, session_id=session_id)

    if user is None:
        return None

    user.avatar = avatar
    user.save()
    return user
예제 #8
0
def delete_training(training_id):
    training = perform_get(Training.objects.get, training_id=training_id)

    if training is None:
        return None

    training.delete()

    return True
예제 #9
0
def login_user_by_id(user_id, new_session_id):
    user = perform_get(User.objects.get, user_id=user_id)

    if user is None:
        return None

    user.session_id = new_session_id
    user.save(update_fields=['session_id'])

    return user
예제 #10
0
def is_password_matching(password, **kwargs):
    db_user = perform_get(User.objects.get, **kwargs)

    if db_user is None:
        return False

    password_and_salt = password + db_user['salt']
    hashed_password_and_salt = hashlib.sha256(
        password_and_salt.encode(encoding="utf-8")).hexdigest()

    return hashed_password_and_salt == db_user['password']
예제 #11
0
def update_user_by_id(user_id, **kwargs):
    user = perform_get(User.objects.get, user_id=user_id)

    if user is None:
        return None

    for k, v in kwargs.items():
        setattr(user, k, v)
    user.save(update_fields=kwargs.keys())

    return user
예제 #12
0
def update_training(training_id, **kwargs):
    training = perform_get(Training.objects.get, training_id=training_id)

    if 'training_id' in kwargs:
        del kwargs['training_id']

    if training is None:
        return None

    for k, v in kwargs.items():
        setattr(training, k, v)

    training.save(update_fields=kwargs.keys())

    return training