Skip to content

Commit

Permalink
linted
Browse files Browse the repository at this point in the history
  • Loading branch information
minghansun1 committed Nov 23, 2024
1 parent 8efe703 commit 15274d4
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 16 deletions.
1 change: 1 addition & 0 deletions backend/market/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ def delete(self, *args, **kwargs):
self.item.delete()
super().delete(*args, **kwargs)

Check warning on line 68 in backend/market/models.py

View check run for this annotation

Codecov / codecov/patch

backend/market/models.py#L66-L68

Added lines #L66 - L68 were not covered by tests


class ItemImage(models.Model):
item = models.ForeignKey(Item, on_delete=models.CASCADE, related_name="images")
image = models.ImageField(upload_to="marketplace/images")
16 changes: 7 additions & 9 deletions backend/market/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand All @@ -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(

Check warning on line 146 in backend/market/serializers.py

View check run for this annotation

Codecov / codecov/patch

backend/market/serializers.py#L145-L146

Added lines #L145 - L146 were not covered by tests
instance=instance.item, data=item_data, context=self.context, partial=True
)
item_serializer.is_valid(raise_exception=True)
validated_data["item"] = item_serializer.save()

Check warning on line 150 in backend/market/serializers.py

View check run for this annotation

Codecov / codecov/patch

backend/market/serializers.py#L149-L150

Added lines #L149 - L150 were not covered by tests

Expand Down
20 changes: 13 additions & 7 deletions backend/market/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -17,15 +16,13 @@
SubletOwnerPermission,
)
from market.serializers import (
CategorySerializer,
ItemImageSerializer,
ItemImageURLSerializer,
ItemSerializer,
ItemSerializerRead,
OfferSerializer,
SubletSerializer,
)
from pennmobile.analytics import Metric, record_analytics


User = get_user_model()
Expand Down Expand Up @@ -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 (

Check warning on line 83 in backend/market/views.py

View check run for this annotation

Codecov / codecov/patch

backend/market/views.py#L83

Added line #L83 was not covered by tests
ItemSerializerRead
if self.action == "list" or self.action == "retrieve"
else ItemSerializer
)

@staticmethod
def get_filter_dict():
Expand Down Expand Up @@ -168,7 +169,9 @@ def list(self, request, *args, **kwargs):
serializer = self.get_serializer(queryset, many=True)

Check warning on line 169 in backend/market/views.py

View check run for this annotation

Codecov / codecov/patch

backend/market/views.py#L169

Added line #L169 was not covered by tests
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"]
Expand All @@ -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 = [

Check warning on line 193 in backend/market/views.py

View check run for this annotation

Codecov / codecov/patch

backend/market/views.py#L191-L193

Added lines #L191 - L193 were not covered by tests
self.get_serializer(data={"item": item_id, "image": img}) for img in images
]
for img_serializer in img_serializers:

Check warning on line 196 in backend/market/views.py

View check run for this annotation

Codecov / codecov/patch

backend/market/views.py#L196

Added line #L196 was not covered by tests
img_serializer.is_valid(raise_exception=True)
instances = [img_serializer.save() for img_serializer in img_serializers]
Expand All @@ -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"]
Expand Down

0 comments on commit 15274d4

Please sign in to comment.