Commits

Viorel Stirbu committed 103dbf9

return 404 instead of raising exception for products not found

Comments (0)

Files changed (3)

atexpc/ancora_api/api.py

     def product(self, product_id):
         def post_process(data):
             json_root = 'product_info'
-            product = data[json_root][0]
-            return self._post_process_product(product)
+            product = data[json_root][0] if len(data[json_root]) else None
+            return self._post_process_product(product) if product else None
             
         product_uri = self.adapter.base_uri_with_args({'cod_formular': '738',
                                                        'pidm': product_id})

atexpc/atex_web/models.py

     def get_and_save(self, product_id, update=False):
         """ Fetch product from API and save basic details in database """
         product_raw = self.get_product(product_id)
+        if product_raw is None:
+            return None
         product_id = int(product_raw['id'])
         product_fields = dict((field.name, product_raw.get(field.name)) 
                               for field in Product._meta.fields)

atexpc/atex_web/views.py

 from django.template import Context, Template
 from django.views.generic.base import TemplateView
 from django.contrib.sites.models import get_current_site
+from django.http import Http404
 from django.conf import settings
 
 from models import Product, Categories
         if not hasattr(self, '_product'):
             product_id = self.kwargs['product_id']
             product_obj = Product.objects.get_and_save(product_id)
+            if product_obj is None:
+                raise Http404()
             product = product_obj.raw
             product['images'] = product_obj.images()
             html_template = product_obj.html_description()