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