Exemple #1
0
def leave():
    if request.method == 'POST':
        data = request.get_json(silent=True)
        email = data['user_email']
        email = email.strip().lower()
        user_in_a_team = coll("users").find_one({
            "_id": email,
            "hasateam": True
        })
        if not user_in_a_team:
            return return_resp(400, "User doesn't have a tram")
        team_name = coll("teams").find_one({"members": {
            "$all": [email]
        }}, {"_id"})['_id']
        team_size = len(coll("teams").find_one({"_id": team_name})['members'])
        if team_size == 1:
            coll("teams").delete_one({"_id": team_name})
        else:
            coll("teams").update_one({"_id": team_name},
                                     {"$pull": {
                                         "members": email
                                     }})
            coll("teams").update_one({"_id": team_name},
                                     {"$set": {
                                         "complete": False
                                     }})
        coll("users").update_one({"_id": email}, {"$set": {"hasateam": False}})
        return return_resp(200, "Success")
Exemple #2
0
 def wrapper():
     if config.ENABLE_FEATURE[feature] == 1:
         return fn()
     elif config.ENABLE_FEATURE[feature] == 0:
         return return_resp(501, "Feature is disabled")
     else:
         return return_resp(502, "Wrong Feature value")
Exemple #3
0
def create_team():
    if request.method == 'POST':
        data = request.get_json(silent=True)
        email = data['user_email']
        email = email.strip().lower()
        if not data or 'name' not in data or 'desc' not in data or 'skills' not in data or not data['name'] or not data['desc'] or not data['skills']:
            return {"statusCode": 400, "body": "Required info not found"}
        team_name = data['name'].strip().lower()
        team_desc = data['desc'].strip().lower()
        partner_skills = data['skills']
        formatted_skills = format_string(partner_skills)
        formatted_prizes = []
        if 'prizes' in data:
            prizes = data['prizes']
            formatted_prizes = format_string(prizes)
        team_exist = coll("teams").find_one({"_id": str(team_name)})
        user_exists = coll("users").find_one({"_id": email})
        if not user_exists:
            return return_resp(403, "Invalid user")
        if team_exist:
            return return_resp(401, "Invalid name")
        else:
            user_in_a_team = coll("users").find_one({"_id": email, "hasateam": True})
            if user_in_a_team:
                return return_resp(402, "User in a team")
            else:
                coll("users").update_one({"_id": email}, {"$set": {"hasateam": True}})
                coll("teams").insert({"_id": team_name, "members": [email], "desc": team_desc, "partnerskills": formatted_skills, "prizes": formatted_prizes, "complete": False, "interested": []})
                return return_resp(200, "Success")
Exemple #4
0
def return_open_teams(search):
    if search is None:
        available_teams = coll("teams").find({"complete": False})
        all_open_teams = []
        for x in available_teams:
            all_open_teams.append(x)
        if not all_open_teams:
            return return_resp(400, "No open teams")
        return return_resp(200, all_open_teams)
    else:
        search = search.strip.lower()
        available_teams = coll("teams").find({
            "complete":
            False,
            "$or": [{
                "desc": {
                    "$regex": ".*" + search + ".*"
                }
            }, {
                "partnerskills": {
                    "$regex": ".*" + search + ".*"
                }
            }, {
                "prizes": {
                    "$regex": ".*" + search + ".*"
                }
            }]
        })
        all_open_teams = []
        for x in available_teams:
            all_open_teams.append(x)
        if not all_open_teams:
            return return_resp(400, "No open teams")
        return return_resp(200, all_open_teams)
Exemple #5
0
def confirm():
    if request.method == 'POST':
        data = request.get_json(silent=True)
        email = data['user_email']
        email = email.strip().lower()
        if not data or 'email' not in data or not data['email']:
            return return_resp(401, "Missing inf")
        hacker = data['email'].strip().lower()
        team_name = coll("teams").find_one({"members": {
            "$all": [email]
        }}, {"_id"})['_id']
        team = coll("teams").find_one({"_id": team_name})
        team_members = team['members']
        complete = coll("teams").find_one({"_id": team_name, "complete": True})
        if len(team_members) >= 4 or complete:
            return return_resp(402, "Team Complete")
        coll("users").update_one({"_id": hacker}, {"$set": {"hasateam": True}})
        coll("users").update_one({"_id": hacker},
                                 {"$pull": {
                                     "potentialteams": team_name
                                 }})
        coll("teams").update_one({"_id": team_name},
                                 {"$push": {
                                     "members": hacker
                                 }})
        coll("teams").update_one({"_id": team_name},
                                 {"$pull": {
                                     "interested": hacker
                                 }})
        return return_resp(200, "Success")
