Esempio n. 1
0
 def test_get_via_lgfk(self):
     mc = MaintenanceComponent(
         maintenance_task=self.task,
         key=self.ee1._meta.db_table,
         value=str(self.ee1.id),
     )
     mc.save()
     self.assertEqual(mc.component, self.ee1)
Esempio n. 2
0
 def test_save_model_with_lgfk(self):
     mc = MaintenanceComponent(
         maintenance_task=self.task,
         key=self.ee1._meta.db_table,
         value=str(self.ee1.id),
     )
     mc.save()
     self.assertEqual(mc.key, self.ee1._meta.db_table)
     self.assertEqual(mc.value, str(self.ee1.id))
Esempio n. 3
0
 def test_set_via_lgfk(self):
     ee2 = ErrorError(message='another message')
     ee2.save()
     mc = MaintenanceComponent(
         maintenance_task=self.task,
         key=self.ee1._meta.db_table,
         value=str(self.ee1.id),
     )
     mc.component = ee2
     mc.save()
     self.assertEqual(mc.component, ee2)
Esempio n. 4
0
def add_maintenance_task(owner, netboxes, description=""):
    """Add a maintenance task with a component for each netbox"""
    if not netboxes:
        return

    task = MaintenanceTask(start_time=datetime.datetime.now(),
                           end_time=INFINITY,
                           description=description,
                           author=owner.login,
                           state=MaintenanceTask.STATE_SCHEDULED)
    task.save()

    for netbox in netboxes:
        component = MaintenanceComponent(maintenance_task=task,
                                         key='netbox',
                                         value='%d' % netbox.id)
        component.save()
Esempio n. 5
0
File: views.py Progetto: LiuJux/nav
def _add_neverending_maintenance_task(owner, netbox):
    _logger.debug('Adding maintenance task...')
    maint_task = MaintenanceTask()
    maint_task.start_time = datetime.now()
    maint_task.end_time = INFINITY
    descr = ("On maintenance till up again; set " +
             "from status page by user %s" % owner.login)
    maint_task.description = descr
    maint_task.author = owner.login
    maint_task.state = MaintenanceTask.STATE_SCHEDULED
    maint_task.save()
    _logger.debug("Maintenance task %d; Adding component %s (id=%d)",
                  maint_task.id, netbox.sysname, netbox.id)
    maint_component = MaintenanceComponent()
    maint_component.maintenance_task = maint_task
    maint_component.key = 'netbox'
    maint_component.value = '%d' % netbox.id
    maint_component.save()
Esempio n. 6
0
def _add_neverending_maintenance_task(owner, netbox):
    logger.debug('Adding maintenance task...')
    maint_task = MaintenanceTask()
    maint_task.start_time = datetime.now()
    maint_task.end_time = INFINITY
    descr = ("On maintenance till up again; set " +
             "from status page by user %s" % owner.login)
    maint_task.description = descr
    maint_task.author = owner.login
    maint_task.state = MaintenanceTask.STATE_SCHEDULED
    maint_task.save()
    logger.debug("Maintenance task %d; Adding component %s (id=%d)",
                 maint_task.id, netbox.sysname, netbox.id)
    maint_component = MaintenanceComponent()
    maint_component.maintenance_task = maint_task
    maint_component.key = 'netbox'
    maint_component.value = '%d' % netbox.id
    maint_component.save()
Esempio n. 7
0
File: views.py Progetto: hmpf/nav
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,
        },
    )
Esempio n. 8
0
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)
    )