예제 #1
0
def format_time(date):
    return {
        '12:00AM': 'Midnight',
        '12:00PM': 'Midday'
    }.get(
        utc_string_to_aware_gmt_datetime(date).strftime('%-I:%M%p'),
        utc_string_to_aware_gmt_datetime(date).strftime('%-I:%M%p')).lower()
예제 #2
0
def format_time(date):
    return ({
        "12:00AM": "Midnight",
        "12:00PM": "Midday"
    }.get(
        utc_string_to_aware_gmt_datetime(date).strftime("%-I:%M%p"),
        utc_string_to_aware_gmt_datetime(date).strftime("%-I:%M%p"),
    ).lower())
def get_service_verify_reply_to_address_partials(service_id, notification_id):
    form = ServiceReplyToEmailForm()
    first_email_address = current_service.count_email_reply_to_addresses == 0
    notification = notification_api_client.get_notification(
        current_app.config["NOTIFY_SERVICE_ID"], notification_id)
    replace = request.args.get('replace', False)
    replace = False if replace == "False" else replace
    existing_is_default = False
    if replace:
        existing = current_service.get_email_reply_to_address(replace)
        existing_is_default = existing['is_default']
    verification_status = "pending"
    is_default = True if (request.args.get('is_default', False)
                          == "True") else False
    if notification["status"] in DELIVERED_STATUSES:
        verification_status = "success"
        if notification["to"] not in [
                i["email_address"]
                for i in current_service.email_reply_to_addresses
        ]:
            if replace:
                service_api_client.update_reply_to_email_address(
                    current_service.id,
                    replace,
                    email_address=notification["to"],
                    is_default=is_default)
            else:
                service_api_client.add_reply_to_email_address(
                    current_service.id,
                    email_address=notification["to"],
                    is_default=is_default)
    seconds_since_sending = (
        utc_string_to_aware_gmt_datetime(datetime.utcnow().isoformat()) -
        utc_string_to_aware_gmt_datetime(notification['created_at'])).seconds
    if notification["status"] in FAILURE_STATUSES or (
            notification["status"] in SENDING_STATUSES
            and seconds_since_sending >
            current_app.config['REPLY_TO_EMAIL_ADDRESS_VALIDATION_TIMEOUT']):
        verification_status = "failure"
        form.email_address.data = notification['to']
        form.is_default.data = is_default
    return {
        'status':
        render_template(
            'views/service-settings/email-reply-to/_verify-updates.html',
            reply_to_email_address=notification["to"],
            service_id=current_service.id,
            notification_id=notification_id,
            verification_status=verification_status,
            is_default=is_default,
            existing_is_default=existing_is_default,
            form=form,
            first_email_address=first_email_address,
            replace=replace),
        'stop':
        0 if verification_status == "pending" else 1
    }
def get_letter_timings(upload_time, postage):

    LetterTimings = namedtuple(
        'LetterTimings',
        'printed_by, is_printed, earliest_delivery, latest_delivery')

    # shift anything after 5:30pm to the next day
    processing_day = utc_string_to_aware_gmt_datetime(upload_time) + timedelta(
        hours=6, minutes=30)
    print_day = get_next_dvla_working_day(processing_day)

    earliest_delivery, latest_delivery = get_earliest_and_latest_delivery(
        print_day, postage)

    # print deadline is 3pm BST
    printed_by = set_gmt_hour(print_day, hour=15)
    now = datetime.utcnow().replace(tzinfo=pytz.utc).astimezone(
        pytz.timezone('Europe/London'))

    return LetterTimings(
        printed_by=printed_by,
        is_printed=(now > printed_by),
        earliest_delivery=set_gmt_hour(earliest_delivery, hour=16),
        latest_delivery=set_gmt_hour(latest_delivery, hour=16),
    )
