Ejemplo n.º 1
0
 def test_refreshing(self):
     app = flask.Flask(__name__)
     babel.Babel(app)
     d = datetime(2010, 4, 12, 13, 46)
     with app.test_request_context():
         assert babel.format_datetime(d) == 'Apr 12, 2010, 1:46:00 PM'
         app.config['BABEL_DEFAULT_TIMEZONE'] = 'Europe/Vienna'
         babel.refresh()
         assert babel.format_datetime(d) == 'Apr 12, 2010, 3:46:00 PM'
Ejemplo n.º 2
0
    def test_custom_formats(self):
        app = flask.Flask(__name__)
        app.config.update(BABEL_DEFAULT_LOCALE='en_US',
                          BABEL_DEFAULT_TIMEZONE='Pacific/Johnston')
        b = babel.Babel(app)
        b.date_formats['datetime'] = 'long'
        b.date_formats['datetime.long'] = 'MMMM d, yyyy h:mm:ss a'
        d = datetime(2010, 4, 12, 13, 46)

        with app.test_request_context():
            assert babel.format_datetime(d) == 'April 12, 2010 3:46:00 AM'
Ejemplo n.º 3
0
    def test_init_app(self):
        b = babel.Babel()
        app = flask.Flask(__name__)
        b.init_app(app)
        d = datetime(2010, 4, 12, 13, 46)

        with app.test_request_context():
            assert babel.format_datetime(d) == 'Apr 12, 2010, 1:46:00 PM'
            assert babel.format_date(d) == 'Apr 12, 2010'
            assert babel.format_time(d) == '1:46:00 PM'

        with app.test_request_context():
            app.config['BABEL_DEFAULT_TIMEZONE'] = 'Europe/Vienna'
            assert babel.format_datetime(d) == 'Apr 12, 2010, 3:46:00 PM'
            assert babel.format_date(d) == 'Apr 12, 2010'
            assert babel.format_time(d) == '3:46:00 PM'

        with app.test_request_context():
            app.config['BABEL_DEFAULT_LOCALE'] = 'de_DE'
            assert babel.format_datetime(
                d, 'long') == '12. April 2010 um 15:46:00 MESZ'
Ejemplo n.º 4
0
    def test_basics(self):
        app = flask.Flask(__name__)
        babel.Babel(app)
        d = datetime(2010, 4, 12, 13, 46)
        delta = timedelta(days=6)

        with app.test_request_context():
            assert babel.format_datetime(d) == 'Apr 12, 2010, 1:46:00 PM'
            assert babel.format_date(d) == 'Apr 12, 2010'
            assert babel.format_time(d) == '1:46:00 PM'
            assert babel.format_timedelta(delta) == '1 week'
            assert babel.format_timedelta(delta, threshold=1) == '6 days'

        with app.test_request_context():
            app.config['BABEL_DEFAULT_TIMEZONE'] = 'Europe/Vienna'
            assert babel.format_datetime(d) == 'Apr 12, 2010, 3:46:00 PM'
            assert babel.format_date(d) == 'Apr 12, 2010'
            assert babel.format_time(d) == '3:46:00 PM'

        with app.test_request_context():
            app.config['BABEL_DEFAULT_LOCALE'] = 'de_DE'
            assert babel.format_datetime(
                d, 'long') == '12. April 2010 um 15:46:00 MESZ'
Ejemplo n.º 5
0
    def test_custom_locale_selector(self):
        app = flask.Flask(__name__)
        b = babel.Babel(app)
        d = datetime(2010, 4, 12, 13, 46)

        the_timezone = 'UTC'
        the_locale = 'en_US'

        @b.localeselector
        def select_locale():
            return the_locale

        @b.timezoneselector
        def select_timezone():
            return the_timezone

        with app.test_request_context():
            assert babel.format_datetime(d) == 'Apr 12, 2010, 1:46:00 PM'

        the_locale = 'de_DE'
        the_timezone = 'Europe/Vienna'

        with app.test_request_context():
            assert babel.format_datetime(d) == '12.04.2010, 15:46:00'
