diff --git a/asset/admin.py b/asset/admin.py index 12af028..b6dd685 100644 --- a/asset/admin.py +++ b/asset/admin.py @@ -1,5 +1,6 @@ from django.contrib import admin -from .models import Asset +from .models import Asset, GtinProduct admin.site.register(Asset) +admin.site.register(GtinProduct) diff --git a/asset/forms.py b/asset/forms.py new file mode 100644 index 0000000..2bc895c --- /dev/null +++ b/asset/forms.py @@ -0,0 +1,7 @@ +from django import forms + +class AssetForm(forms.Form): + named_id = forms.CharField(label='Asset named ID', max_length=30) + description = forms.CharField(label='Description', max_length=250) + color = forms.CharField(label='Color', max_length=20) + container_type = forms.ChoiceField(label='Container Type', ) \ No newline at end of file diff --git a/asset/gtin_service.py b/asset/gtin_service.py new file mode 100644 index 0000000..9fa1965 --- /dev/null +++ b/asset/gtin_service.py @@ -0,0 +1,41 @@ +from .models import GtinProduct +from django.db.models import ObjectDoesNotExist +import requests +import logging + +LOGGER = logging.getLogger(__name__) + + +def ask_upcitemdb_service(gtin): + request = f'https://api.upcitemdb.com/prod/trial/lookup?upc={gtin}' + response = requests.get(request) + if response.status_code == 200: + productJson = response.json() + print(f"Got {productJson['total']} result records") + if productJson['total'] > 0: + product = GtinProduct.objects.create(gtin=gtin, api_request=request, api_response=productJson) + if 'title' in productJson['items'][0]: + product.name = productJson['items'][0]['title'] + if 'brand' in productJson['items'][0]: + product.brand = productJson['items'][0]['brand'] + LOGGER.debug( + f"Creating new product entry for gtin={gtin}:\nrequest={request}\nresponse={response.status_code}:{productJson}") + product.save() + return product + return None + + +def ask_remote_services(gtin): + product = ask_upcitemdb_service(gtin) + if product is not None: + return product + return None + + +def get_by_gtin(gtin): + try: + product = GtinProduct.objects.get(gtin=gtin) + except ObjectDoesNotExist: + # ask remote services + product = ask_remote_services(gtin) + return product diff --git a/asset/migrations/0003_gtinproduct.py b/asset/migrations/0003_gtinproduct.py new file mode 100644 index 0000000..abacaaa --- /dev/null +++ b/asset/migrations/0003_gtinproduct.py @@ -0,0 +1,26 @@ +# Generated by Django 4.0.3 on 2022-04-13 12:08 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('asset', '0002_auto_20220310_1620'), + ] + + operations = [ + migrations.CreateModel( + name='GtinProduct', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('gtin', models.CharField(max_length=40, unique=True)), + ('api_request', models.CharField(max_length=250)), + ('api_response', models.CharField(max_length=4096)), + ('name', models.CharField(blank=True, max_length=200)), + ('brand', models.CharField(blank=True, max_length=40)), + ('created_ts', models.DateTimeField(auto_now_add=True, verbose_name='datetime created')), + ('changed_ts', models.DateTimeField(auto_now=True, verbose_name='datetime updated')), + ], + ), + ] diff --git a/asset/models.py b/asset/models.py index 7e13c23..396be05 100644 --- a/asset/models.py +++ b/asset/models.py @@ -23,3 +23,13 @@ class Asset(models.Model): def get_absolute_url(self): return reverse('asset:detail', kwargs={'pk': self.pk}) + + +class GtinProduct(models.Model): + gtin = models.CharField(max_length=40, unique=True) + api_request = models.CharField(max_length=250) + api_response = models.CharField(max_length=4096) + name = models.CharField(max_length=200, blank=True) + brand = models.CharField(max_length=40, blank=True) + created_ts = models.DateTimeField('datetime created', auto_now_add=True) + changed_ts = models.DateTimeField('datetime updated', auto_now=True) diff --git a/asset/templates/asset/asset_list.html b/asset/templates/asset/asset_list.html index 649566e..3b8aebd 100644 --- a/asset/templates/asset/asset_list.html +++ b/asset/templates/asset/asset_list.html @@ -49,10 +49,23 @@
-
- + Asset - + Container -
+
+ {% csrf_token %} +
+ +
+
+ +
+ +
+
+
+
+
diff --git a/asset/urls.py b/asset/urls.py index b5d4bac..48f612d 100644 --- a/asset/urls.py +++ b/asset/urls.py @@ -1,11 +1,12 @@ from django.urls import path from . import views -from asset.views import AssetCreateView, AssetDeleteView, AssetUpdateView, AssetListView +from asset.views import AssetCreateView, AssetDeleteView, AssetUpdateView, AssetListView, asset_from_gtin app_name = 'asset' urlpatterns = [ path('', AssetListView.as_view(), name='list'), path('add/', AssetCreateView.as_view(), name='add'), + path('addbygtin/', asset_from_gtin, name='add-by-gtin'), path('/', AssetUpdateView.as_view(), name='update'), path('/', AssetUpdateView.as_view(), name='detail'), path('/delete/', AssetDeleteView.as_view(), name='delete'), diff --git a/asset/views.py b/asset/views.py index a8a6df3..db60657 100644 --- a/asset/views.py +++ b/asset/views.py @@ -54,3 +54,15 @@ def asset_save(request, asset_id): asset.quantity = request.POST['quantity'] asset.save(); return HttpResponseRedirect(reverse('asset:list')) + + +def asset_from_gtin(request): + from .gtin_service import get_by_gtin + gtin = request.POST['gtin'] + print(f'Try gtin={gtin}') + product = get_by_gtin(gtin) + if product is None: + print(f'ERROR: Could not create asset for gtin={gtin}') + else: + print(f'Create new product: {product.name}, brand={product.brand}, gtin={product.gtin}') + return HttpResponseRedirect(reverse('asset:list'))