MCPcopy Create free account
hub / github.com/HermanMartinus/bearblog / display_posts

Method display_posts

blogs/admin.py:142–192  ·  view source on GitHub ↗
(self, obj)

Source from the content-addressed store, hash-verified

140
141 # Method to display posts in the admin change view
142 def display_posts(self, obj):
143 posts = obj.posts.all().order_by('-published_date')
144 if not posts.exists():
145 return "No posts yet."
146
147 # Start the table
148 table_header = format_html(
149 '<thead><tr>'
150 '<th>{}</th>'
151 '<th>{}</th>'
152 '<th>{}</th>'
153 '<th>{}</th>'
154 '</tr></thead>',
155 'Post title (Admin)',
156 'Post link (Public)',
157 'Is page',
158 'Published date'
159 )
160
161 table_rows = []
162 for post in posts:
163 admin_url = reverse('admin:blogs_post_change', args=[post.pk])
164 public_url = f"{obj.dynamic_useful_domain}/{post.slug}/"
165 post_title = escape(post.title or "Untitled")
166
167 admin_link = format_html('<a href="{}" target="_blank">{}</a>', admin_url, post_title)
168 public_link = format_html('<a href="{}" target="_blank">{}</a>', public_url, public_url)
169 is_page_display = 'Yes' if post.is_page else 'No'
170 published_date_display = post.published_date.strftime("%Y-%m-%d %H:%M") if post.published_date else '-'
171
172 table_rows.append(format_html(
173 '<tr>'
174 '<td>{}</td>'
175 '<td>{}</td>'
176 '<td>{}</td>'
177 '<td>{}</td>'
178 '</tr>',
179 admin_link,
180 public_link,
181 is_page_display,
182 published_date_display
183 ))
184
185 # Combine header and body within table tags
186 table_html = format_html(
187 '<table style="width: 100%; border-collapse: collapse;" border="1">{}<tbody>{}</tbody></table>',
188 table_header,
189 mark_safe('\n'.join(table_rows))
190 )
191
192 return table_html
193
194 display_posts.short_description = 'Posts'
195

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected