def create_outage(request): """ Create a reservation for a user. """ try: start, end = extract_times(request.POST) except Exception as e: return HttpResponseBadRequest(str(e)) tool = get_object_or_404(Tool, name=request.POST.get('tool_name')) # Create the new reservation: outage = ScheduledOutage() outage.creator = request.user outage.category = request.POST.get('category', '')[:200] outage.tool = tool outage.start = start outage.end = end # If there was a problem in saving the reservation then return the error... policy_problem = check_policy_to_create_outage(outage) if policy_problem: return HttpResponseBadRequest(policy_problem) # Make sure there is at least an outage title if not request.POST.get('title'): dictionary = {'categories': ScheduledOutageCategory.objects.all()} return render(request, 'calendar/scheduled_outage_information.html', dictionary) outage.title = request.POST['title'] outage.details = request.POST.get('details', '') outage.save() return HttpResponse()
def modify_outage(request, start_delta, end_delta): try: outage = ScheduledOutage.objects.get(pk=request.POST.get('id')) except ScheduledOutage.DoesNotExist: return HttpResponseNotFound("The outage that you wish to modify doesn't exist!") if start_delta: outage.start += start_delta outage.end += end_delta policy_problem = check_policy_to_create_outage(outage) if policy_problem: return HttpResponseBadRequest(policy_problem) else: # All policy checks passed, so save the reservation. outage.save() return HttpResponse()
def create_outage(request): """ Create an outage. """ try: start, end = extract_times(request.POST) item_type = ReservationItemType(request.POST['item_type']) item_id = request.POST.get('item_id') except Exception as e: return HttpResponseBadRequest(str(e)) item = get_object_or_404(item_type.get_object_class(), id=item_id) # Create the new reservation: outage = ScheduledOutage() outage.creator = request.user outage.category = request.POST.get('category', '')[:200] outage.outage_item = item outage.start = start outage.end = end # If there is a policy problem for the outage then return the error... policy_problem = check_policy_to_create_outage(outage) if policy_problem: return HttpResponseBadRequest(policy_problem) # Make sure there is at least an outage title if not request.POST.get('title'): dictionary = { 'categories': ScheduledOutageCategory.objects.all(), 'recurrence_intervals': recurrence_frequency_display, 'recurrence_date_start': start.date(), } return render(request, 'calendar/scheduled_outage_information.html', dictionary) outage.title = request.POST['title'] outage.details = request.POST.get('details', '') if request.POST.get('recurring_outage') == 'on': # we have to remove tz before creating rules otherwise 8am would become 7am after DST change for example. start_no_tz = outage.start.replace(tzinfo=None) end_no_tz = outage.end.replace(tzinfo=None) submitted_frequency = request.POST.get('recurrence_frequency') submitted_date_until = request.POST.get('recurrence_until', None) date_until = end.replace(hour=0, minute=0, second=0) if submitted_date_until: date_until = localize(datetime.strptime(submitted_date_until, '%m/%d/%Y')) date_until += timedelta(days=1, seconds=-1) # set at the end of the day by_week_day = None if submitted_frequency == 'DAILY_WEEKDAYS': by_week_day = (rrule.MO, rrule.TU, rrule.WE, rrule.TH, rrule.FR) elif submitted_frequency == 'DAILY_WEEKENDS': by_week_day = (rrule.SA, rrule.SU) frequency = recurrence_frequencies.get(submitted_frequency, rrule.DAILY) rules: Iterable[datetime] = rrule.rrule(dtstart=start, freq=frequency, interval=int(request.POST.get('recurrence_interval',1)), until=date_until, byweekday=by_week_day) for rule in list(rules): recurring_outage = ScheduledOutage() recurring_outage.creator = outage.creator recurring_outage.category = outage.category recurring_outage.outage_item = outage.outage_item recurring_outage.title = outage.title recurring_outage.details = outage.details recurring_outage.start = localize(start_no_tz.replace(year=rule.year, month=rule.month, day=rule.day)) recurring_outage.end = localize(end_no_tz.replace(year=rule.year, month=rule.month, day=rule.day)) recurring_outage.save() else: outage.save() return HttpResponse()