diff --git a/backend/market/models.py b/backend/market/models.py index ab683e8c..b80e6bc8 100644 --- a/backend/market/models.py +++ b/backend/market/models.py @@ -67,6 +67,7 @@ def delete(self, *args, **kwargs): self.item.delete() super().delete(*args, **kwargs) + class ItemImage(models.Model): item = models.ForeignKey(Item, on_delete=models.CASCADE, related_name="images") image = models.ImageField(upload_to="marketplace/images") diff --git a/backend/market/serializers.py b/backend/market/serializers.py index fcceffb9..3dc3ad70 100644 --- a/backend/market/serializers.py +++ b/backend/market/serializers.py @@ -15,7 +15,9 @@ class Meta: fields = "__all__" read_only_fields = [field.name for field in model._meta.fields] -# TODO: We could make a Read-Only Serializer in a PennLabs core library. This could inherit from that. + +# TODO: We could make a Read-Only Serializer in a PennLabs core library. +# This could inherit from that. class CategorySerializer(serializers.ModelSerializer): class Meta: model = Category @@ -129,13 +131,7 @@ class SubletSerializer(serializers.ModelSerializer): class Meta: model = Sublet - fields = ["id", - "item", - "address", - "beds", - "baths", - "start_date", - "end_date"] + fields = ["id", "item", "address", "beds", "baths", "start_date", "end_date"] read_only_fields = ["id"] def create(self, validated_data): @@ -147,7 +143,9 @@ def create(self, validated_data): def update(self, instance, validated_data): if item_data := validated_data.pop("item", None): - item_serializer = ItemSerializer(instance=instance.item, data=item_data, context=self.context, partial=True) + item_serializer = ItemSerializer( + instance=instance.item, data=item_data, context=self.context, partial=True + ) item_serializer.is_valid(raise_exception=True) validated_data["item"] = item_serializer.save() diff --git a/backend/market/views.py b/backend/market/views.py index 97740158..4198c678 100644 --- a/backend/market/views.py +++ b/backend/market/views.py @@ -6,7 +6,6 @@ from rest_framework.permissions import IsAuthenticated from rest_framework.response import Response from rest_framework.views import APIView -from django.db.models import Q from market.models import Category, Item, ItemImage, Offer, Sublet, Tag from market.permissions import ( @@ -17,7 +16,6 @@ SubletOwnerPermission, ) from market.serializers import ( - CategorySerializer, ItemImageSerializer, ItemImageURLSerializer, ItemSerializer, @@ -25,7 +23,6 @@ OfferSerializer, SubletSerializer, ) -from pennmobile.analytics import Metric, record_analytics User = get_user_model() @@ -83,7 +80,11 @@ class Items(viewsets.ModelViewSet): queryset = Item.objects.all() def get_serializer_class(self): - return ItemSerializerRead if self.action == "list" or self.action == "retrieve" else ItemSerializer + return ( + ItemSerializerRead + if self.action == "list" or self.action == "retrieve" + else ItemSerializer + ) @staticmethod def get_filter_dict(): @@ -168,7 +169,9 @@ def list(self, request, *args, **kwargs): serializer = self.get_serializer(queryset, many=True) return Response(serializer.data) -# TODO: This doesn't use CreateAPIView's functionality since we overrode the create method. Think about if there's a better way + +# TODO: This doesn't use CreateAPIView's functionality since we overrode the create method. +# Think about if there's a better way class CreateImages(generics.CreateAPIView): serializer_class = ItemImageSerializer http_method_names = ["post"] @@ -187,7 +190,9 @@ def post(self, request, *args, **kwargs): images = request.data.getlist("images") item_id = int(self.kwargs["item_id"]) self.get_queryset() # check if item exists - img_serializers = [self.get_serializer(data={"item": item_id, "image": img}) for img in images] + img_serializers = [ + self.get_serializer(data={"item": item_id, "image": img}) for img in images + ] for img_serializer in img_serializers: img_serializer.is_valid(raise_exception=True) instances = [img_serializer.save() for img_serializer in img_serializers] @@ -211,7 +216,8 @@ def destroy(self, request, *args, **kwargs): return Response(status=status.HTTP_204_NO_CONTENT) -#TODO: We don't use the CreateModelMixin or DestroyModelMixin's functionality here. Think about if there's a better way +# TODO: We don't use the CreateModelMixin or DestroyModelMixin's functionality here. +# Think about if there's a better way class Favorites(mixins.DestroyModelMixin, mixins.CreateModelMixin, viewsets.GenericViewSet): serializer_class = ItemSerializer http_method_names = ["post", "delete"]