Skip to content

Commit

Permalink
Merge 34947fe into dc65048
Browse files Browse the repository at this point in the history
  • Loading branch information
King-Benx committed May 18, 2018
2 parents dc65048 + 34947fe commit 60d5fda
Show file tree
Hide file tree
Showing 28 changed files with 224 additions and 154 deletions.
2 changes: 1 addition & 1 deletion hc/accounts/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.10 on 2018-05-15 08:28
# Generated by Django 1.10 on 2018-05-18 09:32
from __future__ import unicode_literals

from django.conf import settings
Expand Down
4 changes: 3 additions & 1 deletion hc/api/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,9 @@ def email(self, obj):
return obj.user.email if obj.user else None

def formatted_kind(self, obj):
if obj.kind == "pd":
if obj.kind == "aftsms":
return "AFT_SMS"
elif obj.kind == "pd":
return "PagerDuty"
elif obj.kind == "victorops":
return "VictorOps"
Expand Down
8 changes: 5 additions & 3 deletions hc/api/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.10 on 2018-05-15 08:27
# Generated by Django 1.10 on 2018-05-18 09:33
from __future__ import unicode_literals

import datetime
Expand All @@ -14,8 +14,8 @@ class Migration(migrations.Migration):
initial = True

dependencies = [
('accounts', '__first__'),
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('accounts', '0001_initial'),
]

operations = [
Expand All @@ -32,9 +32,11 @@ class Migration(migrations.Migration):
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('code', models.UUIDField(default=uuid.uuid4, editable=False)),
('created', models.DateTimeField(auto_now_add=True)),
('kind', models.CharField(choices=[('email', 'Email'), ('webhook', 'Webhook'), ('hipchat', 'HipChat'), ('slack', 'Slack'), ('pd', 'PagerDuty'), ('po', 'Pushover'), ('victorops', 'VictorOps')], max_length=20)),
('kind', models.CharField(choices=[('email', 'Email'), ('webhook', 'Webhook'), ('hipchat', 'HipChat'), ('aftsms', 'AfricasTalkingSms'), ('slack', 'Slack'), ('pd', 'PagerDuty'), ('po', 'Pushover'), ('victorops', 'VictorOps')], max_length=20)),
('value', models.TextField(blank=True)),
('email_verified', models.BooleanField(default=False)),
('africas_talking_username', models.CharField(blank=True, help_text='Africas Talking Username', max_length=30)),
('africas_talking_api_key', models.CharField(blank=True, default='', max_length=100, null=True)),
],
),
migrations.CreateModel(
Expand Down
6 changes: 5 additions & 1 deletion hc/api/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
DEFAULT_TIMEOUT = td(days=1)
DEFAULT_GRACE = td(hours=1)
CHANNEL_KINDS = (("email", "Email"), ("webhook", "Webhook"),
("hipchat", "HipChat"),
("hipchat", "HipChat"), ("aftsms","AfricasTalkingSms"),
("slack", "Slack"), ("pd", "PagerDuty"), ("po", "Pushover"),
("victorops", "VictorOps"))

Expand Down Expand Up @@ -177,6 +177,8 @@ class Channel(models.Model):
value = models.TextField(blank=True)
email_verified = models.BooleanField(default=False)
checks = models.ManyToManyField(Check)
africas_talking_username = models.CharField(max_length=30, blank=True, help_text="Africas Talking Username")
africas_talking_api_key = models.CharField(max_length=100, default="", blank=True, null=True)

def assign_all_checks(self):
checks = Check.objects.filter(user=self.user)
Expand All @@ -197,6 +199,8 @@ def send_verify_link(self):

@property
def transport(self):
if self.kind == "aftsms":
return transports.AfricasTalking(self)
if self.kind == "email":
return transports.Email(self)
elif self.kind == "webhook":
Expand Down
12 changes: 11 additions & 1 deletion hc/api/transports.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import requests
from six.moves.urllib.parse import quote

from hc.lib import emails
from hc.lib import emails, aft_sms


def tmpl(template_name, **ctx):
Expand Down Expand Up @@ -216,3 +216,13 @@ def notify(self, check):
}

return self.post(self.channel.value, payload)

class AfricasTalking(Transport):
def notify(self, check):
if self.channel.africas_talking_api_key == "":
return "AfrciasTalkingAPI Key cannot be empty"
ctx = {
'check': check,
'channel': self.channel
}
return aft_sms.send_sms(ctx)
8 changes: 8 additions & 0 deletions hc/front/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,14 @@ class Meta:
model = FrequentlyAskedQuestion
fields = ('email', 'question')

class AfricasTalkingForm(forms.ModelForm):
class Meta:
model = Channel
fields = ['kind', 'africas_talking_username', 'africas_talking_api_key', 'value']

def clean_value(self):
value = self.cleaned_data["value"]
return value.strip()

class EmailTaskForm(forms.ModelForm):

Expand Down
17 changes: 15 additions & 2 deletions hc/front/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.10 on 2018-05-15 08:28
# Generated by Django 1.10 on 2018-05-18 09:34
from __future__ import unicode_literals

