示例#1
0
文件: helpers.py 项目: fwenzel/mdn
def date_diff(timestamp, to=None):
    if not timestamp:
        return ""

    compare_with = to or datetime.date.today()
    delta = timestamp - compare_with

    if delta.days == 0:
        return u"today"
    elif delta.days == -1:
        return u"yesterday"
    elif delta.days == 1:
        return u"tomorrow"

    chunks = (
        (365.0, lambda n: ungettext("year", "years", n)),
        (30.0, lambda n: ungettext("month", "months", n)),
        (7.0, lambda n: ungettext("week", "weeks", n)),
        (1.0, lambda n: ungettext("day", "days", n)),
    )

    for i, (chunk, name) in enumerate(chunks):
        if abs(delta.days) >= chunk:
            count = abs(round(delta.days / chunk, 0))
            break

    date_str = ugettext("%(number)d %(type)s") % {"number": count, "type": name(count)}

    if delta.days > 0:
        return "in " + date_str
    else:
        return date_str + " ago"
示例#2
0
def date_diff(timestamp, to=None):
    if not timestamp:
        return ""

    compare_with = to or datetime.date.today()
    delta = timestamp - compare_with

    if delta.days == 0:
        return u"today"
    elif delta.days == -1:
        return u"yesterday"
    elif delta.days == 1:
        return u"tomorrow"

    chunks = (
        (365.0, lambda n: ungettext('year', 'years', n)),
        (30.0, lambda n: ungettext('month', 'months', n)),
        (7.0, lambda n: ungettext('week', 'weeks', n)),
        (1.0, lambda n: ungettext('day', 'days', n)),
    )

    for i, (chunk, name) in enumerate(chunks):
        if abs(delta.days) >= chunk:
            count = abs(round(delta.days / chunk, 0))
            break

    date_str = (ugettext('%(number)d %(type)s') % {
        'number': count,
        'type': name(count)
    })

    if delta.days > 0:
        return "in " + date_str
    else:
        return date_str + " ago"
示例#3
0
文件: helpers.py 项目: g1angel/kuma
def date_diff(timestamp, to=None):
    if not timestamp:
        return ""

    compare_with = to or datetime.date.today()
    delta = timestamp - compare_with

    if delta.days == 0:
        return u"today"
    elif delta.days == -1:
        return u"yesterday"
    elif delta.days == 1:
        return u"tomorrow"

    chunks = (
        (365.0, lambda n: ungettext('year', 'years', n)),
        (30.0, lambda n: ungettext('month', 'months', n)),
        (7.0, lambda n: ungettext('week', 'weeks', n)),
        (1.0, lambda n: ungettext('day', 'days', n)),
    )

    for i, (chunk, name) in enumerate(chunks):
        if abs(delta.days) >= chunk:
            count = abs(round(delta.days / chunk, 0))
            break

    date_str = (ugettext('%(number)d %(type)s') %
                        {'number': count, 'type': name(count)})

    if delta.days > 0:
        return "in " + date_str
    else:
        return date_str + " ago"
def get_locked_legend_alternate(count, level):
    if level == 1:
        return FIRST_CHALLENGE
    
    # L10n: Legend associated to a locked challenge. Example: "Complete 4 more challenges in Level 3 to unlock."
    msg = ungettext('Complete %(count)d more challenge in Level %(level)d to unlock.',
                    'Complete %(count)d more challenges in Level %(level)d to unlock.', count)

    return msg % {'count': count, 'level': level}
示例#5
0
def timesince(d, now=None):
    """Take two datetime objects and return the time between d and now as a
    nicely formatted string, e.g. "10 minutes".  If d is None or occurs after
    now, return ''.

    Units used are years, months, weeks, days, hours, and minutes. Seconds and
    microseconds are ignored.  Just one unit is displayed.  For example,
    "2 weeks" and "1 year" are possible outputs, but "2 weeks, 3 days" and "1
    year, 5 months" are not.

    Adapted from django.utils.timesince to have better i18n (not assuming
    commas as list separators and including "ago" so order of words isn't
    assumed), show only one time unit, and include seconds.

    """
    if d is None:
        return u''
    chunks = [
        (60 * 60 * 24 * 365,
         lambda n: ungettext('%(number)d year ago', '%(number)d years ago', n)
         ),
        (60 * 60 * 24 * 30, lambda n: ungettext('%(number)d month ago',
                                                '%(number)d months ago', n)),
        (60 * 60 * 24 * 7,
         lambda n: ungettext('%(number)d week ago', '%(number)d weeks ago', n)
         ),
        (60 * 60 * 24,
         lambda n: ungettext('%(number)d day ago', '%(number)d days ago', n)),
        (60 * 60,
         lambda n: ungettext('%(number)d hour ago', '%(number)d hours ago', n)
         ),
        (60, lambda n: ungettext('%(number)d minute ago',
                                 '%(number)d minutes ago', n)),
        (1, lambda n: ungettext('%(number)d second ago',
                                '%(number)d seconds ago', n))
    ]
    if not now:
        if d.tzinfo:
            now = datetime.datetime.now(LocalTimezone(d))
        else:
            now = datetime.datetime.now()

    # Ignore microsecond part of 'd' since we removed it from 'now'
    delta = now - (d - datetime.timedelta(0, 0, d.microsecond))
    since = delta.days * 24 * 60 * 60 + delta.seconds
    if since <= 0:
        # d is in the future compared to now, stop processing.
        return u''
    for i, (seconds, name) in enumerate(chunks):
        count = since // seconds
        if count != 0:
            break
    return name(count) % {'number': count}
