示例#1
0
def current_user_change_password():
    creds = current_user._get_current_object()

    if request.method == 'PATCH':

        data = request.get_json()
        if not creds.verify_pass(data['current_password']):
            response = {"error": "Current password is wrong", "code": 1}
            return jsonify(response), 400

        new_password = data['new_password']
        if new_password is None or new_password == "":
            response = {"error": "The new password cannot be empty", "code": 2}
            return jsonify(response), 400

        confirm_password = data['confirm_new_password']
        if new_password != confirm_password:
            response = {"error": "Passwords don't match", "code": 3}
            return jsonify(response), 400

        creds.password = user_credentials.__model__.hash_pass(new_password)
        try:
            creds.save(validate=False
                       )  # FIXME This is related to the MongoEngine bug.
        except ValidationError as e:
            response = {"error": "Could not update user", "message": e.message}
            return jsonify(response), 400

        return creds
示例#2
0
def absorb_user():
    data = request.get_json()
    try:
        local_server = ObjectId(data.get('local_server', None))
    except:
        local_server = None

    username = data.get('username', None)
    password = data.get('password', None)

    try:
        creds = user_credentials.get(local_server=local_server,
                                     username=username,
                                     password=password)
    except DoesNotExist as e:
        return jsonify(code=1, error=e.message), 404
    except Exception as e:
        return jsonify(code=2, error=e.message), 500
    else:
        user = current_user.user
        try:
            user.phagocyte(other=creds.user,
                           self_credentials=current_user._get_current_object())
        except Exception as e:
            return jsonify(code=3, error=e.message), 500
        else:
            return user
示例#3
0
def current_user_dashboard():
    creds = current_user._get_current_object()
    user = current_user.user

    return jsonify(data=user,
                   username=creds.username,
                   courses_info=user.courses_info(analytics=True))
示例#4
0
def post_exercise_attempt():
    exercise_id = request.get_json()['exercise']
    exercise = exercise_resources.get_or_404(exercise_id)

    attempt = exercise_attempts.__model__.init_with_exercise(exercise)
    attempt.credentials = current_user._get_current_object()
    attempt.save()

    return attempt, 201
示例#5
0
def post_skill_validation_attempt():
    skill_id = request.get_json()['skill']
    skill = skills.get_or_404(skill_id)

    print "CREATING skill validation attempt for skill {skill}".format(
        skill=skill.id)

    attempt = skill_validation_attempts.__model__.init_with_skill(skill)
    attempt.credentials = current_user._get_current_object()
    attempt.save()

    return attempt, 201
示例#6
0
def record_visited_track_analytic():
    """ Creates a new VisitedDashboardActivity object which is used to track analytics on the platform
    :param user_id: the id of the user from which we want to get the dashboard
    """
    try:
        data = request.get_json()
        track = tracks.get_or_404(data['track'])
        credentials = current_user._get_current_object()
        obj = visited_tracks.create(credentials=credentials, track=track)
    except Exception as e:
        return jsonify(error=e.message), 400
    else:
        return obj, 201
示例#7
0
def record_visited_resource_analytic():
    """ Creates a new VisitedDashboardActivity object which is used to track analytics on the platform
    :param user_id: the id of the user from which we want to get the dashboard
    """
    try:
        data = request.get_json()
        resource = resources.get_or_404(data['resource'])
        credentials = current_user._get_current_object()
        visited_resource, achievements = credentials.add_visited_resource(
            resource=resource)

    except Exception as e:
        return jsonify(error=e.message), 400
    else:
        return jsonify(data=visited_resource, achievements=achievements), 201
示例#8
0
def mark_video_resource_watched():
    """
    Set the is_validated attribute for the given video resource
    :return: the updated resource data
    """
    data = request.get_json()
    resource = resources.get_or_404(data['resource'])
    if video_resources._isinstance(
            resource) or external_video_resources._isinstance(resource):
        try:
            credentials = current_user._get_current_object()
            achievements = credentials.add_completed_resource(
                resource=resource)
        except Exception as e:
            return jsonify(error=e.message), 400
        else:
            return jsonify(achievements=achievements), 201

    abort(404)
示例#9
0
def record_misc_analytic():
    """ Creates a new MiscActivity object which is used to track analytics on the platform
    :param misc_type: the type of the analytic
    """

    data = request.get_json()
    obj = misc_activities.new(**data)
    try:
        verify_jwt()
    except:
        pass
    else:
        obj.credentials = current_user._get_current_object()

    try:
        obj.save()
    except Exception as e:
        return jsonify(error=e.message), 400
    else:
        return obj, 201
示例#10
0
def current_user_info():
    creds = current_user._get_current_object()
    user = current_user.user

    if request.method == 'GET':
        return jsonify(data=user,
                       username=creds.username,
                       courses_info=user.courses_info())

    elif request.method == 'PATCH':

        data = request.get_json()
        user.full_name = data.get('full_name')
        user.email = data.get('email')
        user.country = data.get('country')
        user.occupation = data.get('occupation')
        user.organization = data.get('organization')
        try:
            user.save()
        except ValidationError as e:
            message = "Could not update user: %s" % e.message
            return jsonify(error=message), 400

        return user
示例#11
0
def is_anonymous():
    """
    Check if local proxy object is set
    :return: Boolean
    """
    return current_user._get_current_object() is None