Esempio n. 1
0
def importDigikey(request):
    response = check_access(request)
    if response is None:
        return HttpResponseRedirect('/electroInv/login-page/')
    invoice = request.POST.get('invoice', None)
    invoiceNumber = request.POST.get('invoiceNumber', '')

    if invoice is None or invoice == '':
        error = "You must provide a digikey invoice for importing"
        return HttpResponse(json.dumps({'errors': [error]}),
                            content_type="application/json")

    if invoiceNumber is None or invoiceNumber == '':
        error = "You must provide a digikey invoiceNumber for importing"
        return HttpResponse(json.dumps({'errors': [error]}),
                            content_type="application/json")

    previousLog = Log.objects.filter(invoice=invoiceNumber)
    if (len(previousLog.all()) > 0):
        error = "Invoice number already exists, data may have already been imported."
        return HttpResponse(json.dumps({'errors': [error]}),
                            content_type="application/json")

    invoiceData, errors = parseDigikeyCSV(invoice)
    if errors:
        return HttpResponse(json.dumps({'errors': errors}),
                            content_type="application/json")

    vendor = Vendor.objects.get(name="Digi-Key")
    for data in invoiceData:
        try:
            part = Part.objects.get(vendor_sku=data['vendor_sku'])
        except Part.DoesNotExist:
            part = Part()
            part.description = data['description']
        except KeyError:
            errors.append('Was not able to find part number in data.')
            continue

        for field, value in data.iteritems():
            if field in ['id', 'pk']:
                continue

            if field == "qty":
                part.qty += value
            else:
                setattr(part, field, value)

        part.vendor = vendor

        try:
            part.full_clean()
            part.save(invoiceNumber=invoiceNumber)
        except ValidationError as e:
            errors.append(str(e))

    return HttpResponse(json.dumps({'errors': errors}),
                        content_type="application/json")
Esempio n. 2
0
def importDigikey(request):
    response = check_access(request)
    if response is None:
        return HttpResponseRedirect('/electroInv/login-page/')
    invoice = request.POST.get('invoice', None)
    invoiceNumber = request.POST.get('invoiceNumber', '')

    if invoice is None or invoice == '':
        error = "You must provide a digikey invoice for importing"
        return HttpResponse(json.dumps({'errors': [error]}), content_type="application/json")

    if invoiceNumber is None or invoiceNumber == '':
        error = "You must provide a digikey invoiceNumber for importing"
        return HttpResponse(json.dumps({'errors': [error]}), content_type="application/json")

    previousLog = Log.objects.filter(invoice=invoiceNumber)
    if(len(previousLog.all()) > 0):
        error = "Invoice number already exists, data may have already been imported."
        return HttpResponse(json.dumps({'errors': [error]}), content_type="application/json")

    invoiceData, errors = parseDigikeyCSV(invoice)
    if errors:
        return HttpResponse(json.dumps({'errors': errors}), content_type="application/json")

    vendor = Vendor.objects.get(name="Digi-Key")
    for data in invoiceData:
        try:
            part = Part.objects.get(vendor_sku=data['vendor_sku'])
        except Part.DoesNotExist:
            part = Part()
            part.description = data['description']
        except KeyError:
            errors.append('Was not able to find part number in data.')
            continue

        for field, value in data.iteritems():
            if field in ['id', 'pk']:
                continue

            if field == "qty":
                part.qty += value
            else:
                setattr(part, field, value)

        part.vendor = vendor

        try:
            part.full_clean()
            part.save(invoiceNumber=invoiceNumber)
        except ValidationError as e:
            errors.append(str(e))

    return HttpResponse(json.dumps({'errors': errors}), content_type="application/json")
Esempio n. 3
0
    def testParseCSV(self):
        csvtxt = open(FILE_PATH + 'csv/digikey.csv').read()
        
        data =parseDigikeyCSV(csvtxt)

        print json.dumps(data,indent=4)
Esempio n. 4
0
    def testParseCSV(self):
        csvtxt = open(FILE_PATH + 'csv/digikey.csv').read()

        data = parseDigikeyCSV(csvtxt)

        print json.dumps(data, indent=4)