Skip to content

Commit

Permalink
Merge pull request #4 from maykinmedia/command-class-methods
Browse files Browse the repository at this point in the history
Adding hook methods to the management command
  • Loading branch information
sergei-maertens committed Oct 16, 2017
2 parents 356134f + f76d241 commit 07b1183
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 19 deletions.
6 changes: 3 additions & 3 deletions tests/test_management_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,11 @@ def test_days_parameter_successful(self):

# The 1st log `self.log_1` is NOT present in the email, because it was
# generated before 15 days ago from today.
self.assertNotIn(self.log_1.timestamp.strftime('%B %-d, %Y'), mail.outbox[0].body)
self.assertNotIn(self.log_1.timestamp.strftime('%b. %-d, %Y'), mail.outbox[0].body)

# The other logs `self.log_2` and `self.log_3` are properly
self.assertIn(self.log_2.timestamp.strftime('%B %-d, %Y'), mail.outbox[0].body)
self.assertIn(self.log_3.timestamp.strftime('%B %-d, %Y'), mail.outbox[0].body)
self.assertIn(self.log_2.timestamp.strftime('%b. %-d, %Y'), mail.outbox[0].body)
self.assertIn(self.log_3.timestamp.strftime('%b. %-d, %Y'), mail.outbox[0].body)

def test_no_logs_recorded(self):
"""
Expand Down
47 changes: 31 additions & 16 deletions timeline_logger/management/commands/report_mailing.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,19 @@ def add_arguments(self, parser):
)

def handle(self, *args, **options):
# figure out the recipients
if options['recipients_from_setting']:
recipients = settings.TIMELINE_DIGEST_EMAIL_RECIPIENTS
else:
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)
recipients = users.values_list(settings.TIMELINE_USER_EMAIL_FIELD, flat=True)

# filter the list of log objects to display
recipients = self.get_recipients(**options)
queryset = self.get_queryset(**options)

if not queryset.exists():
logger.info('No logs in timeline. No emails sent.')
return

self.send_email(recipients, queryset)

def get_queryset(self, **options):
"""
Filters the list of log objects to display
"""
days = options.get('days')
queryset = TimelineLog.objects.order_by('-timestamp')
if days:
Expand All @@ -55,16 +56,30 @@ def handle(self, *args, **options):
except TypeError:
raise CommandError("Incorrect 'days' parameter. 'days' must be a number of days.")
else:
queryset = queryset.filter(timestamp__gte=start)
return queryset.filter(timestamp__gte=start)

if not queryset.exists():
logger.info('No logs in timeline. No emails sent.')
return
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 send_email(self, recipients, queryset):
context = {
'logs': queryset,
'start_date': queryset[0].timestamp,
}

html_content = render_to_string('timeline_logger/notifications.html', context)
text_content = html.unescape(strip_tags(html_content))

Expand Down

0 comments on commit 07b1183

Please sign in to comment.