Skip to content

Commit

Permalink
Merge pull request #236 from iragm/auction-filter
Browse files Browse the repository at this point in the history
Auction filter
  • Loading branch information
iragm authored Oct 5, 2024
2 parents a892f4c + a654049 commit 6397c94
Show file tree
Hide file tree
Showing 7 changed files with 197 additions and 97 deletions.
31 changes: 31 additions & 0 deletions auctions/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,37 @@
)


class AuctionFilter(django_filters.FilterSet):
"""Filter for the main auctions list"""

query = django_filters.CharFilter(
method="auction_search",
label="",
widget=TextInput(
attrs={
"placeholder": "Filter by auction name, or type a number to see nearby auctions",
"hx-get": "",
"hx-target": "div.table-container",
"hx-trigger": "keyup changed delay:300ms",
"hx-swap": "outerHTML",
"hx-indicator": ".progress",
}
),
)

class Meta:
model = Auction
fields = [] # nothing here so no buttons show up

def auction_search(self, queryset, name, value):
if value == "joined":
return queryset.exclude(joined=False).exclude(joined=0)
if value.isnumeric():
return queryset.filter(distance__lte=int(value))
else:
return queryset.filter(title__icontains=value)


class AuctionTOSFilter(django_filters.FilterSet):
"""This filter is used on any admin views that allow adding users to an auction and on lot creation/winner screens"""

Expand Down
4 changes: 2 additions & 2 deletions auctions/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1073,12 +1073,12 @@ def template_lot_link(self):
@property
def template_lot_link_first_column(self):
"""Shown on small screens only"""
return mark_safe(f'<span class="d-md-none"><br>{self.template_lot_link}</span>')
return mark_safe(f'<small><span class="d-md-none"><br>{self.template_lot_link}</span></small>')

@property
def template_lot_link_seperate_column(self):
"""Shown on big screens only"""
return mark_safe(f'<td class="d-none d-md-table-cell">{self.template_lot_link}</td>')
return mark_safe(f'<span class="d-none d-md-inline">{self.template_lot_link}</span>')

@property
def can_submit_lots(self):
Expand Down
53 changes: 52 additions & 1 deletion auctions/tables.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import django_tables2 as tables
from django.urls import reverse
from django.utils import formats
from django.utils.safestring import mark_safe

from .models import AuctionTOS, Lot
from .models import Auction, AuctionTOS, Lot


class AuctionTOSHTMxTable(tables.Table):
Expand Down Expand Up @@ -159,6 +160,56 @@ class Meta:
# }


class AuctionHTMxTable(tables.Table):
hide_string = "d-md-table-cell d-none"

auction = tables.Column(accessor="title", verbose_name="Auction")
date = tables.Column(accessor="date_start", verbose_name="Status")
lots = tables.Column(
accessor="template_lot_link_seperate_column",
verbose_name="Lots",
orderable=False,
attrs={"th": {"class": hide_string}, "cell": {"class": hide_string}},
)

def render_date(self, value, record):
localized_date = formats.date_format(record.template_date_timestamp, use_l10n=True)
return mark_safe(f"{record.template_status}{localized_date}{record.ended_badge}")

def render_auction(self, value, record):
auction = record
result = f"<a href='{auction.get_absolute_url()}'>{auction.title}</a><br class='d-md-none'>"
if auction.is_last_used:
result += " <span class='ms-1 badge bg-light text-black'>Your last auction</span>"
if auction.is_online:
result += " <span class='badge bg-info'>Online</span>"
if not auction.promote_this_auction:
result += " <span class='badge bg-dark'>Not promoted</span>"
if auction.distance:
result += f" <span class='badge bg-primary'>{int(auction.distance)} miles from you</span>"
if auction.joined:
result += " <span class='badge bg-success text-black'>Joined</span>"
result += auction.template_lot_link_first_column + auction.template_promo_info
return mark_safe(result)

class Meta:
model = Auction
template_name = "tables/bootstrap_htmx.html"
fields = (
"auction",
"date",
"lots",
)
row_attrs = {
# 'class': lambda record: str(record.table_class),
# 'style':'cursor:pointer;',
# 'hx-get': lambda record: "/api/lot/" + str(record.pk),
# 'hx-target':"#modals-here",
# 'hx-trigger':"click",
# '_':"on htmx:afterOnLoad wait 10ms then add .show to #modal then add .show to #modal-backdrop"
}


class LotHTMxTableForUsers(tables.Table):
hide_string = "d-md-table-cell d-none"
# seller = tables.Column(accessor='auctiontos_seller', verbose_name="Seller")
Expand Down
67 changes: 15 additions & 52 deletions auctions/templates/all_auctions.html
Original file line number Diff line number Diff line change
@@ -1,64 +1,27 @@
{% extends "base.html" %}
{% load render_table from django_tables2 %}
{% load crispy_forms_tags %}

