Skip to content

Commit

Permalink
Merge 101cb25 into 0d571dd
Browse files Browse the repository at this point in the history
  • Loading branch information
Ahmedsebit committed Apr 4, 2017
2 parents 0d571dd + 101cb25 commit ba471b4
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 21 deletions.
20 changes: 20 additions & 0 deletions hc/accounts/migrations/0007_auto_20170404_1607.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.10 on 2017-04-04 16:07
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

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

operations = [
migrations.AlterField(
model_name='profile',
name='reports_allowed',
field=models.CharField(choices=[('1', 'Daily'), ('2', 'Weekly'), ('3', 'Monthly')], max_length=1),
),
]
27 changes: 22 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 @@ -15,11 +14,16 @@

class Profile(models.Model):
# Owner:
PERIODIC_CHOICES = (
('1', 'Daily'),
('2', 'Weekly'),
('3', 'Monthly'),
)
user = models.OneToOneField(User, blank=True, null=True)
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, choices=PERIODIC_CHOICES)
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 +58,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 +82,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
9 changes: 7 additions & 2 deletions hc/accounts/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
from hc.api.models import Channel, Check
from hc.lib.badges import get_badge_url

PERIODIC_CHOICES = (
('1', 'Dayly'),
('2', 'Weekly'),
('3', 'Monthly'),
)

def _make_user(email):
username = str(uuid.uuid4())[:30]
Expand Down Expand Up @@ -156,7 +161,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", PERIODIC_CHOICES)
profile.save()
messages.success(request, "Your settings have been updated!")
elif "invite_team_member" in request.POST:
Expand Down Expand Up @@ -253,7 +258,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
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 ba471b4

Please sign in to comment.