From a83ce746b55715afa5a891c85ad378f35afa2818 Mon Sep 17 00:00:00 2001 From: badbl0cks <4161747+badbl0cks@users.noreply.github.com> Date: Tue, 8 Apr 2025 14:12:54 -0700 Subject: [PATCH] When a user is thanking, check state to see if other user thanked first, if so switch new_state to THANKED_BY_BOTH. fixes #13 --- cards/views.py | 12 +++++----- theme/templates/cards/_trade_offer_list.html | 10 -------- theme/templates/trades/_trade_offer_list.html | 2 +- theme/templatetags/trade_offer.html | 2 +- trades/models.py | 23 +++++++++---------- trades/signals.py | 10 ++++---- trades/views.py | 18 +++++++++------ 7 files changed, 35 insertions(+), 42 deletions(-) delete mode 100644 theme/templates/cards/_trade_offer_list.html diff --git a/cards/views.py b/cards/views.py index e98652f..18244fc 100644 --- a/cards/views.py +++ b/cards/views.py @@ -26,9 +26,9 @@ class CardDetailView(DetailView): class TradeOfferHaveCardListView(ListView): model = TradeOffer - template_name = "cards/_trade_offer_list.html" - context_object_name = "trade_offers" - paginate_by = 6 + template_name = "trades/_trade_offer_list.html" + context_object_name = "offers" + paginate_by = 2 def get_queryset(self): card_id = self.kwargs.get("pk") @@ -46,9 +46,9 @@ class TradeOfferHaveCardListView(ListView): class TradeOfferWantCardListView(ListView): model = TradeOffer - template_name = "cards/_trade_offer_list.html" - context_object_name = "trade_offers" - paginate_by = 6 + template_name = "trades/_trade_offer_list.html" + context_object_name = "offers" + paginate_by = 2 def get_queryset(self): card_id = self.kwargs.get("pk") diff --git a/theme/templates/cards/_trade_offer_list.html b/theme/templates/cards/_trade_offer_list.html deleted file mode 100644 index e7f586e..0000000 --- a/theme/templates/cards/_trade_offer_list.html +++ /dev/null @@ -1,10 +0,0 @@ -{% load trade_offer_tags %} -{% if trade_offers %} -
- {% for offer in trade_offers %} - {% render_trade_offer offer %} - {% endfor %} -
-{% else %} -

No trade offers found.

