示例#1
0
def earliest_event_by_short_url_server(short_url):
    try:
        middle = Event.get_from_key_or_date(short_url)
    except TimeGhostError as err:
        raise TimeGhostError("timeghost doesn't know your birthday")
    timeghost = TimeGhostFactory.build(middle=middle, get_earliest=True)
    return render_template('timeghost.html', timeghost=timeghost)
示例#2
0
def form_for_now_middle(fieldname, form, description, do_events=False):
    """
    Render a form, or the response to the form. Pulls out the specified fied
    and uses that to generate a TimeGhost.middle. TimeGhost.now is Event.now().
    """
    try:
        # Render requested timeghost
        if request.method == "POST":
            now = Event.now()
            middle_key_or_date = request.form[fieldname]
            middle = Event.get_from_key_or_date(middle_key_or_date, description)

            timeghost = TimeGhostFactory.build(now=now, middle=middle)
            if not do_events:
                timeghost.display_prefix = ""

            return render_template('timeghost.html', timeghost=timeghost)
        # draw the form:
        else:
            events = None
            if do_events:
                events = Event.query().order(-Event.date).fetch()
            return render_template(form, events=events)
    except TimeGhostError as err:
        return render_template('error.html', err=err), 404
示例#3
0
def timeghost_json():
    """JSON page: generate a random Timeghost and return it as a JSON object"""
    now = Event.now()
    middle = Event.get_random(before=now)
    timeghost = TimeGhostFactory.build(now=now, middle=middle)
    tg_dict = { 
        'factoid':  timeghost.factoid,
        'permalink':  timeghost.permalink
    }
    return jsonify(tg_dict)
示例#4
0
def timeghost_json():
    """JSON page: generate a random Timeghost and return it as a JSON object"""
    middle = Event.get_random(before=Event.now())
    tg = TimeGhostFactory.build(middle=middle)
    tg_dict = dict(
        factoid=tg.factoid,
        permalink=tg.permalink_fully_qualified,
        tweet="{} #timeghost {}".format(
            tg.factoid[:110], tg.permalink_fully_qualified),
    )
    return jsonify(tg_dict)
示例#5
0
def birthday_server(birthday_date=None):
    """ Generate a timeghost for a user-selected birth year."""
    fieldname = 'bday'
    form = 'birthday.html'
    description = "Your birthday"

    if birthday_date:
        middle = Event.get_from_key_or_date(birthday_date)
        timeghost = TimeGhostFactory.build(middle=middle)
        return render_template('timeghost.html', timeghost=timeghost)
    else:
        return form_for_now_middle(fieldname, form, description)
示例#6
0
def permalink_server(middle_key_urlsafe, long_ago_key_urlsafe=None):
    """
    Handles permalinks for a particular timeghost if both arguments
    are given, and TimeGhosts between now and a particular middle event if
    only the middle event key is given.
    """
    try:
        now = Event.now()
        middle = Event.get_from_key_or_date(middle_key_urlsafe)
        long_ago = None

        if long_ago_key_urlsafe is not None:
            long_ago = Event.get_from_key_or_date(long_ago_key_urlsafe)

        timeghost = TimeGhostFactory.build(now=now,
                                           middle=middle,
                                           long_ago=long_ago)
        return render_template('timeghost.html', timeghost=timeghost)
    except TimeGhostError as err:
        return render_template('error.html', err=err), 404
示例#7
0
def permalink_server(middle_key_urlsafe, long_ago_key_urlsafe=None):
    """
    Handles permalinks for a particular timeghost if both arguments
    are given, and TimeGhosts between now and a particular middle event if
    only the middle event key is given.
    """
    try:
        if middle_key_urlsafe == 'your-birthday':
            raise TimeGhostError("timeghost doesn't know your birthday")

        middle = Event.get_from_key_or_date(middle_key_urlsafe)
        long_ago = None

        if long_ago_key_urlsafe is not None:
            long_ago = Event.get_from_key_or_date(long_ago_key_urlsafe)


        tg = TimeGhostFactory.build(middle=middle, long_ago=long_ago)
        return render_template('timeghost.html', timeghost=tg)
    except TimeGhostError as err:
        return render_template('error.html', err=err), 404
示例#8
0
def fast_timeghost_server(middle_date_str=None, now_date_str=None):
    """
    Generates a random timeghost, a timeghost between now and a particular
    time, or a timeghost between two specified times, depending on the number
    of arguments given.
    """
    try:
        if now_date_str is None:
            now = Event.now()
        else:
            now = Event.build(date_str=now_date_str)

        if middle_date_str is None:
            middle = Event.get_random(before=now)
        else:
            middle = Event.build(date_str=middle_date_str)

        timeghost = TimeGhostFactory.build(middle=middle)

        return render_template('timeghost.html', timeghost=timeghost)
    except TimeGhostError as err:
        return render_template('error.html', err=err), 404
示例#9
0
def form_for_now_middle(fieldname, form, description, do_events=False, get_earliest=False):
    """
    Render a form, or the response to the form. Pulls out the specified field
    and uses that to generate a TimeGhost.middle. TimeGhost.now is Event.now().
    """
    try:
        # Render requested timeghost ('form' input seems to be ignored):
        if request.method == "POST":
            middle_key_or_date = request.form[fieldname]
            middle = Event.get_from_key_or_date(middle_key_or_date, description)
            timeghost = TimeGhostFactory.build(middle=middle, get_earliest=get_earliest)
            if not do_events:
                timeghost.display_prefix = ""

            return render_template('timeghost.html', timeghost=timeghost)
        # GET request; draw the form:
        else:
            events = None
            if do_events:
                events = Event.query().order(-Event.date).fetch()
            return render_template(form, events=events)
    except TimeGhostError as err:
        return render_template('error.html', err=err), 404
示例#10
0
def timeghost_server(middle_date_str=None, now_date_str=None):
    """
    Generates a random timeghost, a timeghost between now and a particular
    time, or a timeghost between two specified times, depending on the number
    of arguments given.
    """
    try:
        if now_date_str is None:
            now = Event.now()
        else:
            now = Event.build(date_str=now_date_str)

        if middle_date_str is None:
            middle = Event.get_random(before=now)
        else:
            middle = Event.build(date_str=middle_date_str)

        timeghost = TimeGhostFactory.build(now=now, middle=middle)
        logging.debug("output timeghost: %s", timeghost)

        return render_template('timeghost.html', timeghost=timeghost)
    except TimeGhostError as err:
        return render_template('error.html', err=err), 404
示例#11
0
def fight_club_server():
    """ Generate a timeghost for the release of Fight Club """
    fight_club = Event.get_from_key_or_date(
        'ag9zfnRpbWVnaG9zdC1hcHByEgsSBUV2ZW50GICAgICG7IcKDA')
    timeghost = TimeGhostFactory.build(middle=fight_club)
    return render_template('fight_club.html', timeghost=timeghost)