def signup():
    error = None
    req = request.json
    request_email = req['email'].lower()
    password = req['password']
    entry = user.findSingleUser({'email': request_email})

    if entry is not None:
        error = 'Email is already in use'
        return jsonify(LoggedIn=False, error=error), HTTP_400_BAD_REQUEST
    try:
        invite_code = req['invite']
        if not invite.is_valid(invite_code):
            raise Exception("Invalid invite code")
        new_user = user.create_user(req)
        if (new_user is None):
            raise Exception()

        database_wrapper.save_entity(new_user)
        invite.consume(invite_code, new_user['_id'])

        # We need to log in the just-registered user.
        status = Auth.login(new_user, password)
        return jsonify(user.get_basic_info_from_users([new_user])[0])
    except Exception as e:
        return jsonify(error=str(e)), HTTP_400_BAD_REQUEST
def post_wall(startup_object, request, current_user_id):
    msg = {'user': current_user_id, 'message': request['message'], 'date': datetime.datetime.utcnow(), 'id': str(ObjectId())}
    startup_object.wall.insert(0, msg)

    database_wrapper.save_entity(startup_object)

    msg['user'] = utils.jsonFields(user.findUserByID(current_user_id), user.User.basic_info_fields, response=False)
    return msg
def give_answer(startup_object, question_id, request):
    for question in startup_object.qa:  # O(N). Probably not too bad.
        if question['id'] == question_id:
            question['answer'] = request['answer']
            question['status'] = Startup.question_status['ANSWERED']
            database_wrapper.save_entity(startup_object)
            return startup_object

    raise Exception('Not found')
def update_basic_startup(startup_object, request):
    fields = list(Startup.basic_info_fields)
    fields.remove('_id')
    utils.mergeFrom(request, startup_object, fields, require=False)
    #put the object ids for all the markets in the request into the startup
    put_markets(startup_object, request)
   # startup_object['markets']=[market._id for market in get_markets_from_name(request['markets'])]
    database_wrapper.save_entity(startup_object)
    return startup_object
def remove_question(startup_object, question_id, request):
    index = 0
    while index < len(startup_object.qa):
        if startup_object.qa[index]['id'] == question_id:
            startup_object.qa.pop(index)
            database_wrapper.save_entity(startup_object)
            return startup_object
        index += 1

    raise Exception('Not found')
def remove_wall(startup_object, post_id, request):
    index = 0
    while index < len(startup_object.wall):
        if startup_object.wall[index]['id'] == post_id:
            startup_object.wall.pop(index)
            database_wrapper.save_entity(startup_object)
            return startup_object
        index += 1

    raise Exception('Not found')
def user_basic_info(user_id):
    entry = user.findUserByID(user_id)
    if entry is None:
        return '', HTTP_404_NOT_FOUND

    req = request.get_json()
    try:
        utils.mergeFrom(req, entry, user.User.basic_info_fields, require=False)
        database_wrapper.save_entity(entry)
    except:
        return jsonify(error='Invalid key'), HTTP_400_BAD_REQUEST
    return '', HTTP_200_OK
def delete_basic_user_info(user_id, attribute):
    entry = user.findUserByID(user_id)
    if entry is None:
        return '', HTTP_404_NOT_FOUND
    try:
        entry[attribute] = None
        database_wrapper.save_entity(entry)
    except:
        print sys.exc_info()[0]
        return jsonify(error='Invalid key or field cannot be deleted'), HTTP_400_BAD_REQUEST
        #return empty response with 200 status ok
    return '', HTTP_200_OK
Esempio n. 9
0
def create_startup(user_id, request):
    startup = Startups.Startup()
    utils.mergeFrom(request, startup, Startup.required_fields)
    startup.date = datetime.datetime.utcnow()
    startup.owners.append(str(user_id))
    startup.people.append(str(user_id))
    #convert the market names in the request into market ids, increment and decrement occurences accordingly
    put_markets(startup, request)
    optional = Startup.basic_info_fields.difference(Startup.required_fields)
    optional.remove('_id')
    utils.mergeFrom(request, startup, optional, require=False)
    database_wrapper.save_entity(startup)
    return startup
Esempio n. 10
0
def add_question(startup_object, request, current_user_id):
    question = {
        'question': request['question'],
        'answer': '',
        'date': datetime.datetime.utcnow(),
        'status': Startup.question_status['PENDING_ANSWER'],
        'id': str(ObjectId()),
        'asker': str(current_user_id)
    }

    startup_object.qa.insert(0, question)
    database_wrapper.save_entity(startup_object)

    return startup_object