-{% endif %} \ No newline at end of file diff --git a/theme/templates/trades/_trade_offer_list.html b/theme/templates/trades/_trade_offer_list.html index 3b5d9f6..f19fce7 100644 --- a/theme/templates/trades/_trade_offer_list.html +++ b/theme/templates/trades/_trade_offer_list.html @@ -1,6 +1,6 @@ {% load trade_offer_tags pagination_tags %} -
+
{% for offer in offers %} {% if offer.accepted_by %} {# Render a trade acceptance using our new tag #} diff --git a/theme/templatetags/trade_offer.html b/theme/templatetags/trade_offer.html index 0610834..d620f8e 100644 --- a/theme/templatetags/trade_offer.html +++ b/theme/templatetags/trade_offer.html @@ -1,7 +1,7 @@ {% load gravatar card_badge cache %} {% cache 60 trade_offer offer_pk %} -
+
diff --git a/trades/models.py b/trades/models.py index eab9fda..53d4ce8 100644 --- a/trades/models.py +++ b/trades/models.py @@ -182,7 +182,7 @@ class TradeAcceptance(models.Model): REJECTED_BY_ACCEPTOR = 'REJECTED_BY_ACCEPTOR', 'Rejected by Acceptor' # DRY improvement: define active states once as a class-level constant. - ACTIVE_STATES = [ + POSITIVE_STATES = [ AcceptanceState.ACCEPTED, AcceptanceState.SENT, AcceptanceState.RECEIVED, @@ -223,8 +223,8 @@ class TradeAcceptance(models.Model): # A mapping for alternate action labels ALTERNATE_ACTION_LABELS = { - AcceptanceState.REJECTED_BY_INITIATOR: "Cancel This Trade", - AcceptanceState.REJECTED_BY_ACCEPTOR: "Cancel This Trade", + AcceptanceState.REJECTED_BY_INITIATOR: "Reject This Trade", + AcceptanceState.REJECTED_BY_ACCEPTOR: "Reject This Trade", # Optionally add alternate labels for other states: AcceptanceState.ACCEPTED: "Accept This Trade Offer", AcceptanceState.SENT: "Mark as Sent", @@ -321,6 +321,10 @@ class TradeAcceptance(models.Model): if new_state not in [choice[0] for choice in self.AcceptanceState.choices]: raise ValueError(f"'{new_state}' is not a valid state.") + if (new_state == self.AcceptanceState.THANKED_BY_ACCEPTOR and self.state == self.AcceptanceState.THANKED_BY_INITIATOR) or \ + (new_state == self.AcceptanceState.THANKED_BY_INITIATOR and self.state == self.AcceptanceState.THANKED_BY_ACCEPTOR): + new_state = self.AcceptanceState.THANKED_BY_BOTH + if self.state in [ self.AcceptanceState.THANKED_BY_BOTH, self.AcceptanceState.REJECTED_BY_INITIATOR, @@ -361,9 +365,7 @@ class TradeAcceptance(models.Model): self.AcceptanceState.THANKED_BY_INITIATOR, self.AcceptanceState.REJECTED_BY_INITIATOR, }, - self.AcceptanceState.THANKED_BY_INITIATOR: { - self.AcceptanceState.REJECTED_BY_INITIATOR, - }, + self.AcceptanceState.THANKED_BY_INITIATOR: { }, self.AcceptanceState.THANKED_BY_ACCEPTOR: { self.AcceptanceState.REJECTED_BY_INITIATOR, self.AcceptanceState.THANKED_BY_BOTH, @@ -379,14 +381,11 @@ class TradeAcceptance(models.Model): self.AcceptanceState.REJECTED_BY_ACCEPTOR, }, self.AcceptanceState.RECEIVED: { - self.AcceptanceState.THANKED_BY_ACCEPTOR, - self.AcceptanceState.REJECTED_BY_ACCEPTOR, - }, - self.AcceptanceState.THANKED_BY_ACCEPTOR: { - self.AcceptanceState.REJECTED_BY_ACCEPTOR, + self.AcceptanceState.THANKED_BY_ACCEPTOR, #allow early thanks (uses THANKED_BY_ACCEPTOR state) + self.AcceptanceState.REJECTED_BY_ACCEPTOR }, + self.AcceptanceState.THANKED_BY_ACCEPTOR: { }, self.AcceptanceState.THANKED_BY_INITIATOR: { - self.AcceptanceState.REJECTED_BY_ACCEPTOR, self.AcceptanceState.THANKED_BY_BOTH, }, } diff --git a/trades/signals.py b/trades/signals.py index 0d94e5c..8e034fe 100644 --- a/trades/signals.py +++ b/trades/signals.py @@ -13,7 +13,7 @@ from django.conf import settings from django.template.loader import render_to_string from django.contrib.sites.models import Site -ACTIVE_STATES = [ +POSITIVE_STATES = [ TradeAcceptance.AcceptanceState.ACCEPTED, TradeAcceptance.AcceptanceState.SENT, TradeAcceptance.AcceptanceState.RECEIVED, @@ -70,14 +70,14 @@ def trade_acceptance_pre_save(sender, instance, **kwargs): def trade_acceptance_post_save(sender, instance, created, **kwargs): delta = 0 if created: - if instance.state in ACTIVE_STATES: + if instance.state in POSITIVE_STATES: delta = 1 else: old_state = getattr(instance, '_old_state', None) if old_state is not None: - if old_state in ACTIVE_STATES and instance.state not in ACTIVE_STATES: + if old_state in POSITIVE_STATES and instance.state not in POSITIVE_STATES: delta = -1 - elif old_state not in ACTIVE_STATES and instance.state in ACTIVE_STATES: + elif old_state not in POSITIVE_STATES and instance.state in POSITIVE_STATES: delta = 1 if delta != 0: @@ -88,7 +88,7 @@ def trade_acceptance_post_save(sender, instance, created, **kwargs): @receiver(post_delete, sender=TradeAcceptance) def trade_acceptance_post_delete(sender, instance, **kwargs): - if instance.state in ACTIVE_STATES: + if instance.state in POSITIVE_STATES: delta = -1 trade_offer = instance.trade_offer adjust_qty_for_trade_offer(trade_offer, instance.requested_card, side='have', delta=delta) diff --git a/trades/views.py b/trades/views.py index 003b05b..192c8d7 100644 --- a/trades/views.py +++ b/trades/views.py @@ -498,9 +498,9 @@ class TradeOfferPNGView(View): trade_offer = get_object_or_404(TradeOffer, pk=kwargs['pk']) # If the image is already generated and stored, serve it directly. - # if trade_offer.image: - # trade_offer.image.open() - # return HttpResponse(trade_offer.image.read(), content_type="image/png") + if trade_offer.image and not request.GET.get("debug"): + trade_offer.image.open() + return HttpResponse(trade_offer.image.read(), content_type="image/png") # Acquire PostgreSQL advisory lock to prevent concurrent generation. from django.db import connection @@ -509,10 +509,10 @@ class TradeOfferPNGView(View): cursor.execute("SELECT pg_advisory_lock(%s)", [lock_key]) try: # Double-check if the image was generated while waiting for the lock. - # trade_offer.refresh_from_db() - # if trade_offer.image: - # trade_offer.image.open() - # return HttpResponse(trade_offer.image.read(), content_type="image/png") + trade_offer.refresh_from_db() + if trade_offer.image and not request.GET.get("debug"): + trade_offer.image.open() + return HttpResponse(trade_offer.image.read(), content_type="image/png") tag_context = render_trade_offer_png( {'request': request}, trade_offer, show_friend_code=True @@ -523,6 +523,10 @@ class TradeOfferPNGView(View): raise ValueError("Could not determine image dimensions from tag_context") html = render_to_string("templatetags/trade_offer_png.html", tag_context) + # if query string has "debug=true", render the HTML instead of the PNG + if request.GET.get("debug") == "html": + return render(request, "trades/trade_offer_png_debug.html", {"html": html}) + with sync_playwright() as p: browser = p.chromium.launch( headless=True,