From 5db340c9b85887a03cf1010399776b840b8bda8e Mon Sep 17 00:00:00 2001 From: Filipp Lepalaan Date: Thu, 10 Sep 2015 14:19:40 +0300 Subject: Added GSX repair import --- servo/forms/repairs.py | 8 ++++++++ servo/models/repair.py | 29 ++++++++++++++------------- servo/templates/orders/toolbar.html | 2 -- servo/templates/repairs/import_repair.html | 13 ++++++++++++ servo/urls/order.py | 30 ++++++++++++++++++++++++++-- servo/views/gsx.py | 32 +++++++++++++++++++++++++----- 6 files changed, 91 insertions(+), 23 deletions(-) create mode 100644 servo/templates/repairs/import_repair.html (limited to 'servo') diff --git a/servo/forms/repairs.py b/servo/forms/repairs.py index b821ea9..d705152 100644 --- a/servo/forms/repairs.py +++ b/servo/forms/repairs.py @@ -35,6 +35,14 @@ from servo.models import User, Repair, Template from servo.forms import BaseForm, AutocompleteTextarea, DateTimePickerInput, ChoiceField +class ImportForm(BaseForm): + confirmation = forms.CharField( + min_length=8, + max_length=15, + label=_('Confirmation') + ) + + class GsxCustomerForm(BaseForm): firstName = forms.CharField(max_length=100, label=_('First name')) lastName = forms.CharField(max_length=100, label=_('Last name')) diff --git a/servo/models/repair.py b/servo/models/repair.py index 8d9ffab..24931b6 100644 --- a/servo/models/repair.py +++ b/servo/models/repair.py @@ -34,10 +34,8 @@ from django.db import models from django.conf import settings from django.utils import timezone from django.core.urlresolvers import reverse -from django.contrib.sites.models import Site from django.utils.translation import ugettext_lazy as _ from django.core.validators import MaxLengthValidator -from django.contrib.sites.managers import CurrentSiteManager from servo import defaults from servo.lib.utils import cache_getset @@ -49,12 +47,6 @@ from servo.models.purchases import PurchaseOrder, PurchaseOrderItem class Checklist(models.Model): - site = models.ForeignKey( - Site, - editable=False, - default=defaults.site_id - ) - title = models.CharField( max_length=255, unique=True, @@ -68,7 +60,6 @@ class Checklist(models.Model): ) enabled = models.BooleanField(default=True, verbose_name=_("Enabled")) - objects = CurrentSiteManager() def get_admin_url(self): return reverse('admin-edit_checklist', args=[self.pk]) @@ -249,7 +240,7 @@ class Repair(models.Model): return len(replacements) == 1 @classmethod - def create_from_gsx(cls, order, confirmation): + def create_from_gsx(cls, confirmation, order, device, user): """ Creates a new Repair for order with confirmation number """ @@ -260,11 +251,21 @@ class Repair(models.Model): except cls.DoesNotExist: pass - repair = cls(order=order) - repair.confirmation=confirmation + repair = cls(order=order, created_by=user) + repair.device = device + repair.confirmation = confirmation + repair.gsx_account = GsxAccount.default(user, order.queue) + repair.submitted_at = timezone.now() # RepairDetails doesn't have this! repair.save() - repair.update_details() - repair.update_status() + + try: + repair.get_details() + repair.update_status(user) + except gsxws.GsxError as e: + if e.code == 'RPR.LKP.01': # repair not found + repair.delete() + raise ValueError(_('Repair %s not found in GSX') % confirmation) + return repair def create_purchase_order(self): diff --git a/servo/templates/orders/toolbar.html b/servo/templates/orders/toolbar.html index 4bd9fef..6588a52 100755 --- a/servo/templates/orders/toolbar.html +++ b/servo/templates/orders/toolbar.html @@ -105,8 +105,6 @@
  • {% trans "Order Products" %}
  • {% endif %}
  • -
  • {% trans "Import GSX Repair" %}
  • -
  • {% if perms.servo.delete_order and order.is_editable %}
  • {% trans "Delete Order" %}
  • {% else %} diff --git a/servo/templates/repairs/import_repair.html b/servo/templates/repairs/import_repair.html new file mode 100644 index 0000000..571b19e --- /dev/null +++ b/servo/templates/repairs/import_repair.html @@ -0,0 +1,13 @@ +{% extends "modal.html" %} +{% load i18n %} + +{% block header %} + {% trans "Import GSX Repair" %} +{% endblock header %} + +{% block body %} +
    + {% csrf_token %} + {% include "form_snippet.html" %} +
    +{% endblock body %} diff --git a/servo/urls/order.py b/servo/urls/order.py index da0b3e0..d57e5eb 100644 --- a/servo/urls/order.py +++ b/servo/urls/order.py @@ -1,3 +1,29 @@ +# -*- coding: utf-8 -*- +# Copyright (c) 2013, First Party Software +# All rights reserved. + +# Redistribution and use in source and binary forms, with or without modification, +# are permitted provided that the following conditions are met: + +# 1. Redistributions of source code must retain the above copyright notice, +# this list of conditions and the following disclaimer. + +# 2. Redistributions in binary form must reproduce the above copyright notice, +# this list of conditions and the following disclaimer in the documentation +# and/or other materials provided with the distribution. + +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE +# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT +# OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, +# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +# SUCH DAMAGE. + from servo.views import note from django.conf.urls import patterns, url from servo.views.order import update_order @@ -98,8 +124,8 @@ urlpatterns = patterns( name="orders-edit_note"), url(r'^(\d+)/device/(\d+)/repairs/(\w+)/create/$', create_repair, name="repairs-create_repair"), + url(r'^(\d+)/device/(\d+)/import_repair/$', import_repair, + name="repairs-import_repair"), url(r'^(\d+)/repairs/(\d+)/edit/$', edit_repair, name="repairs-edit_repair"), - url(r'^(\d+)/repairs/import/$', import_repair, name="repairs-import_repair"), - ) diff --git a/servo/views/gsx.py b/servo/views/gsx.py index 1e20354..0269462 100644 --- a/servo/views/gsx.py +++ b/servo/views/gsx.py @@ -34,7 +34,7 @@ from django.shortcuts import render, redirect, get_object_or_404 from django.contrib.auth.decorators import permission_required from servo.models import Order, GsxAccount, Repair, ServicePart -from servo.forms import GsxCustomerForm, GsxRepairForm, GsxComponentForm +from servo.forms import GsxCustomerForm, GsxRepairForm, GsxComponentForm, ImportForm class RepairDetails(object): @@ -56,15 +56,37 @@ def register_return(request, part_id): try: part.register_for_return(request.user) messages.success(request, _(u"Part %s updated") % part.order_item.code) - except Exception, e: + except Exception as e: messages.error(request, e) return redirect(part.repair.order) @permission_required("servo.change_repair") -def import_repair(request, pk): - pass +def import_repair(request, order_pk, device_pk): + from servo.models import Device + order = get_object_or_404(Order, pk=order_pk) + device = get_object_or_404(Device, pk=device_pk) + + action = request.path + form = ImportForm() + + if request.method == 'POST': + form = ImportForm(request.POST) + if form.is_valid(): + confirmation = form.cleaned_data['confirmation'] + + try: + repair = Repair.create_from_gsx(confirmation, + order, + device, + request.user) + return redirect(repair) + except Exception as e: + messages.error(request, e) + return redirect(order) + + return render(request, "repairs/import_repair.html", locals()) @permission_required("servo.change_order") @@ -309,7 +331,7 @@ def create_repair(request, order_id, device_id, type): try: repair.gsx_account = GsxAccount.default(request.user, order.queue) - except Exception, e: + except Exception as e: messages.error(request, e) return redirect(order) -- cgit v1.2.3