139 lines
5.3 KiB
Python
139 lines
5.3 KiB
Python
from django.conf import settings
|
|
from django.utils.timesince import timesince
|
|
import django_tables2 as tables
|
|
from django.utils.translation import gettext_lazy as _
|
|
from .models import Car
|
|
from .utils import get_local_name
|
|
from django.utils.html import format_html
|
|
|
|
|
|
class CarTable(tables.Table):
|
|
"""
|
|
Represents a table for displaying information about cars.
|
|
|
|
The `CarTable` class is a Django Tables2 table configuration designed for displaying
|
|
details of cars, including their stock type, VIN, make, model, year, series, trim,
|
|
mileage, price, colors, receiving date, and status. This table renders each column with
|
|
customizable settings, including cell attributes and formatting. The class utilizes
|
|
custom render methods to format specific columns, such as dates, localized names, and
|
|
status badges.
|
|
|
|
:ivar stock_type: Specifies whether the car is new or used.
|
|
:type stock_type: tables.Column
|
|
:ivar vin: Contains the VIN of the car, rendered as a hyperlink to the car detail page.
|
|
:type vin: tables.LinkColumn
|
|
:ivar id_car_make: Defines the make (brand) of the car.
|
|
:type id_car_make: tables.Column
|
|
:ivar id_car_model: Defines the model of the car.
|
|
:type id_car_model: tables.Column
|
|
:ivar year: Specifies the year the car was manufactured.
|
|
:type year: tables.Column
|
|
:ivar id_car_serie: Refers to the series of the car.
|
|
:type id_car_serie: tables.Column
|
|
:ivar id_car_trim: Refers to the trim of the car.
|
|
:type id_car_trim: tables.Column
|
|
:ivar mileage: Indicates the mileage of the car.
|
|
:type mileage: tables.Column
|
|
:ivar selling_price: Displays the selling price, derived from a related field on the finances object.
|
|
:type selling_price: tables.Column
|
|
:ivar exterior_color: Displays the exterior color of the car, derived from a related field on the colors object.
|
|
:type exterior_color: tables.Column
|
|
:ivar interior_color: Displays the interior color of the car, derived from a related field on the colors object.
|
|
:type interior_color: tables.Column
|
|
:ivar receiving_date: Represents the date the car was received, formatted as the
|
|
time elapsed since receiving or as "-" if the date is missing.
|
|
:type receiving_date: tables.Column
|
|
:ivar status: Shows the status of the car (e.g., available, reserved, sold, transfer)
|
|
with badge-style formatting based on the status value.
|
|
:type status: tables.Column
|
|
"""
|
|
|
|
stock_type = tables.Column(verbose_name=_("Stock Type"))
|
|
vin = tables.LinkColumn(
|
|
"car_detail",
|
|
args=[tables.A("pk")],
|
|
verbose_name=_("VIN"),
|
|
attrs={"td": {"class": "fw-bold"}},
|
|
)
|
|
id_car_make = tables.Column(verbose_name=_("Make"))
|
|
id_car_model = tables.Column(verbose_name=_("Model"))
|
|
year = tables.Column(verbose_name=_("Year"))
|
|
id_car_serie = tables.Column(verbose_name=_("Series"))
|
|
id_car_trim = tables.Column(verbose_name=_("Trim"))
|
|
mileage = tables.Column(verbose_name=_("Mileage"))
|
|
selling_price = tables.Column(
|
|
accessor="finances.selling_price", verbose_name=_("Price")
|
|
)
|
|
exterior_color = tables.Column(
|
|
accessor="colors.exterior.name", verbose_name=_("Exterior Color")
|
|
)
|
|
interior_color = tables.Column(
|
|
accessor="colors.interior.name", verbose_name=_("Interior Color")
|
|
)
|
|
receiving_date = tables.Column(verbose_name=_("Age"))
|
|
status = tables.Column(verbose_name=_("Status"))
|
|
|
|
class Meta:
|
|
model = Car
|
|
template_name = settings.DJANGO_TABLES2_TEMPLATE
|
|
export_formats = ["xlsx"]
|
|
fields = (
|
|
"stock_type",
|
|
"vin",
|
|
"id_car_make",
|
|
"id_car_model",
|
|
"year",
|
|
"id_car_serie",
|
|
"id_car_trim",
|
|
"mileage",
|
|
"selling_price",
|
|
"exterior_color",
|
|
"interior_color",
|
|
"receiving_date",
|
|
"status",
|
|
)
|
|
attrs = {"class": "table table-hover fs-9 mb-0 p-1"}
|
|
|
|
def render_id_car_make(self, value):
|
|
return get_local_name(value)
|
|
|
|
def render_id_car_model(self, value):
|
|
return get_local_name(value)
|
|
|
|
def render_id_car_serie(self, value):
|
|
return get_local_name(value)
|
|
|
|
def render_id_car_trim(self, value):
|
|
return str(value)
|
|
|
|
def render_exterior_color(self, value):
|
|
return str(value)
|
|
|
|
def render_interior_color(self, value):
|
|
return str(value)
|
|
|
|
def render_receiving_date(self, value):
|
|
return timesince(value) if value else "-"
|
|
|
|
def render_status(self, value):
|
|
status_badges = {
|
|
"available": "badge-phoenix-success",
|
|
"reserved": "badge-phoenix-danger",
|
|
"sold": "badge-phoenix-info",
|
|
"transfer": "badge-phoenix-warning",
|
|
}
|
|
badge_class = status_badges.get(value.lower(), "badge-secondary")
|
|
return format_html(
|
|
'<span class="badge badge-phoenix fs-11 {}">{}</span>', badge_class, value
|
|
)
|
|
|
|
def render_stock_type(self, value):
|
|
type_badges = {
|
|
"new": "badge-phoenix-success",
|
|
"used": "badge-phoenix-warning",
|
|
}
|
|
badge_class = type_badges.get(value.lower(), "badge-secondary")
|
|
return format_html(
|
|
'<span class="badge badge-phoenix fs-11 {}">{}</span>', badge_class, value
|
|
)
|