示例#6
0
文件: helpers.py 项目: g1angel/kuma
def timesince(d, now=None):
    """Take two datetime objects and return the time between d and now as a
    nicely formatted string, e.g. "10 minutes". If d is None or occurs after
    now, return ''.

    Units used are years, months, weeks, days, hours, and minutes. Seconds and
    microseconds are ignored. Just one unit is displayed. For example,
    "2 weeks" and "1 year" are possible outputs, but "2 weeks, 3 days" and "1
    year, 5 months" are not.

    Adapted from django.utils.timesince to have better i18n (not assuming
    commas as list separators and including "ago" so order of words isn't
    assumed), show only one time unit, and include seconds.

    """
    if d is None:
        return u''
    chunks = [
        (60 * 60 * 24 * 365, lambda n: ungettext('%(number)d year ago',
                                                 '%(number)d years ago', n)),
        (60 * 60 * 24 * 30, lambda n: ungettext('%(number)d month ago',
                                                '%(number)d months ago', n)),
        (60 * 60 * 24 * 7, lambda n: ungettext('%(number)d week ago',
                                               '%(number)d weeks ago', n)),
        (60 * 60 * 24, lambda n: ungettext('%(number)d day ago',
                                           '%(number)d days ago', n)),
        (60 * 60, lambda n: ungettext('%(number)d hour ago',
                                      '%(number)d hours ago', n)),
        (60, lambda n: ungettext('%(number)d minute ago',
                                 '%(number)d minutes ago', n)),
        (1, lambda n: ungettext('%(number)d second ago',
                                 '%(number)d seconds ago', n))]
    if not now:
        if d.tzinfo:
            now = datetime.datetime.now(LocalTimezone(d))
        else:
            now = datetime.datetime.now()

    # Ignore microsecond part of 'd' since we removed it from 'now'
    delta = now - (d - datetime.timedelta(0, 0, d.microsecond))
    since = delta.days * 24 * 60 * 60 + delta.seconds
    if since <= 0:
        # d is in the future compared to now, stop processing.
        return u''
    for i, (seconds, name) in enumerate(chunks):
        count = since // seconds
        if count != 0:
            break
    return name(count) % {'number': count}
示例#7
0
def send_pending_membership_emails():
    """
    For each curated group that has pending memberships that the curators have
    not yet been emailed about, send to all the curators an email with the count
    of all pending memberships and a link to view and manage the requests.
    """
    Group = get_model('groups', 'Group')
    GroupMembership = get_model('groups', 'GroupMembership')

    # Curated groups that have pending membership requests
    groups = Group.objects.exclude(curators__isnull=True)
    groups = groups.filter(
        groupmembership__status=GroupMembership.PENDING).distinct()

    for group in groups:
        # what's the max pk of pending memberships?
        pending_memberships = group.groupmembership_set.filter(
            status=GroupMembership.PENDING)
        max_pk = pending_memberships.aggregate(max_pk=Max('pk'))['max_pk']
        # Only send reminder if there are newer requests than we'd previously reminded about
        if max_pk > group.max_reminder:
            # TODO: Switch locale to curator's preferred language so translation will occur
            # Using English for now
            tower.activate('en-us')

            count = pending_memberships.count()
            subject = tower.ungettext(
                '%(count)d outstanding request to join Mozillians group "%(name)s"',
                '%(count)d outstanding requests to join Mozillians group "%(name)s"',
                count) % {
                    'count': count,
                    'name': group.name
                }
            body = render_to_string('groups/email/memberships_pending.txt', {
                'group': group,
                'count': count,
            })

            send_mail(subject,
                      body,
                      settings.FROM_NOREPLY,
                      [profile.user.email for profile in group.curators.all()],
                      fail_silently=False)

            group.max_reminder = max_pk
            group.save()
