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