def get_team_recommendations():
    if request.method == 'GET':
        data = request.get_json(silent=True)
        email = data['user_email']
        email = email.strip().lower()
        user = coll("users").find_one({"_id": email})
        if not user:
            return return_resp(403, "Invalid user")
        user_in_a_team = coll("users").find_one({
            "_id": email,
            "hasateam": True
        })
        if user_in_a_team:
            return return_resp(402, "User in a team")
        if 'skills' not in user or not user['skills']:
            return return_resp(400, "No recommendations found")
        if 'prizes' not in user or not user['prizes']:
            prizes = []
        else:
            prizes = user['prizes']
        skills = user['skills']
        names = set()
        matches = []
        for skill in skills:
            match = coll("teams").aggregate([{
                "$match": {
                    "complete": False,
                    "partnerskills": {
                        "$all": [skill]
                    }
                }
            }])
            if not match:
                continue
            for m in match:
                if m['_id'] not in names:
                    names.add(m['_id'])
                    matches.append(m)

        for prize in prizes:
            match = coll("teams").aggregate([{
                "$match": {
                    "complete": False,
                    "pries": {
                        "$all": [prize]
                    }
                }
            }])
            if not match:
                continue
            for m in match:
                if m['_id'] not in names:
                    names.add(m['_id'])
                    matches.append(m)
        if not matches:
            return return_resp(400, "No recommendations found")
        else:
            return return_resp(200, matches)
Exemple #7
0
 def wrapped():
     data = request.get_json(force=True)
     if not data or 'user_email' not in data or not data[
             'user_email'] or 'token' not in data or not data['token']:
         return return_resp(408, "Missing email or token")
     email = data['user_email']
     token = data['token']
     email = email.strip().lower()
     if call_validate_endpoint(email, token) != 200:
         return return_resp(404, "Invalid request")
     else:
         return fn()
Exemple #8
0
def add_member():
    if request.method == 'POST':
        data = request.get_json(silent=True)
        email = data['user_email']
        email = email.strip().lower()
        if not data or 'email' not in data or not data['email']:
            return return_resp(400, "Required info not found")
        partner_email = data['email'].strip().lower()
        dir_token = call_auth_endpoint()
        if dir_token == 400:
            return return_resp(401, "auth endpoint failed")
        if get_name(dir_token, partner_email) == 400:
            return return_resp(402, "Partner doesn't have a hackru account")
        team = coll("teams").find_one({"members": {"$all": [email]}})
        if not team:
            return return_resp(405, "User not in a team")
        team_name = team['_id']
        team_size = len(team['members'])
        team_full = coll("teams").find_one({
            "_id": team_name,
            "complete": True
        })
        if team_full or team_size >= 4:
            return return_resp(403, "Team complete")
        user_exist = coll("users").find_one({"_id": partner_email})
        if not user_exist:
            coll("users").insert({
                "_id": partner_email,
                "hasateam": True,
                "skills": [],
                "prizes": []
            })
            coll("teams").update_one({"_id": team_name},
                                     {"$push": {
                                         "members": partner_email
                                     }})
            return return_resp(200, "Success")
        else:
            partner_in_a_team = coll("users").find_one({
                "_id": partner_email,
                "hasateam": True
            })
            if not partner_in_a_team:
                coll("users").update_one({"_id": partner_email},
                                         {"$set": {
                                             "hasateam": True
                                         }})
                coll("teams").update_one({"_id": team_name},
                                         {"$push": {
                                             "members": partner_email
                                         }})
                if team_size == 4:
                    coll("teams").update_one({"_id": team_name},
                                             {"$set": {
                                                 "complete": True
                                             }})
                return return_resp(200, "Success")
            else:
                return return_resp(406, "Partner in a team")
