예제 #1
0
def delete_project(r, project_id):
    project = get_project_by_id(project_id)
    # get bearer token from auth header
    auth_header = request.headers.get("authorization")
    access_token = auth_header[len("Bearer "):]

    # get user id to compare against owner id.
    user_info = get_user_info(access_token)
    user_id = user_info["user_id"]

    comments = get_comments_for_project(project_id)
    delete_check = delete_owners_project(project_id, user_id)

    if delete_check:
        status_code = 200
        response_dict = {
            "project_id": project_id,
            "owner_id": project['owner_id'],
            "owner_username": project['owner_username'],
            "project_name": project['project_name'],
            "comments": [dict(c) for c in comments]
        }
    else:
        status_code = 403
        response_dict = {
            "message":
            "You are not the owner\
                          of project " + project_id
        }
    return Response(json.dumps(response_dict),
                    status=status_code,
                    mimetype='application/json')
예제 #2
0
def create_comment(r, project_id):
    # get bearer token from auth header
    auth_header = request.headers.get("authorization")
    access_token = auth_header[len("Bearer "):]

    # get username and user id to respond with
    user_info = get_user_info(access_token)
    username = user_info["username"]
    user_id = user_info["user_id"]

    data = json.loads(r.data)

    # Create the comment id.
    comment_id = str(uuid.uuid1())

    # Insert the comment
    add_comment(comment_id, user_id, username, data['message'], project_id)

    response_dict = {
        "comment_id": comment_id,
        "commenter_id": user_id,
        "commenter_username": username,
        "message": data['message']
    }
    return Response(json.dumps(response_dict),
                    status=200,
                    mimetype='application/json')
예제 #3
0
def project_post(r):
    """
    POST request to projects.
    Reponse contains
    project_id: the project id of the submitted project
    owner_id: owner of the submitted project.
    owner_username: username of the owner
    project_name: name of the submitted project
    comments: Comments on the project, initialized to an empty list.
    """
    # get bearer token from auth header
    auth_header = request.headers.get("authorization")
    access_token = auth_header[len("Bearer "):]

    # get username and user id to respond with
    try:
        user_info = get_user_info(access_token)
    except ValueError:
        return bad_auth()

    username = user_info["username"]
    user_id = user_info["user_id"]
    print("Posting user ID is {}.".format(user_id))

    data = json.loads(r.data)
    # Make a unique identifier for the new project
    project_id = str(uuid.uuid1())
    if 'project_name' not in data:
        return bad_request()
    project_name = data['project_name']

    # Actually add the project to the database
    add_project(project_id, project_name, username, user_id)

    response_dict = {
        "message": "Post Projects",
        "project_id": project_id,
        "owner_id": user_id,
        "owner_username": username,
        "project_name": project_name,
        "comments": []
    }
    return Response(json.dumps(response_dict),
                    status=200,
                    mimetype='application/json')
예제 #4
0
def index():
    if auth.is_logged_in():
        if request.method == 'GET':
            user_info = auth.get_user_info()
            global user_data
            user_data = {
                'email': user_info['email'],
                'name': user_info['name']
            }

            if len(list(collection.find({"email": user_info['email']}))) < 1:
                #For the condition that thte user hasnt signed up, ask for phone number
                return open('get_number.html').read()
                if request.method == 'POST':
                    result = request.form['number']
                    user_data['phone'] = result
                    db.users.insert_one(user_data)
                    return list(collection.find({"email":
                                                 user_info['email']}))[0]
                return 'You are Logged In'
    return 'You are not logged in.'
예제 #5
0
def example():
    """
    Basic example of GET to / using Flask
    Does not handle missing or invalid Access Tokens
    """
    if request.method == "GET":
        # get bearer token from auth header
        auth_header = request.headers.get("authorization")
        access_token = auth_header[len("Bearer "):]

        # get username and num_projects to respond with
        user_info = get_user_info(access_token)
        username = user_info["username"]
        num_projects = get_num_projects()
        # respond
        response_dict = {
            "message":
            ("Hello {}, there are {} projects in the database!".format(
                username, num_projects))
        }
        return Response(json.dumps(response_dict),
                        status=200,
                        mimetype='application/json')