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