Skip to content
Open
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
125 changes: 125 additions & 0 deletions backend/market/management/commands/seed_offers.py
Comment thread
LautaroJBeck marked this conversation as resolved.
Outdated
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
from decimal import Decimal
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file is a script to artificially add offers to one listing

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok maybe make a comment in the file. Also not sure seed_offer is the best name


from django.contrib.auth import get_user_model
from django.core.management.base import BaseCommand
from django.utils import timezone

from market.models import Category, Item, Listing, Offer


User = get_user_model()


class Command(BaseCommand):
help = "Seed a listing with two pending offers for testing"

def add_arguments(self, parser):
parser.add_argument(
"--listing-id",
type=int,
default=None,
help="Add offers to an existing listing by ID",
)

def handle(self, *args, **options):
alice, _ = User.objects.get_or_create(
username="alice",
defaults={
"email": "alice@example.com",
"first_name": "Alice",
"last_name": "Johnson",
"phone_number": "+12155551234",
"phone_verified": True,
},
)
alice.set_password("testpassword123")
alice.save()

bob, _ = User.objects.get_or_create(
username="bob",
defaults={
"email": "bob@example.com",
"first_name": "Bob",
"last_name": "Williams",
"phone_number": "+12155555678",
"phone_verified": True,
},
)
bob.set_password("testpassword123")
bob.save()

self.stdout.write(
self.style.SUCCESS("Buyers ready: Alice Johnson, Bob Williams")
)

listing_id = options["listing_id"]
if listing_id:
try:
listing = Listing.objects.get(pk=listing_id)
except Listing.DoesNotExist:
self.stdout.write(
self.style.ERROR(f"Listing with id={listing_id} not found")
)
return
self.stdout.write(
self.style.SUCCESS(
f"Using existing listing: {listing.title} (id={listing.id})"
)
)
else:
seller, _ = User.objects.get_or_create(
username="lautaro",
defaults={
"email": "lautaro@example.com",
"first_name": "Lautaro",
"last_name": "Beck",
},
)
seller.set_password("testpassword123")
seller.save()
self.stdout.write(
self.style.SUCCESS(f"Seller ready: {seller.get_full_name()}")
)

category, _ = Category.objects.get_or_create(name="Furniture")
listing = Item.objects.create(
seller=seller,
title="New offer",
description="ASDFASFAFS",
price=Decimal("12312.00"),
negotiable=True,
expires_at=timezone.now() + timezone.timedelta(days=60),
condition=Item.Condition.GOOD,
category=category,
)
self.stdout.write(self.style.SUCCESS(f"Created listing: {listing.title}"))

_, created_alice = Offer.objects.get_or_create(
user=alice,
listing=listing,
defaults={
"offered_price": Decimal("40.00"),
"message": "Would you take $40? I can pick up today.",
},
)

_, created_bob = Offer.objects.get_or_create(
user=bob,
listing=listing,
defaults={
"offered_price": Decimal("45.00"),
"message": "Interested! Is the price negotiable?",
},
)

new_count = sum([created_alice, created_bob])
skipped = 2 - new_count

self.stdout.write(
self.style.SUCCESS(
f"Created {new_count} offers, skipped {skipped} (already existed)"
)
)
self.stdout.write(
self.style.SUCCESS(f"\nDone! Offers added to listing id={listing.id}")
)
26 changes: 26 additions & 0 deletions backend/market/migrations/0005_offer_status.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Generated by Django 5.0.2 on 2026-03-27 21:34

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("market", "0004_rename_address_sublet_street_address_and_more"),
]

operations = [
migrations.AddField(
model_name="offer",
name="status",
field=models.CharField(
choices=[
("pending", "Pending"),
("accepted", "Accepted"),
("rejected", "Rejected"),
],
default="pending",
max_length=10,
),
),
]
8 changes: 8 additions & 0 deletions backend/market/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ class User(AbstractUser):


class Offer(models.Model):
class Status(models.TextChoices):
PENDING = "pending", "Pending"
ACCEPTED = "accepted", "Accepted"
REJECTED = "rejected", "Rejected"