예제 #5
0
def get_letter_timings(upload_time):

    LetterTimings = namedtuple(
        'LetterTimings',
        'printed_by, is_printed, earliest_delivery, latest_delivery'
    )

    # shift anything after 5pm to the next day
    processing_day = utc_string_to_aware_gmt_datetime(upload_time) + timedelta(hours=(7))

    print_day, earliest_delivery, latest_delivery = (
        processing_day + timedelta(days=days)
        for days in {
            'Wednesday': (1, 3, 5),
            'Thursday': (1, 4, 5),
            'Friday': (3, 5, 6),
            'Saturday': (2, 4, 5),
        }.get(processing_day.strftime('%A'), (1, 3, 4))
    )

    printed_by = set_gmt_hour(print_day, hour=15)
    now = datetime.utcnow().replace(tzinfo=pytz.timezone('Australia/Sydney'))

    return LetterTimings(
        printed_by=printed_by,
        is_printed=(now > printed_by),
        earliest_delivery=set_gmt_hour(earliest_delivery, hour=16),
        latest_delivery=set_gmt_hour(latest_delivery, hour=16),
    )
def get_letter_timings(upload_time, postage='second'):

    LetterTimings = namedtuple(
        'LetterTimings',
        'printed_by, is_printed, earliest_delivery, latest_delivery')

    # shift anything after 5:30pm to the next day
    processing_day = utc_string_to_aware_gmt_datetime(upload_time) + timedelta(
        hours=6, minutes=30)

    def next_monday(date):
        """
        If called with a monday, will still return the next monday
        """
        return date + timedelta(days=7 - date.weekday())

    def get_next_dvla_working_day(date):
        """
        Printing takes place monday to friday
        """
        # monday to thursday inclusive
        if 0 <= date.weekday() <= 3:
            return date + timedelta(days=1)
        else:
            return next_monday(date)

    def get_next_royal_mail_working_day(date):
        """
        Royal mail deliver letters on monday to saturday
        """
        # monday to friday inclusive
        if 0 <= date.weekday() <= 4:
            return date + timedelta(days=1)
        else:
            return next_monday(date)

    print_day = get_next_dvla_working_day(processing_day)

    # first class post is printed earlier in the day, so will actually transit on the printing day,
    # and be posted the next day
    transit_day = get_next_royal_mail_working_day(print_day)
    if postage == 'first':
        earliest_delivery = latest_delivery = transit_day
    else:
        # second class has one day in transit, then a two day delivery window
        earliest_delivery = get_next_royal_mail_working_day(transit_day)
        latest_delivery = get_next_royal_mail_working_day(earliest_delivery)

    # print deadline is 3pm EST
    printed_by = set_gmt_hour(print_day, hour=15)
    now = datetime.utcnow().replace(tzinfo=pytz.utc).astimezone(
        pytz.timezone('America/Toronto'))

    return LetterTimings(
        printed_by=printed_by,
        is_printed=(now > printed_by),
        earliest_delivery=set_gmt_hour(earliest_delivery, hour=16),
        latest_delivery=set_gmt_hour(latest_delivery, hour=16),
    )
예제 #7
0
def format_delta(date):
    delta = (datetime.now(
        timezone.utc)) - (utc_string_to_aware_gmt_datetime(date))
    if delta < timedelta(seconds=30):
        return "just now"
    if delta < timedelta(seconds=60):
        return "in the last minute"
    return naturaltime_without_indefinite_article(delta)
예제 #8
0
def format_delta_days(date):
    now = datetime.now(timezone.utc)
    date = utc_string_to_aware_gmt_datetime(date)
    if date.strftime('%Y-%m-%d') == now.strftime('%Y-%m-%d'):
        return "today"
    if date.strftime('%Y-%m-%d') == (now - timedelta(days=1)).strftime('%Y-%m-%d'):
        return "yesterday"
    return naturaltime_without_indefinite_article(now - date)
def printing_today_or_tomorrow(created_at):
    print_cutoff = convert_bst_to_utc(
        convert_utc_to_bst(datetime.utcnow()).replace(
            hour=17, minute=30)).replace(tzinfo=pytz.utc)
    created_at = utc_string_to_aware_gmt_datetime(created_at)

    if created_at < print_cutoff:
        return 'today'
    else:
        return 'tomorrow'
 def letter_printing_statement(self):
     if self.upload_type != 'letter_day':
         raise TypeError()
     return get_letter_printing_statement(
         'created',
         # We have to make the time just before 5:30pm because a
         # letter uploaded at 5:30pm will be printed the next day
         (utc_string_to_aware_gmt_datetime(self.created_at) -
          timedelta(minutes=1)).astimezone(pytz.utc).isoformat(),
         long_form=False,
     )
