admin.py
114 lines
| 3.7 KiB
| text/x-python
|
PythonLexer
/ boards / admin.py
|
r0 | from django.contrib import admin | ||
|
r968 | from django.utils.translation import ugettext_lazy as _ | ||
|
r1500 | from django.core.urlresolvers import reverse | ||
from boards.models import Post, Tag, Ban, Thread, Banner, PostImage | ||||
|
r0 | |||
|
r333 | |||
|
r906 | @admin.register(Post) | ||
|
r305 | class PostAdmin(admin.ModelAdmin): | ||
|
r1500 | list_display = ('id', 'title', 'text', 'poster_ip', 'linked_images') | ||
|
r969 | list_filter = ('pub_time',) | ||
|
r1130 | search_fields = ('id', 'title', 'text', 'poster_ip') | ||
|
r1540 | exclude = ('referenced_posts', 'refmap', 'images') | ||
|
r1500 | readonly_fields = ('poster_ip', 'threads', 'thread', 'linked_images', | ||
|
r1389 | 'attachments', 'uid', 'url', 'pub_time', 'opening') | ||
|
r305 | |||
|
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)) | ||||
|
r1495 | def ban_with_hiding(self, request, queryset): | ||
bans = 0 | ||||
hidden = 0 | ||||
for post in queryset: | ||||
poster_ip = post.poster_ip | ||||
ban, created = Ban.objects.get_or_create(ip=poster_ip) | ||||
if created: | ||||
bans += 1 | ||||
|
r1496 | posts = Post.objects.filter(poster_ip=poster_ip, id__gte=post.id) | ||
|
r1495 | hidden += posts.count() | ||
posts.update(hidden=True) | ||||
self.message_user(request, _('{} posters were banned, {} messages were hidden').format(bans, hidden)) | ||||
|
r1500 | def linked_images(self, obj: Post): | ||
images = obj.images.all() | ||||
|
r1541 | image_urls = ['<a href="{}"><img src="{}" /></a>'.format( | ||
reverse('admin:%s_%s_change' % (image._meta.app_label, | ||||
image._meta.model_name), | ||||
args=[image.id]), image.image.url_200x150) for image in images] | ||||
|
r1500 | return ', '.join(image_urls) | ||
linked_images.allow_tags = True | ||||
|
r1495 | |||
actions = ['ban_poster', 'ban_with_hiding'] | ||||
|
r968 | |||
|
r333 | |||
|
r906 | @admin.register(Tag) | ||
|
r305 | class TagAdmin(admin.ModelAdmin): | ||
|
r910 | def thread_count(self, obj: Tag) -> int: | ||
return obj.get_thread_count() | ||||
|
r1356 | def display_children(self, obj: Tag): | ||
return ', '.join([str(child) for child in obj.get_children().all()]) | ||||
|
r1471 | def save_model(self, request, obj, form, change): | ||
super().save_model(request, obj, form, change) | ||||
for thread in obj.get_threads().all(): | ||||
thread.refresh_tags() | ||||
|
r1356 | list_display = ('name', 'thread_count', 'display_children') | ||
|
r906 | search_fields = ('name',) | ||
|
r305 | |||
|
r883 | |||
|
r906 | @admin.register(Thread) | ||
|
r604 | class ThreadAdmin(admin.ModelAdmin): | ||
|
r910 | def title(self, obj: Thread) -> str: | ||
|
r883 | return obj.get_opening_post().get_title() | ||
|
r604 | |||
|
r910 | def reply_count(self, obj: Thread) -> int: | ||
|
r604 | return obj.get_reply_count() | ||
|
r910 | def ip(self, obj: Thread): | ||
|
r906 | return obj.get_opening_post().poster_ip | ||
|
r1356 | def display_tags(self, obj: Thread): | ||
return ', '.join([str(tag) for tag in obj.get_tags().all()]) | ||||
def op(self, obj: Thread): | ||||
return obj.get_opening_post_id() | ||||
|
r1470 | # Save parent tags when editing tags | ||
def save_related(self, request, form, formsets, change): | ||||
super().save_related(request, form, formsets, change) | ||||
|
r1471 | form.instance.refresh_tags() | ||
|
r1470 | |||
|
r1414 | list_display = ('id', 'op', 'title', 'reply_count', 'status', 'ip', | ||
|
r1356 | 'display_tags') | ||
|
r1414 | list_filter = ('bump_time', 'status') | ||
|
r604 | search_fields = ('id', 'title') | ||
|
r921 | filter_horizontal = ('tags',) | ||
|
r604 | |||
|
r883 | |||
|
r906 | @admin.register(Ban) | ||
|
r804 | class BanAdmin(admin.ModelAdmin): | ||
list_display = ('ip', 'can_read') | ||||
list_filter = ('can_read',) | ||||
|
r921 | search_fields = ('ip',) | ||
|
r1148 | |||
@admin.register(Banner) | ||||
class BannerAdmin(admin.ModelAdmin): | ||||
list_display = ('title', 'text') | ||||
|
r1500 | |||
@admin.register(PostImage) | ||||
class PostImageAdmin(admin.ModelAdmin): | ||||
search_fields = ('alias',) | ||||