这是indexloc提供的服务,不要输入任何密码
Skip to content
This repository was archived by the owner on Feb 8, 2019. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions notification/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@

from notification.models import ScheduledNotification, UserNotificationMethod
from django.conf import settings
from django.utils import timezone

from openduty.models import EventLog

@app.task(ignore_result=True)
def send_notifications(notification_id):
Expand All @@ -31,7 +34,28 @@ def send_notifications(notification_id):
elif notification.notifier == UserNotificationMethod.METHOD_PROWL:
notifier = ProwlNotifier(settings.PROWL_SETTINGS)
notifier.notify(notification)
# Log successful notification
logmessage = EventLog()
logmessage.service_key = notification.incident.service_key
logmessage.incident_key = notification.incident
logmessage.user = notification.user_to_notify
logmessage.action = 'notified'
logmessage.data = "Notification sent to %s about %s service" % (notification.user_to_notify, logmessage.service_key, )
logmessage.occurred_at = timezone.now()
logmessage.save()

notification.delete()
except ScheduledNotification.DoesNotExist:
pass #Incident was resolved. NOP.
except:
# Log successful notification
logmessage = EventLog()
logmessage.service_key = notification.incident.service_key
logmessage.incident_key = notification.incident
logmessage.user = notification.user_to_notify
logmessage.action = 'notification_failed'
logmessage.data = "Sending notification failed to %s about %s service" % (notification.user_to_notify, logmessage.service_key, )
logmessage.occurred_at = timezone.now()
logmessage.save()
raise

31 changes: 10 additions & 21 deletions openduty/escalation_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,16 @@
from .models import User, SchedulePolicyRule, Service
from datetime import datetime, timedelta
from django.utils import timezone
from schedule.periods import Day


def get_current_events_users(calendar):
now = timezone.make_aware(datetime.now(), timezone.get_current_timezone())
result = []
for event in calendar.events.all():
if event.rule:
# TODO: This assumes no more than monthly occurance. There
# must be a better way to ask an event if x date falls within
# an occurence
for o in event.get_occurrences(now, now + timedelta(days=32)):
if o.start <= now <= o.end:
usernames = event.title.split(',')
for username in usernames:
result.append(User.objects.get(username=username.strip()))
break
elif event.start <= now <= event.end:
usernames = event.title.split(',')
day = Day(calendar.events.all(), now)
for o in day.get_occurrences():
if o.start <= now <= o.end:
usernames = o.event.title.split(',')
for username in usernames:
result.append(User.objects.get(username=username.strip()))
return result
Expand All @@ -40,11 +32,8 @@ def get_escalation_for_service(service):
return result

def services_where_user_is_on_call(user):
result = []

for service in Service.objects.all():
escalation_users = map(lambda user: user.id, get_escalation_for_service(service))
if user.id in escalation_users:
result.append(service.id)

return result
from django.db.models import Q
services = Service.objects.filter(
Q(policy__rules__user_id=user) | Q(policy__rules__schedule__event__title__icontains=user)
)
return services
3 changes: 2 additions & 1 deletion openduty/event_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@

@login_required()
def list(request):
filtered_actions = ('notified', 'notification_failed')
services = Service.objects.filter()
events = EventLog.objects.all().order_by('-occurred_at')
events = EventLog.objects.exclude(action__in=filtered_actions).order_by('-occurred_at')
page = request.GET.get('page')
p = Paginator(events, settings.PAGINATION_DEFAULT_PAGINATION)
try:
Expand Down
6 changes: 4 additions & 2 deletions openduty/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def create_or_edit_event(request, calendar_slug, event_id=None, next=None,
event.creator = request.user
event.calendar = calendar
event.save()
return HttpResponseRedirect(reverse('openduty.schedules.details', None, [calendar.id]))
return HttpResponseRedirect(reverse('calendar_details', kwargs={'calendar_slug': calendar.slug}))
if instance is not None:
officers = instance.title.split(",")
data["oncall"] = officers[0]
Expand All @@ -54,6 +54,8 @@ def create_or_edit_event(request, calendar_slug, event_id=None, next=None,
data["start_hour"] = instance.start.time().strftime("%H:%M")
data["end_ymd"] = instance.end.date().isoformat()
data["end_hour"] = instance.end.time().strftime("%H:%M")
if instance.end_recurring_period:
data["recurr_ymd"] = instance.end_recurring_period.date().isoformat()
data["description"] = instance.description
data["rule"] = instance.rule and instance.rule.id or ""

Expand All @@ -79,4 +81,4 @@ def destroy_event(request, calendar_slug, event_id=None, next=None,

calendar = get_object_or_404(Calendar, slug=calendar_slug)

return HttpResponseRedirect(reverse('openduty.schedules.details', None, [str(calendar.id)]))
return HttpResponseRedirect(reverse('calendar_details', None, [str(calendar.slug)]))
Loading