示例#1
0
def add_game(request):
    """
    add a new game record according to the given form,
    if it is not in the weekday, do nothing;
    if it happens within one day, do nothing;
    user login is required.
    """
    # after adding new game, redirect to wishes page
    response = redirect('wishes')
    # if it is weekend, directly display the flash message
    if weekend():
        messages.info(request, _("Give me a break, do it on workdays!"))
    else:
        # if add game cookie is set and the consecutive operation is within one day,
        # display the flash message
        # if user doesn't type anything, display the flash message
        if (settings.COOKIE_ADD_GAME_TIME in request.COOKIES) and \
                (one_day_limit(datetime.datetime.strptime(request.COOKIES[settings.COOKIE_ADD_GAME_TIME], settings.COOKIE_TIME_FORMAT))):
            messages.info(request, _("One day's limit, try it tomorrow, or buy me a coffee!"))
        else:
            postdata = request.POST.copy()
            title = postdata.get("game", "")
            link = postdata.get("amazon", "")
            # if user doens't type anything, display the flash message
            if title == "" or title.strip() == "":
                messages.info(request, _("Game title cannot be empty, try something meaningful!"))
            else:
                try:
                    # if the game existed, case insensitively compare
                    game = Game.objects.get(title__iexact=title)
                except Game.DoesNotExist:
                    # if the game doesn't exist, create a new record, with title case
                    game = new_game(title.title(), link)
                    messages.info(request, _("Game '%s' has been added successfully!" % game.title))
                    # set cookie expiration is one day
                    response.set_cookie(settings.COOKIE_ADD_GAME_TIME, datetime.datetime.now(), expires=settings.COOKIE_EXPIRATION)
                else:
                    # otherwise display the game has been added
                    messages.info(request, _("Game '%s' already existed! You don't want to buy it twice, don't you?" % game.title))
    return response
示例#2
0
def thumb_up(request, game_id):
    """
    add one vote to the given game,
    if it is not in the weekday, do nothing;
    if it happens within one day, do nothing;
    """
    assert game_id, "game id cannot be empty"
    
    # after voting the game, redirect to wishes page
    response = redirect("wishes")
    # if it is weekend, directly display the flash message
    if weekend():
        messages.info(request, _("Give me a break, do it on workdays!"))
    else:
        # if vote game cookie is set and the consecutive operation is within one day,
        # display the flash message
        if (settings.COOKIE_VOTE_GAME_TIME in request.COOKIES) and \
                (one_day_limit(datetime.datetime.strptime(request.COOKIES[settings.COOKIE_VOTE_GAME_TIME], settings.COOKIE_TIME_FORMAT))):
            messages.info(request, _("One day's limit, try it tomorrow, or buy me a coffee!"))
        else:
            vote_plus(game_id)
            messages.info(request, _("Vote has been submitted successfully, stay tuned!"))
            response.set_cookie(settings.COOKIE_VOTE_GAME_TIME, datetime.datetime.now(), expires=settings.COOKIE_EXPIRATION)
    return response