def update(request, pk): try: get_item = Item.objects.get(pk=pk) except Item.DoesNotExist: return Response(status=status.HTTP_404_NOT_FOUND) if request.method == 'PATCH': serializer = ItemSerializer(get_item, data=request.data, partial=True) if serializer.is_valid(): serializer.save() return Response({ 'response_code': '200', 'response': status.HTTP_200_OK, 'message': 'Updated successfully', 'data': serializer.data }) return Response({ 'response_code': '500', 'response': status.HTTP_500_INTERNAL_SERVER_ERROR, 'message': 'Something went wrong', 'data': request.data, 'errors': serializer.errors })
def post_post(request): error_val = {} error_data = {} result_data = {} #checking request length request_len = len(request.data) count_valid_data = 0 for info in range(request_len): serializer = ItemSerializer(data=request.data[info]) if serializer.is_valid(): serializer.save() count_valid_data = count_valid_data + 1 else: result_data[info + 1] = { "errors": serializer.errors, "data": serializer.data } result = { "success": count_valid_data, "failed": request_len - count_valid_data } return Response({ 'response_code': '200', 'response': status.HTTP_201_CREATED, 'message': 'Successfully', 'data': [result, result_data] })
def fetch_item_meta_data(item): item_object = Item.objects.get(item=item) try: item_response = client.Item.get(item_object.access_token) except plaid.errors.PlaidError as e: return "Plaid error occurred." item_serializer = ItemSerializer(instance=item_object, data=item_response['item'], context={'available_products': item_response['item']['available_products'], 'billed_products': item_response['item']['billed_products'] }) item_serializer.is_valid(raise_exception=True) item_serializer.save() return "Item meta data updated"
def post(self, request, **kwargs): identifier = request.META.get('HTTP_AUTHORIZATION') identifier = request.GET.get( 'identifier') if not identifier else identifier.split(' ')[1] token = AccessToken.objects.get(token=identifier) user = token.user data = request.DATA data['owner'] = user.id data['condition'] = True if data['condition'] == "brand_new" else False data['subcategory'] = Subcategory.objects.get( name=data.get('subcategory')).id serializer = ItemSerializer(data=data) if serializer.is_valid(): item = serializer.save() profile = UserProfile.objects.get(user__id=int(user.id)) profile.current_latitude = data.get('latitude') profile.current_longitude = data.get('longitude') profile.save() train_system() return Response(item.to_dict(), status=status.HTTP_201_CREATED) else: return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def item_list(request): """ list of all items or create new item """ if request.method == 'GET': items = Item.objects.all() serializer = ItemSerializer(items, many=True) return Response(serializer.data, status=status.HTTP_201_CREATED) elif request.method == 'POST': serializer = ItemSerializer(data=request.data) data = request.data if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) else: return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def store(request): serializer = ItemSerializer(data=request.data) if serializer.is_valid(): serializer.save() return Response({ 'response_code': '201', 'response': status.HTTP_201_CREATED, 'message': 'Created successfully', 'data': serializer.data }) return Response({ 'response_code': '500', 'response': status.HTTP_500_INTERNAL_SERVER_ERROR, 'message': 'Something went wrong', 'data': request.data, 'errors': serializer.errors })
def post(self, request, **kwargs): data = request.DATA item = Item.objects.get(id=data.get('id')) data['owner'] = item.owner.id data['condition'] = True if data['condition'] == "brand_new" else False data['subcategory'] = Subcategory.objects.get( name=data.get('subcategory')).id if not data.get('photo'): item.name = data.get('name') item.description = data.get('description') item.condition = data.get('condition') item.subcategory = Subcategory.objects.get( id=data.get('subcategory')) item.latitude = data.get('latitude') item.longitude = data.get('longitude') item.price_range_minimum = data.get('price_range_minimum') item.price_range_maximum = data.get('price_range_maximum') item.save() return Response(item.to_dict(), status=status.HTTP_201_CREATED) else: serializer = ItemSerializer(item, data=data) if serializer.is_valid(): # Deletes first its previous image to save server storage space if item.photo: item.photo.delete(False) serializer.save() return Response(item.to_dict(), status=status.HTTP_201_CREATED) else: return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)