##// END OF EJS Templates
Added admin action to ban user that posted given messages
neko259 -
r968:29fca307 default
parent child Browse files
Show More
@@ -1,47 +1,59 b''
1 from django.contrib import admin
1 from django.contrib import admin
2 from boards.models import Post, Tag, Ban, Thread
2 from boards.models import Post, Tag, Ban, Thread
3 from django.utils.translation import ugettext_lazy as _
3
4
4
5
5 @admin.register(Post)
6 @admin.register(Post)
6 class PostAdmin(admin.ModelAdmin):
7 class PostAdmin(admin.ModelAdmin):
7
8
8 list_display = ('id', 'title', 'text')
9 list_display = ('id', 'title', 'text')
9 list_filter = ('pub_time', 'thread_new')
10 list_filter = ('pub_time', 'thread_new')
10 search_fields = ('id', 'title', 'text')
11 search_fields = ('id', 'title', 'text')
11 exclude = ('referenced_posts', 'refmap')
12 exclude = ('referenced_posts', 'refmap')
12 readonly_fields = ('poster_ip', 'thread_new')
13 readonly_fields = ('poster_ip', 'thread_new')
13
14
15 def ban_poster(self, request, queryset):
16 bans = 0
17 for post in queryset:
18 poster_ip = post.poster_ip
19 ban, created = Ban.objects.get_or_create(ip=poster_ip)
20 if created:
21 bans += 1
22 self.message_user(request, _('{} posters were banned').format(bans))
23
24 actions = ['ban_poster']
25
14
26
15 @admin.register(Tag)
27 @admin.register(Tag)
16 class TagAdmin(admin.ModelAdmin):
28 class TagAdmin(admin.ModelAdmin):
17
29
18 def thread_count(self, obj: Tag) -> int:
30 def thread_count(self, obj: Tag) -> int:
19 return obj.get_thread_count()
31 return obj.get_thread_count()
20
32
21 list_display = ('name', 'thread_count')
33 list_display = ('name', 'thread_count')
22 search_fields = ('name',)
34 search_fields = ('name',)
23
35
24
36
25 @admin.register(Thread)
37 @admin.register(Thread)
26 class ThreadAdmin(admin.ModelAdmin):
38 class ThreadAdmin(admin.ModelAdmin):
27
39
28 def title(self, obj: Thread) -> str:
40 def title(self, obj: Thread) -> str:
29 return obj.get_opening_post().get_title()
41 return obj.get_opening_post().get_title()
30
42
31 def reply_count(self, obj: Thread) -> int:
43 def reply_count(self, obj: Thread) -> int:
32 return obj.get_reply_count()
44 return obj.get_reply_count()
33
45
34 def ip(self, obj: Thread):
46 def ip(self, obj: Thread):
35 return obj.get_opening_post().poster_ip
47 return obj.get_opening_post().poster_ip
36
48
37 list_display = ('id', 'title', 'reply_count', 'archived', 'ip')
49 list_display = ('id', 'title', 'reply_count', 'archived', 'ip')
38 list_filter = ('bump_time', 'archived', 'bumpable')
50 list_filter = ('bump_time', 'archived', 'bumpable')
39 search_fields = ('id', 'title')
51 search_fields = ('id', 'title')
40 filter_horizontal = ('tags',)
52 filter_horizontal = ('tags',)
41
53
42
54
43 @admin.register(Ban)
55 @admin.register(Ban)
44 class BanAdmin(admin.ModelAdmin):
56 class BanAdmin(admin.ModelAdmin):
45 list_display = ('ip', 'can_read')
57 list_display = ('ip', 'can_read')
46 list_filter = ('can_read',)
58 list_filter = ('can_read',)
47 search_fields = ('ip',)
59 search_fields = ('ip',)
@@ -1,31 +1,30 b''
1 from django.db import transaction
1 from django.db import transaction
2 from django.template import RequestContext
3 from django.views.generic import View
2 from django.views.generic import View
4
3
5 from boards import utils
4 from boards import utils
6 from boards.models.user import Ban
5 from boards.models.user import Ban
7
6
8
7
9 BAN_REASON_SPAM = 'Autoban: spam bot'
8 BAN_REASON_SPAM = 'Autoban: spam bot'
10
9
11 CONTEXT_FORM = 'form'
10 CONTEXT_FORM = 'form'
12
11
13
12
14 class BaseBoardView(View):
13 class BaseBoardView(View):
15
14
16 def get_context_data(self, **kwargs):
15 def get_context_data(self, **kwargs):
17 return dict()
16 return dict()
18
17
19 @transaction.atomic
18 @transaction.atomic
20 def _ban_current_user(self, request):
19 def _ban_current_user(self, request):
21 """
20 """
22 Add current user to the IP ban list
21 Add current user to the IP ban list
23 """
22 """
24
23
25 ip = utils.get_client_ip(request)
24 ip = utils.get_client_ip(request)
26 ban, created = Ban.objects.get_or_create(ip=ip)
25 ban, created = Ban.objects.get_or_create(ip=ip)
27 if created:
26 if created:
28 ban.can_read = False
27 ban.can_read = False
29 ban.reason = BAN_REASON_SPAM
28 ban.reason = BAN_REASON_SPAM
30 ban.save()
29 ban.save()
31
30
General Comments 0
You need to be logged in to leave comments. Login now