import ckeditor.fields
Expand Down Expand Up @@ -49,14 +49,27 @@ class Migration(migrations.Migration):
('name', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
],
),
migrations.CreateModel(
name='EmailTasks',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('task_name', models.CharField(max_length=100, unique=True)),
('subject', models.CharField(max_length=100)),
('recipients', models.CharField(max_length=200)),
('message', models.TextField()),
('interval', models.CharField(max_length=100)),
('time', models.IntegerField()),
('owner', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
],
),
migrations.CreateModel(
name='FrequentlyAskedQuestion',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('question', models.TextField(help_text='Enter any question')),
('answer', models.TextField(blank=True, null=True)),
('email', models.EmailField(help_text='Enter your email', max_length=254)),
('status', models.CharField(choices=[('h', 'hidden'), ('s', 'showing')], default='h', max_length=1)),
('status', models.CharField(choices=[('s', 'showing'), ('h', 'hidden')], default='h', max_length=1)),
],
),
migrations.AddField(
Expand Down
36 changes: 0 additions & 36 deletions hc/front/migrations/0002_auto_20180515_1532.py

This file was deleted.

35 changes: 0 additions & 35 deletions hc/front/migrations/0003_auto_20180516_0443.py

This file was deleted.

28 changes: 0 additions & 28 deletions hc/front/migrations/0004_auto_20180516_0448.py

This file was deleted.

20 changes: 0 additions & 20 deletions hc/front/migrations/0005_auto_20180516_0721.py

This file was deleted.

20 changes: 0 additions & 20 deletions hc/front/migrations/0006_auto_20180516_0821.py

This file was deleted.

1 change: 1 addition & 0 deletions hc/front/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
url(r'^([\w-]+)/remove/$', views.remove_channel, name="hc-remove-channel"),
url(r'^([\w-]+)/verify/([\w-]+)/$', views.verify_email,
name="hc-verify-email"),
url(r'^add_sms/$', views.add_sms, name="hc-add-sms"),
]

urlpatterns = [
Expand Down
15 changes: 10 additions & 5 deletions hc/front/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from hc.api.models import DEFAULT_GRACE, DEFAULT_TIMEOUT, Department, Channel, Check, Ping, PO_PRIORITIES, AssignedChecks
from hc.front.forms import (AddChannelForm, AddDepartmentForm, AddWebhookForm, NameTagsForm,
TimeoutForm, AddBlogForm, AddCategoryForm, AddCommentForm,
FaqForm, EmailTaskForm)
FaqForm, AfricasTalkingForm,EmailTaskForm)

from hc.front.models import FrequentlyAskedQuestion

Expand Down Expand Up @@ -472,7 +472,10 @@ def channels(request):


def do_add_channel(request, data):
form = AddChannelForm(data)
if data.get('kind') == 'aftsms':
form = AfricasTalkingForm(data)
else:
form = AddChannelForm(data)
if form.is_valid():
channel = form.save(commit=False)
channel.user = request.team.user
Expand Down Expand Up @@ -768,6 +771,11 @@ def faq(request):
"frequently_asked_questions":frequently_asked_questions
})

@login_required
def add_sms(request):
ctx = {"page": "channels"}
return render(request, "integrations/add_sms.html", ctx)

@login_required
def departments(request):
""" Display departments created by team members """
Expand Down Expand Up @@ -796,6 +804,3 @@ def add_department(request):

ctx = {"page": "add_department", "form": form}
return render(request, "front/add_department.html", ctx)



35 changes: 35 additions & 0 deletions hc/lib/aft_sms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from africastalking.AfricasTalkingGateway import AfricasTalkingGateway, AfricasTalkingGatewayException

from hc.front.templatetags.hc_extras import hc_duration


class AfricasTalkingSMS(object):
def __init__(self, username, api_key):
self.gateway = AfricasTalkingGateway(username, api_key)

def send(self, recipients, message):
try:
self.gateway.sendMessage(recipients, message)

except AfricasTalkingGatewayException as e:
return 'Encountered an error while sending: %s' % str(e)


def send_sms(ctx):
check = ctx.get('check', None)
channel = ctx.get('channel', None)
dtfmt = "%Y-%m-%d %H:%M"

if not (check and channel):
return False
username, api_key, recipients = channel.africas_talking_username, channel.africas_talking_api_key, channel.value
message = """
HEALTH-CHECKS ALERT!
Name: %(name)s\n
Status: %(status)s\n
Period: %(period)s\n
Last Ping: %(l_ping)s\n
Total Pings: %(t_ping)s\n
""" % dict(name=check.name_then_code(),status=check.status,period=hc_duration(check.timeout),l_ping=check.last_ping.strftime(dtfmt),t_ping=check.n_pings)
instance = AfricasTalkingSMS(username, api_key)
return instance.send(recipients, message)
2 changes: 1 addition & 1 deletion hc/payments/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.10 on 2018-05-15 08:27
# Generated by Django 1.10 on 2018-05-18 09:33
from __future__ import unicode_literals

from django.conf import settings
Expand Down
Loading

0 comments on commit 60d5fda

Please sign in to comment.