Esempio n. 1
0
    def handle(self, *args, **options):

        YESTERDAY = datetime.timedelta(hours=24)

        # If it's been less than 24 hours since our last big email,
        # stop here and don't send any emails.
        if ((self.this_run_covers_things_up_until -
             self.this_run_covers_things_since) < YESTERDAY):
            logger.warning(
                "Not sending emails; emails were last sent within the last "
                "24 hours.")
            return

        # If we made it this far in the function, it's been 24 hours or more
        # since our last email. Let's make a note that the time range of this
        # email extends until the timestamp stored in
        # self.this_run_covers_things_up_until
        Timestamp.update_timestamp_for_string(
            Command.TIMESTAMP_KEY,
            override_time=self.this_run_covers_things_up_until)
        # ^^ This entry in the database will be used for calculating the time
        # range starting point of the next email

        # Now let's send some emails! :-)
        people_who_want_email = Person.objects.filter(
            email_me_re_projects=True)

        count = 0
        for person in people_who_want_email:

            if not person.user.email:
                logger.warning("Uh, the person has no email address: %s" %
                               person.user.username)
                continue  # if the user has no email address, we skip the user.

            message_in_plain_text, message_in_html = self.get_projects_email_for(
                person)
            if message_in_html:
                count += 1
                # TODO: fix the logging message
                logger.info("Emailing %s their project activity." %
                            person.user.email)
                email = EmailMultiAlternatives(
                    subject="News about your OpenHatch projects",
                    body=message_in_plain_text,
                    from_email=
                    "\"OpenHatch Mail-Bot\" <*****@*****.**>",
                    headers={
                        'X-Notion': "I'm feeling a lot less evil now.",
                    },
                    to=[person.user.email])
                email.attach_alternative(message_in_html, "text/html")
                email.send()
        logger.info("Emailed", count)
Esempio n. 2
0
    def handle(self, *args, **options):

        YESTERDAY = datetime.timedelta(hours=24)

        # If it's been less than 24 hours since our last big email,
        # stop here and don't send any emails.
        if ((self.this_run_covers_things_up_until -
                 self.this_run_covers_things_since) < YESTERDAY):
            logger.warning(
                "Not sending emails; emails were last sent within the last "
                "24 hours.")
            return

        # If we made it this far in the function, it's been 24 hours or more
        # since our last email. Let's make a note that the time range of this
        # email extends until the timestamp stored in
        # self.this_run_covers_things_up_until
        Timestamp.update_timestamp_for_string(Command.TIMESTAMP_KEY,
                                              override_time=self.this_run_covers_things_up_until)
        # ^^ This entry in the database will be used for calculating the time
        # range starting point of the next email

        # Now let's send some emails! :-)
        people_who_want_email = Person.objects.filter(
            email_me_re_projects=True)

        count = 0
        for person in people_who_want_email:

            if not person.user.email:
                logger.warning("Uh, the person has no email address: %s" %
                             person.user.username)
                continue  # if the user has no email address, we skip the user.

            message_in_plain_text, message_in_html = self.get_projects_email_for(
                person)
            if message_in_html:
                count += 1
                # TODO: fix the logging message
                logger.info("Emailing %s their project activity." % person.user.email)
                email = EmailMultiAlternatives(
                    subject="News about your OpenHatch projects",
                    body=message_in_plain_text,
                    from_email="\"OpenHatch Mail-Bot\" <*****@*****.**>",
                    headers={
                            'X-Notion': "I'm feeling a lot less evil now.",
                    },
                    to=[person.user.email])
                email.attach_alternative(message_in_html, "text/html")
                email.send()
        logger.debug("Emailed %d", count)