Skip to content

Commit

Permalink
Add a 404 page and improve manufacturer lookup (#36)
Browse files Browse the repository at this point in the history
* Add a custom 404 page
* Better manufacturer and item lookup
* Add migration to populate new field
* Version bump
  • Loading branch information
daniviga authored Apr 23, 2024
1 parent 54a68d9 commit 6a9f37c
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 15 deletions.
2 changes: 1 addition & 1 deletion ram/portal/templates/cards/roster.html
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
</tr>
<tr>
<th scope="row">Item number</th>
<td>{{ d.item.item_number }}{%if d.item.set %} | <a class="badge text-bg-primary" href="{% url 'manufacturer' manufacturer=d.item.manufacturer search=d.item.item_number %}">SET</a>{% endif %}</td>
<td>{{ d.item.item_number }}{%if d.item.set %} | <a class="badge text-bg-primary" href="{% url 'manufacturer' manufacturer=d.item.manufacturer.slug search=d.item.item_number_slug %}">SET</a>{% endif %}</td>
</tr>
</tbody>
</table>
Expand Down
4 changes: 2 additions & 2 deletions ram/portal/templates/rollingstock.html
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@
</tr>
<tr>
<th scope="row">Item number</th>
<td>{{ rolling_stock.item_number }}{%if rolling_stock.set %} | <a class="badge text-bg-primary" href="{% url 'manufacturer' manufacturer=rolling_stock.manufacturer search=rolling_stock.item_number %}">SET</a>{% endif %}</td>
<td>{{ rolling_stock.item_number }}{%if rolling_stock.set %} | <a class="badge text-bg-primary" href="{% url 'manufacturer' manufacturer=rolling_stock.manufacturer.slug search=rolling_stock.item_number_slug %}">SET</a>{% endif %}</td>
</tr>
</tbody>
</table>
Expand Down Expand Up @@ -173,7 +173,7 @@
</tr>
<tr>
<th scope="row">Item number</th>
<td>{{ rolling_stock.item_number }}{%if rolling_stock.set %} | <a class="badge text-bg-primary" href="{% url 'manufacturer' manufacturer=rolling_stock.manufacturer search=rolling_stock.item_number %}">SET</a>{% endif %}</td>
<td>{{ rolling_stock.item_number }}{%if rolling_stock.set %} | <a class="badge text-bg-primary" href="{% url 'manufacturer' manufacturer=rolling_stock.manufacturer.slug search=rolling_stock.item_number_slug %}">SET</a>{% endif %}</td>
</tr>
<tr>
<th scope="row">Era</th>
Expand Down
37 changes: 26 additions & 11 deletions ram/portal/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,15 @@ def get_order_by_field():
return (fields[2], fields[0], fields[1], fields[3])


class Render404(View):
def get(self, request, exception):
return render(
request,
"base.html",
{"title": "404 page not found"}
)


class GetData(View):
title = "Home"
template = "roster.html"
Expand Down Expand Up @@ -229,27 +238,33 @@ def post(self, request, page=1):

class GetManufacturerItem(View):
def get(self, request, manufacturer, search="all", page=1):
manufacturer = get_object_or_404(
Manufacturer,
slug__iexact=manufacturer
)

if search != "all":
rolling_stock = get_list_or_404(
RollingStock.objects.order_by(*get_order_by_field()),
Q(
Q(manufacturer__name__iexact=manufacturer)
& Q(item_number__exact=search)
Q(manufacturer=manufacturer)
& Q(item_number_slug__exact=search)
)
)
title = "{0}: {1}".format(
rolling_stock[0].manufacturer,
search
manufacturer,
# all returned records must have the same `item_number``;
# just pick it up the first result, otherwise `search`
rolling_stock[0].item_number if rolling_stock else search
)
else:
rolling_stock = get_list_or_404(
RollingStock.objects.order_by(*get_order_by_field()),
Q(rolling_class__manufacturer__slug__iexact=manufacturer)
| Q(manufacturer__slug__iexact=manufacturer)
)
title = "Manufacturer: {0}".format(
get_object_or_404(Manufacturer, slug__iexact=manufacturer)
rolling_stock = (
RollingStock.objects.order_by(*get_order_by_field()).filter(
Q(manufacturer=manufacturer)
| Q(rolling_class__manufacturer=manufacturer)
)
)
title = "Manufacturer: {0}".format(manufacturer)

data = []
for item in rolling_stock:
Expand Down
2 changes: 1 addition & 1 deletion ram/ram/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from ram.utils import git_suffix

__version__ = "0.12.2"
__version__ = "0.12.3"
__version__ += git_suffix(__file__)
3 changes: 3 additions & 0 deletions ram/ram/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
from django.urls import include, path

from ram.views import UploadImage
from portal.views import Render404

handler404 = Render404.as_view()

urlpatterns = [
path("", lambda r: redirect("portal/")),
Expand Down
31 changes: 31 additions & 0 deletions ram/roster/migrations/0026_rollingstock_item_number_slug.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Generated by Django 5.0.4 on 2024-04-23 21:10

from django.db import migrations, models
from ram.utils import slugify


def gen_item_number_slug(apps, schema_editor):
RollingStock = apps.get_model('roster', 'RollingStock')
for row in RollingStock.objects.all():
if row.item_number:
row.item_number_slug = slugify(row.item_number)
row.save(update_fields=['item_number_slug'])


class Migration(migrations.Migration):

dependencies = [
("roster", "0025_rollingstock_set_alter_rollingstock_era_and_more"),
]

operations = [
migrations.AddField(
model_name="rollingstock",
name="item_number_slug",
field=models.CharField(blank=True, editable=False, max_length=32),
),
migrations.RunPython(
gen_item_number_slug,
reverse_code=migrations.RunPython.noop
),
]
5 changes: 5 additions & 0 deletions ram/roster/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ class RollingStock(models.Model):
blank=True,
help_text="Catalog item number or code",
)
item_number_slug = models.CharField(
max_length=32,
blank=True,
editable=False
)
set = models.BooleanField(
default=False,
help_text="Part of a set",
Expand Down

0 comments on commit 6a9f37c

Please sign in to comment.