<commit_before>"""
Notification objects for the messages app
"""

from django.conf import settings
from django.contrib.auth.models import User
from django.core.mail import EmailMultiAlternatives
from django.template.loader import render_to_string

import json
import requests

class EmailNotification(EmailMultiAlternatives):
    """A generic base class for composing notification emails."""
    text_template = None
    subject = u'Notification'

    def __init__(self, user, context):
        """Initialize the notification"""
        super(EmailNotification, self).__init__(subject=self.subject)
        if isinstance(user, User):
            self.user = user
            self.to = [user.email]
        else:
            raise TypeError('Notification requires a User to receive it.')
        self.from_email = 'MuckRock <<EMAIL>>'
        self.bcc = ['<EMAIL>']
        self.body = render_to_string(self.get_text_template(), self.get_context_data(context))

    def get_context_data(self, context):
        """Return init keywords and the user-to-notify as context."""
        context['user'] = self.user
        return context

    def get_text_template(self):
        """Every notification should have a text template."""
        if self.text_template is None:
            raise NotImplementedError('Notification requires a text template.')
        else:
            return self.text_template


class SupportNotification(EmailNotification):
    """Send a support email."""
    text_template = 'message/notification/support.txt'
    subject = u'Support'

class SlackNotification(object):
    """
    Sends a Slack notification, conforming to the platform's specification.
    Slack notifications should be initialized with a payload that contains the notification.
    If they aren't, you still have a chance to update the payload before sending the message.
    Notifications with empty payloads will be rejected by Slack.
    Payload should be a dictionary, and the API is described by Slack here:
    https://api.slack.com/docs/formatting
    https://api.slack.com/docs/attachments
    """
    def __init__(self, payload=None):
        """Initializes the request with a payload"""
        self.endpoint = settings.SLACK_WEBHOOK_URL
        if payload is None:
            payload = {}
        self.payload = payload

    def send(self, fail_silently=True):
        """Send the notification to our Slack webhook."""
        if not self.endpoint:
            # don't send when the endpoint value is empty,
            # or the requests module will throw errors like woah
            return 0
        data = json.dumps(self.payload)
        response = requests.post(self.endpoint, data=data)
        if response.status_code == 200:
            return 1
        else:
            if not fail_silently:
                response.raise_for_status()
            return 0
<commit_msg>removes leftover email notification class<commit_after>"""
Notification objects for the messages app
"""

from django.conf import settings
from django.contrib.auth.models import User
from django.core.mail import EmailMultiAlternatives
from django.template.loader import render_to_string

import json
import requests

class SlackNotification(object):
    """
    Sends a Slack notification, conforming to the platform's specification.
    Slack notifications should be initialized with a payload that contains the notification.
    If they aren't, you still have a chance to update the payload before sending the message.
    Notifications with empty payloads will be rejected by Slack.
    Payload should be a dictionary, and the API is described by Slack here:
    https://api.slack.com/docs/formatting
    https://api.slack.com/docs/attachments
    """
    def __init__(self, payload=None):
        """Initializes the request with a payload"""
        self.endpoint = settings.SLACK_WEBHOOK_URL
        if payload is None:
            payload = {}
        self.payload = payload

    def send(self, fail_silently=True):
        """Send the notification to our Slack webhook."""
        if not self.endpoint:
            # don't send when the endpoint value is empty,
            # or the requests module will throw errors like woah
            return 0
        data = json.dumps(self.payload)
        response = requests.post(self.endpoint, data=data)
        if response.status_code == 200:
            return 1
        else:
            if not fail_silently:
                response.raise_for_status()
            return 0

{'fraction_non_alphanumeric': 0.07804878048780488, 'fraction_numerical': 0.0026607538802660754, 'mean_word_length': 2.7497921862011636, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 6, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4020054', 'n_tokens_mistral': 1164, 'n_tokens_neox': 1123, 'n_words': 412}