Esempio n. 11
0
def confirm_connection(other_user):
    conn_type = connection_type(other_user)
    me = findUserByID('me')

    if conn_type != 'pa':
        raise Exception('user not pending approval')

    get_connection(me,
                   other_user._id)['type'] = User.connection_types['CONNECTED']
    get_connection(other_user,
                   me._id)['type'] = User.connection_types['CONNECTED']

    ## only complete transaction after successful addition to both sides
    database_wrapper.save_entity(me)
    database_wrapper.save_entity(other_user)
Esempio n. 12
0
def remove_user_connection(other_user):
    me = findUserByID('me')

    our_connection = get_connection(me, other_user._id)

    if our_connection is None:
        raise Exception('no connection between you and this user')

    current_user_id = str(me._id)
    other_user_id = str(other_user._id)

    status_me = remove_connection(me, other_user_id)
    status_other = remove_connection(other_user, current_user_id)

    if not status_me or not status_other:
        raise Exception(
            'removal failed due to missing links. database likely corrupt')

    ## only complete transaction after successful removal from both sides
    database_wrapper.save_entity(me)
    database_wrapper.save_entity(other_user)
Esempio n. 13
0
def put_markets(startup,req):
    markets = req.get('markets')
    previous_markets_list = get_markets_from_id(startup['markets'])
    put_markets_list = get_markets_from_name(markets)
    removed_markets = set(previous_markets_list).difference(put_markets_list)
    added_markets = set(put_markets_list).difference(previous_markets_list)
    for removed_market in removed_markets:
        if removed_market is None:
            #this should never happen
            raise Exception("Startup had reference to market that doesn't exist")
        else:
            market.decrement_market(removed_market)

    for added_market in added_markets:
        if added_market is None:
            #this should never happen
            raise Exception("Something broke.. Added market is None?!")
        else:
            market.increment_market(added_market)
    startup.markets = [put_market._id for put_market in put_markets_list]
    database_wrapper.save_entity(startup)
    return True
Esempio n. 14
0
def put_skills(user, req):
    skills = req.get('skills')
    previous_skills_list = get_skills_from_id(user['skills'])
    put_skills_list = get_skills_from_name(skills)
    removed_skills = set(previous_skills_list).difference(put_skills_list)
    added_skills = set(put_skills_list).difference(previous_skills_list)

    for removed_skill in removed_skills:
        if removed_skill is None:
            #this should never happen
            raise Exception("User had reference to skill that doesn't exist")
        skill.decrement_skill(removed_skill)

    for added_skill in added_skills:
        if added_skill is None:
            #this should never happen
            raise Exception("Something broke.. Added skill is None?!")
        skill.increment_skill(added_skill)

    user.skills = [str(put_skill._id) for put_skill in put_skills_list]
    database_wrapper.save_entity(user)
    return True
def decrement_market(market):
    if market.occurrences == 1:
        database_wrapper.remove_entity(market)
    else:
        market.occurrences -= 1
        database_wrapper.save_entity(market)
Esempio n. 16
0
def increment_skill(skill):
    skill.occurrences += 1
    database_wrapper.save_entity(skill)
Esempio n. 17
0
def put_projects(user, req):
    projects = req.get('projects')
    user.projects = projects
    database_wrapper.save_entity(user)
    return True
Esempio n. 18
0
def put_interests(user, req):
    interests = req.get('interests')
    user.interests = interests
    database_wrapper.save_entity(user)
    return True
Esempio n. 19
0
def increment_market(market):
    market.occurrences += 1
    database_wrapper.save_entity(market)
Esempio n. 20
0
def decrement_market(market):
    if market.occurrences == 1:
        database_wrapper.remove_entity(market)
    else:
        market.occurrences -= 1
        database_wrapper.save_entity(market)
Esempio n. 21
0
def create_skill(name, occurrences):
    skill = Skills.Skill()
    skill['name'] = name
    skill['occurrences'] = occurrences
    database_wrapper.save_entity(skill)
    return skill
Esempio n. 22
0
def create_market(name, occurrences):
    market = Markets.Market()
    market['name'] = name
    market['occurrences'] = occurrences
    database_wrapper.save_entity(market)
    return market
def increment_market(market):
    market.occurrences += 1
    database_wrapper.save_entity(market)
def create_market(name, occurrences):
    market = Markets.Market()
    market["name"] = name
    market["occurrences"] = occurrences
    database_wrapper.save_entity(market)
    return market
Esempio n. 25
0
def decrement_skill(skill):
    if skill.occurrences == 1:
        database_wrapper.remove_entity(skill)
    else:
        skill.occurrences -= 1
        database_wrapper.save_entity(skill)
Esempio n. 26
0
def put_people(startup_object, request):
    startup_object.people = request['people']
    database_wrapper.save_entity(startup_object)
    return startup_object
Esempio n. 27
0
def put_overview(startup_object, request):
    startup_object.overview = request['overview']
    database_wrapper.save_entity(startup_object)
    return startup_object