示例#8
0
def send_pending_membership_emails():
    """
    For each curated group that has pending memberships that the curators have
    not yet been emailed about, send to all the curators an email with the count
    of all pending memberships and a link to view and manage the requests.
    """
    Group = get_model('groups', 'Group')
    GroupMembership = get_model('groups', 'GroupMembership')

    # Curated groups that have pending membership requests
    groups = Group.objects.exclude(curators__isnull=True)
    groups = groups.filter(groupmembership__status=GroupMembership.PENDING).distinct()

    for group in groups:
        # what's the max pk of pending memberships?
        pending_memberships = group.groupmembership_set.filter(status=GroupMembership.PENDING)
        max_pk = pending_memberships.aggregate(max_pk=Max('pk'))['max_pk']
        # Only send reminder if there are newer requests than we'd previously reminded about
        if max_pk > group.max_reminder:
            # TODO: Switch locale to curator's preferred language so translation will occur
            # Using English for now
            tower.activate('en-us')

            count = pending_memberships.count()
            subject = tower.ungettext(
                '%(count)d outstanding request to join Mozillians group "%(name)s"',
                '%(count)d outstanding requests to join Mozillians group "%(name)s"',
                count
            ) % {
                'count': count,
                'name': group.name
            }
            body = render_to_string('groups/email/memberships_pending.txt', {
                'group': group,
                'count': count,
            })

            send_mail(subject, body, settings.FROM_NOREPLY,
                      [profile.user.email for profile in group.curators.all()],
                      fail_silently=False)

            group.max_reminder = max_pk
            group.save()
示例#9
0
文件: views.py 项目: zctyhj/kitsune
def delete(request, template, msgid=None, msgtype='inbox'):
    if msgid:
        msgids = [msgid]
    else:
        try:
            msgids = [int(m) for m in request.POST.getlist('id')]
        except ValueError:
            return HttpResponseBadRequest()

    if msgtype == 'inbox':
        messages = InboxMessage.objects.filter(pk__in=msgids, to=request.user)
    else:
        messages = OutboxMessage.objects.filter(pk__in=msgids,
                                                sender=request.user)

    if request.method == 'POST' and 'confirmed' in request.POST:
        if messages.count() != len(msgids):
            contrib_messages.add_message(
                request, contrib_messages.ERROR,
                _("Messages didn't add up. Try again."))
        else:
            messages.delete()
            msg = ungettext(u'The message was deleted!',
                            u'The messages were deleted!', len(msgids))
            contrib_messages.add_message(request, contrib_messages.SUCCESS,
                                         msg)

        if request.is_ajax():
            return HttpResponse(json.dumps({'message': m} for m in messages))

        return HttpResponseRedirect(reverse('messages.{t}'.format(t=msgtype)))

    if msgtype == 'outbox':
        for message in messages:
            _add_recipients(message)

    return render(request, template, {
        'msgs': messages,
        'msgid': msgid,
        'msgtype': msgtype
    })
示例#10
0
def delete(request, template, msgid=None, msgtype='inbox'):
    if msgid:
        msgids = [msgid]
    else:
        try:
            msgids = [int(m) for m in request.POST.getlist('id')]
        except ValueError:
            return HttpResponseBadRequest()

    if msgtype == 'inbox':
        messages = InboxMessage.objects.filter(pk__in=msgids, to=request.user)
    else:
        messages = OutboxMessage.objects.filter(pk__in=msgids,
                                                sender=request.user)

    if request.method == 'POST' and 'confirmed' in request.POST:
        if messages.count() != len(msgids):
            contrib_messages.add_message(request, contrib_messages.ERROR,
                                         _("Messages didn't add up. Try again."))
        else:
            messages.delete()
            msg = ungettext(u'The message was deleted!',
                            u'The messages were deleted!',
                            len(msgids))
            contrib_messages.add_message(request, contrib_messages.SUCCESS,
                                         msg)

        if request.is_ajax():
            return HttpResponse(json.dumps({'message': m} for m in messages))

        return HttpResponseRedirect(reverse('messages.{t}'.format(t=msgtype)))

    if msgtype == 'outbox':
        for message in messages:
            _add_recipients(message)

    return render(request, template, {
        'msgs': messages, 'msgid': msgid, 'msgtype': msgtype})
def get_locked_legend(count, level):
    # L10n: Legend associated to a locked challenge. Example: "You must complete at least 1 challenge in Level 1 to unlock this level."
    msg = ungettext('You must complete at least %(count)d challenge in Level %(level)d to unlock this level.',
                    'You must complete at least %(count)d challenges in Level %(level)d to unlock this level.', count)

    return msg % {'count': count, 'level': level}
示例#12
0
def get_locked_legend(count, level):
    # L10n: Legend associated to a locked challenge. Example: "Complete 4 more challenges in level 3 to unlock."
    msg = ungettext('Complete %(count)d more challenge in level %(level)d to unlock.',
                    'Complete %(count)d more challenges in level %(level)d to unlock.', count)
    
    return msg % {'count': count, 'level': level}