From e9b0f10c534c10b239301a58f8c00a4effe9adbf Mon Sep 17 00:00:00 2001 From: Filipp Lepalaan Date: Tue, 13 Oct 2015 22:43:45 +0300 Subject: AST 2fixes --- servo/models/device.py | 17 ++++- servo/templates/diagnostics/select_test.html | 13 ++++ servo/templates/orders/devices.html | 1 + servo/urls/device.py | 20 +++-- servo/views/device.py | 70 ------------------ servo/views/diagnostics.py | 105 +++++++++++++++++++++++++++ 6 files changed, 150 insertions(+), 76 deletions(-) create mode 100644 servo/templates/diagnostics/select_test.html create mode 100644 servo/views/diagnostics.py (limited to 'servo') diff --git a/servo/models/device.py b/servo/models/device.py index fdd5614..cf3b861 100644 --- a/servo/models/device.py +++ b/servo/models/device.py @@ -203,7 +203,7 @@ class Device(models.Model): @property def has_warranty(self): - return self.warranty_status in ('ALW', 'APP', 'CBC') + return self.warranty_status in ('ALW', 'APP', 'CBC',) @property def tag_choices(self): @@ -475,6 +475,21 @@ class Device(models.Model): return countries.countries.get(self.purchase_country, '') + def run_test(self, test_id, request): + from gsxws import diagnostics + GsxAccount.default(request.user) + diags = diagnostics.Diagnostics(self.sn) + diags.shipTo = request.user.location.gsx_shipto + diags.diagnosticSuiteId = test_id + return diags.run_test() + + def fetch_tests(self, request): + from gsxws import diagnostics + GsxAccount.default(request.user) + diags = diagnostics.Diagnostics(self.sn) + diags.shipTo = request.user.location.gsx_shipto + return diags.fetch_suites() + def __unicode__(self): return '%s (%s)' % (self.description, self.sn) diff --git a/servo/templates/diagnostics/select_test.html b/servo/templates/diagnostics/select_test.html new file mode 100644 index 0000000..95618f8 --- /dev/null +++ b/servo/templates/diagnostics/select_test.html @@ -0,0 +1,13 @@ +{% extends "modal.html" %} +{% load i18n %} +{% block header %}{% trans "Choose test to run" %}{% endblock header %} +{% block body %} + {% if error %} +

{{ error }}

