Ejemplo n.º 1
0
def by():
    if len(request.args) < 1:
        session.flash = "Invalid URL!"
        redirect(URL("default", "index"))

    stopstalk_handle = request.args[0]

    atable = db.auth_user
    pstable = db.problem_setters
    ptable = db.problem

    return_val = dict(stopstalk_handle=stopstalk_handle,
                      problems_count=0)

    table = TABLE(_class="bordered centered")
    thead = THEAD(TR(TH("Problem")))

    problems = utilities.get_problems_authored_by(stopstalk_handle)
    if len(problems) == 0:
        return return_val

    return_val["table"] = utilities.get_problems_table(problems,
                                                       session.user_id,
                                                       None)

    return_val["problems_count"] = len(problems)
    return return_val
Ejemplo n.º 2
0
def recommendations():
    """
        Problem recommendations for the user.
    """
    import recommendations.problems as recommendations

    ptable = db.problem
    rtable = db.problem_recommendations
    user_id = session.user_id
    refresh = request.vars.get("refresh", "false") == "true"

    output = {}
    recommendation_pids = []

    rows = db(rtable.user_id == user_id).select()
    if len(rows) == 0:
        refresh = True

    if refresh:
        recommendation_pids = recommendations.generate_recommendations(user_id)
    else:
        recommendation_pids, _ = recommendations.retrieve_past_recommendations(
            user_id)

    output["recommendations_length"] = len(recommendation_pids)
    if len(recommendation_pids) > 0:
        problem_details = db(ptable.id.belongs(recommendation_pids)).select()
        output["table"] = utilities.get_problems_table(problem_details,
                                                       user_id,
                                                       "recommendation", None)

    output["can_update"] = recommendations.can_refresh_recommendations(user_id)

    return output
Ejemplo n.º 3
0
def recommendations():
    """
        Problem recommendations for the user.
    """
    import recommendations.problems as recommendations

    ptable = db.problem
    rtable = db.problem_recommendations
    user_id = session.user_id
    refresh = request.vars.get("refresh", "false") == "true"

    output = {}
    recommendation_pids = []

    rows = db(rtable.user_id == user_id).select()
    if len(rows) == 0:
        refresh = True

    if refresh:
        recommendation_pids = recommendations.generate_recommendations(user_id)
    else:
        recommendation_pids, _ = recommendations.retrieve_past_recommendations(
            user_id)

    output["recommendations_length"] = len(recommendation_pids)
    if len(recommendation_pids) > 0:
        problem_details = db(ptable.id.belongs(recommendation_pids)).select()
        output["table"] = utilities.get_problems_table(problem_details,
                                                       user_id,
                                                       "recommendation", None)

    query = (rtable.user_id == user_id) & (rtable.is_active == True)
    rows = db(query).select(rtable.generated_at)

    output["can_update"] = True
    if len(rows) > 0:
        output["can_update"] = (datetime.datetime.now().date() - rows[0].generated_at).days \
            >= RECOMMENDATION_REFRESH_INTERVAL

    return output
