Exemple #1
0
def update_user_feeling():
    feeling = request.args.get('feeling', 0, type=int)

    from app import mongo

    mongo.db.users.update(
        {"_id": ObjectId(session.get('user_id'))},
        {"$set": {
            'last_feel': feeling,
            'user_feel.' + today_f(): feeling
        }},
        upsert=True)
    """
              UPDATING COUNTY FEELINGS 
           """
    update_county_feel(mongo, session.get('user_country_code'),
                       session.get('user_cl'), 0,
                       int(feeling) - int(session.get('last_feel')))
    """updating country feel"""
    update_country_feel(mongo, session.get('user_country_code'), 0,
                        int(feeling) - int(session.get('last_feel')))
    """updating world feel"""
    update_world_feel(mongo, 0, int(feeling) - int(session.get('last_feel')))

    session['last_feel'] = feeling
    session['user_feel'] = feeling

    return jsonify(updated='updated')
Exemple #2
0
def update_feel():
    from app import mongo
    mongo.db.post_users.update(
        {"_id": ObjectId("5eb7d77e4e127c150fd2deaf")},
        {"$set": {

            'last_feel': 77,
            'user_feel.' + today_f(): 77}},
        upsert=True
    )

    return 'feelING updated'
def update_country_feel(mongo, country_code, people, feeling):
    today = today_f()
    """if logged in user changing his feel, we will just recalculate his feel
     without adding new person to the mix, 
     if new user we will add 1 person to the mix"""

    mongo.db.country_feel.update({"country_code": country_code}, {
        "$inc": {
            "feels." + today + ".num_of_people": int(people),
            "feels." + today + ".sum_of_feelings": int(feeling),
            "total_people": int(people),
            "total_feelings": int(feeling),
        },
    },
                                 upsert=True)
Exemple #4
0
def login():
    user = request.form.to_dict()
    from app import mongo
    user_to_check = mongo.db.users.find_one({"email": user['email']})
    if not user_to_check:
        session['form_email'] = user['email']
        flash('We have no user with those credentials', 'flash_error')

        return redirect(url_for('authorize_bp.sign_in'))

    if user['user_feel'] == '':
        """remembering user email and displaying it 
        in the form or redirect, so that user doesn't have to type 
        it again"""
        session['form_email'] = user['email']

        flash('Please select how you feel!', 'flash_error')

        return redirect(url_for('authorize_bp.sign_in'))

    user_password = user_to_check['password']
    user_email = user_to_check['email']
    """if we have user with those credentials we will log user """
    if check_password_hash(user_password,
                           user['password']) and user_email == user['email']:

        db_feelists = user_to_check[
            'my_feelists'] if 'my_feelists' in user_to_check else []
        user['last_login'] = datetime.datetime.now()
        user_name = user_to_check['name']

        last_login = user_to_check['last_login']
        country_code = user_to_check['cc']
        cl = user_to_check['cl']

        feeling = int(user['user_feel']) - int(user_to_check['last_feel'])

        my_glob = user_to_check[
            'my_globs'] if 'my_globs' in user_to_check else []
        my_likes = user_to_check['likes'] if 'likes' in user_to_check else []
        user_id = str(user_to_check['_id'])

        user_feelists = feelists(db_feelists)
        mongo.db.users.update({"email": user_email}, {
            "$set": {
                'last_login': datetime.datetime.now(),
                'last_feel': user['user_feel'],
                'user_feel.' + today_f(): user['user_feel']
            }
        },
                              upsert=True)

        session.clear()
        """
           ADDING USER DATA TO SESSION FOR FUTURE USE 
        """
        session['user_name'] = user_name
        session['last_login'] = last_login
        session['user_country_code'] = country_code
        session['user_cl'] = cl
        session['my_feelists'] = user_feelists
        session['user_id'] = user_id
        session['my_globs'] = my_glob
        session['my_likes'] = my_likes
        session['last_feel'] = user['user_feel']

        session_user(user)
        """IF CURRENTLY LOGGED IN USER ALREADY SET HIS FEELING FOR THE DAY WE WILL NOT INCREASE 
        NUMBER OF PEOPLE IN world_feel and country_feel COLECTIONS , 
        WE WILL ONLY RECALCULATE FEELINGS 
        
        OTHERWISE IF IT IS FIRST LOGIN IN NEW DAY WE WILL INCREASE NUM OF PEOPLE 
        WHO EXPRESSED THEIR FEELINGS IN THAT DAY AND WILL ADD THEIR FEELINGS TO THE MIX"""

        if (mongo.db.todays_users.find_one({
                "day":
                datetime.datetime.now().strftime("%F"),
                "users": {
                    "$in": [user_id]
                },
        })):

            increase_people = 0
        else:
            mongo.db.todays_users.update(
                {"day": datetime.datetime.now().strftime("%F")},
                {"$push": {
                    'users': user_id
                }},
                upsert=True)

            increase_people = 1
        """updating country feel"""
        update_county_feel(mongo, country_code, cl, increase_people, feeling)
        """updating country feel"""
        update_country_feel(mongo, country_code, increase_people, feeling)
        """updating world feel"""
        update_world_feel(mongo, increase_people, feeling)

        session['user_feel'] = user['user_feel']
        """
           REDIRECT ADMIN TO ADMIN DASHBOARD 
        """
        if user['email'] == '*****@*****.**' and check_password_hash(
                user_password, user['password']):
            session['authorized_admin'] = True
            return redirect(url_for('admin_bp.admin'))

        return redirect(url_for('user_bp.user'))
    else:
        session['form_email'] = user['email']
        flash('Please provide valid email and password', 'flash_error')
        return redirect(url_for('authorize_bp.sign_in'))