+ {% endif %} + +{% endblock body %} diff --git a/servo/templates/orders/devices.html b/servo/templates/orders/devices.html index 9098cfe..6529735 100755 --- a/servo/templates/orders/devices.html +++ b/servo/templates/orders/devices.html @@ -83,6 +83,7 @@ {% endif %}
  • {% trans "Fetch Diagnostics" %}
  • {% trans "Open Diagnostics Console" %}
  • +
  • {% trans "Select Test" %}
  • diff --git a/servo/urls/device.py b/servo/urls/device.py index 132ffbf..defef62 100644 --- a/servo/urls/device.py +++ b/servo/urls/device.py @@ -5,6 +5,8 @@ from django.views.decorators.cache import cache_page from servo.views.order import create from servo.views.device import get_gsx_search_results +from servo.views.diagnostics import diagnostics, select_test, run_test + urlpatterns = patterns( "servo.views.device", @@ -14,9 +16,14 @@ urlpatterns = patterns( url(r'^find/$', "find", name="devices-find"), url(r'^add/$', "edit_device", name="devices-add"), - url(r'^(?P\d+)/diags/$', 'diagnostics', name="devices-diagnostics"), + url(r'^(?P\d+)/diags/$', diagnostics, name="devices-diagnostics"), + url(r'^(?P\d+)/diags/select_test/$', select_test, + name="devices-select_test"), + url(r'^(?P\d+)/diags/(?P\d+)/run/$', run_test, + name="devices-run_test"), - url(r'^(?P\d+)/update_gsx_details/$', "update_gsx_details", name="devices-update_gsx_details"), + url(r'^(?P\d+)/update_gsx_details/$', "update_gsx_details", + name="devices-update_gsx_details"), url(r'^(?P\d+)/orders/(?P\d+)/queue/(?P\d+)/parts/$', "parts", name="devices-parts"), @@ -30,7 +37,8 @@ urlpatterns = patterns( url(r'^choose/order/(\d+)/$', 'choose', name="devices-choose"), url(r'^upload/$', 'upload_devices', name="devices-upload_devices"), - url(r'^(?P\d+)/orders/create/$', create, name="devices-create_order"), + url(r'^(?P\d+)/orders/create/$', create, + name="devices-create_order"), url(r'^(?P\d+)/get_info/$', 'get_info', name="devices-get_info"), url(r'^(?P\w+)/$', "index", name="devices-list_devices"), @@ -41,11 +49,13 @@ urlpatterns = patterns( url(r'^(?P\w+)/(?P[\w\-]+)/(?P\d+)/$', "view_device", name="devices-view_device"), - url(r'^(?P\w+)/(?P[\w-]+)/(?P\d+)/edit/$', "edit_device", + url(r'^(?P\w+)/(?P[\w-]+)/(?P\d+)/edit/$', + "edit_device", name="devices-edit_device"), url(r'^(?P\w+)/(?P[\w-]+)/create/$', "edit_device", name="devices-create_device"), - url(r'^(?P\w+)/(?P[\w-]+)/(?P\d+)/delete/$', "delete_device", + url(r'^(?P\w+)/(?P[\w-]+)/(?P\d+)/delete/$', + "delete_device", name="devices-delete_device"), url(r'^search$', 'search', name="devices-search"), diff --git a/servo/views/device.py b/servo/views/device.py index 9a4a6fb..d828e47 100644 --- a/servo/views/device.py +++ b/servo/views/device.py @@ -17,30 +17,6 @@ from servo.models import Device, Order, Product, GsxAccount, ServiceOrderItem from servo.forms.devices import DeviceForm, DeviceUploadForm, DeviceSearchForm -class RepairDiagnosticResults: - pass - - -class DiagnosticResults(object): - def __init__(self, diags): - if not diags.diagnosticTestData: - raise gsxws.GsxError('Missing diagnostic data') - - self.diags = dict(result={}, profile={}, report={}) - - for r in diags.diagnosticTestData.testResult.result: - self.diags['result'][r.name] = r.value - - for r in diags.diagnosticProfileData.profile.unit.key: - self.diags['profile'][r.name] = r.value - - for r in diags.diagnosticProfileData.report.reportData.key: - self.diags['report'][r.name] = r.value - - def __iter__(self): - return iter(self.diags) - - def model_from_slug(product_line, model=None): """ Returns product description for model slug or models dict for @@ -192,52 +168,6 @@ def view_device(request, pk, product_line=None, model=None): return render(request, "devices/view.html", data) -def diagnostics(request, pk): - """ - Fetches MRI diagnostics or initiates iOS diags from GSX - """ - device = get_object_or_404(Device, pk=pk) - - if request.GET.get('a') == 'init': - if request.method == 'POST': - from gsxws import diagnostics - order = request.POST.get('order') - order = device.order_set.get(pk=order) - email = request.POST.get('email') - diag = diagnostics.Diagnostics(serialNumber=device.sn) - diag.emailAddress = email - diag.shipTo = order.location.gsx_shipto - - try: - GsxAccount.default(request.user) - res = diag.initiate() - msg = _('Diagnostics initiated - diags://%s') % res - order.notify("init_diags", msg, request.user) - messages.success(request, msg) - except gsxws.GsxError as e: - messages.error(request, e) - - return redirect(order) - - order = request.GET.get('order') - order = device.order_set.get(pk=order) - customer = order.customer - url = request.path - return render(request, "devices/diagnostic_init.html", locals()) - - if request.GET.get('a') == 'get': - try: - diagnostics = device.get_diagnostics(request.user) - if device.is_ios(): - diagnostics = DiagnosticResults(diagnostics) - return render(request, "devices/diagnostic_ios.html", locals()) - return render(request, "devices/diagnostic_results.html", locals()) - except gsxws.GsxError as e: - return render(request, "devices/diagnostic_error.html", {'error': e}) - - return render(request, "devices/diagnostics.html", locals()) - - def get_gsx_search_results(request, what, param, query): """ The second phase of a GSX search. diff --git a/servo/views/diagnostics.py b/servo/views/diagnostics.py new file mode 100644 index 0000000..5e2d222 --- /dev/null +++ b/servo/views/diagnostics.py @@ -0,0 +1,105 @@ +# -*- coding: utf-8 -*- +import gsxws + +from django.db.models import Q +from django.contrib import messages + +from django.core.cache import cache +from django.utils.translation import ugettext as _ +from django.shortcuts import render, redirect, get_object_or_404 + + +from servo.models import Device, Order, Product, GsxAccount, ServiceOrderItem + + +class RepairDiagnosticResults: + pass + + +class DiagnosticResults(object): + def __init__(self, diags): + if not diags.diagnosticTestData: + raise gsxws.GsxError('Missing diagnostic data') + + self.diags = dict(result={}, profile={}, report={}) + + for r in diags.diagnosticTestData.testResult.result: + self.diags['result'][r.name] = r.value + + for r in diags.diagnosticProfileData.profile.unit.key: + self.diags['profile'][r.name] = r.value + + for r in diags.diagnosticProfileData.report.reportData.key: + self.diags['report'][r.name] = r.value + + def __iter__(self): + return iter(self.diags) + + +def run_test(request, device, test_id): + device = get_object_or_404(Device, pk=device) + try: + device.run_test(test_id, request) + except Exception as e: + messages.error(request, e) + + +def select_test(request, pk): + """ + Fetch test suite selector + """ + error = None + device = get_object_or_404(Device, pk=pk) + + try: + tests = device.fetch_tests(request) + except Exception as e: + error = e + + return render(request, "diagnostics/select_test.html", locals()) + + +def diagnostics(request, pk): + """ + Fetches MRI diagnostics or initiates iOS diags from GSX + """ + device = get_object_or_404(Device, pk=pk) + + if request.GET.get('a') == 'init': + if request.method == 'POST': + from gsxws import diagnostics + order = request.POST.get('order') + order = device.order_set.get(pk=order) + email = request.POST.get('email') + diag = diagnostics.Diagnostics(serialNumber=device.sn) + diag.emailAddress = email + diag.shipTo = order.location.gsx_shipto + + try: + GsxAccount.default(request.user) + res = diag.initiate() + msg = _('Diagnostics initiated - diags://%s') % res + order.notify("init_diags", msg, request.user) + messages.success(request, msg) + except gsxws.GsxError as e: + messages.error(request, e) + + return redirect(order) + + order = request.GET.get('order') + order = device.order_set.get(pk=order) + customer = order.customer + url = request.path + return render(request, "devices/diagnostic_init.html", locals()) + + if request.GET.get('a') == 'get': + try: + diagnostics = device.get_diagnostics(request.user) + if device.is_ios(): + diagnostics = DiagnosticResults(diagnostics) + return render(request, "devices/diagnostic_ios.html", locals()) + return render(request, "devices/diagnostic_results.html", locals()) + except gsxws.GsxError as e: + return render(request, "devices/diagnostic_error.html", {'error': e}) + + return render(request, "devices/diagnostics.html", locals()) -- cgit v1.2.3