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