Ejemplo n.º 4
0
def search():
    """
        Search page for problems
    """

    ttable = db.tag
    uetable = db.user_editorials

    problem_name = request.vars.get("name", None)
    orderby = request.vars.get("orderby", None)
    clubbed_tags = request.vars.get("generalized_tags", None)
    q = request.vars.get("q", None)
    sites = request.vars.get("site", None)
    include_editorials = request.vars.get("include_editorials", "")
    exclude_solved = request.vars.get("exclude_solved", None)

    generalized_tags = db(ttable).select(ttable.value, orderby=ttable.value)
    generalized_tags = [x.value for x in generalized_tags]

    if any([problem_name, orderby, clubbed_tags, q, sites]) is False:
        if request.extension == "json":
            return dict(total_pages=0)
        else:
            if len(request.get_vars):
                # No filter is applied
                response.flash = "No filter is applied"
            return dict(table=DIV(), generalized_tags=generalized_tags)

    clubbed_tags = None if clubbed_tags == "" else clubbed_tags

    try:
        if sites == None or sites == "":
            sites = []
        elif isinstance(sites, str):
            sites = [sites]
    except:
        sites = []

    if orderby not in ("accuracy-asc", "accuracy-desc",
                       "solved-count-asc", "solved-count-desc"):
        orderby = None

    try:
        curr_page = int(request.vars["page"])
    except:
        curr_page = 1
    PER_PAGE = current.PER_PAGE

    ptable = db.problem
    query = True

    rows = db(uetable.verification == "accepted").select(uetable.problem_id)
    problem_with_user_editorials = set([x["problem_id"] for x in rows])

    if q is not None and not clubbed_tags:
        # Enables multiple space seperated tag search
        q = q.split(" ")
        for tag in q:
            if tag == "":
                continue
            # Decision to make & or |
            # & => Search for problem containing all these tags
            # | => Search for problem containing one of the tags
            query &= ptable.tags.contains(tag)
    elif clubbed_tags:
        clubbed_tags = [clubbed_tags] if isinstance(clubbed_tags, str) else clubbed_tags
        ttable = db.tag
        sttable = db.suggested_tags

        tag_ids = db(ttable.value.belongs(clubbed_tags)).select(ttable.id)
        tag_ids = [x.id for x in tag_ids]

        problem_ids = db(sttable.tag_id.belongs(tag_ids)).select(sttable.problem_id)
        problem_ids = [x.problem_id for x in problem_ids]

        query &= ptable.id.belongs(problem_ids)

    if problem_name:
        query &= ptable.name.contains(problem_name)

    if include_editorials:
        # Check if the site editorial link is present or the problem id exists
        # in user_editorials table with accepted status
        query &= (((ptable.editorial_link != None) & \
                   (ptable.editorial_link != "")) | \
                  (ptable.id.belongs(problem_with_user_editorials)))

    if exclude_solved and auth.is_logged_in():
        solved_pids, _ = utilities.get_solved_problems(session.user_id, False)
        query &= ~ptable.id.belongs(solved_pids)
    elif exclude_solved and request.extension == "html":
        response.flash = T("Login to apply this filter")

    site_query = None
    for site in sites:
        if site_query is not None:
            site_query |= ptable.link.contains(current.SITES[site])
        else:
            site_query = ptable.link.contains(current.SITES[site])
    if site_query:
        query &= site_query

    accuracy_column = (ptable.solved_submissions * 1.0 / ptable.total_submissions)
    kwargs = dict(distinct=True,
                  limitby=((curr_page - 1) * PER_PAGE,
                           curr_page * PER_PAGE))
    if orderby and orderby.__contains__("accuracy"):
        query &= ~(ptable.link.contains("hackerrank.com"))
        kwargs["orderby"] = ~accuracy_column if orderby == "accuracy-desc" else accuracy_column

    if orderby and orderby.__contains__("solved-count"):
        kwargs["reverse"] = True if orderby == "solved-count-desc" else False

    query &= (ptable.solved_submissions != None)
    query &= (ptable.total_submissions != None) & (ptable.total_submissions != 0)
    query &= (ptable.user_ids != None)
    query &= (ptable.custom_user_ids != None)

    if request.extension == "json":
        total_problems = db(query).count()

        total_pages = total_problems / PER_PAGE
        if total_problems % PER_PAGE != 0:
            total_pages = total_problems / PER_PAGE + 1

        return dict(total_pages=total_pages)

    if orderby and orderby.__contains__("solved-count"):
        all_problems = db(query).select().as_list()
        all_problems.sort(key=lambda x: x["user_count"] + \
                                        x["custom_user_count"],
                          reverse=kwargs["reverse"])
        all_problems = all_problems[kwargs["limitby"][0]:kwargs["limitby"][1]]
    else:
        # No need of caching here
        all_problems = db(query).select(**kwargs)

    return dict(table=utilities.get_problems_table(all_problems,
                                                   session.user_id,
                                                   problem_with_user_editorials),
                generalized_tags=generalized_tags)