# Path: timeline_logger/compat.py

# 
# Path: timeline_logger/models.py
# class TimelineLog(models.Model):
#     content_type = models.ForeignKey(
#         ContentType,
#         verbose_name=_('content type'),
#         on_delete=models.SET_NULL,
#         blank=True, null=True,
#     )
#     object_id = models.TextField(
#         verbose_name=_('object id'),
#         blank=True, null=True
#     )
#     content_object = GenericForeignKey('content_type', 'object_id')
#     timestamp = models.DateTimeField(verbose_name=_('timestamp'), auto_now_add=True)
#     extra_data = JSONField(
#         verbose_name=_('extra data'),
#         null=True, blank=True,
#     )
#     user = models.ForeignKey(
#         settings.AUTH_USER_MODEL,
#         verbose_name=_('user'),
#         on_delete=models.SET_NULL,
#         blank=True, null=True,
#     )
#     template = models.CharField(max_length=200, default=DEFAULT_TEMPLATE)
# 
#     class Meta:
#         verbose_name = _('timeline log entry')
#         verbose_name_plural = _('timeline log entries')
# 
#     def __str__(self):
#         if self.object_id:
#             return "{ct} - {pk}".format(
#                 ct=self.content_type.name,
#                 pk=self.object_id
#             )
# 
#         return ugettext('TimelineLog Object')
# 
#     @classmethod
#     def log_from_request(cls, request, content_object, template=None, **extra_data):
#         """
#         Given an ``HTTPRequest`` object and a generic content, it creates a
#         ``TimelineLog`` object to store the data of that request.
# 
#         :param request: A Django ``HTTPRequest`` object.
#         :param content_object: A Django model instance. Any object can be
#         related.
#         :param template: String representing the template to be used to render
#         the message. Defaults to 'default.txt'.
#         :param extra_data: A dictionary (translatable into JSON) determining
#         specifications of the action performed.
#         :return: A newly created ``TimelineLog`` instance.
#         """
#         try:
#             user = request.user
#         except AttributeError:
#             user = None
# 
#         if template is not None:
#             # Ensure that the expected template actually exists.
#             get_template(template)
# 
#         timeline_log = cls.objects.create(
#             content_object=content_object,
#             extra_data=extra_data or None,
#             template=template or DEFAULT_TEMPLATE,
#             user=user,
#         )
#         logger.debug('Logged event in %s %s', content_object._meta.object_name, content_object.pk)
#         return timeline_log
# 
#     def get_message(self, template=None, **extra_context):
#         """
#         Gets the 'log' message, describing the event performed.
# 
#         :param template: String representing the template to be used to render
#         the message. Defaults to ``self.template``.
#         :return: The log message string.
#         """
#         context = {'log': self}
#         context.update(**extra_context)
#         return render_to_string(template or self.template, context)


import logging
from datetime import timedelta
from django.conf import settings
from django.contrib.auth import get_user_model
from django.core.mail import send_mail
from django.core.management.base import BaseCommand, CommandError
from django.template.loader import render_to_string
from django.utils import timezone
from django.utils.html import strip_tags
from django.utils.translation import ugettext_lazy as _
from timeline_logger.compat import html
from timeline_logger.models import TimelineLog
            except TypeError:
                raise CommandError("Incorrect 'days' parameter. 'days' must be a number of days.")
            else:
                return queryset.filter(timestamp__gte=start)

        return queryset

    def get_recipients(self, **options):
        """
        Figures out the recipients
        """
        if options['recipients_from_setting']:
            return settings.TIMELINE_DIGEST_EMAIL_RECIPIENTS

        users = get_user_model()._default_manager.all()
        if options['staff']:
            users = users.filter(is_staff=True)
        elif not options['all']:
            users = users.filter(is_staff=True, is_superuser=True)
        return users.values_list(settings.TIMELINE_USER_EMAIL_FIELD, flat=True)

    def get_context(self, queryset):
        return {
            'logs': queryset,
            'start_date': queryset[0].timestamp,
        }

    def send_email(self, recipients, queryset):
        context = self.get_context(queryset)
        html_content = render_to_string(self.template_name, context)

# Path: timeline_logger/compat.py