Ejemplo n.º 6
0
 def test_non_initialized(self):
     app = flask.Flask(__name__)
     d = datetime(2010, 4, 12, 13, 46)
     with app.test_request_context():
         assert babel.format_datetime(d) == 'Apr 12, 2010, 1:46:00 PM'
Ejemplo n.º 7
0
def viewstats_helper(jobpost_id, interval, limit, daybatch=False):
    post = JobPost.query.get(jobpost_id)
    if not post.datetime:
        return {}
    viewed = UserJobView.query.filter_by(jobpost_id=jobpost_id).all()
    opened = [v for v in viewed if v.applied is True]
    applied = (db.session.query(
        JobApplication.created_at).filter_by(jobpost_id=jobpost_id).all())

    # Now batch them by size
    now = utcnow()
    delta = now - post.datetime
    if daybatch:
        batches, remainder = divmod(delta.days, interval)
        if delta.seconds:
            remainder = True
    else:
        batches, remainder = divmod(int(delta.total_seconds()), interval)

    if remainder or batches == 0:
        batches += 1

    cviewed = batches * [0]
    copened = batches * [0]
    capplied = batches * [0]
    cbuckets = batches * ['']

    interval_hour = interval // 3600
    # these are used as initial values for hourly stats
    # buckets are like "HH:00 - HH:00"

    # if now is 09:45, bucket ending hour will be 10:00
    to_datetime = now + timedelta(hours=1)
    # starting hour will be, 06:00, if interval is 4 hours
    from_datetime = to_datetime - timedelta(hours=interval_hour)

    for delta in range(batches):
        if daybatch:
            # here delta=0 at first, and last item is the latest date/hour
            cbuckets[batches - delta - 1] = format_datetime(
                (now - timedelta(days=delta)), 'd MMM')
        else:
            from_hour = format_datetime(from_datetime, 'd MMM HH:00')
            to_hour = format_datetime(to_datetime, 'HH:00')
            cbuckets[batches - delta - 1] = "{from_hour} — {to_hour}".format(
                from_hour=from_hour, to_hour=to_hour)
            # if current bucket was 18:00-22:00, then
            # previous bucket becomes 14:00-18:00
            to_datetime = from_datetime
            from_datetime = to_datetime - timedelta(hours=interval_hour)

    for clist, source, attr in [
        (cviewed, viewed, 'created_at'),
        (copened, opened, 'updated_at'),
        (capplied, applied, 'created_at'),
    ]:
        for item in source:
            sourcedate = getattr(item, attr)
            if sourcedate < post.datetime:
                # This happens when the user creates a post when logged in. Their 'viewed' date will be
                # for the draft, whereas the confirmed post's datetime will be later. There should
                # be just one instance of this. This can also happen if the server's clock is reset, such
                # as by an NTP error after reboot (has happened to us).
                sourcedate = post.datetime
            itemdelta = sourcedate - post.datetime
            try:
                if daybatch:
                    clist[int(itemdelta.days / interval)] += 1
                else:
                    clist[int(int(itemdelta.total_seconds()) / interval)] += 1
            except IndexError:
                # Server time got messed up. Ouch! Ignore for now.
                pass

    if limit and batches > limit:
        cviewed = cviewed[:limit]
        copened = copened[:limit]
        capplied = capplied[:limit]
        cbuckets = cbuckets[:limit]

    return {
        'max':
        max([
            max(cviewed) if cviewed else 0,
            max(copened) if copened else 0,
            max(capplied) if capplied else 0,
        ]),
        'length':
        max([len(cviewed), len(copened),
             len(capplied)]),
        'viewed':
        cviewed,
        'opened':
        copened,
        'applied':
        capplied,
        'buckets':
        cbuckets,
    }