@login_required()
@require_http_methods(["POST"])
def save(request):
    try:
        policy = SchedulePolicy.objects.get(id=request.POST['id'])
    except SchedulePolicy.DoesNotExist:
        policy = SchedulePolicy()

    policy.name = request.POST['name']
    policy.repeat_times = request.POST['repeat']
    try:
        policy.save()
    except IntegrityError:
        messages.error(request, 'Schedule already exists')
        if int(request.POST['id']) > 0:
            return HttpResponseRedirect(reverse('openduty.escalation.edit', None, [str(request.POST['id'])]))
        else:
            return HttpResponseRedirect(reverse('openduty.escalation.new'))

    elements = request.POST.getlist('escalate_to[]')
    try:
        SchedulePolicyRule.objects.filter(schedule_policy=policy).delete()
    except SchedulePolicyRule.DoesNotExist:
        pass  # Nothing to clear
    for idx,item in enumerate(elements):
        rule = SchedulePolicyRule()
        rule.schedule_policy = policy
        parts = item.split("|")
        rule.escalate_after = 0  # HACK!
        rule.position = idx + 1
        if parts[0] == "user":
            rule.user_id = User.objects.get(id=parts[1])
            rule.schedule = None
        if parts[0] == "calendar":
            rule.schedule = Calendar.objects.get(id=parts[1])
            rule.user_id = None
        try:
            rule.save()
        except IntegrityError:
            return HttpResponseRedirect(reverse('openduty.escalation.edit', None, [str(request.POST['id'])]))


    return HttpResponseRedirect('/policies/')
dataset/ETHPy150Open ustream/openduty/openduty/escalation.py/save/original