TandoorRecipes/cookbook/tables.py

149 lines
4.6 KiB
Python

from typing import Any, Dict
import django_tables2 as tables
from django.utils.html import format_html
from django.utils.translation import gettext as _
from django.views.generic import TemplateView
from django_tables2 import MultiTableMixin
from django_tables2.utils import A
from .helper.permission_helper import GroupRequiredMixin
from .models import CookLog, InviteLink, RecipeImport, Storage, Sync, SyncLog, ViewLog, HomeAssistantConfig, ExampleConfig
class StorageTable(tables.Table):
id = tables.LinkColumn('edit_storage', args=[A('id')])
class Meta:
model = Storage
template_name = 'generic/table_template.html'
fields = ('id', 'name', 'method')
class ExampleConfigTable(tables.Table):
id = tables.LinkColumn('edit_example_config', args=[A('id')])
class Meta:
model = ExampleConfig
template_name = 'generic/table_template.html'
fields = ('id', 'name', 'enabled', 'feed_url')
attrs = {'table_name': "Example Configs", 'create_url': 'new_example_config'}
class HomeAssistantConfigTable(tables.Table):
id = tables.LinkColumn('edit_home_assistant_config', args=[A('id')])
class Meta:
model = HomeAssistantConfig
template_name = 'generic/table_template.html'
fields = ('id', 'name', 'enabled', 'url')
attrs = {'table_name': "HomeAssistant Configs", 'create_url': 'new_home_assistant_config'}
class ConnectorConfigTable(GroupRequiredMixin, MultiTableMixin, TemplateView):
groups_required = ['admin']
template_name = "list_connectors.html"
table_pagination = {
"per_page": 25
}
def get_context_data(self, **kwargs: Any) -> Dict[str, Any]:
kwargs = super().get_context_data(**kwargs)
kwargs['title'] = _("Connectors")
return kwargs
def get_tables(self):
example_configs = ExampleConfig.objects.filter(space=self.request.space).all()
home_assistant_configs = HomeAssistantConfig.objects.filter(space=self.request.space).all()
return [
ExampleConfigTable(example_configs),
HomeAssistantConfigTable(home_assistant_configs)
]
class ImportLogTable(tables.Table):
sync_id = tables.LinkColumn('edit_sync', args=[A('sync_id')])
@staticmethod
def render_status(value):
if value == 'SUCCESS':
return format_html(
'<span class="badge badge-success">%s</span>' % value
)
else:
return format_html(
'<span class="badge badge-danger">%s</span>' % value
)
class Meta:
model = SyncLog
template_name = 'generic/table_template.html'
fields = ('status', 'msg', 'sync_id', 'created_at')
class SyncTable(tables.Table):
id = tables.LinkColumn('edit_sync', args=[A('id')])
@staticmethod
def render_path(value):
return format_html('<code>%s</code>' % value)
@staticmethod
def render_storage(value):
return format_html(
'<span class="badge badge-success">%s</span>' % value
)
class Meta:
model = Sync
template_name = 'generic/table_template.html'
fields = ('id', 'path', 'storage', 'last_checked')
class RecipeImportTable(tables.Table):
id = tables.LinkColumn('new_recipe_import', args=[A('id')])
delete = tables.TemplateColumn(
"<a href='{% url 'delete_recipe_import' record.id %}' >" + _('Delete') + "</a>" # noqa: E501
)
class Meta:
model = RecipeImport
template_name = 'generic/table_template.html'
fields = ('id', 'name', 'file_path')
class InviteLinkTable(tables.Table):
link = tables.TemplateColumn(
"<input value='{{ request.scheme }}://{{ request.get_host }}{% url 'view_invite' record.uuid %}' class='form-control' />"
)
delete_link = tables.TemplateColumn(
"<a href='{% url 'delete_invite_link' record.pk %}' >" + _('Delete') + "</a>", verbose_name=_('Delete')
)
class Meta:
model = InviteLink
template_name = 'generic/table_template.html'
fields = (
'username', 'group', 'valid_until',
)
class ViewLogTable(tables.Table):
recipe = tables.LinkColumn('view_recipe', args=[A('recipe_id')])
class Meta:
model = ViewLog
template_name = 'generic/table_template.html'
fields = ('recipe', 'created_at')
class CookLogTable(tables.Table):
recipe = tables.LinkColumn('view_recipe', args=[A('recipe_id')])
class Meta:
model = CookLog
template_name = 'generic/table_template.html'
fields = ('recipe', 'rating', 'serving', 'created_at')