Exemple #1
0
def notifications():
    """
        Check if any of the friends(includes CUSTOM) of
        the logged-in user is on a streak
    """

    if not auth.is_logged_in():
        redirect(URL("default", "index"))

    ftable = db.friends
    atable = db.auth_user
    cftable = db.custom_friend

    # Check for streak of friends on stopstalk
    query = (ftable.user_id == session.user_id)
    rows = db(query).select(atable.first_name,
                            atable.last_name,
                            atable.stopstalk_handle,
                            join=ftable.on(ftable.friend_id == atable.id))

    # Will contain list of handles of all the friends along
    # with the Custom Users added by the logged-in user
    handles = []

    complete_dict = {}
    friends_stopstalk_handles = []
    for user in rows:
        complete_dict[user.stopstalk_handle] = []
        friends_stopstalk_handles.append("'" + user.stopstalk_handle + "'")
        handles.append(
            (user.stopstalk_handle, user.first_name + " " + user.last_name,
             user.stopstalk_handle))

    # Check for streak of custom friends
    query = (cftable.user_id == session.user_id)
    rows = db(query).select(cftable.first_name, cftable.last_name,
                            cftable.duplicate_cu, cftable.stopstalk_handle)

    for user in rows:
        name = user.first_name + " " + user.last_name
        actual_handle = user.stopstalk_handle

        if user.duplicate_cu:
            stopstalk_handle = user.duplicate_cu.stopstalk_handle
        else:
            stopstalk_handle = user.stopstalk_handle

        handles.append((stopstalk_handle, name, actual_handle))
        complete_dict[stopstalk_handle] = []
        friends_stopstalk_handles.append("'" + stopstalk_handle + "'")

    if friends_stopstalk_handles == []:
        friends_stopstalk_handles = ["-1"]

    # Build the complex SQL query
    sql_query = """
                    SELECT stopstalk_handle, DATE(time_stamp), COUNT(*) as cnt
                    FROM submission
                    WHERE stopstalk_handle in (%s)
                    GROUP BY stopstalk_handle, DATE(submission.time_stamp)
                    ORDER BY time_stamp;
                """ % (", ".join(friends_stopstalk_handles))

    user_rows = db.executesql(sql_query)
    for user in user_rows:
        if complete_dict[user[0]] != []:
            complete_dict[user[0]].append((user[1], user[2]))
        else:
            complete_dict[user[0]] = [(user[1], user[2])]

    # List of users with non-zero streak
    users_on_day_streak = []

    for handle in handles:
        curr_streak = utilities.get_max_streak(complete_dict[handle[0]])[2]

        # If streak is non-zero append to users_on_streak list
        if curr_streak:
            users_on_day_streak.append((handle, curr_streak))

    # Sort the users on streak by their streak
    users_on_day_streak.sort(key=lambda k: k[1], reverse=True)

    # The table containing users on streak(days)
    table = TABLE(THEAD(TR(TH(STRONG("User")), TH(STRONG("Days")))),
                  _class="col offset-s3 s6 striped centered")

    tbody = TBODY()

    # Append all the users to the final table
    for users in users_on_day_streak:
        handle = users[0]
        curr_streak = users[1]
        tr = TR(
            TD((A(handle[1],
                  _href=URL("user", "profile", args=[handle[2]]),
                  _target="_blank"))),
            TD(str(curr_streak) + " ",
               I(_class="fa fa-bolt", _style="color:red"),
               _class="center"))
        tbody.append(tr)

    table.append(tbody)
    if len(users_on_day_streak) == 0:
        table = H6("No friends on day streak", _class="center")

    return dict(table=table)
def notifications():
    """
        Check if any of the friends(includes CUSTOM) of
        the logged-in user is on a streak
    """

    if not auth.is_logged_in():
        redirect(URL("default", "index"))

    ftable = db.friends
    atable = db.auth_user
    cftable = db.custom_friend

    # Check for streak of friends on stopstalk
    query = (ftable.user_id == session.user_id)
    rows = db(query).select(atable.first_name,
                            atable.last_name,
                            atable.stopstalk_handle,
                            join=ftable.on(ftable.friend_id == atable.id))

    # Will contain list of handles of all the friends along
    # with the Custom Users added by the logged-in user
    handles = []

    complete_dict = {}
    friends_stopstalk_handles = []
    for user in rows:
        complete_dict[user.stopstalk_handle] = []
        friends_stopstalk_handles.append("'" + user.stopstalk_handle + "'")
        handles.append((user.stopstalk_handle,
                        user.first_name + " " + user.last_name,
                        user.stopstalk_handle))

    # Check for streak of custom friends
    query = (cftable.user_id == session.user_id)
    rows = db(query).select(cftable.first_name,
                            cftable.last_name,
                            cftable.duplicate_cu,
                            cftable.stopstalk_handle)

    for user in rows:
        name = user.first_name + " " + user.last_name
        actual_handle = user.stopstalk_handle

        if user.duplicate_cu:
            stopstalk_handle = user.duplicate_cu.stopstalk_handle
        else:
            stopstalk_handle = user.stopstalk_handle

        handles.append((stopstalk_handle, name, actual_handle))
        complete_dict[stopstalk_handle] = []
        friends_stopstalk_handles.append("'" + stopstalk_handle + "'")

    if friends_stopstalk_handles == []:
        friends_stopstalk_handles = ["-1"]

    # Build the complex SQL query
    sql_query = """
                    SELECT stopstalk_handle, DATE(time_stamp), COUNT(*) as cnt
                    FROM submission
                    WHERE stopstalk_handle in (%s)
                    GROUP BY stopstalk_handle, DATE(submission.time_stamp)
                    ORDER BY time_stamp;
                """ % (", ".join(friends_stopstalk_handles))

    user_rows = db.executesql(sql_query)
    for user in user_rows:
        if complete_dict[user[0]] != []:
            complete_dict[user[0]].append((user[1], user[2]))
        else:
            complete_dict[user[0]] = [(user[1], user[2])]

    # List of users with non-zero streak
    users_on_day_streak = []

    for handle in handles:
        curr_streak = utilities.get_max_streak(complete_dict[handle[0]])[2]

        # If streak is non-zero append to users_on_streak list
        if curr_streak:
            users_on_day_streak.append((handle, curr_streak))

    # Sort the users on streak by their streak
    users_on_day_streak.sort(key=lambda k: k[1], reverse=True)

    # The table containing users on streak(days)
    table = TABLE(THEAD(TR(TH(STRONG("User")),
                           TH(STRONG("Days")))),
                  _class="col offset-s3 s6 striped centered")

    tbody = TBODY()

    # Append all the users to the final table
    for users in users_on_day_streak:
        handle = users[0]
        curr_streak = users[1]
        tr = TR(TD((A(handle[1],
                      _href=URL("user", "profile", args=[handle[2]]),
                      _target="_blank"))),
                TD(str(curr_streak) + " ",
                   I(_class="fa fa-bolt",
                     _style="color:red"),
                   _class="center"))
        tbody.append(tr)

    table.append(tbody)
    if len(users_on_day_streak) == 0:
        table = H6("No friends on day streak", _class="center")

    return dict(table=table)