예제 #11
0
def get_human_day(time):

    #  Add 1 minute to transform 00:00 into ‘midnight today’ instead of ‘midnight tomorrow’
    date = (utc_string_to_aware_gmt_datetime(time) - timedelta(minutes=1)).date()
    if date == (datetime.utcnow() + timedelta(days=1)).date():
        return 'tomorrow'
    if date == datetime.utcnow().date():
        return 'today'
    if date == (datetime.utcnow() - timedelta(days=1)).date():
        return 'yesterday'
    return _format_datetime_short(date)
def format_delta(date):
    delta = (datetime.now(
        timezone.utc)) - (utc_string_to_aware_gmt_datetime(date))
    if delta < timedelta(seconds=30):
        return "just now"
    if delta < timedelta(seconds=60):
        return "in the last minute"
    return ago.human(
        delta,
        future_tense='{} from now',  # No-one should ever see this
        past_tense='{} ago',
        precision=1)
def get_letter_timings(upload_time, postage='second'):

    LetterTimings = namedtuple(
        'LetterTimings',
        'printed_by, is_printed, earliest_delivery, latest_delivery'
    )

    # shift anything after 5:30pm to the next day
    processing_day = utc_string_to_aware_gmt_datetime(upload_time) + timedelta(hours=6, minutes=30)

    def get_next_work_day(date, non_working_days):
        next_day = date + timedelta(days=1)
        if non_working_days.is_work_day(
            date=next_day.date(),
            division=BankHolidays.ENGLAND_AND_WALES,
        ):
            return next_day
        return get_next_work_day(next_day, non_working_days)

    def get_next_dvla_working_day(date):
        """
        Printing takes place monday to friday, excluding bank holidays
        """
        return get_next_work_day(date, non_working_days=non_working_days_dvla)

    def get_next_royal_mail_working_day(date):
        """
        Royal mail deliver letters on monday to saturday
        """
        return get_next_work_day(date, non_working_days=non_working_days_royal_mail)

    print_day = get_next_dvla_working_day(processing_day)

    # first class post is printed earlier in the day, so will actually transit on the printing day,
    # and be posted the next day
    transit_day = get_next_royal_mail_working_day(print_day)
    if postage == 'first':
        earliest_delivery = latest_delivery = transit_day
    else:
        # second class has one day in transit, then a two day delivery window
        earliest_delivery = get_next_royal_mail_working_day(transit_day)
        latest_delivery = get_next_royal_mail_working_day(earliest_delivery)

    # print deadline is 3pm BST
    printed_by = set_gmt_hour(print_day, hour=15)
    now = datetime.utcnow().replace(tzinfo=pytz.utc).astimezone(pytz.timezone('Europe/London'))

    return LetterTimings(
        printed_by=printed_by,
        is_printed=(now > printed_by),
        earliest_delivery=set_gmt_hour(earliest_delivery, hour=16),
        latest_delivery=set_gmt_hour(latest_delivery, hour=16),
    )
def get_letter_printing_statement(status, created_at):
    created_at_dt = parser.parse(created_at).replace(tzinfo=None)

    if letter_can_be_cancelled(status, created_at_dt):
        return 'Printing starts {} at 5:30pm'.format(
            printing_today_or_tomorrow())
    else:
        printed_datetime = utc_string_to_aware_gmt_datetime(
            created_at) + timedelta(hours=6, minutes=30)
        printed_date = _format_datetime_short(printed_datetime)

        return 'Printed on {}'.format(printed_date)
예제 #15
0
def get_letter_printing_statement(status, created_at):
    created_at_dt = parser.parse(created_at).replace(tzinfo=None)
    if letter_can_be_cancelled(status, created_at_dt):
        return 'Printing starts {} at 5:30pm'.format(printing_today_or_tomorrow())
    else:
        printed_datetime = utc_string_to_aware_gmt_datetime(created_at) + timedelta(hours=6, minutes=30)
        if printed_datetime.date() == datetime.now().date():
            return 'Printed today at 5:30pm'
        elif printed_datetime.date() == datetime.now().date() - timedelta(days=1):
            return 'Printed yesterday at 5:30pm'

        printed_date = printed_datetime.strftime('%d %B').lstrip('0')

        return 'Printed on {} at 5:30pm'.format(printed_date)
