Show More
@@ -1,22 +1,22 b'' | |||
|
1 | from django.core.paginator import Paginator | |
|
1 | __author__ = 'neko259' | |
|
2 | 2 | |
|
3 | __author__ = 'neko259' | |
|
3 | from django.core.paginator import Paginator | |
|
4 | 4 | |
|
5 | 5 | PAGINATOR_LOOKAROUND_SIZE = 3 |
|
6 | 6 | |
|
7 | 7 | |
|
8 | 8 | def get_paginator(*args, **kwargs): |
|
9 | 9 | return DividedPaginator(*args, **kwargs) |
|
10 | 10 | |
|
11 | 11 | |
|
12 | 12 | class DividedPaginator(Paginator): |
|
13 | 13 | |
|
14 | 14 | lookaround_size = PAGINATOR_LOOKAROUND_SIZE |
|
15 | 15 | current_page = 0 |
|
16 | 16 | |
|
17 | 17 | def center_range(self): |
|
18 | 18 | index = self.page_range.index(self.current_page) |
|
19 | 19 | |
|
20 | 20 | start = max(0, index - self.lookaround_size) |
|
21 | 21 | end = min(len(self.page_range), index + self.lookaround_size + 1) |
|
22 | 22 | return self.page_range[start:end] No newline at end of file |
@@ -1,133 +1,138 b'' | |||
|
1 | 1 | import string |
|
2 | 2 | |
|
3 | 3 | from django.db import transaction |
|
4 | 4 | from django.shortcuts import render, redirect |
|
5 | 5 | |
|
6 | 6 | from boards import utils, settings |
|
7 | 7 | from boards.abstracts.paginator import get_paginator |
|
8 | 8 | from boards.forms import ThreadForm, PlainErrorList |
|
9 | 9 | from boards.models import Post, Thread, Ban, Tag |
|
10 | 10 | from boards.views.banned import BannedView |
|
11 |
from boards.views.base import BaseBoardView, |
|
|
11 | from boards.views.base import BaseBoardView, CONTEXT_FORM | |
|
12 | 12 | from boards.views.posting_mixin import PostMixin |
|
13 | 13 | |
|
14 | FORM_TAGS = 'tags' | |
|
15 | FORM_TEXT = 'text' | |
|
16 | FORM_TITLE = 'title' | |
|
17 | FORM_IMAGE = 'image' | |
|
18 | ||
|
14 | 19 | TAG_DELIMITER = ' ' |
|
15 | 20 | |
|
16 | 21 | PARAMETER_CURRENT_PAGE = 'current_page' |
|
17 | 22 | PARAMETER_PAGINATOR = 'paginator' |
|
18 | 23 | PARAMETER_THREADS = 'threads' |
|
19 | 24 | |
|
20 | 25 | TEMPLATE = 'boards/posting_general.html' |
|
21 | 26 | DEFAULT_PAGE = 1 |
|
22 | 27 | |
|
23 | 28 | |
|
24 | 29 | class AllThreadsView(PostMixin, BaseBoardView): |
|
25 | 30 | |
|
26 | 31 | user = None |
|
27 | 32 | |
|
28 | 33 | def get(self, request, page=DEFAULT_PAGE, form=None): |
|
29 | 34 | context = self.get_context_data(request=request) |
|
30 | 35 | |
|
31 | 36 | self.user = utils.get_user(request) |
|
32 | 37 | |
|
33 | 38 | if not form: |
|
34 | 39 | form = ThreadForm(error_class=PlainErrorList) |
|
35 | 40 | |
|
36 | 41 | paginator = get_paginator(self.get_threads(), |
|
37 | 42 | settings.THREADS_PER_PAGE) |
|
38 | 43 | paginator.current_page = int(page) |
|
39 | 44 | |
|
40 | 45 | threads = paginator.page(page).object_list |
|
41 | 46 | |
|
42 | 47 | context[PARAMETER_THREADS] = threads |
|
43 |
context[ |
|
|
48 | context[CONTEXT_FORM] = form | |
|
44 | 49 | |
|
45 | 50 | self._get_page_context(paginator, context, page) |
|
46 | 51 | |
|
47 | 52 | return render(request, TEMPLATE, context) |
|
48 | 53 | |
|
49 | 54 | def post(self, request, page=DEFAULT_PAGE): |
|
50 | 55 | form = ThreadForm(request.POST, request.FILES, |
|
51 | 56 | error_class=PlainErrorList) |
|
52 | 57 | form.session = request.session |
|
53 | 58 | |
|
54 | 59 | if form.is_valid(): |
|
55 | 60 | return self.create_thread(request, form) |
|
56 | 61 | if form.need_to_ban: |
|
57 | 62 | # Ban user because he is suspected to be a bot |
|
58 | 63 | self._ban_current_user(request) |
|
59 | 64 | |
|
60 | 65 | return self.get(request, page, form) |
|
61 | 66 | |
|
62 | 67 | @staticmethod |
|
63 | 68 | def _get_page_context(paginator, context, page): |
|
64 | 69 | """ |
|
65 | 70 | Get pagination context variables |
|
66 | 71 | """ |
|
67 | 72 | |
|
68 | 73 | context[PARAMETER_PAGINATOR] = paginator |
|
69 | 74 | context[PARAMETER_CURRENT_PAGE] = paginator.page(int(page)) |
|
70 | 75 | |
|
71 | 76 | @staticmethod |
|
72 | 77 | def parse_tags_string(tag_strings): |
|
73 | 78 | """ |
|
74 | 79 | Parses tag list string and returns tag object list. |
|
75 | 80 | """ |
|
76 | 81 | |
|
77 | 82 | tags = [] |
|
78 | 83 | |
|
79 | 84 | if tag_strings: |
|
80 | 85 | tag_strings = tag_strings.split(TAG_DELIMITER) |
|
81 | 86 | for tag_name in tag_strings: |
|
82 | 87 | tag_name = string.lower(tag_name.strip()) |
|
83 | 88 | if len(tag_name) > 0: |
|
84 | 89 | tag, created = Tag.objects.get_or_create(name=tag_name) |
|
85 | 90 | tags.append(tag) |
|
86 | 91 | |
|
87 | 92 | return tags |
|
88 | 93 | |
|
89 | 94 | @transaction.atomic |
|
90 | 95 | def create_thread(self, request, form, html_response=True): |
|
91 | 96 | """ |
|
92 | 97 | Creates a new thread with an opening post. |
|
93 | 98 | """ |
|
94 | 99 | |
|
95 | 100 | ip = utils.get_client_ip(request) |
|
96 | 101 | is_banned = Ban.objects.filter(ip=ip).exists() |
|
97 | 102 | |
|
98 | 103 | if is_banned: |
|
99 | 104 | if html_response: |
|
100 | 105 | return redirect(BannedView().as_view()) |
|
101 | 106 | else: |
|
102 | 107 | return |
|
103 | 108 | |
|
104 | 109 | data = form.cleaned_data |
|
105 | 110 | |
|
106 |
title = data[ |
|
|
107 |
text = data[ |
|
|
111 | title = data[FORM_TITLE] | |
|
112 | text = data[FORM_TEXT] | |
|
108 | 113 | |
|
109 | 114 | text = self._remove_invalid_links(text) |
|
110 | 115 | |
|
111 |
if |
|
|
112 |
image = data[ |
|
|
116 | if FORM_IMAGE in data.keys(): | |
|
117 | image = data[FORM_IMAGE] | |
|
113 | 118 | else: |
|
114 | 119 | image = None |
|
115 | 120 | |
|
116 |
tag_strings = data[ |
|
|
121 | tag_strings = data[FORM_TAGS] | |
|
117 | 122 | |
|
118 | 123 | tags = self.parse_tags_string(tag_strings) |
|
119 | 124 | |
|
120 | 125 | post = Post.objects.create_post(title=title, text=text, ip=ip, |
|
121 | 126 | image=image, tags=tags, |
|
122 | 127 | user=utils.get_user(request)) |
|
123 | 128 | |
|
124 | 129 | if html_response: |
|
125 | 130 | return redirect(post.get_url()) |
|
126 | 131 | |
|
127 | 132 | def get_threads(self): |
|
128 | 133 | """ |
|
129 | 134 | Gets list of threads that will be shown on a page. |
|
130 | 135 | """ |
|
131 | 136 | |
|
132 | 137 | return Thread.objects.all().order_by('-bump_time')\ |
|
133 | 138 | .exclude(tags__in=self.user.hidden_tags.all()) |
@@ -1,35 +1,35 b'' | |||
|
1 | 1 | from django.db import transaction |
|
2 | 2 | from django.template import RequestContext |
|
3 | 3 | from django.views.generic import View |
|
4 | 4 | |
|
5 | 5 | from boards import utils |
|
6 | 6 | from boards.models.user import Ban |
|
7 | 7 | |
|
8 | 8 | |
|
9 | 9 | BAN_REASON_SPAM = 'Autoban: spam bot' |
|
10 | 10 | |
|
11 |
|
|
|
11 | CONTEXT_FORM = 'form' | |
|
12 | 12 | |
|
13 | 13 | |
|
14 | 14 | class BaseBoardView(View): |
|
15 | 15 | |
|
16 | 16 | def get_context_data(self, **kwargs): |
|
17 | 17 | request = kwargs['request'] |
|
18 | 18 | # context = self._default_context(request) |
|
19 | 19 | context = RequestContext(request) |
|
20 | 20 | |
|
21 | 21 | return context |
|
22 | 22 | |
|
23 | 23 | @transaction.atomic |
|
24 | 24 | def _ban_current_user(self, request): |
|
25 | 25 | """ |
|
26 | 26 | Add current user to the IP ban list |
|
27 | 27 | """ |
|
28 | 28 | |
|
29 | 29 | ip = utils.get_client_ip(request) |
|
30 | 30 | ban, created = Ban.objects.get_or_create(ip=ip) |
|
31 | 31 | if created: |
|
32 | 32 | ban.can_read = False |
|
33 | 33 | ban.reason = BAN_REASON_SPAM |
|
34 | 34 | ban.save() |
|
35 | 35 |
@@ -1,30 +1,30 b'' | |||
|
1 | 1 | from django.shortcuts import render, redirect |
|
2 | 2 | from boards.forms import LoginForm, PlainErrorList |
|
3 | 3 | from boards.models import User |
|
4 |
from boards.views.base import BaseBoardView, |
|
|
4 | from boards.views.base import BaseBoardView, CONTEXT_FORM | |
|
5 | 5 | |
|
6 | 6 | __author__ = 'neko259' |
|
7 | 7 | |
|
8 | 8 | |
|
9 | 9 | class LoginView(BaseBoardView): |
|
10 | 10 | |
|
11 | 11 | def get(self, request, form=None): |
|
12 | 12 | context = self.get_context_data(request=request) |
|
13 | 13 | |
|
14 | 14 | if not form: |
|
15 | 15 | form = LoginForm() |
|
16 |
context[ |
|
|
16 | context[CONTEXT_FORM] = form | |
|
17 | 17 | |
|
18 | 18 | return render(request, 'boards/login.html', context) |
|
19 | 19 | |
|
20 | 20 | def post(self, request): |
|
21 | 21 | form = LoginForm(request.POST, request.FILES, |
|
22 | 22 | error_class=PlainErrorList) |
|
23 | 23 | form.session = request.session |
|
24 | 24 | |
|
25 | 25 | if form.is_valid(): |
|
26 | 26 | user = User.objects.get(user_id=form.cleaned_data['user_id']) |
|
27 | 27 | request.session['user_id'] = user.id |
|
28 | 28 | return redirect('index') |
|
29 | 29 | else: |
|
30 | 30 | return self.get(request, form) |
@@ -1,53 +1,53 b'' | |||
|
1 | 1 | from django.db import transaction |
|
2 | 2 | from django.shortcuts import render, redirect |
|
3 | 3 | from boards import utils |
|
4 | 4 | |
|
5 |
from boards.views.base import BaseBoardView, |
|
|
5 | from boards.views.base import BaseBoardView, CONTEXT_FORM | |
|
6 | 6 | from boards.forms import SettingsForm, ModeratorSettingsForm, PlainErrorList |
|
7 | 7 | from boards.models.post import SETTING_MODERATE |
|
8 | 8 | |
|
9 | 9 | |
|
10 | 10 | class SettingsView(BaseBoardView): |
|
11 | 11 | |
|
12 | 12 | def get(self, request): |
|
13 | 13 | context = self.get_context_data(request=request) |
|
14 | 14 | user = utils.get_user(request) |
|
15 | 15 | is_moderator = user.is_moderator() |
|
16 | 16 | |
|
17 | 17 | selected_theme = utils.get_theme(request, user) |
|
18 | 18 | |
|
19 | 19 | if is_moderator: |
|
20 | 20 | form = ModeratorSettingsForm(initial={ |
|
21 | 21 | 'theme': selected_theme, |
|
22 | 22 | 'moderate': user.get_setting(SETTING_MODERATE) and \ |
|
23 | 23 | user.is_moderator() |
|
24 | 24 | }, error_class=PlainErrorList) |
|
25 | 25 | else: |
|
26 | 26 | form = SettingsForm(initial={'theme': selected_theme}, |
|
27 | 27 | error_class=PlainErrorList) |
|
28 | 28 | |
|
29 |
context[ |
|
|
29 | context[CONTEXT_FORM] = form | |
|
30 | 30 | |
|
31 | 31 | return render(request, 'boards/settings.html', context) |
|
32 | 32 | |
|
33 | 33 | def post(self, request): |
|
34 | 34 | user = utils.get_user(request) |
|
35 | 35 | is_moderator = user.is_moderator() |
|
36 | 36 | |
|
37 | 37 | with transaction.atomic(): |
|
38 | 38 | if is_moderator: |
|
39 | 39 | form = ModeratorSettingsForm(request.POST, |
|
40 | 40 | error_class=PlainErrorList) |
|
41 | 41 | else: |
|
42 | 42 | form = SettingsForm(request.POST, error_class=PlainErrorList) |
|
43 | 43 | |
|
44 | 44 | if form.is_valid(): |
|
45 | 45 | selected_theme = form.cleaned_data['theme'] |
|
46 | 46 | |
|
47 | 47 | user.save_setting('theme', selected_theme) |
|
48 | 48 | |
|
49 | 49 | if is_moderator: |
|
50 | 50 | moderate = form.cleaned_data['moderate'] |
|
51 | 51 | user.save_setting(SETTING_MODERATE, moderate) |
|
52 | 52 | |
|
53 | 53 | return redirect('settings') |
@@ -1,132 +1,144 b'' | |||
|
1 | 1 | from django.core.urlresolvers import reverse |
|
2 | 2 | from django.db import transaction |
|
3 | 3 | from django.http import Http404 |
|
4 | 4 | from django.shortcuts import get_object_or_404, render, redirect |
|
5 | 5 | from django.views.generic.edit import FormMixin |
|
6 | 6 | |
|
7 | 7 | from boards import utils, settings |
|
8 | 8 | from boards.forms import PostForm, PlainErrorList |
|
9 | 9 | from boards.models import Post, Ban |
|
10 | 10 | from boards.views.banned import BannedView |
|
11 |
from boards.views.base import BaseBoardView, |
|
|
11 | from boards.views.base import BaseBoardView, CONTEXT_FORM | |
|
12 | 12 | from boards.views.posting_mixin import PostMixin |
|
13 | 13 | |
|
14 | TEMPLATE_GALLERY = 'boards/thread_gallery.html' | |
|
15 | TEMPLATE_NORMAL = 'boards/thread.html' | |
|
16 | ||
|
17 | CONTEXT_POSTS = 'posts' | |
|
18 | CONTEXT_OP = 'opening_post' | |
|
19 | CONTEXT_BUMPLIMIT_PRG = 'bumplimit_progress' | |
|
20 | CONTEXT_POSTS_LEFT = 'posts_left' | |
|
21 | CONTEXT_LASTUPDATE = "last_update" | |
|
22 | CONTEXT_MAX_REPLIES = 'max_replies' | |
|
23 | CONTEXT_THREAD = 'thread' | |
|
24 | CONTEXT_BUMPABLE = 'bumpable' | |
|
25 | ||
|
26 | FORM_TITLE = 'title' | |
|
27 | FORM_TEXT = 'text' | |
|
28 | FORM_IMAGE = 'image' | |
|
14 | 29 | |
|
15 | 30 | MODE_GALLERY = 'gallery' |
|
16 | 31 | MODE_NORMAL = 'normal' |
|
17 | 32 | |
|
18 | PARAMETER_MAX_REPLIES = 'max_replies' | |
|
19 | PARAMETER_THREAD = 'thread' | |
|
20 | PARAMETER_BUMPABLE = 'bumpable' | |
|
21 | ||
|
22 | 33 | |
|
23 | 34 | class ThreadView(BaseBoardView, PostMixin, FormMixin): |
|
24 | 35 | |
|
25 | 36 | def get(self, request, post_id, mode=MODE_NORMAL, form=None): |
|
26 | 37 | try: |
|
27 | 38 | opening_post = Post.objects.filter(id=post_id).only('thread_new')[0] |
|
28 | 39 | except IndexError: |
|
29 | 40 | raise Http404 |
|
30 | 41 | |
|
31 | 42 | # If this is not OP, don't show it as it is |
|
32 | 43 | if not opening_post or not opening_post.is_opening(): |
|
33 | 44 | raise Http404 |
|
34 | 45 | |
|
35 | 46 | if not form: |
|
36 | 47 | form = PostForm(error_class=PlainErrorList) |
|
37 | 48 | |
|
38 | 49 | thread_to_show = opening_post.get_thread() |
|
39 | 50 | |
|
40 | 51 | context = self.get_context_data(request=request) |
|
41 | 52 | |
|
42 |
context[ |
|
|
43 |
context[ |
|
|
53 | context[CONTEXT_FORM] = form | |
|
54 | context[CONTEXT_LASTUPDATE] = utils.datetime_to_epoch( | |
|
44 | 55 | thread_to_show.last_edit_time) |
|
45 |
context[ |
|
|
46 |
context[ |
|
|
56 | context[CONTEXT_THREAD] = thread_to_show | |
|
57 | context[CONTEXT_MAX_REPLIES] = settings.MAX_POSTS_PER_THREAD | |
|
47 | 58 | |
|
48 | 59 | if MODE_NORMAL == mode: |
|
49 |
|
|
|
50 |
|
|
|
51 | context['posts_left'] = settings.MAX_POSTS_PER_THREAD \ | |
|
52 | - thread_to_show.get_reply_count() | |
|
53 | context['bumplimit_progress'] = str( | |
|
54 | float(context['posts_left']) / | |
|
55 | settings.MAX_POSTS_PER_THREAD * 100) | |
|
60 | bumpable = thread_to_show.can_bump() | |
|
61 | context[CONTEXT_BUMPABLE] = bumpable | |
|
62 | if bumpable: | |
|
63 | left_posts = settings.MAX_POSTS_PER_THREAD \ | |
|
64 | - thread_to_show.get_reply_count() | |
|
65 | context[CONTEXT_POSTS_LEFT] = left_posts | |
|
66 | context[CONTEXT_BUMPLIMIT_PRG] = str( | |
|
67 | float(left_posts) / settings.MAX_POSTS_PER_THREAD * 100) | |
|
56 | 68 | |
|
57 |
context[ |
|
|
69 | context[CONTEXT_OP] = opening_post | |
|
58 | 70 | |
|
59 | document = 'boards/thread.html' | |
|
71 | document = TEMPLATE_NORMAL | |
|
60 | 72 | elif MODE_GALLERY == mode: |
|
61 |
context[ |
|
|
73 | context[CONTEXT_POSTS] = thread_to_show.get_replies_with_images( | |
|
62 | 74 | view_fields_only=True) |
|
63 | 75 | |
|
64 | document = 'boards/thread_gallery.html' | |
|
76 | document = TEMPLATE_GALLERY | |
|
65 | 77 | else: |
|
66 | 78 | raise Http404 |
|
67 | 79 | |
|
68 | 80 | return render(request, document, context) |
|
69 | 81 | |
|
70 | 82 | def post(self, request, post_id, mode=MODE_NORMAL): |
|
71 | 83 | opening_post = get_object_or_404(Post, id=post_id) |
|
72 | 84 | |
|
73 | 85 | # If this is not OP, don't show it as it is |
|
74 | 86 | if not opening_post.is_opening(): |
|
75 | 87 | raise Http404 |
|
76 | 88 | |
|
77 | 89 | if not opening_post.get_thread().archived: |
|
78 | 90 | form = PostForm(request.POST, request.FILES, |
|
79 | 91 | error_class=PlainErrorList) |
|
80 | 92 | form.session = request.session |
|
81 | 93 | |
|
82 | 94 | if form.is_valid(): |
|
83 | 95 | return self.new_post(request, form, opening_post) |
|
84 | 96 | if form.need_to_ban: |
|
85 | 97 | # Ban user because he is suspected to be a bot |
|
86 | 98 | self._ban_current_user(request) |
|
87 | 99 | |
|
88 | 100 | return self.get(request, post_id, mode, form) |
|
89 | 101 | |
|
90 | 102 | @transaction.atomic |
|
91 | 103 | def new_post(self, request, form, opening_post=None, html_response=True): |
|
92 | 104 | """Add a new post (in thread or as a reply).""" |
|
93 | 105 | |
|
94 | 106 | ip = utils.get_client_ip(request) |
|
95 | 107 | is_banned = Ban.objects.filter(ip=ip).exists() |
|
96 | 108 | |
|
97 | 109 | if is_banned: |
|
98 | 110 | if html_response: |
|
99 | 111 | return redirect(BannedView().as_view()) |
|
100 | 112 | else: |
|
101 | 113 | return None |
|
102 | 114 | |
|
103 | 115 | data = form.cleaned_data |
|
104 | 116 | |
|
105 |
title = data[ |
|
|
106 |
text = data[ |
|
|
117 | title = data[FORM_TITLE] | |
|
118 | text = data[FORM_TEXT] | |
|
107 | 119 | |
|
108 | 120 | text = self._remove_invalid_links(text) |
|
109 | 121 | |
|
110 |
if |
|
|
111 |
image = data[ |
|
|
122 | if FORM_IMAGE in data.keys(): | |
|
123 | image = data[FORM_IMAGE] | |
|
112 | 124 | else: |
|
113 | 125 | image = None |
|
114 | 126 | |
|
115 | 127 | tags = [] |
|
116 | 128 | |
|
117 | 129 | post_thread = opening_post.get_thread() |
|
118 | 130 | |
|
119 | 131 | post = Post.objects.create_post(title=title, text=text, ip=ip, |
|
120 | 132 | thread=post_thread, image=image, |
|
121 | 133 | tags=tags, |
|
122 | 134 | user=utils.get_user(request)) |
|
123 | 135 | |
|
124 | 136 | thread_to_show = (opening_post.id if opening_post else post.id) |
|
125 | 137 | |
|
126 | 138 | if html_response: |
|
127 | 139 | if opening_post: |
|
128 |
return redirect( |
|
|
129 | 'thread', | |
|
130 |
|
|
|
140 | return redirect( | |
|
141 | reverse('thread', kwargs={'post_id': thread_to_show}) | |
|
142 | + '#' + str(post.id)) | |
|
131 | 143 | else: |
|
132 | 144 | return post |
General Comments 0
You need to be logged in to leave comments.
Login now