def post(self, request, retailer_id, question_id): response = JsonResponse() try: retailer = Retailer.objects.get(merchant_id=retailer_id) except Retailer.DoesNotExist: # Retailer doesn't exist response.errors['request_failed'] = "No retailer found for id %s" % retailer_id return HttpResponse(json.dumps(response.to_dict()), content_type="application/json", status=403) try: question = retailer.question_set.get(id=question_id) except Question.DoesNotExist: response.errors['request_failed'] = "No question found for id %s and retailer %s" % (question_id, retailer_id,) return HttpResponse(json.dumps(response.to_dict()), status=404, content_type='application/json') # At this point we're happy that both Retailer and Question are correct so the Answer can be processed answer_data = json.loads(request.raw_post_data) answer_form = AnswerForm(answer_data) if answer_form.is_valid(): answer = Answer(answer_text=answer_data['answer_text'], retailer=retailer, question=question, up_votes=0, down_votes=0) question.answer_set.add(answer) answer.save() return HttpResponse(status=201, content_type='application/json') else: response.errors['request_failed'] = "Answer data is not valid" response.errors['messages'] = answer_form.errors return HttpResponse(json.dumps(response.to_dict()), content_type="application/json", status=400)
def post(self, request, retailer_id, product_id): # The request will always be satisfied by a JsonResponse response = JsonResponse() # Attempt to get the specified retailer try: retailer = Retailer.objects.get(merchant_id=retailer_id) except Retailer.DoesNotExist: # Retailer doesn't exist response.errors['request_failed'] = "No retailer found for id %s" % retailer_id return HttpResponse(json.dumps(response.to_dict()), content_type="application/json", status=403) try: # At this point we have found a retailer so now get the product through that retailer item = retailer.item_set.get(retailer_product_id=product_id) except Item.DoesNotExist: # If the item doesn't exist then create a new one item = Item.objects.create(retailer=retailer, retailer_product_id=product_id) # At this point we're happy that both Retailer and Item are correct so the Question can be processed question_data = json.loads(request.body) question_form = QuestionForm(question_data) if question_form.is_valid(): question = Question(question_text=question_data['question_text'], username=question_data['username'], retailer=retailer) item.question_set.add(question) question.save() return HttpResponse(status=201, content_type="application/json") else: response.errors['request_failed'] = "Question data is not valid" response.errors['messages'] = question_form.errors return HttpResponse(json.dumps(response.to_dict()), content_type="application/json", status=400)
def get(self, request, retailer_id, product_id): # logger = logging.getLogger(__name__) # The request will always be satisfied by a JsonResponse response = JsonResponse() # Attempt to get the specified retailer try: retailer = Retailer.objects.get(merchant_id=retailer_id) except Retailer.DoesNotExist: # Retailer doesn't exist so update the JsonResponse and send it back response.success = False response.errors['request_failed'] = "No retailer found for id %s" % retailer_id return HttpResponse(json.dumps(response.to_dict()), content_type="application/json", status=403) # Now try to find the specified item try: item = Item.objects.get(retailer_id=retailer.id, retailer_product_id=product_id) response.payload = item.to_dict() response.success = True except Item.DoesNotExist: # no item found so return a response response.errors['request_failed'] = "No item found for id %s" % product_id return HttpResponse(json.dumps(response.to_dict()), content_type="application/json", status=404) except Item.MultipleObjectsReturned: # this shouldn't happen but need to handle it just in case response.errors['request_failed'] = "Multiple items found for id %s and retailer %s. This should not happen!" % (product_id, retailer_id) return HttpResponse(json.dumps(response.to_dict()), content_type="application/json", status=404) return HttpResponse(json.dumps(response.to_dict()), content_type="application/json", status=200)