Esempio n. 1
0
def contest_splash(request, slug):
	"""
	Splash page for contest. 

	Example of adding a contest record to the DB:
	db.contests.save({
			"contest_start"    : datetime.strptime("3/5/14", "%m/%d/%y"),
			"contest_end"      : datetime.strptime("3/26/14", "%m/%d/%y"),
			"version"          : "Sefaria Community Translation",
			"ref_regex"        : "^Shulchan Arukh, Even HaEzer ",
			"assignment_url"   : "/translate/Shulchan_Arukh,_Even_HaEzer",
			"title"            : "Translate Shulchan Arukh, Even HaEzer",
			"slug"             : "shulchan-arukh-even-haezer"
	})
	"""
	settings = db.contests.find_one({"slug": slug})
	if not settings:
		raise Http404

	settings["copy_template"] = "static/contest/%s.html" % settings["slug"]

	leaderboard_condition = make_leaderboard_condition( start     = settings["contest_start"], 
														end       = settings["contest_end"], 
														version   = settings["version"], 
														ref_regex = settings["ref_regex"])

	now = datetime.now()
	if now < settings["contest_start"]:
		settings["phase"] = "pre"
		settings["leaderboard"] = None
		settings["time_to_start"] = td_format(settings["contest_start"] - now)

	elif settings["contest_start"] < now < settings["contest_end"]:
		settings["phase"] = "active"
		settings["leaderboard_title"] = "Current Leaders"
		settings["leaderboard"] = make_leaderboard(leaderboard_condition)
		settings["time_to_end"] = td_format(settings["contest_end"] - now)

	elif settings["contest_end"] < now:
		settings["phase"] = "post"
		settings["leaderboard_title"] = "Contest Leaders (Unreviewed)"

		settings["leaderboard"] = make_leaderboard(leaderboard_condition)


	return render_to_response("contest_splash.html",
								settings,
								RequestContext(request))
def update_top_contributors(days=None):
	"""
	Calculate leaderboard scores for the past days, or all time if days is None.
	Store in a collection named for the length of time.
	Remove old scores.
	"""

	if days:
		cutoff = datetime.now() - timedelta(days)
		condition = { "date": { "$gt": cutoff }, "method": {"$ne": "API"} }
		collection = "leaders_%d" % days
	else:
		cutoff = None
		condition = { "method": {"$ne": "API"} }
		collection = "leaders_alltime"

	leaders = make_leaderboard(condition)

	oldtime = datetime.now()

	for l in leaders:
		doc = {"_id": l["user"], "count": l["count"], "date": datetime.now()}
		db[collection].save(doc)
	
	if cutoff:	
		db[collection].remove({"date": {"$lt": oldtime }})
Esempio n. 3
0
def update_top_contributors(days=None):
    """
	Calculate leaderboard scores for the past days, or all time if days is None.
	Store in a collection named for the length of time.
	Remove old scores.
	"""

    if days:
        cutoff = datetime.now() - timedelta(days)
        condition = {"date": {"$gt": cutoff}, "method": {"$ne": "API"}}
        collection = "leaders_%d" % days
    else:
        cutoff = None
        condition = {"method": {"$ne": "API"}}
        collection = "leaders_alltime"

    leaders = make_leaderboard(condition)

    oldtime = datetime.now()

    for l in leaders:
        doc = {"_id": l["user"], "count": l["count"], "date": datetime.now()}
        db[collection].save(doc)

    if cutoff:
        db[collection].remove({"date": {"$lt": oldtime}})
Esempio n. 4
0
def update_top_contributors(days=None):
    """
    Calculate leaderboard scores for the past n 'days', or all time if 'days' is None.
    Store in a collection named for the length of time.
    Remove old scores.
    """

    if days:
        cutoff = datetime.now() - timedelta(days)
        # condition = { "date": { "$gt": cutoff }, "method": {"$ne": "API"} }
        condition = {"date": {"$gt": cutoff}}
        collection = "leaders_%d" % days
    else:
        cutoff = None
        # condition = { "method": {"$ne": "API"} }
        condition = {}
        collection = "leaders_alltime"

    leaders = make_leaderboard(condition)

    oldtime = datetime.now()

    # Tally points for Public Source Sheets
    query = {"status": {"$in": LISTED_SHEETS}}
    if cutoff:
        query["$or"] = [{"dateCreated": {"$gt": cutoff.isoformat()}}, {"datePublished": {"$gt": cutoff.isoformat()}}]
    sheets = db.sheets.find(query)
    sheet_points = defaultdict(int)
    sheet_counts = defaultdict(int)
    for sheet in sheets:
        sheet_points[sheet["owner"]] += len(sheet["sources"]) * 50
        sheet_counts[sheet["owner"]] += 1

    for l in leaders:
        points = l["count"] + sheet_points[l["user"]]
        del sheet_points[l["user"]]
        if points:
            doc = {
                "_id": l["user"],
                "count": points,
                "translateCount": int(l["translateCount"]),
                "editCount": int(l["editCount"]),
                "addCount": int(l["addCount"]),
                "noteCount": int(l["noteCount"]),
                "linkCount": int(l["linkCount"]),
                "reviewCount": int(l["reviewCount"]),
                "sheetCount": sheet_counts[l["user"]],
                "texts": sorted(l["texts"], key=lambda key: -l["texts"][key]),
                "date": datetime.now(),
            }
            db[collection].save(doc)

    # Add points for those who only have sheet points
    for s in sheet_points.items():
        if s[1]:
            doc = {"_id": s[0], "count": s[1], "sheetCount": sheet_counts[s[0]], "date": datetime.now()}
            db[collection].save(doc)

    if cutoff:
        db[collection].remove({"date": {"$lt": oldtime}})
