From b833648bcbfe0a3cf03b83f88b39916981130b78 Mon Sep 17 00:00:00 2001
From: Christian Dresen <c.dresen@fh-muenster.de>
Date: Tue, 4 Apr 2017 23:24:08 +0200
Subject: [PATCH] [WarpPay] Added Barcode to REST

---
 www/web/warppay/urls.py  |  1 +
 www/web/warppay/views.py | 17 ++++++++++++++++-
 2 files changed, 17 insertions(+), 1 deletion(-)

diff --git a/www/web/warppay/urls.py b/www/web/warppay/urls.py
index 626d238..0551827 100644
--- a/www/web/warppay/urls.py
+++ b/www/web/warppay/urls.py
@@ -9,5 +9,6 @@ urlpatterns = [
     url(r'^api/categories/$', views.category_list),
     url(r'^api/gen_token/$', views.gen_token),
     url(r'^api/transaction/(?P<user_id>\w+)/$', views.transaction),
+    url(r'^api/products/(?P<prod_id>/barcode\w+)/$', views.addBarcode),
 
 ]
diff --git a/www/web/warppay/views.py b/www/web/warppay/views.py
index 6ea282f..2e48de1 100644
--- a/www/web/warppay/views.py
+++ b/www/web/warppay/views.py
@@ -37,6 +37,22 @@ def product_list(request):
         return Response()
     return Response()
 
+@api_view(['PUT'])
+def addBarcode(request, prod_id=0):
+    if request.method == 'PUT':
+        try:
+            product = Product.objects.get(id=prod_id)
+        except ObjectDoesNotExist:
+            return Response(status=status.HTTP_404_NOT_FOUND)
+        if not product.barcode:
+            product.barcode = request.data['barcode']
+            product.save()
+            return Response()
+        else:
+            return Response(status=status.HTTP_406_NOT_ACCEPTABLE)
+    else:
+        return Response(status=status.HTTP_406_NOT_ACCEPTABLE)
+
 
 @api_view(['GET'])
 def gen_token(request):
@@ -143,7 +159,6 @@ def transaction(request, user_id=None):
         u.save()
         return Response()
 
-
 def sync_users():
     for user in LdapUser.objects.all():
         try:
-- 
GitLab