class ArtistAjaxDatatableView(AjaxDatatableView): model = Artist code = 'artist' title = _('Artist') initial_order = [ ["name", "asc"], ] length_menu = [[10, 20, 50, 100, -1], [10, 20, 50, 100, 'all']] search_values_separator = '+' column_defs = [ AjaxDatatableView.render_row_tools_column_def(), { 'name': 'pk', 'visible': False, }, { 'name': 'name', 'visible': True, }, { 'name': 'edit', 'title': 'Edit', 'searchable': False, 'orderable': False, }, ] def customize_row(self, row, obj): row['edit'] = """
class TrackAjaxDatatableView(AjaxDatatableView): model = Track code = 'track' title = _('Tracks') initial_order = [["name", "asc"], ] length_menu = [[10, 20, 50, 100, -1], [10, 20, 50, 100, 'all']] search_values_separator = '+' column_defs = [ AjaxDatatableView.render_row_tools_column_def(), {'name': 'pk', 'visible': False, }, {'name': 'name', 'visible': True, }, {'name': 'album', 'foreign_field': 'album__name', 'visible': True, }, {'name': 'artist', 'title':'Artist', 'foreign_field': 'album__artist__name', 'visible': True, 'choices': True, 'autofilter': True, }, {'name': 'tags', 'visible': True, }, ] @query_debugger def dispatch(self, request, *args, **kwargs): return super().dispatch(request, *args, **kwargs) def get_initial_queryset(self, request=None): # Optimization: Reduce the number of queries due to ManyToMany "tags" relation return Track.objects.prefetch_related('tags') def customize_row(self, row, obj): # 'row' is a dictionary representing the current row, and 'obj' is the current object. # Display tags as a list of strings row['tags'] = ','.join( [t.name for t in obj.tags.all()]) return
class AlbumAjaxDatatableView(AjaxDatatableView): model = Album code = 'album' title = _('Album') initial_order = [ ["name", "asc"], ] length_menu = [[10, 20, 50, 100, -1], [10, 20, 50, 100, 'all']] search_values_separator = '+' column_defs = [ AjaxDatatableView.render_row_tools_column_def(), { 'name': 'pk', 'visible': False, }, { 'name': 'name', 'visible': True, }, { 'name': 'artist', 'title': 'Artist', 'foreign_field': 'artist__name', 'visible': True, 'choices': True, 'autofilter': True, }, ]
class PermissionAjaxDatatableView(AjaxDatatableView): model = Permission title = 'Permissions' initial_order = [ ["app_label", "asc"], ] length_menu = [[10, 20, 50, 100, -1], [10, 20, 50, 100, 'all']] search_values_separator = '+' column_defs = [ AjaxDatatableView.render_row_tools_column_def(), { 'name': 'id', 'visible': False, }, { 'name': 'codename', 'visible': True, }, { 'name': 'name', 'visible': True, }, { 'name': 'app_label', 'foreign_field': 'content_type__app_label', 'visible': True, }, { 'name': 'model', 'foreign_field': 'content_type__model', 'visible': True, }, ]
class TrackAjaxDatatableView(AjaxDatatableView): model = Track code = 'track' title = _('Tracks') initial_order = [ ["name", "asc"], ] length_menu = [[10, 20, 50, 100, -1], [10, 20, 50, 100, 'all']] search_values_separator = '+' show_date_filters = False column_defs = [ AjaxDatatableView.render_row_tools_column_def(), { 'name': 'pk', 'visible': False, }, { 'name': 'name', 'visible': True, }, { 'name': 'album', 'foreign_field': 'album__name', 'visible': True, 'lookup_field': '__istartswith', }, { 'name': 'artist', 'title': 'Artist', 'foreign_field': 'album__artist__name', 'visible': True, 'choices': True, 'autofilter': True, }, # {'name': 'tags', 'visible': True, 'searchable': False, }, { 'name': 'tags', 'm2m_foreign_field': 'tags__name', 'searchable': True, 'choices': True, 'autofilter': True, }, { 'name': 'tags2', 'm2m_foreign_field': 'tags2__name', 'searchable': True, 'choices': True, 'autofilter': False, }, ] @query_debugger def dispatch(self, request, *args, **kwargs): return super().dispatch(request, *args, **kwargs)
class ProductAjaxDatatableView(AjaxDatatableView): model = Product title = 'Products' initial_order = [ ["name", "asc"], ] length_menu = [[10, 20, 50, 100, -1], [10, 20, 50, 100, 'all']] search_values_separator = '+' column_defs = [ AjaxDatatableView.render_row_tools_column_def(), { 'title': 'Product Name', 'name': 'name', 'visible': True, }, { 'title': 'Description', 'name': 'description', 'visible': True, }, { 'title': 'Category', 'name': 'category', 'foreign_field': 'category__name', 'visible': True, }, { 'title': 'Brand', 'name': 'brand', 'foreign_field': 'brand__name', 'visible': True, }, { 'title': 'Color', 'name': 'color', 'foreign_field': 'color__name', 'visible': True, }, { 'title': 'size', 'name': 'price', 'visible': True, }, { 'title': 'Price', 'name': 'size', 'visible': True, }, { 'title': 'Type', 'name': 'type', 'visible': True, }, ]
class PermissionAjaxDatatableView(AjaxDatatableView): model = Semrush title = 'SemrushData' column_defs = [ AjaxDatatableView.render_row_tools_column_def(), { 'name': 'id', }, { 'name': 'country', }, { 'name': 'keyword', }, { 'name': 'seed_keyword', }, { 'name': 'volume', }, { 'name': 'keyword_difficulty', }, { 'name': 'created_at', 'title': 'created_at', 'className': 'date_input', 'searchable': True, 'orderable': True, }, { 'name': 'action', 'searchable': False, 'orderable': False } ] def customize_row(self, row, obj): row['action'] = '<a class="btn btn-info client-status" href="%s">%s</a>' % ( # obj.id, reverse('semrush_serp', args=(obj.keyword, obj.pk)), 'Get Link'), return
class LayerDataView(AjaxDatatableView): model = LayerData title = 'Ajax Annotations' initial_order = [["animal", "asc"], ["layer", "asc"], ["structure", "asc"], ["user", "asc"], ["input_type", "asc"]] length_menu = [[10, 20, 50, 100, -1], [10, 20, 50, 100, 'all']] search_values_separator = ' ' column_defs = [ AjaxDatatableView.render_row_tools_column_def(), { 'name': 'id', 'visible': False, }, { 'name': 'animal', 'foreign_field': 'prep__prep_id', 'visible': True, 'title': 'Animal' }, { 'name': 'layer', 'visible': True, }, { 'name': 'structure', 'foreign_field': 'structure__abbreviation', 'visible': True, }, { 'name': 'x_f', 'visible': True, 'placeholder': True, 'title': 'X', 'sort_field': 'x', 'searchable': False }, { 'name': 'y_f', 'visible': True, 'placeholder': True, 'title': 'Y', 'sort_field': 'y', 'searchable': False }, { 'name': 'z_f', 'visible': True, 'placeholder': True, 'title': 'Section', 'sort_field': 'section', 'searchable': False }, { 'name': 'user', 'foreign_field': 'person__username', 'visible': True, }, { 'name': 'input_type', 'foreign_field': 'input_type__input_type', 'visible': True, }, { 'name': 'updated', 'visible': True, 'searchable': False }, ] def get_initial_queryset(self, request=None): # Optimization: Reduce the number of queries due to ManyToMany "tags" relation return LayerData.objects.filter(active=True) scales = {'dk': 0.325, 'md': 0.452, 'at': 10} def customize_row(self, row, obj): initial = str(obj.prep_id[0:2]).lower() xnumber = int(round(obj.x / self.scales[initial])) row['x_f'] = format_html( f"<div style='text-align:left;'>{xnumber:,}</div>") ynumber = int(round(obj.y / self.scales[initial])) row['y_f'] = format_html( f"<div style='text-align:left;'>{ynumber:,}</div>") znumber = int(round(obj.section / 20)) row['z_f'] = format_html( f"<div style='text-align:left;'>{znumber:,}</div>") return """
class AlbumAjaxDatatableView(AjaxDatatableView): model = Album code = 'album' title = _('Album') initial_order = [ ["name", "asc"], ] length_menu = [[10, 20, 50, 100, -1], [10, 20, 50, 100, 'all']] search_values_separator = '+' column_defs = [ AjaxDatatableView.render_row_tools_column_def(), { 'name': 'pk', 'visible': False, }, { 'name': 'name', 'visible': True, }, { 'name': 'release_date', 'visible': True, }, { 'name': 'year', 'visible': True, }, { 'name': 'artist', 'title': 'Artist', 'foreign_field': 'artist__name', 'visible': True, 'choices': True, 'autofilter': True, }, ] def get_initial_queryset(self, request=None): def get_numeric_param(key): try: value = int(request.POST.get(key)) except (ValueError, AttributeError, TypeError): value = None return value queryset = super().get_initial_queryset(request=request) check_year_null = get_numeric_param('check_year_null') if check_year_null is not None: if check_year_null == 0: queryset = queryset.filter(year=None) elif check_year_null == 1: queryset = queryset.exclude(year=None) from_year = get_numeric_param('from_year') if from_year is not None: queryset = queryset.filter(year__gte=from_year) to_year = get_numeric_param('to_year') if to_year is not None: queryset = queryset.filter(year__lte=to_year) return queryset