add quickinfo app
This commit is contained in:
0
quickinfo/__init__.py
Normal file
0
quickinfo/__init__.py
Normal file
5
quickinfo/apps.py
Normal file
5
quickinfo/apps.py
Normal file
@ -0,0 +1,5 @@
|
||||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class QuickinfoConfig(AppConfig):
|
||||
name = 'quickinfo'
|
||||
0
quickinfo/migrations/__init__.py
Normal file
0
quickinfo/migrations/__init__.py
Normal file
26
quickinfo/tests.py
Normal file
26
quickinfo/tests.py
Normal file
@ -0,0 +1,26 @@
|
||||
from django.test import SimpleTestCase
|
||||
|
||||
from . import views
|
||||
|
||||
|
||||
class IDCleanTest(SimpleTestCase):
|
||||
|
||||
def test_clean_nothing(self):
|
||||
self.assertEqual(None, views.clean_id(None))
|
||||
|
||||
def test_clean_digit(self):
|
||||
self.assertEqual('1', views.clean_id('1'))
|
||||
|
||||
def test_clean_character(self):
|
||||
self.assertEqual(None, views.clean_id('a'))
|
||||
|
||||
def test_clean_digits(self):
|
||||
self.assertEqual('123456', views.clean_id('123456'))
|
||||
|
||||
def test_clean_digits_space(self):
|
||||
self.assertEqual('123456', views.clean_id("\t 123456 \t "))
|
||||
|
||||
def test_clean_multiple(self):
|
||||
self.assertEqual('123456', views.clean_id('123456 2'))
|
||||
self.assertEqual('123456', views.clean_id('123456,2'))
|
||||
self.assertEqual('123456', views.clean_id('123456b2x'))
|
||||
8
quickinfo/urls.py
Normal file
8
quickinfo/urls.py
Normal file
@ -0,0 +1,8 @@
|
||||
from django.urls import path
|
||||
|
||||
from . import views
|
||||
|
||||
urlpatterns = [
|
||||
path('search', views.search, name='quickinfo_search'),
|
||||
path('result', views.result, name='quickinfo_result'),
|
||||
]
|
||||
37
quickinfo/views.py
Normal file
37
quickinfo/views.py
Normal file
@ -0,0 +1,37 @@
|
||||
import re
|
||||
|
||||
from django.shortcuts import render, get_object_or_404
|
||||
from django.core.exceptions import ObjectDoesNotExist
|
||||
|
||||
from products.models import Product, ProductImage, ProductImageFormat
|
||||
|
||||
|
||||
def search(request):
|
||||
return render(request, 'quickinfo/search.html')
|
||||
|
||||
|
||||
def result(request):
|
||||
id = clean_id(request.GET['id'])
|
||||
|
||||
context = {
|
||||
'id': id,
|
||||
}
|
||||
|
||||
try:
|
||||
context['prod'] = Product.objects.filter(sap=id).first()
|
||||
except ObjectDoesNotExist:
|
||||
context['prod'] = None
|
||||
|
||||
context['image'] = ProductImage.get_image_url(id, ProductImageFormat.MED_JPG)
|
||||
|
||||
return render(request, 'quickinfo/result.html', context)
|
||||
|
||||
|
||||
def clean_id(text):
|
||||
if not text: return None
|
||||
|
||||
words = list(filter(None, re.compile(r'[^\d]').split(text)))
|
||||
if len(words):
|
||||
return words[0]
|
||||
else:
|
||||
return None
|
||||
Reference in New Issue
Block a user