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 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 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 query_status_update(request): item_status = request.data.get('status', None) item_type = request.data.get('type', None) get_item = Item.objects.all() #check try: if item_status is not None: get_item = get_item.filter(status=item_status) if item_type is not None: get_item = get_item.filter(type=item_type) except Item.DoesNotExist: return Response({ 'response': status.HTTP_404_NOT_FOUND, 'message': 'Something went wrong' }) if request.method == 'POST': try: serializer = ItemSerializer(get_item, many=True, partial=True) quer = serializer if serializer.data: #return Response('Found') return Response({ 'response_code': '200', 'response': status.HTTP_200_OK, 'message': 'Ok', 'data': serializer.data }) else: #return Response('Not Found') return Response({ 'response_code': '500', 'response': status.HTTP_500_INTERNAL_SERVER_ERROR, 'message': 'No data found' }) except: return Response({ 'response_code': '500', 'response': status.HTTP_500_INTERNAL_SERVER_ERROR, 'message': 'Something went wrong', 'data': request.data })
def test_retrieve_item_seller(self): """Test retrieving an item by a seller is successful""" item = Item.objects.create( user=self.user, name='Test Item', price=12, description='Item description', url='c2c.com/static/item.jpg' ) RESOURCE_URL = reverse('item:resource', args=[item.id]) res = self.client.get(RESOURCE_URL) serializer = ItemSerializer(item) self.assertEqual(res.status_code, status.HTTP_200_OK) self.assertEqual(res.data, serializer.data)
def user(self, request, item_group_id): """ Method returns items which should be shown to user when its approving a request :param request: default :param item_group_id: ItemGroup ID of the request being approved :return: List of items """ item_ids = ApprovedItem.objects.select_related('approved_item__item_group').filter( approved_to=request.user, status=constants.ACKNOWLEDGE_STATUS.ACKNOWLEDGED, approved_item__item_group__id=item_group_id, approved_item__type=constants.ITEM_TYPE.SHAREABLE ).values_list('approved_item__id', flat=True) qs = self.get_queryset().filter(id__in=item_ids) item_serializer = ItemSerializer(qs, many=True) return Response(data=item_serializer.data, status=status.HTTP_200_OK)
def get_query_by_date(request): start_date = request.data.get('start_date', None) end_date = request.data.get('end_date', None) if (start_date) is not None: if (end_date) is not None: samples = Item.objects.filter(created_at__gte=start_date, created_at__lte=end_date) else: samples = Item.objects.filter(created_at__gte=start_date) else: if (end_date) is not None: samples = Item.objects.filter(created_at__lte=end_date) else: samples = Item.objects.filter() if request.method == 'POST': try: serializer = ItemSerializer(samples, many=True, partial=True) #return Response('Found') if serializer.data: #return Response('Found') return Response({ 'response_code': '200', 'response': status.HTTP_200_OK, 'message': 'Ok', 'data': serializer.data }) else: #return Response('Not Found') return Response({ 'response_code': '500', 'response': status.HTTP_500_INTERNAL_SERVER_ERROR, 'message': 'No data found' }) except: return Response({ 'response_code': '500', 'response': status.HTTP_500_INTERNAL_SERVER_ERROR, 'message': 'Something went wrong', 'data': request.data })
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 index(request): try: get_all_items = Item.objects.all() serializer = ItemSerializer(get_all_items, many=True) return Response({ 'response_code': '200', 'response': status.HTTP_200_OK, 'message': 'Ok', 'data': serializer.data }) except: return Response({ 'response_code': '500', 'response': status.HTTP_500_INTERNAL_SERVER_ERROR, 'message': 'Something went wrong' })
def post(self, request): id = request.data.get('id', None) name = request.data.get('name', None) email = request.data.get('email', None) location = request.data.get('location', None) message = self.validate(id, name, email, location) if message: return Response(message, status=status.HTTP_400_BAD_REQUEST) try: item = Item.objects.get(id=id) buyer = Buyer.objects.create(name=name, email=email, location=location) item.buyers.add(buyer) data = ItemSerializer(item).data return Response(data, status=status.HTTP_201_CREATED) except ObjectDoesNotExist: message = {'id': 'Item with provided id does not exist'} return Response(message, status=status.HTTP_404_NOT_FOUND)
def post(self, request): if not self.request.user.is_authenticated: message = {'detail': ErrorDetail(string='Authentication credentials were not provided.', code='not_authenticated')} return Response(message, status=status.HTTP_401_UNAUTHORIZED) id = request.data.get('id', None) message = self.validate(id) if message: return Response(message, status=status.HTTP_400_BAD_REQUEST) try: item = Item.objects.get(id=id) item.is_sold = True item.save() data = ItemSerializer(item).data return Response(data, status=status.HTTP_200_OK) except ObjectDoesNotExist: message = {'id': 'Item with provided id does not exist'} return Response(message, status=status.HTTP_404_NOT_FOUND)
def test_update_item_seller(self): """Test updating an item with valid parameters by a seller passes""" item = Item.objects.create( user=self.user, name='Test Item Other Seller', price=12, description='Item description', url='c2c.com/static/item.jpg' ) RESOURCE_URL = reverse('item:resource', args=[item.id]) payload = { 'name': 'Updated Item Name', 'url': 'c2c.com/static/updated-item.jpg' } res = self.client.patch(RESOURCE_URL, payload) item.refresh_from_db() serializer = ItemSerializer(item) self.assertEqual(res.status_code, status.HTTP_200_OK) self.assertEqual(res.data, serializer.data)
def show(request, pk): try: get_item_detail = Item.objects.get(pk=pk) except Item.DoesNotExist: return Response(status=status.HTTP_404_NOT_FOUND) if request.method == 'GET': serializer = ItemSerializer(get_item_detail) return Response({ 'response_code': '200', 'response': status.HTTP_200_OK, 'message': 'Ok', 'data': serializer.data }) return Response({ 'response_code': '500', 'response': status.HTTP_500_INTERNAL_SERVER_ERROR, 'message': 'Something went wrong', 'data': request.data })
def test_retrieve_item_list_seller(self): """Test retrieving a list of items by a seller is successful""" Item.objects.create( user=self.user, name='Test Item1', price=12, description='Item1 description', url='c2c.com/static/item1.jpg' ) Item.objects.create( user=self.user, name='Test Item2', price=12, description='Item2 description', url='c2c.com/static/item2.jpg' ) res = self.client.get(ITEMS_URL) items = Item.objects.all() serializer = ItemSerializer(items, many=True) self.assertEqual(res.status_code, status.HTTP_200_OK) self.assertEqual(res.data, serializer.data)
def search_item(request): name = request.data.get('name', None) get_item = Item.objects.all() try: if name is not None: get_item = get_item.filter(name__icontains=name) except Item.DoesNotExist: return Response({ 'response': status.HTTP_404_NOT_FOUND, 'message': 'Something went wrong' }) try: serializer = ItemSerializer(get_item, many=True, partial=True) if serializer.data: return Response({ 'response_code': '200', 'response': status.HTTP_200_OK, 'message': 'Ok', 'data': serializer.data }) else: return Response({ 'response_code': '500', 'response': status.HTTP_500_INTERNAL_SERVER_ERROR, 'message': 'No data found' }) except: return Response({ 'response_code': '500', 'response': status.HTTP_500_INTERNAL_SERVER_ERROR, 'message': 'Something went wrong', 'data': request.data })
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)
def test_retrieve_item_list_buyer(self): """Test retrieving a list of items by a buyer is successful""" user = get_user_model().objects.create_user('*****@*****.**', 'testpassword') Item.objects.create( user=user, name='Test Item1', price=12, description='Item1 description', url='c2c.com/static/item1.jpg' ) Item.objects.create( user=user, name='Test Item2', price=12, description='Item2 description', url='c2c.com/static/item2.jpg' ) res = self.client.get(ITEMS_URL) items = Item.objects.all().order_by('-created_at') serializer = ItemSerializer(items, many=True) self.assertEqual(res.status_code, status.HTTP_200_OK) self.assertEqual(res.data, serializer.data)
class ItemOrderSerializer(ModelSerializer): item = ItemSerializer(many=False, read_only=True) class Meta: model = ItemOrder fields = '__all__'
class SlotSerializer(serializers.ModelSerializer): item = ItemSerializer() class Meta: model = Slot fields = '__all__'