{% block title %}Auctions
{% endblock %}
{% load static %}
{% block content %}
<h3>Auctions</h3>
<div class='mb-3'>This is a list of club auctions which have been created on this site.</div>
<a href="/auctions/new/" class='btn bg-info mb-3 text-white'><i class="bi bi-plus-circle"></i> Create a new auction</a><br>

<form class="col-sm-12 mt-2">
{% crispy filter.form %}
</form>
<div class="progress d-none"> <div class="indeterminate"></div> </div>
{% render_table table %}

<h3>Auctions</h3>
<div class='mb-3'>This is a listing of club auctions which have been created on this site.</div>
<a href="/auctions/new/" class='btn bg-info mb-3 text-white'><i class="bi bi-plus-circle"></i> Create a new auction</a><br>
<table class="table table-responsive">
<thead>
<tr>
<th scope="col">Auction</th>
<th scope="col">Date</th>
<th scope="col" class="d-none d-md-block">Lots</th>
</tr>
</thead>
<tbody>
{% if last_auction_used %}
<tr>
<td><a href='/auctions/{{last_auction_used.slug}}'>{{ last_auction_used.title }}</a><br class="d-md-none"><span class='ms-1 badge bg-light text-black'>Your last auction</span>
{% if not last_auction_used.promote_this_auction %}<span class='badge bg-dark'>Not promoted</span>{% endif %}
{{ last_auction_used.template_lot_link_first_column }}
{{ last_auction_used.template_promo_info }}
</td>
<td>
{{ last_auction_used.template_status }}
{{ last_auction_used.template_date_timestamp }}
{{ last_auction_used.ended_badge }}
</td>
{{ last_auction_used.template_lot_link_seperate_column }}
</tr>
{% endif %}
{% for auction in object_list %}
<tr>
<td><a href='/auctions/{{auction.slug}}'>{{ auction.title }}</a><br class="d-md-none"> {% if auction.is_online %}<span class="badge bg-info">Online</span>{% endif %}
{% if not auction.promote_this_auction %}<span class='badge bg-dark'>Not promoted</span>{% endif %}
{% if not location_message and auction.number_of_locations %}<span class='badge bg-primary'>{{ auction.distance | floatformat:0 }} miles from you</span>{% endif %}
{{ auction.template_lot_link_first_column }}
{{ auction.template_promo_info }}
</td>
<td>
{{ auction.template_status }}
{{ auction.template_date_timestamp }}
{{ auction.ended_badge }}
</td>
{{ auction.template_lot_link_seperate_column }}
</tr>
{% endfor %}
</tbody>
</table>
<small class="text-muted">Note: Auctions you haven't joined won't appear in this list if they:
<small class="text-muted">Auctions you've joined will always show up here. Other auctions will only be listed here if they:
<ul>
<li>aren't related to the fish hobby</li>
<li>are set to "do not promote"</li>
<li>are starting more than 90 days from today</li>
<li>were created more than 2 years ago</li>
<li>are related to the fish hobby</li>
<li>have "promote this auction" checked</li>
<li>are starting less than 90 days from today</li>
<li>were created less than 2 years ago</li>
</ul>
Auctions you've joined will always show up here.</small></span>
</small>
{% endblock %}
{% block extra_js %}<script type='text/javascript'>pageView();</script>{% endblock %}
7 changes: 6 additions & 1 deletion auctions/templates/tables/table_generic.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
{% load render_table from django_tables2 %}

{% if no_results %}
<div class="table-container mt-2 mb-2">
{{ no_results | safe }}
</div>
{% else %}
{% render_table table %}
{% endif %}
4 changes: 2 additions & 2 deletions auctions/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,8 @@
),
path("images/<int:pk>/delete/", views.ImageDelete.as_view(), name="delete_image"),
path("images/<int:pk>/edit", views.ImageUpdateView.as_view(), name="edit_image"),
path("auctions/", views.allAuctions.as_view(), name="auctions"),
path("auctions/all/", views.allAuctions.as_view()),
path("auctions/", views.AllAuctions.as_view(), name="auctions"),
path("auctions/all/", views.AllAuctions.as_view()),
# path('auctions/new/', views.createAuction, name='createAuction'),
path("auctions/new/", login_required(views.AuctionCreateView.as_view())),
path("auctions/<slug:slug>/edit/", views.AuctionUpdate.as_view(), name="edit_auction"),
Expand Down
Loading

0 comments on commit 6397c94

Please sign in to comment.