示例#1
0
def sysinfo_list(request):
    form = SystemForm(request.POST or None)
    if request.method == "POST":
        if form.is_valid():
            if 'update' in request.POST:
                print 'User requested the firmware update.'
                msg = 'User requested the firmware update.'
                res = controller.update_firmware()
            elif 'restart' in request.POST:
                print 'user requested runner restart.'
                msg = 'user requested runner restart.'
                res = os.system('sudo service orchid_runner restart') == 0

            if res:
                messages.success(request, "Actions taken: " + msg)
            else:
                messages.error(request, "Actions failed: " + msg)
            return redirect('/sysinfo/')
    else:
        form = SystemForm()

    si = sysinfo.get_sysinfo_html()
    chart_data = sysinfo.get_sysinfo_d()
    charts = {
        'CPU': chart_data['cpu']['load']['current'],
        'RAM': chart_data['memory']['RAM_MB']['percent'],
        'Flash': chart_data['memory']['flash_GB']['percent'],
        'Temp': chart_data['cpu']['temp']['current'],
    }

    return render(request, 'orchid_app/sysinfo_list.html', {'form': form, 'sysinfo': si, 'charts': charts})
def edit_system(request, system):
    system = get_object_or_404(System, pk=system, center__user=request.user)

    if request.method == 'POST':
        form = SystemForm(request.POST, instance=system)
        if form.is_valid():
            system = form.save()
            return redirect('show_system', system=center.pk)
    else:
        form = SystemForm(instance=system)
    return render_to_response('supercomputer/edit_system.html', locals(),\
                             context_instance=RequestContext(request))
def add_system(request, center):
    center = get_object_or_404(Center, pk=center, user=request.user)

    if request.method == 'POST':
        form = SystemForm(request.POST)
        if form.is_valid():
            system = form.save(commit=False)
            system.center = center
            system.save()
            return redirect('show_system', system=system.pk)
    else:
        form = SystemForm()
    return render_to_response('supercomputer/add_system.html', locals(),\
                             context_instance=RequestContext(request))
示例#4
0
def system_view(request, template, data, instance=None):
    if request.method == 'POST':
        form = SystemForm(request.POST, instance=instance)
        if form.is_valid():
            s = form.save(commit=False)
            s.save(request=request)
            return redirect(system_show, s.pk)
    else:
        form = SystemForm(instance=instance)

    data['form'] = form

    return render_to_response(template,
        data,
        request
    )
示例#5
0
def index(request, parent_id):
    a = _client(request)
    systems = a.systems.list(type='STORAGE')
    system_choices = []
    for system in systems:

        choice_tuple = (system.id, system.name)
        system_choices.append(choice_tuple)

    if request.method == 'POST':

        form = SystemForm(request.POST, systems=system_choices)

        if form.is_valid():
            choice = form.cleaned_data['system']
            return HttpResponseRedirect('/data/{}/{}/list'.format(
                parent_id, choice))
    else:
        return render(request, 'ids_systems/index.html',
                      {'form': SystemForm(systems=system_choices)})
def index(request, parent_id):
    a = _client(request)
    systems = a.systems.list(type='STORAGE')
    system_choices = []
    for system in systems:

        choice_tuple = (system.id,system.name)
        system_choices.append(choice_tuple)

    if request.method == 'POST':

        form = SystemForm(request.POST, systems=system_choices)

        if form.is_valid():
            choice = form.cleaned_data['system']
            return HttpResponseRedirect(
                '/data/{}/{}/list'.format(parent_id, choice)
            )
    else:
        return render(request, 'ids_systems/index.html',
            {
                'form':SystemForm(systems=system_choices)
            }
        )
def system_view(request, template, data, instance=None):
    if request.method == 'POST':
        form = SystemForm(request.POST, instance=instance)
        if form.is_valid():
            s = form.save(commit=False)
            s.save(request=request)
            return redirect(system_show, s.pk)
    else:
        form = SystemForm(instance=instance)

    data['form'] = form

    return render_to_response(template, data, request)
示例#8
0
def system_build(request):

    system_form = SystemForm(
        Item.objects.filter(sku__category__name="Motherboard", in_stock=True),
        Item.objects.filter(sku__category__name="Case", in_stock=True),
        data=request.POST or None,
    )

    SystemHardDriveFormSet = formset_factory(SystemHardDriveForm, extra=3)
    hard_drive_form_set = SystemHardDriveFormSet(data=request.POST or None)
    if request.method == "POST":
        num_valid_hds = 0
        if hard_drive_form_set.is_valid():
            valid_hard_drive_reqs = [form for form in hard_drive_form_set.cleaned_data if form]
            num_valid_hds = len(valid_hard_drive_reqs)

        if system_form.is_valid() and hard_drive_form_set.is_valid() and num_valid_hds:
            # Keep track of components and qty request for building later
            component2qty = {}

            # Ensure we have enough in stock to build the system
            system_data = system_form.cleaned_data

            has_errors = False
            component2qty[system_data["case"]] = 1
            if system_data["case"].quantity < 1:
                messages.error(request, "Not enough stock of case: %s" % str(system_data["case"]))
                has_errors = True

            component2qty[system_data["motherboard"]] = 1
            if system_data["motherboard"].quantity < 1:
                messages.error(request, "Not enough stock of motherboard: %s" % str(system_data["motherboard"]))
                has_errors = True

            # Check the total number of each type of hard drive
            #  Determine how much we want of each HD
            harddrive2qty = {}

            for hd_req in valid_hard_drive_reqs:
                harddrive2qty.setdefault(hd_req["hard_drive"], 0)
                harddrive2qty[hd_req["hard_drive"]] += hd_req["quantity"]

            #  See if we have enough of what is requested
            for (hd, qty) in harddrive2qty.items():
                component2qty[hd] = qty
                if qty > hd.quantity:
                    messages.error(request, "Not enough stock of hard drive: %s.  Requested %d." % (str(hd), qty))
                    has_errors = True

            if not has_errors:
                # Build the system
                system = Item(
                    sku=system_data["system_type"],
                    tag=system_data["tag"],
                    quantity=1,
                    status="Waiting to be built",
                    in_stock=True,
                    refurb=False,
                    notes="",
                )
                system.save()

                # Then remove the items used from stock
                for (component, qty) in component2qty.items():
                    # First create the new items that are in the system
                    new_item = Item(
                        sku=component.sku,
                        tag=component.tag,
                        quantity=qty,
                        status="In System",
                        in_item=system,
                        in_stock=False,
                        refurb=component.refurb,
                        notes=component.notes,
                    )
                    new_item.save()

                    # Now adjust or delete the source items
                    component.quantity -= qty
                    if component.quantity == 0:
                        component.delete()
                    else:
                        component.save()

                # Let them know the system was build successfully
                messages.success(request, "Successfully added system %s (%s)." % (system.sku.code, system.tag))

                # If they want to build again, return them to this page
                if request.POST.get("build_and_build", None):
                    return HttpResponseRedirect(reverse("system_build"))

                # Else return them to home page
                return HttpResponseRedirect(reverse("home"))

        else:
            # Let them know there was an error
            if num_valid_hds == 0:
                messages.error(request, "You need to add at least 1 hard drive.")
            else:
                messages.error(request, "There was an error.")

    return render(
        request, "inventory/system_build.html", {"system_form": system_form, "hard_drive_form_set": hard_drive_form_set}
    )