Skip to content
This repository has been archived by the owner on Jun 27, 2023. It is now read-only.

Commit

Permalink
Good messaging for time limits.
Browse files Browse the repository at this point in the history
  • Loading branch information
Matir committed Mar 23, 2014
1 parent 5c9d99b commit 770b8e7
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 14 deletions.
17 changes: 11 additions & 6 deletions templates/error.html
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
{% extends "base.html" %}
{% block title %}{{title}}{% endblock %}
{% block body %}
{% if exc.code == 404 %}
<div class='alert alert-danger'>{{request.path}} was not found.</div>
{% elif exc.code == 403 %}
<div class='alert alert-danger'>Access to {{request.path}} denied.</div>
{% elif exc.code == 500 %}
<div class='alert alert-danger'>An internal error occurred.</div>
{% if exc %}
{% if exc.code == 404 %}
<div class='alert alert-danger'>{{request.path}} was not found.</div>
{% elif exc.code == 403 %}
<div class='alert alert-danger'>Access to {{request.path}} denied.</div>
{% elif exc.code == 500 %}
<div class='alert alert-danger'>An internal error occurred.</div>
{% endif %}
{% endif %}
{% if message %}
<div class='alert alert-warning'>{{message}}</div>
{% endif %}
{% endblock %}
33 changes: 25 additions & 8 deletions utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,26 +29,43 @@ def countdown(cls, end=False):
return until - datetime.datetime.utcnow()

@classmethod
def open(cls, after_end=False):
"""Is the game open? If after_end, keeps open."""
def state(cls):
now = datetime.datetime.utcnow()
if cls.start and cls.start > now:
return False
if after_end:
return True
return 'BEFORE'
if cls.end and cls.end < now:
return False
return True
return 'AFTER'
return 'DURING'

@classmethod
def open(cls, after_end=False):
"""Is the game open? If after_end, keeps open."""
state = cls.state()
if state == 'DURING' or (after_end and state == 'AFTER'):
return True
return False

@classmethod
def require_open(cls, f, after_end=False):
"""Decorator for requiring the game is open."""
@functools.wraps(f)
def wrapper(*args, **kwargs):
if cls.open(after_end):
return f(*args, **kwargs)
flask.abort(403)
return flask.make_response(
flask.render_template('error.html',
message=cls.message(), title='Forbidden'), 403)
return wrapper

@classmethod
def message(cls):
state = cls.state()
if state == 'BEFORE':
return 'Game begins in %s.' % str(cls.countdown())
if state == 'AFTER':
return 'Game is over.'
return '%s left in the game.' % str(cls.countdown(end=True))

@staticmethod
def _parsedate(datestr):
if dateutil:
Expand Down

0 comments on commit 770b8e7

Please sign in to comment.