admin.py
193 lines
| 6.3 KiB
| text/x-python
|
PythonLexer
/ boards / admin.py
|
r1942 | from boards.abstracts.sticker_factory import StickerFactory | ||
|
r1951 | from boards.models.attachment import FILE_TYPES_IMAGE, AttachmentSticker, \ | ||
StickerPack | ||||
|
r0 | from django.contrib import admin | ||
|
r968 | from django.utils.translation import ugettext_lazy as _ | ||
|
r1500 | from django.core.urlresolvers import reverse | ||
|
r1862 | from boards.models import Post, Tag, Ban, Thread, Banner, Attachment, \ | ||
|
r1971 | KeyPair, GlobalId, TagAlias, STATUS_ACTIVE | ||
|
r1968 | from boards.models.source import ThreadSource | ||
|
r0 | |||
|
r333 | |||
|
r906 | @admin.register(Post) | ||
|
r305 | class PostAdmin(admin.ModelAdmin): | ||
|
r1583 | list_display = ('id', 'title', 'text', 'poster_ip', 'linked_images', | ||
|
r1620 | 'foreign', 'tags') | ||
|
r969 | list_filter = ('pub_time',) | ||
|
r1130 | search_fields = ('id', 'title', 'text', 'poster_ip') | ||
|
r1559 | exclude = ('referenced_posts', 'refmap', 'images', 'global_id') | ||
|
r1704 | readonly_fields = ('poster_ip', 'thread', 'linked_images', | ||
|
r1569 | 'attachments', 'uid', 'url', 'pub_time', 'opening', 'linked_global_id', | ||
|
r1928 | 'foreign', 'tags') | ||
|
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)) | ||||
|
r1608 | def ban_latter_with_delete(self, request, queryset): | ||
|
r1495 | 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() | ||
|
r1608 | posts.delete() | ||
self.message_user(request, _('{} posters were banned, {} messages were removed.').format(bans, hidden)) | ||||
|
r1609 | ban_latter_with_delete.short_description = _('Ban user and delete posts starting from this one and later') | ||
|
r1495 | |||
|
r1500 | def linked_images(self, obj: Post): | ||
|
r1590 | images = obj.attachments.filter(mimetype__in=FILE_TYPES_IMAGE) | ||
|
r1541 | image_urls = ['<a href="{}"><img src="{}" /></a>'.format( | ||
reverse('admin:%s_%s_change' % (image._meta.app_label, | ||||
image._meta.model_name), | ||||
|
r1606 | args=[image.id]), image.get_thumb_url()) for image in images] | ||
|
r1500 | return ', '.join(image_urls) | ||
linked_images.allow_tags = True | ||||
|
r1559 | def linked_global_id(self, obj: Post): | ||
global_id = obj.global_id | ||||
if global_id is not None: | ||||
return '<a href="{}">{}</a>'.format( | ||||
reverse('admin:%s_%s_change' % (global_id._meta.app_label, | ||||
global_id._meta.model_name), | ||||
args=[global_id.id]), str(global_id)) | ||||
linked_global_id.allow_tags = True | ||||
|
r1495 | |||
|
r1620 | def tags(self, obj: Post): | ||
|
r1885 | return ', '.join([tag.get_name() for tag in obj.get_tags()]) | ||
|
r1620 | |||
|
r1569 | def save_model(self, request, obj, form, change): | ||
obj.save() | ||||
obj.clear_cache() | ||||
|
r1583 | def foreign(self, obj: Post): | ||
return obj is not None and obj.global_id is not None and\ | ||||
not obj.global_id.is_local() | ||||
|
r1608 | actions = ['ban_poster', 'ban_latter_with_delete'] | ||
|
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()]) | ||||
|
r1874 | def name(self, obj: Tag): | ||
return obj.get_name() | ||||
|
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() | ||||
|
r1886 | |||
|
r1356 | list_display = ('name', 'thread_count', 'display_children') | ||
|
r1887 | search_fields = ('id',) | ||
|
r1874 | readonly_fields = ('name',) | ||
|
r305 | |||
|
r883 | |||
|
r1862 | @admin.register(TagAlias) | ||
class TagAliasAdmin(admin.ModelAdmin): | ||||
list_display = ('locale', 'name', 'parent') | ||||
|
r1886 | list_filter = ('locale',) | ||
search_fields = ('name',) | ||||
|
r1862 | |||
|
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() | ||
|
r1569 | |||
def save_model(self, request, obj, form, change): | ||||
op = obj.get_opening_post() | ||||
obj.save() | ||||
op.clear_cache() | ||||
|
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 | |||
|
r933 | @admin.register(KeyPair) | ||
|
r797 | class KeyPairAdmin(admin.ModelAdmin): | ||
list_display = ('public_key', 'primary') | ||||
list_filter = ('primary',) | ||||
search_fields = ('public_key',) | ||||
|
r933 | |||
|
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 | |||
|
r1590 | @admin.register(Attachment) | ||
class AttachmentAdmin(admin.ModelAdmin): | ||||
|
r1937 | list_display = ('__str__', 'mimetype', 'file', 'url') | ||
|
r1511 | |||
|
r1937 | @admin.register(AttachmentSticker) | ||
class AttachmentStickerAdmin(admin.ModelAdmin): | ||||
search_fields = ('name',) | ||||
|
r1750 | |||
|
r1511 | |||
|
r1951 | @admin.register(StickerPack) | ||
class StickerPackAdmin(admin.ModelAdmin): | ||||
search_fields = ('name',) | ||||
|
r1511 | @admin.register(GlobalId) | ||
class GlobalIdAdmin(admin.ModelAdmin): | ||||
def is_linked(self, obj): | ||||
return Post.objects.filter(global_id=obj).exists() | ||||
|
r1550 | list_display = ('__str__', 'is_linked',) | ||
|
r1559 | readonly_fields = ('content',) | ||
|
r1968 | |||
@admin.register(ThreadSource) | ||||
class ThreadSourceAdmin(admin.ModelAdmin): | ||||
search_fields = ('name', 'source') | ||||
|
r1971 | def formfield_for_foreignkey(self, db_field, request, **kwargs): | ||
if db_field.name == 'thread': | ||||
kwargs['queryset'] = Thread.objects.filter(status=STATUS_ACTIVE) | ||||
return super().formfield_for_foreignkey(db_field, request, **kwargs) | ||||