def get_item_list_user_had_or_not(request): """ Get 을 통해서, 관리자가 만들어 둔 Item list 를 얻어 오고, user 가 가지고 있는 것과 비교를 하여, user 가 소유를 하고 있는 item list 와 user 가 소유하고 있지 않는 item list 를 찾아서 Return 한다. """ email = request.user.email profile = Profile.objects.get(email=email) try: had_item_list = UserItem.objects.filter( profile=profile).values('item').distinct() had_items = Item.objects.filter(id__in=had_item_list) not_had_items = Item.objects.exclude(id__in=had_item_list) except UserItem.DoesNotExist: had_items = {} not_had_items = Item.objects.all() had_sz = ItemSerializer(had_items, many=True) not_had_sz = ItemSerializer(not_had_items, many=True) return Response({ "had_items": had_sz.data, "not_had_items": not_had_sz.data })
def generate_return_dict(self): counts = Counter( attrgetter('name')(item) for item in self.items_to_buy) budget = sum(item.price for item in self.items_to_buy) + random.randint(1, 6) self.items_to_buy = list(set(self.items_to_buy)) self.items_to_sell = self.items_to_buy + self.items_to_sell serialized_shopping_list = ItemSerializer(self.items_to_buy, many=True, context=counts) serialized_available_items = ItemSerializer(self.items_to_sell, many=True, context=counts) available_items = serialized_available_items.data random.shuffle(available_items) return { 'budget': budget, 'items_count_limit': settings.ITEMS_COUNT_LIMIT, 'scenario': self.scenario, 'shopping_list': serialized_shopping_list.data, 'available_items': available_items, }
class OrderItemSerializer(serializers.ModelSerializer): item = ItemSerializer() class Meta: model = OrderItem fields = ('item', 'quantity') read_only_fields = ('item', )
def admin_create_item(request): """ Admin 이 아이템을 만들기위해서, 구현된 API 이며, 사실상 Admin 을 통해서 들어가서 수정을 할 일이 많다고 생각하고 있음. """ serializer = ItemSerializer(data=request.data) if serializer.is_valid(): serializer.save() return Response( { 'response': 'success', 'message': '성공적으로 아이템을 업로드하였습니다.' }, status=status.HTTP_201_CREATED) return Response({ "response": "error", "message": serializer.errors }, status=status.HTTP_400_BAD_REQUEST)
class ProfileDetailResSerializer(sz.Serializer): email = sz.EmailField() title = sz.CharField() jorang_nickname = sz.CharField() jorang_items = ItemSerializer() jorang_status = sz.CharField() user_continuity = sz.IntegerField() user_coin = sz.IntegerField() class Meta: fields = [ 'email', 'title', 'jorang_nickname', 'jorang_items', 'jorang_status', 'user_continuity', 'user_coin' ]
def get_item_with_pk(request, pk): """ Item 에 pk를 통해서 접근을 하여 해당 Item 을 돌려준다. """ try: item = Item.objects.get(pk=pk) except Item.DoesNotExist: return Response({ 'response': 'error', 'message': 'pk 에 해당하는 Item 이 없습니다.' }) except: raise Http404 serializer = ItemSerializer(item) return Response(serializer.data)
class CartEntrySerializer(serializers.ModelSerializer): item = ItemSerializer() photo = serializers.SerializerMethodField() class Meta: model = CartEntry fields = ('id', 'item', 'quantity', 'photo') read_only_field = ('item', 'photo') def get_photo(self, cart_entry): request = self.context.get('request') try: photo = cart_entry.item.product.image.url return request.build_absolute_uri(photo) except ValueError: # this product has no avatar return None
class CartListSerializer(serializers.ModelSerializer): item = ItemSerializer(many=False, read_only=True) class Meta: model = Cart fields = ('item', 'quantity',)
class LoginJorangSerializer(sz.Serializer): nickname = sz.CharField(max_length=50) items = ItemSerializer(many=True)