Skip to content

Commit

Permalink
render albums column in file table properly
Browse files Browse the repository at this point in the history
  • Loading branch information
tykling committed May 26, 2024
1 parent aacb588 commit 7320a1a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
30 changes: 29 additions & 1 deletion src/files/tables.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
"""This module defines the table used to show files."""
import django_tables2 as tables
from albums.models import Album
from django.urls import reverse
from django.utils import timezone
from django.utils.safestring import mark_safe

from .models import BaseFile

Expand All @@ -9,10 +13,34 @@ class FileTable(tables.Table):

selection = tables.CheckBoxColumn(accessor="pk", orderable=False)
uuid = tables.Column(linkify=True)
albums = tables.Column(verbose_name="Albums")

def render_albums(self, record: BaseFile) -> str:
"""Render albums as a list of links."""
output = ""
for album in Album.objects.filter(
memberships__basefile__pk__contains=record.pk, memberships__period__contains=timezone.now()
):
output += (
'<a href="' + reverse("albums:album_detail", kwargs={"pk": album.pk}) + '">' + album.title + "</a><br>"
)
return mark_safe(output) # noqa: S308

class Meta:
"""Define model, template, fields."""

model = BaseFile
template_name = "django_tables2/bootstrap.html"
fields = ("uuid", "albums", "attribution", "uploader", "license", "file_size", "status", "title")
fields = (
"selection",
"uuid",
"title",
"albums",
"attribution",
"uploader",
"license",
"file_size",
"approved",
"published",
"deleted",
)
2 changes: 1 addition & 1 deletion src/files/templates/file_list.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<div class="row">
<div class="col-2">
<div class="card">
<h5 class="card-header">Filter</h5>
<h5 class="card-header">Filter Files</h5>
<div class="card-body">
<form method="get">
{% bootstrap_form filter.form %}
Expand Down

0 comments on commit 7320a1a

Please sign in to comment.