This commit is contained in:
Marwan Alwali 2025-02-19 07:46:29 +03:00
parent d937f9f684
commit 7d93bfde9f
11 changed files with 116 additions and 19 deletions

View File

@ -1,26 +1,15 @@
from django.utils.html import format_html
from django.conf import settings
from django.utils.timesince import timesince
from . import models
from django_tables2.utils import A
from django import forms
import django_tables2 as tables
from django.utils.translation import gettext_lazy as _
from .models import Car, CarFinance, ExteriorColors, InteriorColors, CarColors
from .utils import get_local_name
class ImageColumn(tables.Column):
def render(self, value):
return format_html('<img src="{}{}" width="100" height="50" />', settings.MEDIA_URL, value)
from django.utils.html import format_html
class CarTable(tables.Table):
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"}, "span": {"class": "fas fa-bars"}})
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"))
@ -36,7 +25,7 @@ class CarTable(tables.Table):
class Meta:
model = Car
template_name = settings.DJANGO_TABLES2_TEMPLATE
export_formats = ["xlsx", ]
export_formats = ["xlsx"]
fields = (
"stock_type",
"vin",
@ -52,7 +41,7 @@ class CarTable(tables.Table):
"receiving_date",
"status",
)
attrs = {"class": "table table-hover fs-9 mb-0"}
attrs = {"class": "table table-hover fs-9 mb-0 p-1"}
def render_id_car_make(self, value):
return get_local_name(value)
@ -73,4 +62,22 @@ class CarTable(tables.Table):
return str(value)
def render_receiving_date(self, value):
return timesince(value) if value else "-"
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)

View File

@ -1,10 +1,11 @@
{% extends 'base.html' %}
{% load static i18n django_tables2 %}
{% load export_url from django_tables2 %}
{% block content %}
<div class="container-fluid p-4 my-3">
<div class="container-fluid mx-n1 p-3">
<div class="row">
<div class="col-md-12 p-2 d-flex justify-content-end gap-3">
<a href="{% export_url 'xlsx' %}" class="btn btn-sm btn-phoenix-success">
@ -13,10 +14,12 @@
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="table-responsive scrollbar mx-n1 px-1 align-center">
<div class="col-md-12 p-1">
<div class="table-responsive scrollbar mx-n1 p-1 align-center">
{% render_table table %}
</div>
</div>
</div>
</div>

View File

@ -0,0 +1,87 @@
{% load django_tables2 %}
{% load i18n l10n %}
{% block table-wrapper %}
<div class="table-container">
{% block table %}
<table {% render_attrs table.attrs class="table" %}>
{% block table.thead %}
{% if table.show_header %}
<thead {{ table.attrs.thead.as_html }}>
<tr>
{% for column in table.columns %}
<th {{ column.attrs.th.as_html }} scope="col">
{% if column.orderable %}
<a href="{% querystring table.prefixed_order_by_field=column.order_by_alias.next %}">{{ column.header }}</a>
{% else %}
{{ column.header }}
{% endif %}
</th>
{% endfor %}
</tr>
</thead>
{% endif %}
{% endblock table.thead %}
{% block table.tbody %}
<tbody {{ table.attrs.tbody.as_html }}>
{% for row in table.paginated_rows %}
{% block table.tbody.row %}
<tr {{ row.attrs.as_html }}>
{% for column, cell in row.items %}
<td {{ column.attrs.td.as_html }}>{% if column.localize == None %}{{ cell }}{% else %}{% if column.localize %}{{ cell|localize }}{% else %}{{ cell|unlocalize }}{% endif %}{% endif %}</td>
{% endfor %}
</tr>
{% endblock table.tbody.row %}
{% empty %}
{% if table.empty_text %}
{% block table.tbody.empty_text %}
<tr><td colspan="{{ table.columns|length }}">{{ table.empty_text }}</td></tr>
{% endblock table.tbody.empty_text %}
{% endif %}
{% endfor %}
</tbody>
{% endblock table.tbody %}
{% block table.tfoot %}
{% if table.has_footer %}
<tfoot {{ table.attrs.tfoot.as_html }}>
<tr>
{% for column in table.columns %}
<td {{ column.attrs.tf.as_html }}>{{ column.footer }}</td>
{% endfor %}
</tr>
</tfoot>
{% endif %}
{% endblock table.tfoot %}
</table>
{% endblock table %}
<div class="pagination-container">
{% block pagination %}
{% if table.page %}
<ul class="pagination justify-content-center">
{% if table.page.has_previous %}
<li class="page-item">
<a class="page-link" href="?page={{ table.page.previous_page_number }}">
<span class="fas fa-chevron-{% if LANGUAGE_CODE == 'ar' %}right{% else %}left{% endif %}"></span>
</a>
</li>
{% endif %}
<li class="page-item active">
<span class="page-link">{{ _("Page") }} {{ table.page.number }} {{ _("of") }} {{ table.paginator.num_pages }}</span>
</li>
{% if table.page.has_next %}
<li class="page-item">
<a class="page-link" href="?page={{ table.page.next_page_number }}">
<span class="fas fa-chevron-{% if LANGUAGE_CODE == 'ar' %}left{% else %}right{% endif %}"></span>
</a>
</li>
{% endif %}
</ul>
{% endif %}
{% endblock pagination %}
</div>
</div>
{% endblock table-wrapper %}