admin.py
71 lines
| 2.0 KiB
| text/x-python
|
PythonLexer
/ boards / admin.py
neko259
|
r0 | from django.contrib import admin | ||
neko259
|
r1157 | from boards.models import Post, Tag, Ban, Thread, KeyPair, Banner | ||
neko259
|
r968 | from django.utils.translation import ugettext_lazy as _ | ||
neko259
|
r0 | |||
neko259
|
r333 | |||
neko259
|
r906 | @admin.register(Post) | ||
neko259
|
r305 | class PostAdmin(admin.ModelAdmin): | ||
neko259
|
r1130 | list_display = ('id', 'title', 'text', 'poster_ip') | ||
neko259
|
r969 | list_filter = ('pub_time',) | ||
neko259
|
r1130 | search_fields = ('id', 'title', 'text', 'poster_ip') | ||
neko259
|
r906 | exclude = ('referenced_posts', 'refmap') | ||
neko259
|
r1118 | readonly_fields = ('poster_ip', 'threads', 'thread', 'images', 'uid') | ||
neko259
|
r305 | |||
neko259
|
r968 | def ban_poster(self, request, queryset): | ||
bans = 0 | ||||
for post in queryset: | ||||
poster_ip = post.poster_ip | ||||
ban, created = Ban.objects.get_or_create(ip=poster_ip) | ||||
if created: | ||||
bans += 1 | ||||
self.message_user(request, _('{} posters were banned').format(bans)) | ||||
actions = ['ban_poster'] | ||||
neko259
|
r333 | |||
neko259
|
r906 | @admin.register(Tag) | ||
neko259
|
r305 | class TagAdmin(admin.ModelAdmin): | ||
neko259
|
r910 | def thread_count(self, obj: Tag) -> int: | ||
return obj.get_thread_count() | ||||
list_display = ('name', 'thread_count') | ||||
neko259
|
r906 | search_fields = ('name',) | ||
neko259
|
r305 | |||
neko259
|
r883 | |||
neko259
|
r906 | @admin.register(Thread) | ||
neko259
|
r604 | class ThreadAdmin(admin.ModelAdmin): | ||
neko259
|
r910 | def title(self, obj: Thread) -> str: | ||
neko259
|
r883 | return obj.get_opening_post().get_title() | ||
neko259
|
r604 | |||
neko259
|
r910 | def reply_count(self, obj: Thread) -> int: | ||
neko259
|
r604 | return obj.get_reply_count() | ||
neko259
|
r910 | def ip(self, obj: Thread): | ||
neko259
|
r906 | return obj.get_opening_post().poster_ip | ||
list_display = ('id', 'title', 'reply_count', 'archived', 'ip') | ||||
list_filter = ('bump_time', 'archived', 'bumpable') | ||||
neko259
|
r604 | search_fields = ('id', 'title') | ||
neko259
|
r921 | filter_horizontal = ('tags',) | ||
neko259
|
r604 | |||
neko259
|
r883 | |||
neko259
|
r933 | @admin.register(KeyPair) | ||
neko259
|
r797 | class KeyPairAdmin(admin.ModelAdmin): | ||
list_display = ('public_key', 'primary') | ||||
list_filter = ('primary',) | ||||
search_fields = ('public_key',) | ||||
neko259
|
r933 | |||
neko259
|
r906 | @admin.register(Ban) | ||
neko259
|
r804 | class BanAdmin(admin.ModelAdmin): | ||
list_display = ('ip', 'can_read') | ||||
list_filter = ('can_read',) | ||||
neko259
|
r921 | search_fields = ('ip',) | ||
neko259
|
r1148 | |||
@admin.register(Banner) | ||||
class BannerAdmin(admin.ModelAdmin): | ||||
list_display = ('title', 'text') | ||||