##// END OF EJS Templates
Refactored thread views
neko259 -
r1185:ef257c39 default
parent child Browse files
Show More
@@ -1,24 +1,22 b''
1 from boards.views.thread import ThreadView
1 from boards.views.thread import ThreadView
2
2
3 TEMPLATE_GALLERY = 'boards/thread_gallery.html'
3 TEMPLATE_GALLERY = 'boards/thread_gallery.html'
4
4
5 CONTEXT_POSTS = 'posts'
5 CONTEXT_POSTS = 'posts'
6 CONTEXT_OP = 'opening_post'
7
6
8
7
9 class GalleryThreadView(ThreadView):
8 class GalleryThreadView(ThreadView):
10
9
11 def get_template(self):
10 def get_template(self):
12 return TEMPLATE_GALLERY
11 return TEMPLATE_GALLERY
13
12
14 def get_data(self, thread):
13 def get_data(self, thread):
15 params = dict()
14 params = dict()
16
15
17 params[CONTEXT_POSTS] = thread.get_replies_with_images(
16 params[CONTEXT_POSTS] = thread.get_replies_with_images(
18 view_fields_only=True)
17 view_fields_only=True)
19 params[CONTEXT_OP] = thread.get_opening_post()
20
18
21 return params
19 return params
22
20
23 def get_mode(self):
21 def get_mode(self):
24 return 'gallery'
22 return 'gallery'
@@ -1,34 +1,30 b''
1 from boards import settings
2 from boards.views.thread import ThreadView
1 from boards.views.thread import ThreadView
3
2
4 TEMPLATE_NORMAL = 'boards/thread_normal.html'
3 TEMPLATE_NORMAL = 'boards/thread_normal.html'
5
4
6 CONTEXT_OP = 'opening_post'
7 CONTEXT_BUMPLIMIT_PRG = 'bumplimit_progress'
5 CONTEXT_BUMPLIMIT_PRG = 'bumplimit_progress'
8 CONTEXT_POSTS_LEFT = 'posts_left'
6 CONTEXT_POSTS_LEFT = 'posts_left'
9 CONTEXT_BUMPABLE = 'bumpable'
7 CONTEXT_BUMPABLE = 'bumpable'
10
8
11
9
12 class NormalThreadView(ThreadView):
10 class NormalThreadView(ThreadView):
13
11
14 def get_template(self):
12 def get_template(self):
15 return TEMPLATE_NORMAL
13 return TEMPLATE_NORMAL
16
14
17 def get_mode(self):
15 def get_mode(self):
18 return 'normal'
16 return 'normal'
19
17
20 def get_data(self, thread):
18 def get_data(self, thread):
21 params = dict()
19 params = dict()
22
20
23 bumpable = thread.can_bump()
21 bumpable = thread.can_bump()
24 params[CONTEXT_BUMPABLE] = bumpable
22 params[CONTEXT_BUMPABLE] = bumpable
25 max_posts = thread.max_posts
23 max_posts = thread.max_posts
26 if bumpable and thread.has_post_limit():
24 if bumpable and thread.has_post_limit():
27 left_posts = max_posts - thread.get_reply_count()
25 left_posts = max_posts - thread.get_reply_count()
28 params[CONTEXT_POSTS_LEFT] = left_posts
26 params[CONTEXT_POSTS_LEFT] = left_posts
29 params[CONTEXT_BUMPLIMIT_PRG] = str(
27 params[CONTEXT_BUMPLIMIT_PRG] = str(
30 float(left_posts) / max_posts * 100)
28 float(left_posts) / max_posts * 100)
31
29
32 params[CONTEXT_OP] = thread.get_opening_post()
33
34 return params
30 return params
@@ -1,136 +1,138 b''
1 from django.core.exceptions import ObjectDoesNotExist
1 from django.core.exceptions import ObjectDoesNotExist
2 from django.http import Http404
2 from django.http import Http404
3 from django.shortcuts import get_object_or_404, render, redirect
3 from django.shortcuts import get_object_or_404, render, redirect
4 from django.views.generic.edit import FormMixin
4 from django.views.generic.edit import FormMixin
5 from django.utils import timezone
5 from django.utils import timezone
6 from django.utils.dateformat import format
6 from django.utils.dateformat import format
7
7
8 from boards import utils, settings
8 from boards import utils, settings
9 from boards.forms import PostForm, PlainErrorList
9 from boards.forms import PostForm, PlainErrorList
10 from boards.models import Post
10 from boards.models import Post
11 from boards.views.base import BaseBoardView, CONTEXT_FORM
11 from boards.views.base import BaseBoardView, CONTEXT_FORM
12 from boards.views.posting_mixin import PostMixin
12 from boards.views.posting_mixin import PostMixin
13
13
14 import neboard
14 import neboard
15
15
16
16
17 CONTEXT_LASTUPDATE = "last_update"
17 CONTEXT_LASTUPDATE = "last_update"
18 CONTEXT_THREAD = 'thread'
18 CONTEXT_THREAD = 'thread'
19 CONTEXT_WS_TOKEN = 'ws_token'
19 CONTEXT_WS_TOKEN = 'ws_token'
20 CONTEXT_WS_PROJECT = 'ws_project'
20 CONTEXT_WS_PROJECT = 'ws_project'
21 CONTEXT_WS_HOST = 'ws_host'
21 CONTEXT_WS_HOST = 'ws_host'
22 CONTEXT_WS_PORT = 'ws_port'
22 CONTEXT_WS_PORT = 'ws_port'
23 CONTEXT_WS_TIME = 'ws_token_time'
23 CONTEXT_WS_TIME = 'ws_token_time'
24 CONTEXT_MODE = 'mode'
24 CONTEXT_MODE = 'mode'
25 CONTEXT_OP = 'opening_post'
25
26
26 FORM_TITLE = 'title'
27 FORM_TITLE = 'title'
27 FORM_TEXT = 'text'
28 FORM_TEXT = 'text'
28 FORM_IMAGE = 'image'
29 FORM_IMAGE = 'image'
29 FORM_THREADS = 'threads'
30 FORM_THREADS = 'threads'
30
31
31
32
32 class ThreadView(BaseBoardView, PostMixin, FormMixin):
33 class ThreadView(BaseBoardView, PostMixin, FormMixin):
33
34
34 def get(self, request, post_id, form: PostForm=None):
35 def get(self, request, post_id, form: PostForm=None):
35 try:
36 try:
36 opening_post = Post.objects.get(id=post_id)
37 opening_post = Post.objects.get(id=post_id)
37 except ObjectDoesNotExist:
38 except ObjectDoesNotExist:
38 raise Http404
39 raise Http404
39
40
40 # If this is not OP, don't show it as it is
41 # If this is not OP, don't show it as it is
41 if not opening_post.is_opening():
42 if not opening_post.is_opening():
42 return redirect(opening_post.get_thread().get_opening_post()
43 return redirect(opening_post.get_thread().get_opening_post()
43 .get_absolute_url())
44 .get_absolute_url())
44
45
45 if not form:
46 if not form:
46 form = PostForm(error_class=PlainErrorList)
47 form = PostForm(error_class=PlainErrorList)
47
48
48 thread_to_show = opening_post.get_thread()
49 thread_to_show = opening_post.get_thread()
49
50
50 params = dict()
51 params = dict()
51
52
52 params[CONTEXT_FORM] = form
53 params[CONTEXT_FORM] = form
53 params[CONTEXT_LASTUPDATE] = str(thread_to_show.last_edit_time)
54 params[CONTEXT_LASTUPDATE] = str(thread_to_show.last_edit_time)
54 params[CONTEXT_THREAD] = thread_to_show
55 params[CONTEXT_THREAD] = thread_to_show
55 params[CONTEXT_MODE] = self.get_mode()
56 params[CONTEXT_MODE] = self.get_mode()
57 params[CONTEXT_OP] = opening_post
56
58
57 if settings.get_bool('External', 'WebsocketsEnabled'):
59 if settings.get_bool('External', 'WebsocketsEnabled'):
58 token_time = format(timezone.now(), u'U')
60 token_time = format(timezone.now(), u'U')
59
61
60 params[CONTEXT_WS_TIME] = token_time
62 params[CONTEXT_WS_TIME] = token_time
61 params[CONTEXT_WS_TOKEN] = utils.get_websocket_token(
63 params[CONTEXT_WS_TOKEN] = utils.get_websocket_token(
62 timestamp=token_time)
64 timestamp=token_time)
63 params[CONTEXT_WS_PROJECT] = neboard.settings.CENTRIFUGE_PROJECT_ID
65 params[CONTEXT_WS_PROJECT] = neboard.settings.CENTRIFUGE_PROJECT_ID
64 params[CONTEXT_WS_HOST] = request.get_host().split(':')[0]
66 params[CONTEXT_WS_HOST] = request.get_host().split(':')[0]
65 params[CONTEXT_WS_PORT] = neboard.settings.CENTRIFUGE_PORT
67 params[CONTEXT_WS_PORT] = neboard.settings.CENTRIFUGE_PORT
66
68
67 params.update(self.get_data(thread_to_show))
69 params.update(self.get_data(thread_to_show))
68
70
69 return render(request, self.get_template(), params)
71 return render(request, self.get_template(), params)
70
72
71 def post(self, request, post_id):
73 def post(self, request, post_id):
72 opening_post = get_object_or_404(Post, id=post_id)
74 opening_post = get_object_or_404(Post, id=post_id)
73
75
74 # If this is not OP, don't show it as it is
76 # If this is not OP, don't show it as it is
75 if not opening_post.is_opening():
77 if not opening_post.is_opening():
76 raise Http404
78 raise Http404
77
79
78 if not opening_post.get_thread().archived:
80 if not opening_post.get_thread().archived:
79 form = PostForm(request.POST, request.FILES,
81 form = PostForm(request.POST, request.FILES,
80 error_class=PlainErrorList)
82 error_class=PlainErrorList)
81 form.session = request.session
83 form.session = request.session
82
84
83 if form.is_valid():
85 if form.is_valid():
84 return self.new_post(request, form, opening_post)
86 return self.new_post(request, form, opening_post)
85 if form.need_to_ban:
87 if form.need_to_ban:
86 # Ban user because he is suspected to be a bot
88 # Ban user because he is suspected to be a bot
87 self._ban_current_user(request)
89 self._ban_current_user(request)
88
90
89 return self.get(request, post_id, form)
91 return self.get(request, post_id, form)
90
92
91 def new_post(self, request, form: PostForm, opening_post: Post=None,
93 def new_post(self, request, form: PostForm, opening_post: Post=None,
92 html_response=True):
94 html_response=True):
93 """
95 """
94 Adds a new post (in thread or as a reply).
96 Adds a new post (in thread or as a reply).
95 """
97 """
96
98
97 ip = utils.get_client_ip(request)
99 ip = utils.get_client_ip(request)
98
100
99 data = form.cleaned_data
101 data = form.cleaned_data
100
102
101 title = data[FORM_TITLE]
103 title = data[FORM_TITLE]
102 text = data[FORM_TEXT]
104 text = data[FORM_TEXT]
103 image = form.get_image()
105 image = form.get_image()
104 threads = data[FORM_THREADS]
106 threads = data[FORM_THREADS]
105
107
106 text = self._remove_invalid_links(text)
108 text = self._remove_invalid_links(text)
107
109
108 post_thread = opening_post.get_thread()
110 post_thread = opening_post.get_thread()
109
111
110 post = Post.objects.create_post(title=title, text=text, image=image,
112 post = Post.objects.create_post(title=title, text=text, image=image,
111 thread=post_thread, ip=ip,
113 thread=post_thread, ip=ip,
112 threads=threads)
114 threads=threads)
113 post.notify_clients()
115 post.notify_clients()
114
116
115 if html_response:
117 if html_response:
116 if opening_post:
118 if opening_post:
117 return redirect(post.get_absolute_url())
119 return redirect(post.get_absolute_url())
118 else:
120 else:
119 return post
121 return post
120
122
121 def get_data(self, thread):
123 def get_data(self, thread) -> dict:
122 """
124 """
123 Returns context params for the view.
125 Returns context params for the view.
124 """
126 """
125
127
126 pass
128 return dict()
127
129
128 def get_template(self):
130 def get_template(self) -> str:
129 """
131 """
130 Gets template to show the thread mode on.
132 Gets template to show the thread mode on.
131 """
133 """
132
134
133 pass
135 pass
134
136
135 def get_mode(self):
137 def get_mode(self) -> str:
136 pass
138 pass
@@ -1,27 +1,12 b''
1 from boards import settings
2 from boards.views.thread import ThreadView
1 from boards.views.thread import ThreadView
3
2
4 TEMPLATE_TREE = 'boards/thread_tree.html'
3 TEMPLATE_TREE = 'boards/thread_tree.html'
5
4
6 CONTEXT_OP = 'opening_post'
7 CONTEXT_BUMPABLE = 'bumpable'
8
9
5
10 class TreeThreadView(ThreadView):
6 class TreeThreadView(ThreadView):
11
7
12 def get_template(self):
8 def get_template(self):
13 return TEMPLATE_TREE
9 return TEMPLATE_TREE
14
10
15 def get_data(self, thread):
16 params = dict()
17
18 bumpable = thread.can_bump()
19 params[CONTEXT_BUMPABLE] = bumpable
20 max_posts = thread.max_posts
21
22 params[CONTEXT_OP] = thread.get_opening_post()
23
24 return params
25
26 def get_mode(self):
11 def get_mode(self):
27 return 'tree'
12 return 'tree'
General Comments 0
You need to be logged in to leave comments. Login now