Show More
@@ -1,111 +1,114 b'' | |||
|
1 | 1 | from django.contrib import admin |
|
2 | 2 | from django.utils.translation import ugettext_lazy as _ |
|
3 | 3 | from django.core.urlresolvers import reverse |
|
4 | 4 | from boards.models import Post, Tag, Ban, Thread, Banner, PostImage |
|
5 | 5 | |
|
6 | 6 | |
|
7 | 7 | @admin.register(Post) |
|
8 | 8 | class PostAdmin(admin.ModelAdmin): |
|
9 | 9 | |
|
10 | 10 | list_display = ('id', 'title', 'text', 'poster_ip', 'linked_images') |
|
11 | 11 | list_filter = ('pub_time',) |
|
12 | 12 | search_fields = ('id', 'title', 'text', 'poster_ip') |
|
13 | 13 | exclude = ('referenced_posts', 'refmap', 'images') |
|
14 | 14 | readonly_fields = ('poster_ip', 'threads', 'thread', 'linked_images', |
|
15 | 15 | 'attachments', 'uid', 'url', 'pub_time', 'opening') |
|
16 | 16 | |
|
17 | 17 | def ban_poster(self, request, queryset): |
|
18 | 18 | bans = 0 |
|
19 | 19 | for post in queryset: |
|
20 | 20 | poster_ip = post.poster_ip |
|
21 | 21 | ban, created = Ban.objects.get_or_create(ip=poster_ip) |
|
22 | 22 | if created: |
|
23 | 23 | bans += 1 |
|
24 | 24 | self.message_user(request, _('{} posters were banned').format(bans)) |
|
25 | 25 | |
|
26 | 26 | def ban_with_hiding(self, request, queryset): |
|
27 | 27 | bans = 0 |
|
28 | 28 | hidden = 0 |
|
29 | 29 | for post in queryset: |
|
30 | 30 | poster_ip = post.poster_ip |
|
31 | 31 | ban, created = Ban.objects.get_or_create(ip=poster_ip) |
|
32 | 32 | if created: |
|
33 | 33 | bans += 1 |
|
34 | 34 | posts = Post.objects.filter(poster_ip=poster_ip, id__gte=post.id) |
|
35 | 35 | hidden += posts.count() |
|
36 | 36 | posts.update(hidden=True) |
|
37 | 37 | self.message_user(request, _('{} posters were banned, {} messages were hidden').format(bans, hidden)) |
|
38 | 38 | |
|
39 | 39 | def linked_images(self, obj: Post): |
|
40 | 40 | images = obj.images.all() |
|
41 | image_urls = ['<a href="{}">{}</a>'.format(reverse('admin:%s_%s_change' %(image._meta.app_label, image._meta.model_name), args=[image.id]), image.hash) for image in images] | |
|
41 | image_urls = ['<a href="{}"><img src="{}" /></a>'.format( | |
|
42 | reverse('admin:%s_%s_change' % (image._meta.app_label, | |
|
43 | image._meta.model_name), | |
|
44 | args=[image.id]), image.image.url_200x150) for image in images] | |
|
42 | 45 | return ', '.join(image_urls) |
|
43 | 46 | linked_images.allow_tags = True |
|
44 | 47 | |
|
45 | 48 | |
|
46 | 49 | actions = ['ban_poster', 'ban_with_hiding'] |
|
47 | 50 | |
|
48 | 51 | |
|
49 | 52 | @admin.register(Tag) |
|
50 | 53 | class TagAdmin(admin.ModelAdmin): |
|
51 | 54 | |
|
52 | 55 | def thread_count(self, obj: Tag) -> int: |
|
53 | 56 | return obj.get_thread_count() |
|
54 | 57 | |
|
55 | 58 | def display_children(self, obj: Tag): |
|
56 | 59 | return ', '.join([str(child) for child in obj.get_children().all()]) |
|
57 | 60 | |
|
58 | 61 | def save_model(self, request, obj, form, change): |
|
59 | 62 | super().save_model(request, obj, form, change) |
|
60 | 63 | for thread in obj.get_threads().all(): |
|
61 | 64 | thread.refresh_tags() |
|
62 | 65 | |
|
63 | 66 | list_display = ('name', 'thread_count', 'display_children') |
|
64 | 67 | search_fields = ('name',) |
|
65 | 68 | |
|
66 | 69 | |
|
67 | 70 | @admin.register(Thread) |
|
68 | 71 | class ThreadAdmin(admin.ModelAdmin): |
|
69 | 72 | |
|
70 | 73 | def title(self, obj: Thread) -> str: |
|
71 | 74 | return obj.get_opening_post().get_title() |
|
72 | 75 | |
|
73 | 76 | def reply_count(self, obj: Thread) -> int: |
|
74 | 77 | return obj.get_reply_count() |
|
75 | 78 | |
|
76 | 79 | def ip(self, obj: Thread): |
|
77 | 80 | return obj.get_opening_post().poster_ip |
|
78 | 81 | |
|
79 | 82 | def display_tags(self, obj: Thread): |
|
80 | 83 | return ', '.join([str(tag) for tag in obj.get_tags().all()]) |
|
81 | 84 | |
|
82 | 85 | def op(self, obj: Thread): |
|
83 | 86 | return obj.get_opening_post_id() |
|
84 | 87 | |
|
85 | 88 | # Save parent tags when editing tags |
|
86 | 89 | def save_related(self, request, form, formsets, change): |
|
87 | 90 | super().save_related(request, form, formsets, change) |
|
88 | 91 | form.instance.refresh_tags() |
|
89 | 92 | |
|
90 | 93 | list_display = ('id', 'op', 'title', 'reply_count', 'status', 'ip', |
|
91 | 94 | 'display_tags') |
|
92 | 95 | list_filter = ('bump_time', 'status') |
|
93 | 96 | search_fields = ('id', 'title') |
|
94 | 97 | filter_horizontal = ('tags',) |
|
95 | 98 | |
|
96 | 99 | |
|
97 | 100 | @admin.register(Ban) |
|
98 | 101 | class BanAdmin(admin.ModelAdmin): |
|
99 | 102 | list_display = ('ip', 'can_read') |
|
100 | 103 | list_filter = ('can_read',) |
|
101 | 104 | search_fields = ('ip',) |
|
102 | 105 | |
|
103 | 106 | |
|
104 | 107 | @admin.register(Banner) |
|
105 | 108 | class BannerAdmin(admin.ModelAdmin): |
|
106 | 109 | list_display = ('title', 'text') |
|
107 | 110 | |
|
108 | 111 | |
|
109 | 112 | @admin.register(PostImage) |
|
110 | 113 | class PostImageAdmin(admin.ModelAdmin): |
|
111 | 114 | search_fields = ('alias',) |
General Comments 0
You need to be logged in to leave comments.
Login now