Beispiel #1
0
def post_saved(instance, **kwargs):
    if not settings.FORUM_DISABLE_NOTIFICATIONS:
        notify_topic_subscribers(instance)

        if utils.get_bb_profile(instance.author).autosubscribed and \
                perms.may_subscribe_topic(instance.author, instance.topic):
            instance.topic.subscriber.add(instance.author)

    if kwargs['created']:
        profile = utils.get_bb_profile(instance.author)
        profile.post_count = instance.author.posts.count()
        profile.save()
Beispiel #2
0
def post_deleted(instance, **kwargs):
    Profile = utils.get_pybb_profile_model()
    User = compat.get_user_model()
    try:
        profile = utils.get_bb_profile(instance.author)
    except (Profile.DoesNotExist, User.DoesNotExist) as e:
        # When we cascade delete an user, profile and posts are also deleted
        pass
    else:
        profile.post_count = instance.author.posts.count()
        profile.save()
Beispiel #3
0
def notify_topic_subscribers(post):
    """
    邮件订阅提醒通过email送出
    :param post:
    :return:
    """
    topic = post.topic
    if post != topic.head:
        old_lang = translation.get_language()

        # Define constants for templates rendering
        delete_url = reverse('bb:delete_subscription', args=[post.topic.id])
        current_site = Site.objects.get_current()
        from_email = settings.DEFAULT_FROM_EMAIL

        subject = render_to_string('mail/subscription_email_subject.html', {
            'site': current_site,
            'post': post
        })
        # Email subject *must not* contain newlines
        subject = ''.join(subject.splitlines())

        mails = tuple()
        for user in topic.subscribers.exclude(pk=post.user.pk):
            try:
                validate_email(user.email)
            except:
                # Invalid email
                continue

            if user.email == '*****@*****.**' % getattr(
                    user, compat.get_username_field()):
                continue

            lang = utils.get_bb_profile(
                user).language or settings.LANGUAGE_CODE
            translation.activate(lang)

            message = render_to_string(
                'mail/subscription_email_body.html', {
                    'site': current_site,
                    'post': post,
                    'delete_url': delete_url,
                    'user': user
                })
            mails += ((subject, message, from_email, [user.email]), )

        # Send mails
        send_mass_mail(mails, fail_silently=True)

        # Reactivate previous language
        translation.activate(old_lang)
Beispiel #4
0
def notify_topic_subscribers(post):
    """
    邮件订阅提醒通过email送出
    :param post:
    :return:
    """
    topic = post.topic
    if post != topic.head:
        old_lang = translation.get_language()

        # Define constants for templates rendering
        delete_url = reverse('bb:delete_subscription', args=[post.topic.id])
        current_site = Site.objects.get_current()
        from_email = settings.DEFAULT_FROM_EMAIL

        subject = render_to_string('mail/subscription_email_subject.html',
                                   {'site': current_site,
                                    'post': post})
        # Email subject *must not* contain newlines
        subject = ''.join(subject.splitlines())

        mails = tuple()
        for user in topic.subscribers.exclude(pk=post.user.pk):
            try:
                validate_email(user.email)
            except:
                # Invalid email
                continue

            if user.email == '*****@*****.**' % getattr(user, compat.get_username_field()):
                continue

            lang = utils.get_bb_profile(user).language or settings.LANGUAGE_CODE
            translation.activate(lang)

            message = render_to_string('mail/subscription_email_body.html',
                                       {'site': current_site,
                                        'post': post,
                                        'delete_url': delete_url,
                                        'user': user})
            mails += ((subject, message, from_email, [user.email]),)

        # Send mails
        send_mass_mail(mails, fail_silently=True)

        # Reactivate previous language
        translation.activate(old_lang)
Beispiel #5
0
    def render(self, context):
        context_time = self.time.resolve(context)

        delta = tznow() - context_time
        today = tznow().replace(hour=0, minute=0, second=0)
        yesterday = today - timedelta(days=1)
        tomorrow = today + timedelta(days=1)

        if delta.days == 0:
            if delta.seconds < 60:
                if pytils_enabled and context.get('LANGUAGE_CODE',
                                                  '').startswith('ru'):
                    msg = _('seconds ago,seconds ago,seconds ago')
                    msg = pytils.numeral.choose_plural(delta.seconds, msg)
                else:
                    msg = _('seconds ago')
                return '%d %s' % (delta.seconds, msg)

            elif delta.seconds < 3600:
                minutes = int(delta.seconds / 60)
                if pytils_enabled and context.get('LANGUAGE_CODE',
                                                  '').startswith('ru'):
                    msg = _('minutes ago,minutes ago,minutes ago')
                    msg = pytils.numeral.choose_plural(minutes, msg)
                else:
                    msg = _('minutes ago')
                return '%d %s' % (minutes, msg)
        if context['user'].is_authenticated():
            if time.daylight:
                tz1 = time.altzone
            else:
                tz1 = time.timezone
            tz = tz1 + utils.get_bb_profile(
                context['user']).time_zone * 60 * 60
            context_time = context_time + timedelta(seconds=tz)
        if today < context_time < tomorrow:
            return _('today, %s') % context_time.strftime('%H:%M')
        elif yesterday < context_time < today:
            return _('yesterday, %s') % context_time.strftime('%H:%M')
        else:
            return dateformat.format(context_time, 'd M, Y H:i')
Beispiel #6
0
    def render(self, context):
        context_time = self.time.resolve(context)

        delta = tznow() - context_time
        today = tznow().replace(hour=0, minute=0, second=0)
        yesterday = today - timedelta(days=1)
        tomorrow = today + timedelta(days=1)

        if delta.days == 0:
            if delta.seconds < 60:
                if pytils_enabled and context.get('LANGUAGE_CODE', '').startswith('ru'):
                    msg = _('seconds ago,seconds ago,seconds ago')
                    msg = pytils.numeral.choose_plural(delta.seconds, msg)
                else:
                    msg = _('seconds ago')
                return '%d %s' % (delta.seconds, msg)

            elif delta.seconds < 3600:
                minutes = int(delta.seconds / 60)
                if pytils_enabled and context.get('LANGUAGE_CODE', '').startswith('ru'):
                    msg = _('minutes ago,minutes ago,minutes ago')
                    msg = pytils.numeral.choose_plural(minutes, msg)
                else:
                    msg = _('minutes ago')
                return '%d %s' % (minutes, msg)
        if context['user'].is_authenticated():
            if time.daylight:
                tz1 = time.altzone
            else:
                tz1 = time.timezone
            tz = tz1 + utils.get_bb_profile(context['user']).time_zone * 60 * 60
            context_time = context_time + timedelta(seconds=tz)
        if today < context_time < tomorrow:
            return _('today, %s') % context_time.strftime('%H:%M')
        elif yesterday < context_time < today:
            return _('yesterday, %s') % context_time.strftime('%H:%M')
        else:
            return dateformat.format(context_time, 'd M, Y H:i')
Beispiel #7
0
def bb_get_profile(*args, **kwargs):
    try:
        return utils.get_bb_profile(kwargs.get('user') or args[0])
    except:
        return utils.get_pybb_profile_model().objects.none()
Beispiel #8
0
def bb_get_profile(*args, **kwargs):
    try:
        return utils.get_bb_profile(kwargs.get('user') or args[0])
    except:
        return utils.get_pybb_profile_model().objects.none()