def edit(request, task_id=None, start_time=None, **_): account = get_account(request) quickselect = QuickSelect(service=True) component_trail = None component_keys = None task = None if task_id: task = get_object_or_404(MaintenanceTask, pk=task_id) task_form = MaintenanceTaskForm( initial=task_form_initial(task, start_time)) if request.method == 'POST': component_keys = get_component_keys(request.POST) elif task: component_keys = { 'service': [], 'netbox': [], 'room': [], 'location': [], 'netboxgroup': [], } for key, value in task.maintenancecomponent_set.values_list( 'key', 'value'): if key in PRIMARY_KEY_INTEGER: value = int(value) component_keys[key].append(value) else: component_keys = get_component_keys(request.GET) if component_keys: component_data = components_for_keys(component_keys) components = structure_component_data(component_data) component_trail = task_component_trails(component_keys, components) if request.method == 'POST': if 'save' in request.POST: task_form = MaintenanceTaskForm(request.POST) if not any(component_data.values()): new_message(request, "No components selected.", Messages.ERROR) elif task_form.is_valid(): start_time = task_form.cleaned_data['start_time'] end_time = task_form.cleaned_data['end_time'] no_end_time = task_form.cleaned_data['no_end_time'] state = MaintenanceTask.STATE_SCHEDULED if (start_time < datetime.now() and end_time and end_time <= datetime.now()): state = MaintenanceTask.STATE_SCHEDULED new_task = MaintenanceTask() new_task.start_time = task_form.cleaned_data['start_time'] if no_end_time: new_task.end_time = INFINITY elif not no_end_time and end_time: new_task.end_time = task_form.cleaned_data['end_time'] new_task.description = task_form.cleaned_data['description'] new_task.state = state new_task.author = account.login if task: new_task.id = task.id new_task.save() if task: cursor = connection.cursor() sql = """DELETE FROM maint_component WHERE maint_taskid = %s""" cursor.execute(sql, (new_task.id, )) for key in component_data: for component in component_data[key]: task_component = MaintenanceComponent( maintenance_task=new_task, key=key, value="%s" % component['id'], ) task_component.save() new_message(request, "Saved task %s" % new_task.description, Messages.SUCCESS) return HttpResponseRedirect( reverse('maintenance-view', args=[new_task.id])) else: task_form = MaintenanceTaskForm(initial=request.POST) if task: navpath = NAVPATH + [('Edit', '')] heading = 'Editing "%s"' % task.description title = TITLE + " - " + heading else: navpath = NAVPATH + [('New', '')] heading = 'New task' title = TITLE + " - " + heading return render( request, 'maintenance/new_task.html', { 'active': { 'new': True }, 'navpath': navpath, 'title': title, 'heading': heading, 'task_form': task_form, 'task_id': task_id, 'quickselect': mark_safe(quickselect), 'components': component_trail, 'selected': component_keys, }, )
def edit(request, task_id=None, start_time=None): account = get_account(request) quickselect = QuickSelect(service=True) component_trail = None component_keys = None task = None num_components = 0 if task_id: task = get_object_or_404(MaintenanceTask, pk=task_id) task_form = MaintenanceTaskForm( initial=task_form_initial(task, start_time)) if request.method == 'POST': component_keys = get_component_keys(request.POST) elif task: component_keys = {'service': [], 'netbox': [], 'room': [], 'location': []} for key, value in task.maintenancecomponent_set.values_list('key', 'value'): if key in PRIMARY_KEY_INTEGER: value = int(value) component_keys[key].append(value) else: component_keys = get_component_keys(request.GET) if component_keys: component_data = components_for_keys(component_keys) components = structure_component_data(component_data) component_trail = task_component_trails(component_keys, components) num_components += (len(component_data['service']) + len(component_data['netbox'])) num_components += (len(component_data['room']) + len(component_data['location'])) if request.method == 'POST': if 'save' in request.POST: task_form = MaintenanceTaskForm(request.POST) if task_form.is_valid() and num_components > 0: start_time = task_form.cleaned_data['start_time'] end_time = task_form.cleaned_data['end_time'] no_end_time = task_form.cleaned_data['no_end_time'] state = MaintenanceTask.STATE_SCHEDULED if (start_time < datetime.now() and end_time and end_time <= datetime.now()): state = MaintenanceTask.STATE_SCHEDULED new_task = MaintenanceTask() new_task.start_time = task_form.cleaned_data['start_time'] if no_end_time: new_task.end_time = INFINITY elif not no_end_time and end_time: new_task.end_time = task_form.cleaned_data['end_time'] new_task.description = task_form.cleaned_data['description'] new_task.state = state new_task.author = account.login if task: new_task.id = task.id new_task.save() if task: cursor = connection.cursor() sql = """DELETE FROM maint_component WHERE maint_taskid = %s""" cursor.execute(sql, (new_task.id,)) transaction.set_dirty() for key in component_data: for component in component_data[key]: task_component = MaintenanceComponent( maintenance_task=new_task, key=key, value="%s" % component['id']) task_component.save() new_message(request, "Saved task %s" % new_task.description, Messages.SUCCESS) return HttpResponseRedirect(reverse('maintenance-view', args=[new_task.id])) if num_components <= 0: new_message(request, "No components selected.", Messages.ERROR) else: task_form = MaintenanceTaskForm(initial=request.POST) if task: navpath = NAVPATH + [('Edit', '')] heading = 'Editing "%s"' % task.description title = TITLE + " - " + heading else: navpath = NAVPATH + [('New', '')] heading = 'New task' title = TITLE + " - " + heading return render_to_response( 'maintenance/new_task.html', { 'active': {'new': True}, 'navpath': navpath, 'title': title, 'heading': heading, 'task_form': task_form, 'task_id': task_id, 'quickselect': mark_safe(quickselect), 'components': component_trail, 'selected': component_keys, }, RequestContext(request) )