Skip to content

Commit

Permalink
Merge branch 'ft-week-2-work' into 155965540-notify-user-when-job-run…
Browse files Browse the repository at this point in the history
…s-too-often
  • Loading branch information
dngst committed Apr 12, 2018
2 parents e96fe9e + e2d4739 commit 9f5f3c8
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 9 deletions.
2 changes: 1 addition & 1 deletion hc/accounts/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ class EmailPasswordForm(forms.Form):
email = LowercaseEmailField()
password = forms.CharField(required=False)


class ReportSettingsForm(forms.Form):
reports_allowed = forms.BooleanField(required=False)

report_freqs = forms.CharField(required=False)

class SetPasswordForm(forms.Form):
password = forms.CharField()
Expand Down
20 changes: 20 additions & 0 deletions hc/accounts/migrations/0007_profile_report_freqs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.10 on 2018-04-05 12:14
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('accounts', '0006_profile_current_team'),
]

operations = [
migrations.AddField(
model_name='profile',
name='report_freqs',
field=models.CharField(default='weekly', max_length=50),
),
]
6 changes: 4 additions & 2 deletions hc/accounts/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class Profile(models.Model):
team_access_allowed = models.BooleanField(default=False)
next_report_date = models.DateTimeField(null=True, blank=True)
reports_allowed = models.BooleanField(default=True)
report_freqs = models.CharField(max_length=50, default="weekly")
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 @@ -53,10 +54,10 @@ def set_api_key(self):
self.api_key = base64.urlsafe_b64encode(os.urandom(24))
self.save()

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

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

Expand Down
11 changes: 9 additions & 2 deletions hc/accounts/tests/test_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,19 @@ def test_it_sends_set_password_link(self):
### Assert that the email was sent and check email content

def test_it_sends_report(self):
#login user
self.client.login(username="alice@example.org", password="password")

check = Check(name="Test Check", user=self.alice)
check.save()

self.alice.profile.send_report()
form = {"update_reports_allowed":"1" ,"report_freqs": "immediately"}
r = self.client.post("/accounts/profile/", form)
self.assertEquals(200, r.status_code)

###Assert that the email was sent and check email content
self.alice.profile.send_report(7)
self.assertGreater(len(mail.outbox), 0)
self.assertIn(mail.outbox[0].subject, "Recent Reports")

def test_it_adds_team_member(self):
self.client.login(username="alice@example.org", password="password")
Expand Down
18 changes: 18 additions & 0 deletions hc/accounts/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,26 @@ def profile(request):
form = ReportSettingsForm(request.POST)
if form.is_valid():
profile.reports_allowed = form.cleaned_data["reports_allowed"]
report_freq = form.cleaned_data["report_freqs"]

if profile.reports_allowed:
# check user specified time period
num_of_days = 0
if report_freq == "weekly":
num_of_days = 7
if report_freq == "monthly":
num_of_days = 30
if report_freq == "daily":
num_of_days = 1
if report_freq == "immediately":
num_of_days = 1

profile.send_report(num_of_days)


profile.save()
messages.success(request, "Your settings have been updated!")

elif "invite_team_member" in request.POST:
if not profile.team_access_allowed:
return HttpResponseForbidden()
Expand Down
27 changes: 25 additions & 2 deletions templates/accounts/profile.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,32 @@ <h2>Monthly Reports</h2>
<input
name="reports_allowed"
type="checkbox"
{% if profile.reports_allowed %} checked {% endif %}>
Each month send me a summary of my checks
{% if profile.reports_allowed %} checked {% endif %}/>
Send me a summary of my checks reports
</label>

</br>

<label for="report_freqs">Reports Timing:
</label>
<select id="report_freqs" name="report_freqs">
<!-- immediately send reports-->
<option value="immediately" {% if profile.report_freqs == "immediately" %} selected {% endif %}>
immediately
</option>
<!-- RReporting sending should be daily -->
<option value="daily" {% if profile.report_freqs == "daily" %} selected {% endif %}>
daily
</option>
<!-- send weekly reports -->
<option value="weekly" {% if profile.report_freqs == "weekly" %} selected {% endif %}>
weekly
</option>
<!-- send monthly reports -->
<option value="monthly" {% if profile.report_freqs == "monthly" %} selected {% endif %}>
monthly
</option>
</select>
<button
name="update_reports_allowed"
type="submit"
Expand Down
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 { frequency } report sent by <a href="https://healthchecks.io">healthchecks.io</a>.</p>

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

Expand Down
2 changes: 1 addition & 1 deletion templates/emails/report-subject.html
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Monthly Report
Recent Report

0 comments on commit 9f5f3c8

Please sign in to comment.