class Meta:
constraints = [
models.UniqueConstraint(
Expand All @@ -37,6 +42,9 @@ class Meta:
max_digits=10, decimal_places=2, validators=[MinValueValidator(0)]
)
message = models.TextField(max_length=500, blank=True)
status = models.CharField(
max_length=10, choices=Status.choices, default=Status.PENDING
)
created_at = models.DateTimeField(auto_now_add=True)

def __str__(self):
Expand Down
12 changes: 9 additions & 3 deletions backend/market/permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,22 @@ def has_object_permission(self, request, view, obj):
)


class OfferOwnerPermission(permissions.BasePermission):
class ListingOwnerOffersPermission(permissions.BasePermission):
"""
Custom permission to allow owner of an offer to delete it.
Permission for listing-owner offer interactions:
- GET: listing seller can view offers on their listing
- PATCH/PUT: listing seller can update offer status
- DELETE: offer creator can delete/withdraw their own offer
"""

def has_permission(self, request, view):
return request.user.is_authenticated

def has_object_permission(self, request, view, obj):
if request.method in permissions.SAFE_METHODS: # GET
if request.method in permissions.SAFE_METHODS:
return obj.listing.seller == request.user

if request.method in ("PATCH", "PUT"):
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can prob combine these 2 conditions since they are doing the same thing

return obj.listing.seller == request.user

return obj.user == request.user
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if request.method in permissions.SAFE_METHODS:
return obj.listing.seller == request.user
if request.method in ("PATCH", "PUT"):
return obj.listing.seller == request.user
return obj.user == request.user
return obj.listing.seller == request.user

39 changes: 37 additions & 2 deletions backend/market/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,49 @@ class OfferSerializer(ModelSerializer):

class Meta:
model = Offer
fields = ["id", "user", "listing", "offered_price", "message", "created_at"]
read_only_fields = ["id", "created_at", "user"]
fields = [
"id",
"user",
"listing",
"offered_price",
"message",
"status",
"created_at",
]
read_only_fields = ["id", "created_at", "user", "status"]

def create(self, validated_data):
validated_data["user"] = self.context["request"].user
return super().create(validated_data)


class OfferStatusSerializer(ModelSerializer):
class Meta:
model = Offer
fields = ["id", "status"]
read_only_fields = ["id"]

def validate_status(self, value):
valid_statuses = [choice[0] for choice in Offer.Status.choices]
if value not in valid_statuses:
raise ValidationError(
f"Invalid status. Must be one of: {', '.join(valid_statuses)}"
)
return value


class OfferDetailsSerializer(ModelSerializer):
"""
Allows the offer owner to edit the offer's offered_price and message.
Status is intentionally read-only (managed by the listing owner).
"""

class Meta:
model = Offer
fields = ["id", "offered_price", "message", "status"]
read_only_fields = ["id", "status"]


# Create/Update Image Serializer
class ListingImageSerializer(ModelSerializer):
image = ImageField(write_only=True, required=False, allow_null=True)
Expand Down
20 changes: 19 additions & 1 deletion backend/market/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@
OffersReceived,
Tags,
UserFavorites,
change_offer_details,
change_offer_status,
get_current_user,
get_my_offer_for_listing,
get_phone_status,
send_verification_code,
verify_phone_code,
Expand Down Expand Up @@ -46,9 +49,24 @@
# post: create an offer for an listing
# delete: delete an offer for an listing
path(
"listings/<listing_id>/offers/",
"listings/<int:listing_id>/offers/",
Offers.as_view({"get": "list", "post": "create", "delete": "destroy"}),
),
# Current user's offer for an individual listing
# (Returns 404 when the user has no offer for that listing.)
path(
"listings/<int:listing_id>/offers/mine/",
get_my_offer_for_listing,
name="offers-mine",
),
# Update offer status (PATCH)
path("offers/<int:offer_id>/", change_offer_status, name="offer-status"),
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make it clear that this is for modifying status by adding /status at the end (like how you did for details)

# Update offer offered_price + message (PATCH)
path(
"offers/<int:offer_id>/details/",
change_offer_details,
name="offer-details",
),
# Image Creation
path("listings/<listing_id>/images/", CreateImages.as_view()),
# Image Deletion
Expand Down
Loading
Loading