Skip to content

Commit

Permalink
Advanced filters 🐇
Browse files Browse the repository at this point in the history
  • Loading branch information
Sibyx committed Feb 9, 2024
1 parent 09e7f60 commit 8ae5b15
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- **Added**: [Save and prefill image on new test #5](https://github.com/FIIT-Databases/tester/issues/5)
- **Added**: Cool FIIT favicon (super important)
- **Added**: Advanced admin filters
- **Changed**: Django 5.0 upgrade
- **Changed**: Use Debian-based containers

Expand Down
45 changes: 39 additions & 6 deletions apps/core/admin.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,50 @@
from django.contrib import admin
from django.utils.html import format_html
from django.utils.translation import gettext as _

from apps.core.models import Assignment, Scenario, Task, TaskRecord


def get_related_field(name, admin_order_field=None, short_description=None):
related_names = name.split("__")

def dynamic_attribute(obj):
for related_name in related_names:
obj = getattr(obj, related_name)
return obj

dynamic_attribute.admin_order_field = admin_order_field or name
dynamic_attribute.short_description = short_description or related_names[-1].title().replace("_", " ")
return dynamic_attribute


class RelatedFieldAdmin(admin.ModelAdmin):
def __getattr__(self, attr):
if "__" in attr:
return get_related_field(attr)

# not dynamic lookup, default behaviour
return self.__getattribute__(attr)


class TaskAdmin(admin.ModelAdmin):
list_filter = ("assigment__name", "user__username", "user__email")
autocomplete_fields = ("user", )
search_fields = ("user__username", )
list_display = ("id", "created_at", "assigment", "user", "status", "url")
list_filter = ("assigment__name", "status")
autocomplete_fields = ("user",)
search_fields = ("user__username",)
show_facets = admin.ShowFacets.ALWAYS

@admin.display(description=_("Viewer"))
def url(self, obj):
return format_html('<a href="{0}" target="_blank">Show</a>', obj.get_absolute_url())


class TaskRecordAdmin(admin.ModelAdmin):
list_filter = ("task__assigment__name", "task__user__username", "task__user__email", )
autocomplete_fields = ("task", )
class TaskRecordAdmin(RelatedFieldAdmin):
list_display = ("id", "created_at", "task", "task__user")
readonly_fields = ("created_at",)
list_filter = ("task__assigment__name",)
autocomplete_fields = ("task",)
search_fields = ("task__user__username",)
show_facets = admin.ShowFacets.ALWAYS


Expand All @@ -22,6 +54,7 @@ class AssignmentAdmin(admin.ModelAdmin):

class ScenarioAdmin(admin.ModelAdmin):
show_facets = admin.ShowFacets.ALWAYS
list_filter = ("assigment__name",)


admin.site.register(Assignment, AssignmentAdmin)
Expand Down
2 changes: 1 addition & 1 deletion apps/core/management/commands/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def handle(self, *args, **options):
self._cron()

def _cron(self):
cron = CronTab(user='root')
cron = CronTab(user="root")
cron.remove_all()

for command, schedule in settings.CRON_JOBS.items():
Expand Down
2 changes: 1 addition & 1 deletion apps/core/models/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def get_absolute_url(self):
return reverse("task-detail", kwargs={"task_id": self.pk})

def __str__(self):
return f"{self.assigment.name} - {self.user.username} ({self.id})"
return f"{self.assigment.name} - ({self.id})"


__all__ = ["Task"]
4 changes: 1 addition & 3 deletions apps/web/views/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ def __init__(self, **kwargs):
self.object = None

def get_initial(self):
return {
'image': self.request.session.pop('task_form', {}).get('image', '')
}
return {"image": self.request.session.pop("task_form", {}).get("image", "")}

def form_valid(self, form):
self.object = form.save(commit=False)
Expand Down

0 comments on commit 8ae5b15

Please sign in to comment.