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