Esempio n. 5
0
def update_top_contributors(days=None):
	"""
	Calculate leaderboard scores for the past days, or all time if days is None.
	Store in a collection named for the length of time.
	Remove old scores.
	"""

	if days:
		cutoff = datetime.now() - timedelta(days)
		condition = { "date": { "$gt": cutoff }, "method": {"$ne": "API"} }
		collection = "leaders_%d" % days
	else:
		cutoff = None
		condition = { "method": {"$ne": "API"} }
		collection = "leaders_alltime"

	leaders = make_leaderboard(condition)

	oldtime = datetime.now()

	# Tally points for Public Source Sheets
	query = {"status": {"$in": LISTED_SHEETS} }
	if cutoff:
		query["dateCreated"] = {"$gt": cutoff.isoformat()}
	sheets = db.sheets.find(query)
	sheet_points = defaultdict(int)
	for sheet in sheets:
		sheet_points[sheet["owner"]] += len(sheet["sources"]) * 50

	for l in leaders:
		points = l["count"] + sheet_points[l["user"]]
		del sheet_points[l["user"]]
		doc = {"_id": l["user"], "count": points, "date": datetime.now()}
		db[collection].save(doc)
	
	# At points for those who only have sheet points
	for s in sheet_points.items():
		doc = {"_id": s[0], "count": s[1], "date": datetime.now()}
		db[collection].save(doc)

	if cutoff:	
		db[collection].remove({"date": {"$lt": oldtime }})
Esempio n. 6
0
def update_top_contributors(days=None):
    """
    Calculate leaderboard scores for the past n 'days', or all time if 'days' is None.
    Store in a collection named for the length of time.
    Remove old scores.
    """

    if days:
        cutoff = datetime.now() - timedelta(days)
        #condition = { "date": { "$gt": cutoff }, "method": {"$ne": "API"} }
        condition = {"date": {"$gt": cutoff}}
        collection = "leaders_%d" % days
    else:
        cutoff = None
        #condition = { "method": {"$ne": "API"} }
        condition = {}
        collection = "leaders_alltime"

    leaders = make_leaderboard(condition)

    oldtime = datetime.now()

    # Tally points for Public Source Sheets
    query = {"status": {"$in": LISTED_SHEETS}}
    if cutoff:
        query["$or"] = [
            {
                "dateCreated": {
                    "$gt": cutoff.isoformat()
                }
            },
            {
                "datePublished": {
                    "$gt": cutoff.isoformat()
                }
            },
        ]
    sheets = db.sheets.find(query)
    sheet_points = defaultdict(int)
    sheet_counts = defaultdict(int)
    for sheet in sheets:
        sheet_points[sheet["owner"]] += len(sheet["sources"]) * 50
        sheet_counts[sheet["owner"]] += 1

    for l in leaders:
        points = l["count"] + sheet_points[l["user"]]
        del sheet_points[l["user"]]
        if points:
            doc = {
                "_id": l["user"],
                "count": points,
                "translateCount": int(l["translateCount"]),
                "editCount": int(l["editCount"]),
                "addCount": int(l["addCount"]),
                "noteCount": int(l["noteCount"]),
                "linkCount": int(l["linkCount"]),
                "reviewCount": int(l["reviewCount"]),
                "sheetCount": sheet_counts[l["user"]],
                "texts": sorted(l["texts"], key=lambda key: -l["texts"][key]),
                "date": datetime.now()
            }
            db[collection].save(doc)

    # Add points for those who only have sheet points
    for s in sheet_points.items():
        if s[1]:
            doc = {
                "_id": s[0],
                "count": s[1],
                "sheetCount": sheet_counts[s[0]],
                "date": datetime.now()
            }
            db[collection].save(doc)

    if cutoff:
        db[collection].remove({"date": {"$lt": oldtime}})