Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Alex Skrivseth August 19 2019 #10

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions ecommerce/api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,7 @@

urlpatterns = [
# write your URL rules here

path("products/", views.ProductView.as_view()),
path("products/<int:product_id>/", views.ProductView.as_view())
]
154 changes: 149 additions & 5 deletions ecommerce/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,165 @@
from django.views.generic import View
from django.http import HttpResponse, JsonResponse

from django.views.decorators.csrf import csrf_exempt
from django.utils.decorators import method_decorator

# complete the View below with all REST functionality

@method_decorator(csrf_exempt, name='dispatch')
class ProductView(View):
def _get_object(self, product_id):
try:
return Product.objects.get(id=product_id)
except Product.DoesNotExist:
return None

def get(self, *args, **kwargs):
pass
product_id = kwargs.get('product_id')
#product_id may not come in the kwargs
if product_id:
product = self._get_object(product_id)
# got a product_id but we dont know what it is
if not product:
return JsonResponse(
{"success": False, "msg": "Could not find product with id: {}".format(product_id)},
status=404)
# got a product_id that we matched to a product. We put into json format with the serializer
data = serialize_product_as_json(product)
# we did not get a kwargs key for 'product_id'. we just got a GET request.
# we are going to send back all the products that we have in a list of json objects.
else:
# list
qs = Product.objects.all()
# using the serializer for each product in our db.
data = [serialize_product_as_json(product) for product in qs]
return JsonResponse(data, status=200, safe=False)




# POST methods are supposed to create a new product.
def post(self, *args, **kwargs):
pass
# create
if 'product_id' in kwargs:
return JsonResponse(
{"success": False, "msg": "Invalid HTTP method"},
status=400)

try:
payload = json.loads(self.request.body.decode('utf-8'))
except ValueError:
return JsonResponse(
{"success": False, "msg": "Provide a valid JSON payload"},
status=400)
# THIS BLOCK might need to be implemented to include a Category?
# when creating a new procuct we need to know the category id to link it to an
# existing category. If we don't get a category id we return an error message.
category_id = payload.get('category', None)
try:
category = Category.objects.get(id=category_id)
except Category.DoesNotExist:
return JsonResponse(
{"success": False, "msg": "Could not find category with id: {}".format(category_id)},
status=404)
# THIS BLOCK
try:
product = Product.objects.create(
name=payload['name'],
sku=payload['sku'],
category=category,
description=payload['description'],
price=payload['price'])
except (ValueError, KeyError):
return JsonResponse(
{"success": False, "msg": "Provided payload is not valid"},
status=400)
data = serialize_product_as_json(product)
return JsonResponse(data, status=201, safe=False)




def delete(self, *args, **kwargs):
pass
product_id = kwargs.get('product_id')
if not product_id:
return JsonResponse(
{'msg': 'Invalid HTTP method', 'success': False},
status=400)
product = self._get_object(product_id)
if not product:
return JsonResponse(
{"success": False, "msg": "Could not find product with id: {}".format(product_id)},
status=404)
product.delete()
data = {"success": True}
return JsonResponse(data, status=204, safe=False)

# need a helper function

def _update(self, product, payload, partial=False):
for field in ['name', 'sku', 'category', 'description', 'price', 'featured']:
if not field in payload:
if partial:
continue
return JsonResponse(
{"success": False, "msg": "Missing field in full update"},
status=400)
# category
if field == 'category':

try:
payload['category'] = Category.objects.get(id=payload['category'])
except Category.DoesNotExist:
return JsonResponse(
{"success": False, "msg": "Could not find category with id: {}".format(category_id)},
status=404)


try: #product is an instance of product set within the function#
setattr(product, field, payload[field])
product.save()
except ValueError:
return JsonResponse(
{"success": False, "msg": "Provided payload is not valid"},
status=400)
data = serialize_product_as_json(product)
return JsonResponse(data, status=200, safe=False)



# partial update
def patch(self, *args, **kwargs):
pass

product_id = kwargs.get('product_id')
product = self._get_object(product_id)
if not product:
return JsonResponse(
{"success": False, "msg": "Could not find product with id: {}".format(product_id)},
status=404)
try:
payload = json.loads(self.request.body.decode('utf-8'))
except ValueError:
return JsonResponse(
{"success": False, "msg": "Provide a valid JSON payload"},
status=400)
return self._update(product, payload, partial=True)


# full update
def put(self, *args, **kwargs):
pass

product_id = kwargs.get('product_id')
product = self._get_object(product_id)
if not product:
return JsonResponse(
{"success": False, "msg": "Could not find product with id: {}".format(product_id)},
status=404)
try:
payload = json.loads(self.request.body.decode('utf-8'))
except ValueError:
return JsonResponse(
{"success": False, "msg": "Provide a valid JSON payload"},
status=400)
return self._update(product, payload, partial=False)