# 
# Path: timeline_logger/models.py
# class TimelineLog(models.Model):
#     content_type = models.ForeignKey(
#         ContentType,
#         verbose_name=_('content type'),
#         on_delete=models.SET_NULL,
#         blank=True, null=True,
#     )
#     object_id = models.TextField(
#         verbose_name=_('object id'),
#         blank=True, null=True
#     )
#     content_object = GenericForeignKey('content_type', 'object_id')
#     timestamp = models.DateTimeField(verbose_name=_('timestamp'), auto_now_add=True)
#     extra_data = JSONField(
#         verbose_name=_('extra data'),
#         null=True, blank=True,
#     )
#     user = models.ForeignKey(
#         settings.AUTH_USER_MODEL,
#         verbose_name=_('user'),
#         on_delete=models.SET_NULL,
#         blank=True, null=True,
#     )
#     template = models.CharField(max_length=200, default=DEFAULT_TEMPLATE)
# 
#     class Meta:
#         verbose_name = _('timeline log entry')
#         verbose_name_plural = _('timeline log entries')
# 
#     def __str__(self):
#         if self.object_id:
#             return "{ct} - {pk}".format(
#                 ct=self.content_type.name,
#                 pk=self.object_id
#             )
# 
#         return ugettext('TimelineLog Object')
# 
#     @classmethod
#     def log_from_request(cls, request, content_object, template=None, **extra_data):
#         """
#         Given an ``HTTPRequest`` object and a generic content, it creates a
#         ``TimelineLog`` object to store the data of that request.
# 
#         :param request: A Django ``HTTPRequest`` object.
#         :param content_object: A Django model instance. Any object can be
#         related.
#         :param template: String representing the template to be used to render
#         the message. Defaults to 'default.txt'.
#         :param extra_data: A dictionary (translatable into JSON) determining
#         specifications of the action performed.
#         :return: A newly created ``TimelineLog`` instance.
#         """
#         try:
#             user = request.user
#         except AttributeError:
#             user = None
# 
#         if template is not None:
#             # Ensure that the expected template actually exists.
#             get_template(template)
# 
#         timeline_log = cls.objects.create(
#             content_object=content_object,
#             extra_data=extra_data or None,
#             template=template or DEFAULT_TEMPLATE,
#             user=user,
#         )
#         logger.debug('Logged event in %s %s', content_object._meta.object_name, content_object.pk)
#         return timeline_log
# 
#     def get_message(self, template=None, **extra_context):
#         """
#         Gets the 'log' message, describing the event performed.
# 
#         :param template: String representing the template to be used to render
#         the message. Defaults to ``self.template``.
#         :return: The log message string.
#         """
#         context = {'log': self}
#         context.update(**extra_context)
#         return render_to_string(template or self.template, context)


# Path: timeline_logger/management/commands/report_mailing.py
import logging
from datetime import timedelta
from django.conf import settings
from django.contrib.auth import get_user_model
from django.core.mail import send_mail
from django.core.management.base import BaseCommand, CommandError
from django.template.loader import render_to_string
from django.utils import timezone
from django.utils.html import strip_tags
from django.utils.translation import ugettext_lazy as _
from timeline_logger.compat import html
from timeline_logger.models import TimelineLog

            except TypeError:
                raise CommandError("Incorrect 'days' parameter. 'days' must be a number of days.")
            else:
                return queryset.filter(timestamp__gte=start)

        return queryset

    def get_recipients(self, **options):
        """
        Figures out the recipients
        """
        if options['recipients_from_setting']:
            return settings.TIMELINE_DIGEST_EMAIL_RECIPIENTS

        users = get_user_model()._default_manager.all()
        if options['staff']:
            users = users.filter(is_staff=True)
        elif not options['all']:
            users = users.filter(is_staff=True, is_superuser=True)
        return users.values_list(settings.TIMELINE_USER_EMAIL_FIELD, flat=True)

    def get_context(self, queryset):
        return {
            'logs': queryset,
            'start_date': queryset[0].timestamp,
        }

    def send_email(self, recipients, queryset):
        context = self.get_context(queryset)
        html_content = render_to_string(self.template_name, context)

        text_content = html.unescape(strip_tags(html_content))