##// END OF EJS Templates
Moved ban view to class-based
neko259 -
r553:fd6431be 1.7-dev
parent child Browse files
Show More
@@ -0,0 +1,20 b''
1 from django.db import transaction
2
3 from boards.views.base import BaseBoardView
4 from boards.models import Post, Ban
5
6 class BanUserView(BaseBoardView):
7
8 @transaction.atomic
9 def get(self, request, post_id):
10 user = self._get_user(request)
11 post = get_object_or_404(Post, id=post_id)
12
13 if user.is_moderator():
14 # TODO Show confirmation page before ban
15 ban, created = Ban.objects.get_or_create(ip=post.poster_ip)
16 if created:
17 ban.reason = 'Banned for post ' + str(post_id)
18 ban.save()
19
20 return self.redirect_to_next(request)
@@ -1,81 +1,82 b''
1 from django.conf.urls import patterns, url, include
1 from django.conf.urls import patterns, url, include
2 from boards import views
2 from boards import views
3 from boards.rss import AllThreadsFeed, TagThreadsFeed, ThreadPostsFeed
3 from boards.rss import AllThreadsFeed, TagThreadsFeed, ThreadPostsFeed
4 from boards.views import api, tag_threads, all_threads, archived_threads, \
4 from boards.views import api, tag_threads, all_threads, archived_threads, \
5 login, settings, all_tags
5 login, settings, all_tags
6 from boards.views.authors import AuthorsView
6 from boards.views.authors import AuthorsView
7 from boards.views.delete_post import DeletePostView
7 from boards.views.delete_post import DeletePostView
8 from boards.views.ban import BanUserView
8
9
9 js_info_dict = {
10 js_info_dict = {
10 'packages': ('boards',),
11 'packages': ('boards',),
11 }
12 }
12
13
13 urlpatterns = patterns('',
14 urlpatterns = patterns('',
14
15
15 # /boards/
16 # /boards/
16 url(r'^$', all_threads.AllThreadsView.as_view(), name='index'),
17 url(r'^$', all_threads.AllThreadsView.as_view(), name='index'),
17 # /boards/page/
18 # /boards/page/
18 url(r'^page/(?P<page>\w+)/$', all_threads.AllThreadsView.as_view(),
19 url(r'^page/(?P<page>\w+)/$', all_threads.AllThreadsView.as_view(),
19 name='index'),
20 name='index'),
20
21
21 url(r'^archive/$', archived_threads.ArchiveView.as_view(), name='archive'),
22 url(r'^archive/$', archived_threads.ArchiveView.as_view(), name='archive'),
22 url(r'^archive/page/(?P<page>\w+)/$',
23 url(r'^archive/page/(?P<page>\w+)/$',
23 archived_threads.ArchiveView.as_view(), name='archive'),
24 archived_threads.ArchiveView.as_view(), name='archive'),
24
25
25 # login page
26 # login page
26 url(r'^login/$', login.LoginView.as_view(), name='login'),
27 url(r'^login/$', login.LoginView.as_view(), name='login'),
27
28
28 # /boards/tag/tag_name/
29 # /boards/tag/tag_name/
29 url(r'^tag/(?P<tag_name>\w+)/$', tag_threads.TagView.as_view(),
30 url(r'^tag/(?P<tag_name>\w+)/$', tag_threads.TagView.as_view(),
30 name='tag'),
31 name='tag'),
31 # /boards/tag/tag_id/page/
32 # /boards/tag/tag_id/page/
32 url(r'^tag/(?P<tag_name>\w+)/page/(?P<page>\w+)/$',
33 url(r'^tag/(?P<tag_name>\w+)/page/(?P<page>\w+)/$',
33 tag_threads.TagView.as_view(), name='tag'),
34 tag_threads.TagView.as_view(), name='tag'),
34
35
35 # /boards/tag/tag_name/unsubscribe/
36 # /boards/tag/tag_name/unsubscribe/
36 url(r'^tag/(?P<tag_name>\w+)/subscribe/$', views.tag_subscribe,
37 url(r'^tag/(?P<tag_name>\w+)/subscribe/$', views.tag_subscribe,
37 name='tag_subscribe'),
38 name='tag_subscribe'),
38 # /boards/tag/tag_name/unsubscribe/
39 # /boards/tag/tag_name/unsubscribe/
39 url(r'^tag/(?P<tag_name>\w+)/unsubscribe/$', views.tag_unsubscribe,
40 url(r'^tag/(?P<tag_name>\w+)/unsubscribe/$', views.tag_unsubscribe,
40 name='tag_unsubscribe'),
41 name='tag_unsubscribe'),
41
42
42 # /boards/thread/
43 # /boards/thread/
43 url(r'^thread/(?P<post_id>\w+)/$', views.thread.ThreadView.as_view(),
44 url(r'^thread/(?P<post_id>\w+)/$', views.thread.ThreadView.as_view(),
44 name='thread'),
45 name='thread'),
45 url(r'^thread/(?P<post_id>\w+)/(?P<mode>\w+)/$', views.thread.ThreadView
46 url(r'^thread/(?P<post_id>\w+)/(?P<mode>\w+)/$', views.thread.ThreadView
46 .as_view(), name='thread_mode'),
47 .as_view(), name='thread_mode'),
47
48
48 url(r'^settings/$', settings.SettingsView.as_view(), name='settings'),
49 url(r'^settings/$', settings.SettingsView.as_view(), name='settings'),
49 url(r'^tags/$', all_tags.AllTagsView.as_view(), name='tags'),
50 url(r'^tags/$', all_tags.AllTagsView.as_view(), name='tags'),
50 url(r'^captcha/', include('captcha.urls')),
51 url(r'^captcha/', include('captcha.urls')),
51 url(r'^jump/(?P<post_id>\w+)/$', views.jump_to_post, name='jumper'),
52 url(r'^jump/(?P<post_id>\w+)/$', views.jump_to_post, name='jumper'),
52 url(r'^authors/$', AuthorsView.as_view(), name='authors'),
53 url(r'^authors/$', AuthorsView.as_view(), name='authors'),
53 url(r'^delete/(?P<post_id>\w+)/$', DeletePostView.as_view(),
54 url(r'^delete/(?P<post_id>\w+)/$', DeletePostView.as_view(),
54 name='delete'),
55 name='delete'),
55 url(r'^ban/(?P<post_id>\w+)/$', views.ban, name='ban'),
56 url(r'^ban/(?P<post_id>\w+)/$', BanUserView.as_view(), name='ban'),
56
57
57 url(r'^banned/$', views.banned.BannedView.as_view(), name='banned'),
58 url(r'^banned/$', views.banned.BannedView.as_view(), name='banned'),
58 url(r'^staticpage/(?P<name>\w+)/$', views.static_page, name='staticpage'),
59 url(r'^staticpage/(?P<name>\w+)/$', views.static_page, name='staticpage'),
59
60
60 # RSS feeds
61 # RSS feeds
61 url(r'^rss/$', AllThreadsFeed()),
62 url(r'^rss/$', AllThreadsFeed()),
62 url(r'^page/(?P<page>\w+)/rss/$', AllThreadsFeed()),
63 url(r'^page/(?P<page>\w+)/rss/$', AllThreadsFeed()),
63 url(r'^tag/(?P<tag_name>\w+)/rss/$', TagThreadsFeed()),
64 url(r'^tag/(?P<tag_name>\w+)/rss/$', TagThreadsFeed()),
64 url(r'^tag/(?P<tag_name>\w+)/page/(?P<page>\w+)/rss/$', TagThreadsFeed()),
65 url(r'^tag/(?P<tag_name>\w+)/page/(?P<page>\w+)/rss/$', TagThreadsFeed()),
65 url(r'^thread/(?P<post_id>\w+)/rss/$', ThreadPostsFeed()),
66 url(r'^thread/(?P<post_id>\w+)/rss/$', ThreadPostsFeed()),
66
67
67 # i18n
68 # i18n
68 url(r'^jsi18n/$', 'boards.views.cached_js_catalog', js_info_dict, name='js_info_dict'),
69 url(r'^jsi18n/$', 'boards.views.cached_js_catalog', js_info_dict, name='js_info_dict'),
69
70
70 # API
71 # API
71 url(r'^api/post/(?P<post_id>\w+)/$', api.get_post, name="get_post"),
72 url(r'^api/post/(?P<post_id>\w+)/$', api.get_post, name="get_post"),
72 url(r'^api/diff_thread/(?P<thread_id>\w+)/(?P<last_update_time>\w+)/$',
73 url(r'^api/diff_thread/(?P<thread_id>\w+)/(?P<last_update_time>\w+)/$',
73 api.api_get_threaddiff, name="get_thread_diff"),
74 api.api_get_threaddiff, name="get_thread_diff"),
74 url(r'^api/threads/(?P<count>\w+)/$', api.api_get_threads,
75 url(r'^api/threads/(?P<count>\w+)/$', api.api_get_threads,
75 name='get_threads'),
76 name='get_threads'),
76 url(r'^api/tags/$', api.api_get_tags, name='get_tags'),
77 url(r'^api/tags/$', api.api_get_tags, name='get_tags'),
77 url(r'^api/thread/(?P<opening_post_id>\w+)/$', api.api_get_thread_posts,
78 url(r'^api/thread/(?P<opening_post_id>\w+)/$', api.api_get_thread_posts,
78 name='get_thread'),
79 name='get_thread'),
79 url(r'^api/add_post/(?P<opening_post_id>\w+)/$', api.api_add_post, name='add_post'),
80 url(r'^api/add_post/(?P<opening_post_id>\w+)/$', api.api_add_post, name='add_post'),
80
81
81 )
82 )
@@ -1,227 +1,193 b''
1 __author__ = 'neko259'
1 __author__ = 'neko259'
2
2
3 import hashlib
3 import hashlib
4
4
5 from django.core import serializers
5 from django.core import serializers
6 from django.core.urlresolvers import reverse
6 from django.core.urlresolvers import reverse
7 from django.http import HttpResponseRedirect
7 from django.http import HttpResponseRedirect
8 from django.http.response import HttpResponse
8 from django.http.response import HttpResponse
9 from django.template import RequestContext
9 from django.template import RequestContext
10 from django.shortcuts import render, redirect, get_object_or_404
10 from django.shortcuts import render, redirect, get_object_or_404
11 from django.utils import timezone
11 from django.utils import timezone
12 from django.db import transaction
12 from django.db import transaction
13 from django.views.decorators.cache import cache_page
13 from django.views.decorators.cache import cache_page
14 from django.views.i18n import javascript_catalog
14 from django.views.i18n import javascript_catalog
15
15
16 import boards
16 import boards
17 from boards.forms import PlainErrorList
17 from boards.forms import PlainErrorList
18 from boards.models import Post, Tag, Ban, User
18 from boards.models import Post, Tag, Ban, User
19 from boards.models.post import SETTING_MODERATE
19 from boards.models.post import SETTING_MODERATE
20 from boards.models.user import RANK_USER
20 from boards.models.user import RANK_USER
21 from boards import authors
21 from boards import authors
22 import neboard
22 import neboard
23
23
24
24
25 BAN_REASON_SPAM = 'Autoban: spam bot'
25 BAN_REASON_SPAM = 'Autoban: spam bot'
26
26
27 DEFAULT_PAGE = 1
27 DEFAULT_PAGE = 1
28
28
29
29
30 # TODO Maybe this jumper is not needed any more? Only as a hack to find some
30 # TODO Maybe this jumper is not needed any more? Only as a hack to find some
31 # post without knowing its thread
31 # post without knowing its thread
32 def jump_to_post(request, post_id):
32 def jump_to_post(request, post_id):
33 """Determine thread in which the requested post is and open it's page"""
33 """Determine thread in which the requested post is and open it's page"""
34
34
35 post = get_object_or_404(Post, id=post_id)
35 post = get_object_or_404(Post, id=post_id)
36
36
37 if not post.thread:
37 if not post.thread:
38 return redirect('thread', post_id=post.id)
38 return redirect('thread', post_id=post.id)
39 else:
39 else:
40 return redirect(reverse('thread', kwargs={'post_id': post.thread.id})
40 return redirect(reverse('thread', kwargs={'post_id': post.thread.id})
41 + '#' + str(post.id))
41 + '#' + str(post.id))
42
42
43
43
44
44
45
45
46 @transaction.atomic
47 def delete(request, post_id):
48 """Delete post"""
49
50 user = _get_user(request)
51 post = get_object_or_404(Post, id=post_id)
52
53 if user.is_moderator():
54 # TODO Show confirmation page before deletion
55 Post.objects.delete_post(post)
56
57 if not post.thread:
58 return _redirect_to_next(request)
59 else:
60 return redirect('thread', post_id=post.thread.id)
61
62
63 @transaction.atomic
64 def ban(request, post_id):
65 """Ban user"""
66
67 user = _get_user(request)
68 post = get_object_or_404(Post, id=post_id)
69
70 if user.is_moderator():
71 # TODO Show confirmation page before ban
72 ban, created = Ban.objects.get_or_create(ip=post.poster_ip)
73 if created:
74 ban.reason = 'Banned for post ' + str(post_id)
75 ban.save()
76
77 return _redirect_to_next(request)
78
79
80 def page_404(request):
46 def page_404(request):
81 """Show page 404 (not found error)"""
47 """Show page 404 (not found error)"""
82
48
83 context = _init_default_context(request)
49 context = _init_default_context(request)
84 return render(request, 'boards/404.html', context)
50 return render(request, 'boards/404.html', context)
85
51
86
52
87 @transaction.atomic
53 @transaction.atomic
88 def tag_subscribe(request, tag_name):
54 def tag_subscribe(request, tag_name):
89 """Add tag to favorites"""
55 """Add tag to favorites"""
90
56
91 user = _get_user(request)
57 user = _get_user(request)
92 tag = get_object_or_404(Tag, name=tag_name)
58 tag = get_object_or_404(Tag, name=tag_name)
93
59
94 if not tag in user.fav_tags.all():
60 if not tag in user.fav_tags.all():
95 user.add_tag(tag)
61 user.add_tag(tag)
96
62
97 return _redirect_to_next(request)
63 return _redirect_to_next(request)
98
64
99
65
100 @transaction.atomic
66 @transaction.atomic
101 def tag_unsubscribe(request, tag_name):
67 def tag_unsubscribe(request, tag_name):
102 """Remove tag from favorites"""
68 """Remove tag from favorites"""
103
69
104 user = _get_user(request)
70 user = _get_user(request)
105 tag = get_object_or_404(Tag, name=tag_name)
71 tag = get_object_or_404(Tag, name=tag_name)
106
72
107 if tag in user.fav_tags.all():
73 if tag in user.fav_tags.all():
108 user.remove_tag(tag)
74 user.remove_tag(tag)
109
75
110 return _redirect_to_next(request)
76 return _redirect_to_next(request)
111
77
112
78
113 def static_page(request, name):
79 def static_page(request, name):
114 """Show a static page that needs only tags list and a CSS"""
80 """Show a static page that needs only tags list and a CSS"""
115
81
116 context = _init_default_context(request)
82 context = _init_default_context(request)
117 return render(request, 'boards/staticpages/' + name + '.html', context)
83 return render(request, 'boards/staticpages/' + name + '.html', context)
118
84
119
85
120 # TODO This has to be moved under the api module
86 # TODO This has to be moved under the api module
121 def api_get_post(request, post_id):
87 def api_get_post(request, post_id):
122 """
88 """
123 Get the JSON of a post. This can be
89 Get the JSON of a post. This can be
124 used as and API for external clients.
90 used as and API for external clients.
125 """
91 """
126
92
127 post = get_object_or_404(Post, id=post_id)
93 post = get_object_or_404(Post, id=post_id)
128
94
129 json = serializers.serialize("json", [post], fields=(
95 json = serializers.serialize("json", [post], fields=(
130 "pub_time", "_text_rendered", "title", "text", "image",
96 "pub_time", "_text_rendered", "title", "text", "image",
131 "image_width", "image_height", "replies", "tags"
97 "image_width", "image_height", "replies", "tags"
132 ))
98 ))
133
99
134 return HttpResponse(content=json)
100 return HttpResponse(content=json)
135
101
136
102
137 @cache_page(86400)
103 @cache_page(86400)
138 def cached_js_catalog(request, domain='djangojs', packages=None):
104 def cached_js_catalog(request, domain='djangojs', packages=None):
139 return javascript_catalog(request, domain, packages)
105 return javascript_catalog(request, domain, packages)
140
106
141
107
142 # TODO This method is deprecated and should be removed after switching to
108 # TODO This method is deprecated and should be removed after switching to
143 # class-based view
109 # class-based view
144 def _get_theme(request, user=None):
110 def _get_theme(request, user=None):
145 """Get user's CSS theme"""
111 """Get user's CSS theme"""
146
112
147 if not user:
113 if not user:
148 user = _get_user(request)
114 user = _get_user(request)
149 theme = user.get_setting('theme')
115 theme = user.get_setting('theme')
150 if not theme:
116 if not theme:
151 theme = neboard.settings.DEFAULT_THEME
117 theme = neboard.settings.DEFAULT_THEME
152
118
153 return theme
119 return theme
154
120
155
121
156 # TODO This method is deprecated and should be removed after switching to
122 # TODO This method is deprecated and should be removed after switching to
157 # class-based view
123 # class-based view
158 def _init_default_context(request):
124 def _init_default_context(request):
159 """Create context with default values that are used in most views"""
125 """Create context with default values that are used in most views"""
160
126
161 context = RequestContext(request)
127 context = RequestContext(request)
162
128
163 user = _get_user(request)
129 user = _get_user(request)
164 context['user'] = user
130 context['user'] = user
165 context['tags'] = user.get_sorted_fav_tags()
131 context['tags'] = user.get_sorted_fav_tags()
166 context['posts_per_day'] = float(Post.objects.get_posts_per_day())
132 context['posts_per_day'] = float(Post.objects.get_posts_per_day())
167
133
168 theme = _get_theme(request, user)
134 theme = _get_theme(request, user)
169 context['theme'] = theme
135 context['theme'] = theme
170 context['theme_css'] = 'css/' + theme + '/base_page.css'
136 context['theme_css'] = 'css/' + theme + '/base_page.css'
171
137
172 # This shows the moderator panel
138 # This shows the moderator panel
173 moderate = user.get_setting(SETTING_MODERATE)
139 moderate = user.get_setting(SETTING_MODERATE)
174 if moderate == 'True':
140 if moderate == 'True':
175 context['moderator'] = user.is_moderator()
141 context['moderator'] = user.is_moderator()
176 else:
142 else:
177 context['moderator'] = False
143 context['moderator'] = False
178
144
179 return context
145 return context
180
146
181
147
182 # TODO This method is deprecated and should be removed after switching to
148 # TODO This method is deprecated and should be removed after switching to
183 # class-based view
149 # class-based view
184 def _get_user(request):
150 def _get_user(request):
185 """
151 """
186 Get current user from the session. If the user does not exist, create
152 Get current user from the session. If the user does not exist, create
187 a new one.
153 a new one.
188 """
154 """
189
155
190 session = request.session
156 session = request.session
191 if not 'user_id' in session:
157 if not 'user_id' in session:
192 request.session.save()
158 request.session.save()
193
159
194 md5 = hashlib.md5()
160 md5 = hashlib.md5()
195 md5.update(session.session_key)
161 md5.update(session.session_key)
196 new_id = md5.hexdigest()
162 new_id = md5.hexdigest()
197
163
198 while User.objects.filter(user_id=new_id).exists():
164 while User.objects.filter(user_id=new_id).exists():
199 md5.update(str(timezone.now()))
165 md5.update(str(timezone.now()))
200 new_id = md5.hexdigest()
166 new_id = md5.hexdigest()
201
167
202 time_now = timezone.now()
168 time_now = timezone.now()
203 user = User.objects.create(user_id=new_id, rank=RANK_USER,
169 user = User.objects.create(user_id=new_id, rank=RANK_USER,
204 registration_time=time_now)
170 registration_time=time_now)
205
171
206 # TODO This is just a test. This method should be removed
172 # TODO This is just a test. This method should be removed
207 # _delete_old_users()
173 # _delete_old_users()
208
174
209 session['user_id'] = user.id
175 session['user_id'] = user.id
210 else:
176 else:
211 user = User.objects.get(id=session['user_id'])
177 user = User.objects.get(id=session['user_id'])
212
178
213 return user
179 return user
214
180
215
181
216 def _redirect_to_next(request):
182 def _redirect_to_next(request):
217 """
183 """
218 If a 'next' parameter was specified, redirect to the next page. This is
184 If a 'next' parameter was specified, redirect to the next page. This is
219 used when the user is required to return to some page after the current
185 used when the user is required to return to some page after the current
220 view has finished its work.
186 view has finished its work.
221 """
187 """
222
188
223 if 'next' in request.GET:
189 if 'next' in request.GET:
224 next_page = request.GET['next']
190 next_page = request.GET['next']
225 return HttpResponseRedirect(next_page)
191 return HttpResponseRedirect(next_page)
226 else:
192 else:
227 return redirect('index')
193 return redirect('index')
General Comments 0
You need to be logged in to leave comments. Login now