Skip to content

Commit

Permalink
[Feature #141392725] Configure periodic reports: daily, weekly and mo…
Browse files Browse the repository at this point in the history
…nthly
  • Loading branch information
wanjikum committed Mar 29, 2017
1 parent cd93db4 commit afe0f3a
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 26 deletions.
22 changes: 17 additions & 5 deletions hc/accounts/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import os
import uuid
from datetime import timedelta

from django.conf import settings
from django.contrib.auth.hashers import make_password
from django.contrib.auth.models import User
Expand All @@ -19,7 +18,7 @@ class Profile(models.Model):
team_name = models.CharField(max_length=200, blank=True)
team_access_allowed = models.BooleanField(default=False)
next_report_date = models.DateTimeField(null=True, blank=True)
reports_allowed = models.BooleanField(default=True)
reports_allowed = models.CharField(max_length=1, default="0")
ping_log_limit = models.IntegerField(default=100)
token = models.CharField(max_length=128, blank=True)
api_key = models.CharField(max_length=128, blank=True)
Expand Down Expand Up @@ -54,9 +53,21 @@ def set_api_key(self):
self.save()

def send_report(self):
# reset next report date first:
report_period = ""
now = timezone.now()
self.next_report_date = now + timedelta(days=30)

if self.reports_allowed == '1':
self.next_report_date = now + timedelta(days=1)
report_period = 'Daily'

elif self.reports_allowed == '2':
self.next_report_date = now + timedelta(days=7)
report_period = 'Weekly'

else:
self.reports_allowed == '3'
self.next_report_date = now + timedelta(days=30)
report_period = 'Monthly'
self.save()

token = signing.Signer().sign(uuid.uuid4())
Expand All @@ -66,7 +77,8 @@ def send_report(self):
ctx = {
"checks": self.user.check_set.order_by("created"),
"now": now,
"unsub_link": unsub_link
"unsub_link": unsub_link,
"report_period": report_period
}

emails.report(self.user.email, ctx)
Expand Down
4 changes: 2 additions & 2 deletions hc/accounts/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ def profile(request):
elif "update_reports_allowed" in request.POST:
form = ReportSettingsForm(request.POST)
if form.is_valid():
profile.reports_allowed = form.cleaned_data["reports_allowed"]
profile.reports_allowed = request.POST.get("reports_allowed", '0')
profile.save()
messages.success(request, "Your settings have been updated!")
elif "invite_team_member" in request.POST:
Expand Down Expand Up @@ -253,7 +253,7 @@ def unsubscribe_reports(request, username):
return HttpResponseBadRequest()

user = User.objects.get(username=username)
user.profile.reports_allowed = False
user.profile.reports_allowed = "0"
user.profile.save()

return render(request, "accounts/unsubscribed.html")
Expand Down
8 changes: 2 additions & 6 deletions hc/api/management/commands/sendreports.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ def num_pinged_checks(profile):


class Command(BaseCommand):
help = 'Send due monthly reports'
tmpl = "Sending monthly report to %s"
help = 'Send due reports accordance to your choice'
tmpl = "Sending report to %s"

def add_arguments(self, parser):
parser.add_argument(
Expand All @@ -29,14 +29,10 @@ def add_arguments(self, parser):

def handle_one_run(self):
now = timezone.now()
month_before = now - timedelta(days=30)

report_due = Q(next_report_date__lt=now)
report_not_scheduled = Q(next_report_date__isnull=True)

q = Profile.objects.filter(report_due | report_not_scheduled)
q = q.filter(reports_allowed=True)
q = q.filter(user__date_joined__lt=month_before)
sent = 0
for profile in q:
if num_pinged_checks(profile) > 0:
Expand Down
10 changes: 5 additions & 5 deletions hc/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@

USE_TZ = True

SITE_ROOT = "https://healthchecks-bau.herokuapp.com"
SITE_ROOT = "http://127.0.0.1:8000"

PING_ENDPOINT = SITE_ROOT + "/ping/"
PING_EMAIL_DOMAIN = HOST
Expand Down Expand Up @@ -152,12 +152,12 @@
# # Allow all host hosts/domain names for this site
ALLOWED_HOSTS = ['healthchecks-bau.herokuapp.com']

# Parse database configuration from $DATABASE_URL
DATABASE_URL = 'postgresql:///postgresql'
DATABASES = {'default': dj_database_url.config(default=DATABASE_URL)}
# # Parse database configuration from $DATABASE_URL
# DATABASE_URL = 'postgresql:///postgresql'
# DATABASES = {'default': dj_database_url.config(default=DATABASE_URL)}

# Honor the 'X-Forwarded-Proto' header for request.is_secure()
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')

EMAIL_BACKEND = "sgbackend.SendGridBackend"
SENDGRID_API_KEY = os.environ.get('SENDGRID_API_KEY')
SENDGRID_API_KEY = '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'
27 changes: 22 additions & 5 deletions templates/accounts/profile.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,30 @@ <h1 class="settings-title">Settings</h1>
<div class="panel-body settings-block">
<form method="post">
{% csrf_token %}
<h2>Monthly Reports</h2>
<h2>Reports Categories</h2>
<label>
<input
name="reports_allowed"
type="checkbox"
{% if profile.reports_allowed %} checked {% endif %}>
Each month send me a summary of my checks
type="radio"
value = "1"
{% if profile.reports_allowed == "1" %} checked {% endif %}>
Daily Report
</label>
<label>
<input
name="reports_allowed"
type="radio"
value = "2"
{% if profile.reports_allowed == "2" %} checked {% endif %}>
Weekly Report
</label>
<label>
<input
name="reports_allowed"
type="radio"
value = "3"
{% if profile.reports_allowed == "3" %} checked {% endif %}>
Monthly Report
</label>
<button
name="update_reports_allowed"
Expand Down Expand Up @@ -324,4 +341,4 @@ <h4 class="remove-check-title">Set Team Name</h4>
<script src="{% static 'js/bootstrap.min.js' %}"></script>
<script src="{% static 'js/profile.js' %}"></script>
{% endcompress %}
{% endblock %}
{% endblock %}
2 changes: 1 addition & 1 deletion templates/emails/report-body-html.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
{% block content %}

<h1>Hello,</h1>
<p>This is a monthly report sent by <a href="https://healthchecks.io">healthchecks.io</a>.</p>
<p>This is a {{report_period}} report sent by <a href="https://healthchecks.io">healthchecks.io</a>.</p>

{% include "emails/summary-html.html" %}

Expand Down
3 changes: 1 addition & 2 deletions templates/emails/report-body-text.html
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
Hello,

This is a monthly report sent by healthchecks.io.
This is a report sent by healthchecks.io.

{% include 'emails/summary-text.html' %}

--
Cheers,
healthchecks.io

0 comments on commit afe0f3a

Please sign in to comment.