from boards.models.attachment import FILE_TYPES_IMAGE from django.contrib import admin from django.utils.translation import ugettext_lazy as _ from django.core.urlresolvers import reverse from boards.models import Post, Tag, Ban, Thread, Banner, Attachment, KeyPair, GlobalId @admin.register(Post) class PostAdmin(admin.ModelAdmin): list_display = ('id', 'title', 'text', 'poster_ip', 'linked_images', 'foreign', 'tags') list_filter = ('pub_time',) search_fields = ('id', 'title', 'text', 'poster_ip') exclude = ('referenced_posts', 'refmap', 'images', 'global_id') readonly_fields = ('poster_ip', 'threads', 'thread', 'linked_images', 'attachments', 'uid', 'url', 'pub_time', 'opening', 'linked_global_id', 'version', 'foreign', 'tags') 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)) def ban_latter_with_delete(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 posts = Post.objects.filter(poster_ip=poster_ip, id__gte=post.id) hidden += posts.count() posts.delete() self.message_user(request, _('{} posters were banned, {} messages were removed.').format(bans, hidden)) ban_latter_with_delete.short_description = _('Ban user and delete posts starting from this one and later') def linked_images(self, obj: Post): images = obj.attachments.filter(mimetype__in=FILE_TYPES_IMAGE) image_urls = [''.format( reverse('admin:%s_%s_change' % (image._meta.app_label, image._meta.model_name), args=[image.id]), image.get_thumb_url()) for image in images] return ', '.join(image_urls) linked_images.allow_tags = True def linked_global_id(self, obj: Post): global_id = obj.global_id if global_id is not None: return '{}'.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 def tags(self, obj: Post): return ', '.join([tag.name for tag in obj.get_tags()]) def save_model(self, request, obj, form, change): obj.increment_version() obj.save() obj.clear_cache() def foreign(self, obj: Post): return obj is not None and obj.global_id is not None and\ not obj.global_id.is_local() actions = ['ban_poster', 'ban_latter_with_delete'] @admin.register(Tag) class TagAdmin(admin.ModelAdmin): def thread_count(self, obj: Tag) -> int: return obj.get_thread_count() def display_children(self, obj: Tag): return ', '.join([str(child) for child in obj.get_children().all()]) 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() list_display = ('name', 'thread_count', 'display_children') search_fields = ('name',) @admin.register(Thread) class ThreadAdmin(admin.ModelAdmin): def title(self, obj: Thread) -> str: return obj.get_opening_post().get_title() def reply_count(self, obj: Thread) -> int: return obj.get_reply_count() def ip(self, obj: Thread): return obj.get_opening_post().poster_ip 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() # Save parent tags when editing tags def save_related(self, request, form, formsets, change): super().save_related(request, form, formsets, change) form.instance.refresh_tags() def save_model(self, request, obj, form, change): op = obj.get_opening_post() op.increment_version() op.save(update_fields=['version']) obj.save() op.clear_cache() list_display = ('id', 'op', 'title', 'reply_count', 'status', 'ip', 'display_tags') list_filter = ('bump_time', 'status') search_fields = ('id', 'title') filter_horizontal = ('tags',) @admin.register(KeyPair) class KeyPairAdmin(admin.ModelAdmin): list_display = ('public_key', 'primary') list_filter = ('primary',) search_fields = ('public_key',) @admin.register(Ban) class BanAdmin(admin.ModelAdmin): list_display = ('ip', 'can_read') list_filter = ('can_read',) search_fields = ('ip',) @admin.register(Banner) class BannerAdmin(admin.ModelAdmin): list_display = ('title', 'text') @admin.register(Attachment) class AttachmentAdmin(admin.ModelAdmin): search_fields = ('alias',) @admin.register(GlobalId) class GlobalIdAdmin(admin.ModelAdmin): def is_linked(self, obj): return Post.objects.filter(global_id=obj).exists() list_display = ('__str__', 'is_linked',) readonly_fields = ('content',)