Exemple #9
0
def mark_team_complete():
    if request.method == 'POST':
        data = request.get_json(silent=True)
        email = data['user_email']
        email = email.strip().lower()
        team = coll("teams").find_one({"members": {"$all": [email]}})
        if not team:
            return return_resp(401, "User not in a team")
        team_name = team['_id']
        team_complete = team['complete']
        if team_complete is True:
            coll("teams").update_one({"_id": team_name}, {"$set": {"complete": False}})
            return return_resp(200, "False")
        else:
            coll("teams").update_one({"_id": team_name}, {"$set": {"complete": True}})
            return return_resp(200, "True")
Exemple #10
0
 def wrapped():
     test_input = request.get_json(force=True)
     errors = [
         error.message for error in validator.iter_errors(test_input)
     ]
     if errors:
         return return_resp(505, "Invalid Json")
     else:
         return fn()
Exemple #11
0
def update_profile():
    if request.method == 'GET':
        data = request.get_json(silent=True)
        email = data['user_email']
        email = email.strip().lower()
        has_profile = coll("users").find_one({"_id": email})
        if not has_profile:
            return return_resp(200, "User Not found")
        user_profile = coll("users").find_one({"_id": email})
        dir_token = call_auth_endpoint()
        if dir_token != 400:
            name = get_name(dir_token, email)
        else:
            name = ""
        user_profile.update({"name": name})
        return return_resp(200, user_profile)
    elif request.method == 'POST':
        data = request.get_json(silent=True)
        email = data['user_email']
        email = email.strip().lower()
        if not data or 'skills' not in data or not data['skills']:
            return return_resp(400, "Required info not found")
        if 'prizes' not in data:
            prizes = []
        else:
            prizes = format_string(data['prizes'].strip().lower())
        skills = format_string(data['skills'].strip().lower())
        user_exists = coll("users").find_one({"_id": email})
        if user_exists:
            coll("users").update(
                {"_id": email}, {"$set": {
                    "skills": skills,
                    "prizes": prizes
                }})
            return return_resp(200, "Successful update")
        else:
            coll("users").insert_one({
                "_id": email,
                "skills": skills,
                "prizes": prizes,
                "hasateam": False,
                "potentialteams": []
            })
            return return_resp(201, "Profile created")
Exemple #12
0
def get_team_profile():
    if request.method == 'GET':
        data = request.get_json(silent=True)
        email = data['user_email']
        email = email.strip().lower()
        team = coll("teams").find_one({"members": {"$all": [email]}})
        if not team:
            return return_resp(400, "Team Not found")
        else:
            members = team['members']
            members_names = []
            for member in members:
                token = call_auth_endpoint()
                if token == 200:
                    continue
                name = get_name(token, member)
                if name == 200:
                    continue
                members_names.append(name)
                team.update({"names": members_names})
            return return_resp(200, team)
def get_individual_recommendations():
    if request.method == 'GET':
        data = request.get_json(silent=True)
        email = data['user_email']
        email = email.strip().lower()
        team = coll("teams").find_one({"members": {"$all": [email]}})
        if not team:
            return return_resp(400, "User not in a team")
        if 'partnerskills' not in team or not team['partnerskills']:
            return return_resp(401, "Profile not complete")
        if 'prizes' not in team or not team['prizes']:
            prizes = []
        else:
            prizes = team['prizes']
        skills = team['partnerskills']
        emails = set()
        matches = []
        for skill in skills:
            match = coll("users").aggregate([{
                "$match": {
                    "hasateam": False,
                    "skills": {
                        "$all": [skill]
                    }
                }
            }])
            if not match:
                continue
            for m in match:
                if m['_id'] not in emails:
                    emails.add(m['_id'])
                    dir_token = call_auth_endpoint()
                    if dir_token != 400:
                        name = get_name(dir_token, email)
                    else:
                        name = ""
                    m.update({"name": name})
                    matches.append(m)
        for prize in prizes:
            match = coll("users").aggregate([{
                "$match": {
                    "hasateam": False,
                    "prizes": {
                        "$all": [prize]
                    }
                }
            }])
            if not match:
                continue
            for m in match:
                if m['_id'] not in emails:
                    emails.add(m['_id'])
                    dir_token = call_auth_endpoint()
                    if dir_token != 400:
                        name = get_name(dir_token, email)
                    else:
                        name = ""
                    m.update({"name": name})
                    matches.append(m)
        if not matches:
            return return_resp(402, "No recommendations found")
        else:
            return return_resp(200, matches)