Ejemplo n.º 1
0
def claim_notification(sender, **kwargs):
    """
    Оповещение о претензиях.

    """

    claim = kwargs['claim']
    request = kwargs['request']
    creation = kwargs['creation']
    score = claim.score

    theme = _('New claim') if creation else _('Delete claim')

    subject = _('%(prefix)s%(monitoring)s - %(org)s: %(code)s - %(theme)s') % {
        'prefix': config_value('EmailServer', 'EMAIL_SUBJECT_PREFIX'),
        'monitoring': score.task.organization.monitoring,
        'org': score.task.organization.name.split(':')[0],
        'code': score.parameter.code,
        'theme': theme,
    }

    url = '%s://%s%s' % (request.is_secure() and 'https' or 'http',
                         request.get_host(),
                         reverse('exmo2010:score_view',
                                 args=[score.pk]))

    t_plain = loader.get_template('score_claim.txt')
    t_html = loader.get_template('score_claim.html')

    c = Context({'score': score,
                 'claim': claim,
                 'url': url,
                 'creation': creation,
                 'current_user': request.user.userprofile.legal_name,
                 })

    message_plain = t_plain.render(c)
    message_html = t_html.render(c)

    headers = {
        'X-iifd-exmo': 'claim_notification'
    }

    recipients = list(get_experts().values_list('email', flat=True))

    if score.task.user.email and score.task.user.email not in recipients:
        recipients.append(score.task.user.email)

    for r in recipients:
        email = EmailMultiAlternatives(subject,
                                       message_plain,
                                       config_value('EmailServer', 'DEFAULT_FROM_EMAIL'),
                                       [r],
                                       [],
                                       headers=headers,)
        email.attach_alternative(message_html, "text/html")
        email.send()
Ejemplo n.º 2
0
def comment_notification(sender, **kwargs):
    """
    Comments notification.

    """
    comment = kwargs['comment']
    comments = [comment]
    user = comment.user
    email = user.email
    request = kwargs['request']
    score = comment.content_object
    admin_rcpt, nonadmin_rcpt = [], []
    admin_users = []

    # Update user.email
    if not user.email and comment.user_email:
        user.email = comment.user_email
        user.save()

    subject = u'%(prefix)s%(monitoring)s - %(org)s: %(code)s' % {
        'prefix': config_value('EmailServer', 'EMAIL_SUBJECT_PREFIX'),
        'monitoring': score.task.organization.monitoring,
        'org': score.task.organization.name.split(':')[0],
        'code': score.parameter.code,
    }

    headers = {
        'X-iifd-exmo': 'comment_notification',
        'X-iifd-exmo-comment-organization-url': score.task.organization.url,
    }

    if user.profile.notify_comment_preference.get('self', False):
        admin_users = [user]

    # experts A, experts B managers
    if get_experts():
        admin_users.extend(get_experts())

    # experts B
    if score.task.user.is_active:
        admin_users.extend([score.task.user])

    for user in admin_users:
        if user.is_active and user.email and \
                user.profile.notify_comment_preference['type'] == \
                UserProfile.NOTIFICATION_TYPE_ONEBYONE:
            admin_rcpt.append(user)

    admin_all_comments, admin_one_comment = _comments_lists(admin_rcpt)

    # Organizations
    nonadmin_users = User.objects.filter(
        userprofile__organization=score.task.organization,
        email__isnull=False,
    ).exclude(email__exact='').distinct()

    for user in nonadmin_users:
        if user.email and user.email not in admin_rcpt and \
                user.profile.notify_comment_preference['type'] == \
                UserProfile.NOTIFICATION_TYPE_ONEBYONE:
            nonadmin_rcpt.append(user)

    nonadmin_all_comments, nonadmin_one_comment = _comments_lists(nonadmin_rcpt)

    url = '%s://%s%s' % (request.is_secure() and 'https' or 'http',
                         request.get_host(),
                         reverse('exmo2010:score_view',
                         args=[score.pk]))

    t_plain = loader.get_template('score_comment.txt')
    t_html = loader.get_template('score_comment.html')

    context = {'score': score,
               'user': user,
               'comments': comments,
               'url': url}

    _send_mails(t_plain, t_html, context, False, email,
                subject, headers, nonadmin_one_comment)
    _send_mails(t_plain, t_html, context, True, email,
                subject, headers, admin_one_comment)

    context['comments'] = list(Comment.objects.filter(
        object_pk=comment.object_pk
    )) + comments

    _send_mails(t_plain, t_html, context, False, email,
                subject, headers, nonadmin_all_comments)
    _send_mails(t_plain, t_html, context, True, email,
                subject, headers, admin_all_comments)