def test_column_media(self): class FancyColumn(Column): class Media: js = ["js/gradient-fill.js"] data = [ { "first_name": "Paul", "last_name": "Simon" }, { "first_name": "Art", "last_name": "Garfunkel" }, ] table = Table( [ FancyColumn("first_name"), Column("last_name"), ], data, ) self.assertIn('src="/static/js/gradient-fill.js"', str(table.media["js"]))
def test_table_render_with_width(self): data = [ {'first_name': 'Paul', 'last_name': 'Simon'}, {'first_name': 'Art', 'last_name': 'Garfunkel'}, ] table = Table([ Column('first_name'), Column('last_name', width='75%'), ], data) html = self.render_component(table) self.assertHTMLEqual(html, ''' <table class="listing"> <col /> <col width="75%" /> <thead> <tr><th>First name</th><th>Last name</th></tr> </thead> <tbody> <tr><td>Paul</td><td>Simon</td></tr> <tr><td>Art</td><td>Garfunkel</td></tr> </tbody> </table> ''')
def test_table_render(self): data = [ { "first_name": "Paul", "last_name": "Simon" }, { "first_name": "Art", "last_name": "Garfunkel" }, ] table = Table( [ Column("first_name"), Column("last_name"), ], data, ) html = self.render_component(table) self.assertHTMLEqual( html, """ <table class="listing"> <thead> <tr><th>First name</th><th>Last name</th></tr> </thead> <tbody> <tr><td>Paul</td><td>Simon</td></tr> <tr><td>Art</td><td>Garfunkel</td></tr> </tbody> </table> """, )
def test_title_column(self): root_page = Page.objects.filter(depth=2).first() blog = Site.objects.create( hostname="blog.example.com", site_name="My blog", root_page=root_page ) gallery = Site.objects.create( hostname="gallery.example.com", site_name="My gallery", root_page=root_page ) data = [blog, gallery] table = Table( [ TitleColumn( "hostname", url_name="wagtailsites:edit", link_classname="choose-site", ), Column("site_name", label="Site name"), ], data, ) html = self.render_component(table) self.assertHTMLEqual( html, """ <table class="listing"> <thead> <tr><th>Hostname</th><th>Site name</th></tr> </thead> <tbody> <tr> <td class="title"> <div class="title-wrapper"> <a href="/admin/sites/%d/" class="choose-site">blog.example.com</a> </div> </td> <td>My blog</td> </tr> <tr> <td class="title"> <div class="title-wrapper"> <a href="/admin/sites/%d/" class="choose-site">gallery.example.com</a> </div> </td> <td>My gallery</td> </tr> </tbody> </table> """ % (blog.pk, gallery.pk), )
def get_context_data(self, **kwargs): self.object_list = self.get_queryset() context = super().get_context_data(**kwargs) index_url = self.url_helper.get_action_url('history', quote(self.instance.pk)) table = Table( self.columns, context['object_list'], base_url=index_url, ordering=self.get_ordering() ) context['table'] = table context['media'] = table.media context['index_url'] = index_url context['is_paginated'] = True return context
def get(self, request): objects = self.get_object_list() self.is_searching = False self.search_query = None self.filter_form = self.get_filter_form() if self.filter_form.is_valid(): objects = self.filter_object_list(objects, self.filter_form) paginator = Paginator(objects, per_page=self.per_page) self.results = paginator.get_page(request.GET.get("p")) self.table = Table(self.columns, self.results) return self.render_to_response()
def test_column_media(self): class FancyColumn(Column): class Media: js = ['js/gradient-fill.js'] data = [ {'first_name': 'Paul', 'last_name': 'Simon'}, {'first_name': 'Art', 'last_name': 'Garfunkel'}, ] table = Table([ FancyColumn('first_name'), Column('last_name'), ], data) self.assertIn('src="/static/js/gradient-fill.js"', str(table.media['js']))
def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) index_url = self.get_index_url() table = Table(self.columns, context['object_list'], base_url=index_url, ordering=self.get_ordering()) context['can_add'] = (self.permission_policy is None or self.permission_policy.user_has_permission( self.request.user, 'add')) context['table'] = table context['media'] = table.media context['index_url'] = index_url context['is_paginated'] = bool(self.paginate_by) return context
def get_context_data(self, **kwargs): self.object_list = self.get_queryset() context = super().get_context_data(**kwargs) index_url = self.url_helper.get_action_url("history", quote(self.instance.pk)) table = Table( self.columns, context["object_list"], base_url=index_url, ordering=self.get_ordering(), ) context["table"] = table context["media"] = table.media context["index_url"] = index_url context["is_paginated"] = True return context
def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) index_url = self.get_index_url() table = Table( self.columns, context["object_list"], base_url=index_url, ordering=self.get_ordering(), ) context["can_add"] = (self.permission_policy is None or self.permission_policy.user_has_permission( self.request.user, "add")) context["table"] = table context["media"] = table.media context["index_url"] = index_url context["is_paginated"] = bool(self.paginate_by) return context
def test_title_column(self): root_page = Page.objects.filter(depth=2).first() blog = Site.objects.create(hostname='blog.example.com', site_name='My blog', root_page=root_page) gallery = Site.objects.create(hostname='gallery.example.com', site_name='My gallery', root_page=root_page) data = [blog, gallery] table = Table([ TitleColumn('hostname', url_name='wagtailsites:edit'), Column('site_name', label="Site name"), ], data) html = self.render_component(table) self.assertHTMLEqual(html, ''' <table class="listing"> <thead> <tr><th>Hostname</th><th>Site name</th></tr> </thead> <tbody> <tr> <td class="title"> <div class="title-wrapper"> <a href="/admin/sites/%d/">blog.example.com</a> </div> </td> <td>My blog</td> </tr> <tr> <td class="title"> <div class="title-wrapper"> <a href="/admin/sites/%d/">gallery.example.com</a> </div> </td> <td>My gallery</td> </tr> </tbody> </table> ''' % (blog.pk, gallery.pk))
def get(self, request): self.filter_form = self.get_filter_form() self.results = self.get_results_page(request) self.table = Table(self.columns, self.results) return self.render_to_response()
def get(self, request): Document = get_document_model() if permission_policy.user_has_permission(request.user, "add"): DocumentForm = get_document_form(Document) self.uploadform = DocumentForm( user=request.user, prefix="document-chooser-upload" ) else: self.uploadform = None documents = permission_policy.instances_user_has_any_permission_for( request.user, ["choose"] ) # allow hooks to modify the queryset for hook in hooks.get_hooks("construct_document_chooser_queryset"): documents = hook(documents, request) self.q = None self.is_searching = False self.collection_id = request.GET.get("collection_id") if self.collection_id: documents = documents.filter(collection=self.collection_id) self.documents_exist = documents.exists() if "q" in request.GET: self.searchform = SearchForm(request.GET) if self.searchform.is_valid(): self.q = self.searchform.cleaned_data["q"] documents = documents.search(self.q) self.is_searching = True else: self.searchform = SearchForm() if not self.is_searching: documents = documents.order_by("-created_at") paginator = Paginator(documents, per_page=10) self.documents = paginator.get_page(request.GET.get("p")) self.collections = permission_policy.collections_user_has_permission_for( request.user, "choose" ) if len(self.collections) < 2: self.collections = None columns = [ TitleColumn( "title", label=_("Title"), url_name="wagtaildocs:document_chosen", link_classname="document-choice", ), DownloadColumn("filename", label=_("File")), DateColumn("created_at", label=_("Created"), width="16%"), ] if self.collections: columns.insert(2, Column("collection", label=_("Collection"))) self.table = Table(columns, self.documents) return self.render_to_response()
def get(self, request, app_label, model_name): self.model = get_snippet_model_from_url_params(app_label, model_name) items = self.model.objects.all() # Preserve the snippet's model-level ordering if specified, but fall back on PK if not # (to ensure pagination is consistent) if not items.ordered: items = items.order_by("pk") # Filter by locale self.locale = None self.locale_filter = None self.selected_locale = None if issubclass(self.model, TranslatableMixin): # 'locale' is the Locale of the object that this snippet is being chosen for if request.GET.get("locale"): self.locale = get_object_or_404( Locale, language_code=request.GET["locale"] ) # 'locale_filter' is the current value of the "Locale" selector in the UI if request.GET.get("locale_filter"): self.locale_filter = get_object_or_404( Locale, language_code=request.GET["locale_filter"] ) self.selected_locale = self.locale_filter or self.locale if self.selected_locale: items = items.filter(locale=self.selected_locale) # Search self.is_searchable = class_is_indexed(self.model) self.is_searching = False self.search_query = None if self.is_searchable and "q" in request.GET: self.search_form = SearchForm( request.GET, placeholder=_("Search %(snippet_type_name)s") % {"snippet_type_name": self.model._meta.verbose_name}, ) if self.search_form.is_valid(): self.search_query = self.search_form.cleaned_data["q"] search_backend = get_search_backend() items = search_backend.search(self.search_query, items) self.is_searching = True else: self.search_form = SearchForm( placeholder=_("Search %(snippet_type_name)s") % {"snippet_type_name": self.model._meta.verbose_name} ) # Pagination paginator = Paginator(items, per_page=25) self.paginated_items = paginator.get_page(request.GET.get("p")) self.table = Table( [ SnippetTitleColumn( "title", self.model, label=_("Title"), link_classname="snippet-choice", ), ], self.paginated_items, ) return self.render_to_response()