예제 #16
0
    def letter_job_can_be_cancelled(self):

        if self.template['template_type'] != 'letter':
            return False

        if any(self.uncancellable_notifications):
            return False

        if not letter_can_be_cancelled(
            'created',
            utc_string_to_aware_gmt_datetime(self.created_at).replace(tzinfo=None)
        ):
            return False

        return True
예제 #17
0
def get_letter_printing_statement(status, created_at, long_form=True):
    created_at_dt = parser.parse(created_at).replace(tzinfo=None)
    if letter_can_be_cancelled(status, created_at_dt):
        decription = 'Printing starts' if long_form else 'Printing'
        return f'{decription} {printing_today_or_tomorrow(created_at)} at 5:30pm'
    else:
        printed_datetime = utc_string_to_aware_gmt_datetime(created_at) + timedelta(hours=6, minutes=30)
        if printed_datetime.date() == datetime.now().date():
            return 'Printed today at 5:30pm'
        elif printed_datetime.date() == datetime.now().date() - timedelta(days=1):
            return 'Printed yesterday at 5:30pm'

        printed_date = printed_datetime.strftime('%d %B').lstrip('0')
        description = 'Printed on' if long_form else 'Printed'

        return f'{description} {printed_date} at 5:30pm'
예제 #18
0
def get_human_day(time):

    #  Add 1 minute to transform 00:00 into ‘midnight today’ instead of ‘midnight tomorrow’
    date = (utc_string_to_aware_gmt_datetime(time) -
            timedelta(minutes=1)).date()
    now = datetime.utcnow()

    if date == (now + timedelta(days=1)).date():
        return 'tomorrow'
    if date == now.date():
        return 'today'
    if date == (now - timedelta(days=1)).date():
        return 'yesterday'
    if date.strftime('%Y') != now.strftime('%Y'):
        return '{} {}'.format(_format_datetime_short(date),
                              date.strftime('%Y'))
    return _format_datetime_short(date)
예제 #19
0
def format_delta(_date):
    lang = get_current_locale(current_app)
    date = utc_string_to_aware_gmt_datetime(_date)
    now = datetime.now(timezone.utc)
    return timeago.format(date, now, lang)
예제 #20
0
def format_date_short(date):
    return _format_datetime_short(utc_string_to_aware_gmt_datetime(date))
예제 #21
0
def format_date_normal(date):
    return utc_string_to_aware_gmt_datetime(date).strftime('%d %B %Y').lstrip('0')
예제 #22
0
def format_date(date):
    return utc_string_to_aware_gmt_datetime(date).strftime('%A %d %B %Y')
예제 #23
0
def format_time_24h(date):
    return utc_string_to_aware_gmt_datetime(date).strftime('%H:%M')
예제 #24
0
def format_date_numeric(date):
    return utc_string_to_aware_gmt_datetime(date).strftime('%Y-%m-%d')
예제 #25
0
def format_day_of_week(date):
    return utc_string_to_aware_gmt_datetime(date).strftime('%A')
예제 #26
0
def test_utc_string_to_aware_gmt_datetime_handles_summer_and_winter(
    naive_time,
    expected_aware_hour,
):
    assert utc_string_to_aware_gmt_datetime(naive_time).strftime('%H:%M') == expected_aware_hour
예제 #27
0
def test_utc_string_to_aware_gmt_datetime_rejects_bad_input(input_value):
    with pytest.raises(Exception):
        utc_string_to_aware_gmt_datetime(input_value)
예제 #28
0
 def password_changed_more_recently_than(self, datetime_string):
     if not self.password_changed_at:
         return False
     return utc_string_to_aware_gmt_datetime(
         self.password_changed_at) > utc_string_to_aware_gmt_datetime(
             datetime_string)
예제 #29
0
 def processing_started(self):
     if not self._dict.get('processing_started'):
         return None
     return utc_string_to_aware_gmt_datetime(self._dict['processing_started'])
예제 #30
0
def test_utc_string_to_aware_gmt_datetime_accepts_datetime_objects():
    input_value = datetime(2017, 5, 12, 14, 0)
    expected = '2017-05-12T15:00:00+01:00'
    assert utc_string_to_aware_gmt_datetime(
        input_value).isoformat() == expected