##// END OF EJS Templates
Translate ban admin action
neko259 -
r1609:5daaaa13 default
parent child Browse files
Show More
@@ -1,157 +1,157 b''
1 1 from boards.models.attachment import FILE_TYPES_IMAGE
2 2 from django.contrib import admin
3 3 from django.utils.translation import ugettext_lazy as _
4 4 from django.core.urlresolvers import reverse
5 5 from boards.models import Post, Tag, Ban, Thread, Banner, Attachment, KeyPair, GlobalId
6 6
7 7
8 8 @admin.register(Post)
9 9 class PostAdmin(admin.ModelAdmin):
10 10
11 11 list_display = ('id', 'title', 'text', 'poster_ip', 'linked_images',
12 12 'foreign')
13 13 list_filter = ('pub_time',)
14 14 search_fields = ('id', 'title', 'text', 'poster_ip')
15 15 exclude = ('referenced_posts', 'refmap', 'images', 'global_id')
16 16 readonly_fields = ('poster_ip', 'threads', 'thread', 'linked_images',
17 17 'attachments', 'uid', 'url', 'pub_time', 'opening', 'linked_global_id',
18 18 'version', 'foreign')
19 19
20 20 def ban_poster(self, request, queryset):
21 21 bans = 0
22 22 for post in queryset:
23 23 poster_ip = post.poster_ip
24 24 ban, created = Ban.objects.get_or_create(ip=poster_ip)
25 25 if created:
26 26 bans += 1
27 27 self.message_user(request, _('{} posters were banned').format(bans))
28 28
29 29 def ban_latter_with_delete(self, request, queryset):
30 30 bans = 0
31 31 hidden = 0
32 32 for post in queryset:
33 33 poster_ip = post.poster_ip
34 34 ban, created = Ban.objects.get_or_create(ip=poster_ip)
35 35 if created:
36 36 bans += 1
37 37 posts = Post.objects.filter(poster_ip=poster_ip, id__gte=post.id)
38 38 hidden += posts.count()
39 39 posts.delete()
40 40 self.message_user(request, _('{} posters were banned, {} messages were removed.').format(bans, hidden))
41 ban_latter_with_delete.short_description = 'Ban user and delete posts starting from this one and later'
41 ban_latter_with_delete.short_description = _('Ban user and delete posts starting from this one and later')
42 42
43 43 def linked_images(self, obj: Post):
44 44 images = obj.attachments.filter(mimetype__in=FILE_TYPES_IMAGE)
45 45 image_urls = ['<a href="{}"><img src="{}" /></a>'.format(
46 46 reverse('admin:%s_%s_change' % (image._meta.app_label,
47 47 image._meta.model_name),
48 48 args=[image.id]), image.get_thumb_url()) for image in images]
49 49 return ', '.join(image_urls)
50 50 linked_images.allow_tags = True
51 51
52 52 def linked_global_id(self, obj: Post):
53 53 global_id = obj.global_id
54 54 if global_id is not None:
55 55 return '<a href="{}">{}</a>'.format(
56 56 reverse('admin:%s_%s_change' % (global_id._meta.app_label,
57 57 global_id._meta.model_name),
58 58 args=[global_id.id]), str(global_id))
59 59 linked_global_id.allow_tags = True
60 60
61 61 def save_model(self, request, obj, form, change):
62 62 obj.increment_version()
63 63 obj.save()
64 64 obj.clear_cache()
65 65
66 66 def foreign(self, obj: Post):
67 67 return obj is not None and obj.global_id is not None and\
68 68 not obj.global_id.is_local()
69 69
70 70 actions = ['ban_poster', 'ban_latter_with_delete']
71 71
72 72
73 73 @admin.register(Tag)
74 74 class TagAdmin(admin.ModelAdmin):
75 75
76 76 def thread_count(self, obj: Tag) -> int:
77 77 return obj.get_thread_count()
78 78
79 79 def display_children(self, obj: Tag):
80 80 return ', '.join([str(child) for child in obj.get_children().all()])
81 81
82 82 def save_model(self, request, obj, form, change):
83 83 super().save_model(request, obj, form, change)
84 84 for thread in obj.get_threads().all():
85 85 thread.refresh_tags()
86 86 list_display = ('name', 'thread_count', 'display_children')
87 87 search_fields = ('name',)
88 88
89 89
90 90 @admin.register(Thread)
91 91 class ThreadAdmin(admin.ModelAdmin):
92 92
93 93 def title(self, obj: Thread) -> str:
94 94 return obj.get_opening_post().get_title()
95 95
96 96 def reply_count(self, obj: Thread) -> int:
97 97 return obj.get_reply_count()
98 98
99 99 def ip(self, obj: Thread):
100 100 return obj.get_opening_post().poster_ip
101 101
102 102 def display_tags(self, obj: Thread):
103 103 return ', '.join([str(tag) for tag in obj.get_tags().all()])
104 104
105 105 def op(self, obj: Thread):
106 106 return obj.get_opening_post_id()
107 107
108 108 # Save parent tags when editing tags
109 109 def save_related(self, request, form, formsets, change):
110 110 super().save_related(request, form, formsets, change)
111 111 form.instance.refresh_tags()
112 112
113 113 def save_model(self, request, obj, form, change):
114 114 op = obj.get_opening_post()
115 115 op.increment_version()
116 116 op.save(update_fields=['version'])
117 117 obj.save()
118 118 op.clear_cache()
119 119
120 120 list_display = ('id', 'op', 'title', 'reply_count', 'status', 'ip',
121 121 'display_tags')
122 122 list_filter = ('bump_time', 'status')
123 123 search_fields = ('id', 'title')
124 124 filter_horizontal = ('tags',)
125 125
126 126
127 127 @admin.register(KeyPair)
128 128 class KeyPairAdmin(admin.ModelAdmin):
129 129 list_display = ('public_key', 'primary')
130 130 list_filter = ('primary',)
131 131 search_fields = ('public_key',)
132 132
133 133
134 134 @admin.register(Ban)
135 135 class BanAdmin(admin.ModelAdmin):
136 136 list_display = ('ip', 'can_read')
137 137 list_filter = ('can_read',)
138 138 search_fields = ('ip',)
139 139
140 140
141 141 @admin.register(Banner)
142 142 class BannerAdmin(admin.ModelAdmin):
143 143 list_display = ('title', 'text')
144 144
145 145
146 146 @admin.register(Attachment)
147 147 class AttachmentAdmin(admin.ModelAdmin):
148 148 search_fields = ('alias',)
149 149
150 150
151 151 @admin.register(GlobalId)
152 152 class GlobalIdAdmin(admin.ModelAdmin):
153 153 def is_linked(self, obj):
154 154 return Post.objects.filter(global_id=obj).exists()
155 155
156 156 list_display = ('__str__', 'is_linked',)
157 157 readonly_fields = ('content',)
General Comments 0
You need to be logged in to leave comments. Login now