##// END OF EJS Templates
Implemented search over posts. Moved get_user and get_theme to utils module. Use context processors instead of creating context in the base view. Removed unused imports in some modules
neko259 -
r690:a8dffe47 1.8-dev
parent child Browse files
Show More
@@ -0,0 +1,31 b''
1 from boards import utils
2 from boards.models import Post
3 from boards.models.post import SETTING_MODERATE
4 import neboard
5
6 __author__ = 'neko259'
7
8
9 def user_and_ui_processor(request):
10 context = {}
11
12 user = utils.get_user(request)
13 context['user'] = user
14 context['tags'] = user.fav_tags.all()
15 context['posts_per_day'] = float(Post.objects.get_posts_per_day())
16
17 theme = utils.get_theme(request, user)
18 context['theme'] = theme
19 context['theme_css'] = 'css/' + theme + '/base_page.css'
20
21 # This shows the moderator panel
22 moderate = user.get_setting(SETTING_MODERATE)
23 if moderate == 'True':
24 context['moderator'] = user.is_moderator()
25 else:
26 context['moderator'] = False
27
28 context['version'] = neboard.settings.VERSION
29 context['site_name'] = neboard.settings.SITE_NAME
30
31 return context No newline at end of file
@@ -0,0 +1,14 b''
1 from haystack import indexes
2 from boards.models import Post
3
4 __author__ = 'neko259'
5
6
7 class PostIndex(indexes.SearchIndex, indexes.Indexable):
8 text = indexes.CharField(document=True, use_template=True)
9
10 def get_model(self):
11 return Post
12
13 def index_queryset(self, using=None):
14 return self.get_model().objects.all() No newline at end of file
@@ -0,0 +1,2 b''
1 {{ object.title }}
2 {{ object.text }} No newline at end of file
@@ -0,0 +1,29 b''
1 {% extends 'boards/base.html' %}
2
3 {% load board %}
4 {% load i18n %}
5
6 {% block content %}
7 <div class="post-form-w">
8 <h3>{% trans 'Search' %}</h3>
9 <form method="get" action=".">
10 {{ form.as_p }}
11 <input type="submit" value="{% trans 'Search' %}">
12 </form>
13 </div>
14
15 {% if query %}
16 {% for result in page.object_list %}
17 {% post_view result.object %}
18 {% empty %}
19 <p>{% trans 'No results found.' %}</p>
20 {% endfor %}
21
22 {% if page.has_previous or page.has_next %}
23 <div>
24 {% if page.has_previous %}<a href="?q={{ query }}&amp;page={{ page.previous_page_number }}">{% endif %}&laquo; {% trans 'Previous' %}{% if page.has_previous %}</a>{% endif %}
25 {% if page.has_next %}<a href="?q={{ query }}&amp;page= {{ page.next_page_number }}">{% endif %}{% trans 'Next' %} &raquo; {% if page.has_next %}</a>{% endif %}
26 </div>
27 {% endif %}
28 {% endif %}
29 {% endblock %} No newline at end of file
1 NO CONTENT: modified file, binary diff hidden
NO CONTENT: modified file, binary diff hidden
@@ -7,7 +7,7 b' msgid ""'
7 msgstr ""
7 msgstr ""
8 "Project-Id-Version: PACKAGE VERSION\n"
8 "Project-Id-Version: PACKAGE VERSION\n"
9 "Report-Msgid-Bugs-To: \n"
9 "Report-Msgid-Bugs-To: \n"
10 "POT-Creation-Date: 2014-05-08 21:35+0300\n"
10 "POT-Creation-Date: 2014-06-15 12:34+0300\n"
11 "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
11 "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
12 "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
12 "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
13 "Language-Team: LANGUAGE <LL@li.org>\n"
13 "Language-Team: LANGUAGE <LL@li.org>\n"
@@ -98,27 +98,27 b' msgstr "\xd0\x9f\xd0\xbe\xd0\xb4\xd0\xbe\xd0\xb6\xd0\xb4\xd0\xb8\xd1\x82\xd0\xb5 %s \xd1\x81\xd0\xb5\xd0\xba\xd1\x83\xd0\xbd\xd0\xb4 \xd0\xbf\xd0\xbe\xd1\x81\xd0\xbb\xd0\xb5 \xd0\xbf\xd0\xbe\xd1\x81\xd0\xbb\xd0\xb5\xd0\xb4\xd0\xbd\xd0\xb5\xd0\xb3\xd0\xbe \xd0\xbf\xd0\xbe\xd1\x81\xd1\x82\xd0\xb8\xd0\xbd\xd0\xb3\xd0\xb0"'
98 msgid "Tags"
98 msgid "Tags"
99 msgstr "Π’Π΅Π³ΠΈ"
99 msgstr "Π’Π΅Π³ΠΈ"
100
100
101 #: forms.py:225 forms.py:344
101 #: forms.py:224 forms.py:343
102 msgid "Inappropriate characters in tags."
102 msgid "Inappropriate characters in tags."
103 msgstr "НСдопустимыС символы Π² Ρ‚Π΅Π³Π°Ρ…."
103 msgstr "НСдопустимыС символы Π² Ρ‚Π΅Π³Π°Ρ…."
104
104
105 #: forms.py:253 forms.py:274
105 #: forms.py:252 forms.py:273
106 msgid "Captcha validation failed"
106 msgid "Captcha validation failed"
107 msgstr "ΠŸΡ€ΠΎΠ²Π΅Ρ€ΠΊΠ° ΠΊΠ°ΠΏΡ‡ΠΈ ΠΏΡ€ΠΎΠ²Π°Π»Π΅Π½Π°"
107 msgstr "ΠŸΡ€ΠΎΠ²Π΅Ρ€ΠΊΠ° ΠΊΠ°ΠΏΡ‡ΠΈ ΠΏΡ€ΠΎΠ²Π°Π»Π΅Π½Π°"
108
108
109 #: forms.py:280
109 #: forms.py:279
110 msgid "Theme"
110 msgid "Theme"
111 msgstr "Π’Π΅ΠΌΠ°"
111 msgstr "Π’Π΅ΠΌΠ°"
112
112
113 #: forms.py:285
113 #: forms.py:284
114 msgid "Enable moderation panel"
114 msgid "Enable moderation panel"
115 msgstr "Π’ΠΊΠ»ΡŽΡ‡ΠΈΡ‚ΡŒ панСль ΠΌΠΎΠ΄Π΅Ρ€Π°Ρ†ΠΈΠΈ"
115 msgstr "Π’ΠΊΠ»ΡŽΡ‡ΠΈΡ‚ΡŒ панСль ΠΌΠΎΠ΄Π΅Ρ€Π°Ρ†ΠΈΠΈ"
116
116
117 #: forms.py:300
117 #: forms.py:299
118 msgid "No such user found"
118 msgid "No such user found"
119 msgstr "Π”Π°Π½Π½Ρ‹ΠΉ ΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚Π΅Π»ΡŒ Π½Π΅ Π½Π°ΠΉΠ΄Π΅Π½"
119 msgstr "Π”Π°Π½Π½Ρ‹ΠΉ ΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚Π΅Π»ΡŒ Π½Π΅ Π½Π°ΠΉΠ΄Π΅Π½"
120
120
121 #: forms.py:314
121 #: forms.py:313
122 #, python-format
122 #, python-format
123 msgid "Wait %s minutes after last login"
123 msgid "Wait %s minutes after last login"
124 msgstr "ΠŸΠΎΠ΄ΠΎΠΆΠ΄ΠΈΡ‚Π΅ %s ΠΌΠΈΠ½ΡƒΡ‚ послС послСднСго Π²Ρ…ΠΎΠ΄Π°"
124 msgstr "ΠŸΠΎΠ΄ΠΎΠΆΠ΄ΠΈΡ‚Π΅ %s ΠΌΠΈΠ½ΡƒΡ‚ послС послСднСго Π²Ρ…ΠΎΠ΄Π°"
@@ -147,41 +147,41 b' msgstr "\xd0\xbb\xd0\xb8\xd1\x86\xd0\xb5\xd0\xbd\xd0\xb7\xd0\xb8\xd0\xb5\xd0\xb9"'
147 msgid "Repository"
147 msgid "Repository"
148 msgstr "Π Π΅ΠΏΠΎΠ·ΠΈΡ‚ΠΎΡ€ΠΈΠΉ"
148 msgstr "Π Π΅ΠΏΠΎΠ·ΠΈΡ‚ΠΎΡ€ΠΈΠΉ"
149
149
150 #: templates/boards/base.html:14
150 #: templates/boards/base.html:11
151 msgid "Feed"
151 msgid "Feed"
152 msgstr "Π›Π΅Π½Ρ‚Π°"
152 msgstr "Π›Π΅Π½Ρ‚Π°"
153
153
154 #: templates/boards/base.html:31
154 #: templates/boards/base.html:28
155 msgid "All threads"
155 msgid "All threads"
156 msgstr "ВсС Ρ‚Π΅ΠΌΡ‹"
156 msgstr "ВсС Ρ‚Π΅ΠΌΡ‹"
157
157
158 #: templates/boards/base.html:36
158 #: templates/boards/base.html:33
159 msgid "Tag management"
159 msgid "Tag management"
160 msgstr "Π£ΠΏΡ€Π°Π²Π»Π΅Π½ΠΈΠ΅ Ρ‚Π΅Π³Π°ΠΌΠΈ"
160 msgstr "Π£ΠΏΡ€Π°Π²Π»Π΅Π½ΠΈΠ΅ Ρ‚Π΅Π³Π°ΠΌΠΈ"
161
161
162 #: templates/boards/base.html:38 templates/boards/settings.html:7
162 #: templates/boards/base.html:35 templates/boards/settings.html:7
163 msgid "Settings"
163 msgid "Settings"
164 msgstr "Настройки"
164 msgstr "Настройки"
165
165
166 #: templates/boards/base.html:50 templates/boards/login.html:6
166 #: templates/boards/base.html:47 templates/boards/login.html:6
167 #: templates/boards/login.html.py:21
167 #: templates/boards/login.html.py:16
168 msgid "Login"
168 msgid "Login"
169 msgstr "Π’Ρ…ΠΎΠ΄"
169 msgstr "Π’Ρ…ΠΎΠ΄"
170
170
171 #: templates/boards/base.html:52
171 #: templates/boards/base.html:48
172 msgid "Search"
173 msgstr "Поиск"
174
175 #: templates/boards/base.html:50
172 #, python-format
176 #, python-format
173 msgid "Speed: %(ppd)s posts per day"
177 msgid "Speed: %(ppd)s posts per day"
174 msgstr "Π‘ΠΊΠΎΡ€ΠΎΡΡ‚ΡŒ: %(ppd)s сообщСний Π² дСнь"
178 msgstr "Π‘ΠΊΠΎΡ€ΠΎΡΡ‚ΡŒ: %(ppd)s сообщСний Π² дСнь"
175
179
176 #: templates/boards/base.html:54
180 #: templates/boards/base.html:52
177 msgid "Up"
181 msgid "Up"
178 msgstr "Π’Π²Π΅Ρ€Ρ…"
182 msgstr "Π’Π²Π΅Ρ€Ρ…"
179
183
180 #: templates/boards/login.html:15
184 #: templates/boards/login.html:19
181 msgid "User ID"
182 msgstr "ID ΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚Π΅Π»Ρ"
183
184 #: templates/boards/login.html:24
185 msgid "Insert your user id above"
185 msgid "Insert your user id above"
186 msgstr "Π’ΡΡ‚Π°Π²ΡŒΡ‚Π΅ свой ID ΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚Π΅Π»Ρ Π²Ρ‹ΡˆΠ΅"
186 msgstr "Π’ΡΡ‚Π°Π²ΡŒΡ‚Π΅ свой ID ΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚Π΅Π»Ρ Π²Ρ‹ΡˆΠ΅"
187
187
@@ -386,5 +386,5 b' msgstr "\xd0\x9f\xd0\xb5\xd1\x80\xd0\xb5\xd0\xb4 \xd1\x8d\xd1\x82\xd0\xb8\xd0\xbc\xd0\xb8 \xd1\x82\xd0\xb5\xd0\xb3\xd0\xb0\xd0\xbc\xd0\xb8 \xd0\xbd\xd1\x83\xd0\xb6\xd0\xbd\xd0\xb0 \xd0\xbd\xd0\xbe\xd0\xb2\xd0\xb0\xd1\x8f \xd1\x81\xd1\x82\xd1\x80\xd0\xbe\xd0\xba\xd0\xb0:"'
386 msgid "Comment"
386 msgid "Comment"
387 msgstr "ΠšΠΎΠΌΠΌΠ΅Π½Ρ‚Π°Ρ€ΠΈΠΉ"
387 msgstr "ΠšΠΎΠΌΠΌΠ΅Π½Ρ‚Π°Ρ€ΠΈΠΉ"
388
388
389 #~ msgid "Archive"
389 #~ msgid "User ID"
390 #~ msgstr "Архив"
390 #~ msgstr "ID ΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚Π΅Π»Ρ"
@@ -1,8 +1,9 b''
1 from django.shortcuts import redirect
1 from django.shortcuts import redirect
2 from boards import views, utils
2 from boards import utils
3 from boards.models import Ban
3 from boards.models import Ban
4 from django.utils.html import strip_spaces_between_tags
4 from django.utils.html import strip_spaces_between_tags
5 from django.conf import settings
5 from django.conf import settings
6 from boards.views.banned import BannedView
6
7
7 RESPONSE_CONTENT_TYPE = 'Content-Type'
8 RESPONSE_CONTENT_TYPE = 'Content-Type'
8
9
@@ -17,7 +18,7 b' class BanMiddleware:'
17
18
18 def process_view(self, request, view_func, view_args, view_kwargs):
19 def process_view(self, request, view_func, view_args, view_kwargs):
19
20
20 if view_func != views.banned.BannedView.as_view:
21 if view_func != BannedView.as_view:
21 ip = utils.get_client_ip(request)
22 ip = utils.get_client_ip(request)
22 bans = Ban.objects.filter(ip=ip)
23 bans = Ban.objects.filter(ip=ip)
23
24
@@ -6,12 +6,9 b''
6 <!DOCTYPE html>
6 <!DOCTYPE html>
7 <html>
7 <html>
8 <head>
8 <head>
9 <link rel="stylesheet" type="text/css"
9 <link rel="stylesheet" type="text/css" href="{% static 'css/base.css' %}" media="all"/>
10 href="{% static 'css/base.css' %}" media="all"/>
10 <link rel="stylesheet" type="text/css" href="{% static theme_css %}" media="all"/>
11 <link rel="stylesheet" type="text/css"
11 <link rel="alternate" type="application/rss+xml" href="rss/" title="{% trans 'Feed' %}"/>
12 href="{% static theme_css %}" media="all"/>
13 <link rel="alternate" type="application/rss+xml" href="rss/" title=
14 "{% trans 'Feed' %}"/>
15
12
16 <link rel="icon" type="image/png"
13 <link rel="icon" type="image/png"
17 href="{% static 'favicon.png' %}">
14 href="{% static 'favicon.png' %}">
@@ -48,6 +45,7 b''
48 <div class="navigation_panel">
45 <div class="navigation_panel">
49 {% block metapanel %}{% endblock %}
46 {% block metapanel %}{% endblock %}
50 [<a href="{% url "login" %}">{% trans 'Login' %}</a>]
47 [<a href="{% url "login" %}">{% trans 'Login' %}</a>]
48 [<a href="{% url "haystack_search" %}">{% trans 'Search' %}</a>]
51 {% with ppd=posts_per_day|floatformat:2 %}
49 {% with ppd=posts_per_day|floatformat:2 %}
52 {% blocktrans %}Speed: {{ ppd }} posts per day{% endblocktrans %}
50 {% blocktrans %}Speed: {{ ppd }} posts per day{% endblocktrans %}
53 {% endwith %}
51 {% endwith %}
@@ -75,7 +75,10 b" urlpatterns = patterns('',"
75 name='get_thread'),
75 name='get_thread'),
76 url(r'^api/add_post/(?P<opening_post_id>\w+)/$', api.api_add_post,
76 url(r'^api/add_post/(?P<opening_post_id>\w+)/$', api.api_add_post,
77 name='add_post'),
77 name='add_post'),
78 url(r'api/get_tag_popularity/(?P<tag_name>\w+)$', api.get_tag_popularity,
78 url(r'^api/get_tag_popularity/(?P<tag_name>\w+)$', api.get_tag_popularity,
79 name='get_tag_popularity'),
79 name='get_tag_popularity'),
80
80
81 # Search
82 url(r'^search/', include('haystack.urls')),
83
81 )
84 )
@@ -1,10 +1,15 b''
1 """
1 """
2 This module contains helper functions and helper classes.
2 This module contains helper functions and helper classes.
3 """
3 """
4 import hashlib
4 from django.utils import timezone
5 from django.utils import timezone
5
6
6 from neboard import settings
7 from neboard import settings
8 from boards.models import Post, User
9 from boards.models.post import SETTING_MODERATE
10 from boards.models.user import RANK_USER
7 import time
11 import time
12 import neboard
8
13
9
14
10 KEY_CAPTCHA_FAILS = 'key_captcha_fails'
15 KEY_CAPTCHA_FAILS = 'key_captcha_fails'
@@ -77,4 +82,48 b' def get_client_ip(request):'
77 def datetime_to_epoch(datetime):
82 def datetime_to_epoch(datetime):
78 return int(time.mktime(timezone.localtime(
83 return int(time.mktime(timezone.localtime(
79 datetime,timezone.get_current_timezone()).timetuple())
84 datetime,timezone.get_current_timezone()).timetuple())
80 * 1000000 + datetime.microsecond) No newline at end of file
85 * 1000000 + datetime.microsecond)
86
87
88 def get_user(request):
89 """
90 Get current user from the session. If the user does not exist, create
91 a new one.
92 """
93
94 session = request.session
95 if not 'user_id' in session:
96 request.session.save()
97
98 md5 = hashlib.md5()
99 md5.update(session.session_key)
100 new_id = md5.hexdigest()
101
102 while User.objects.filter(user_id=new_id).exists():
103 md5.update(str(timezone.now()))
104 new_id = md5.hexdigest()
105
106 time_now = timezone.now()
107 user = User.objects.create(user_id=new_id, rank=RANK_USER,
108 registration_time=time_now)
109
110 session['user_id'] = user.id
111 else:
112 user = User.objects.select_related('fav_tags').get(
113 id=session['user_id'])
114
115 return user
116
117
118 def get_theme(request, user=None):
119 """
120 Get user's CSS theme
121 """
122
123 if not user:
124 user = get_user(request)
125 theme = user.get_setting('theme')
126 if not theme:
127 theme = neboard.settings.DEFAULT_THEME
128
129 return theme No newline at end of file
@@ -3,6 +3,7 b' from django.shortcuts import render'
3 from boards.views.base import BaseBoardView
3 from boards.views.base import BaseBoardView
4 from boards.models.tag import Tag
4 from boards.models.tag import Tag
5
5
6
6 class AllTagsView(BaseBoardView):
7 class AllTagsView(BaseBoardView):
7
8
8 def get(self, request):
9 def get(self, request):
@@ -1,6 +1,5 b''
1 import string
1 import string
2
2
3 from django.core.urlresolvers import reverse
4 from django.db import transaction
3 from django.db import transaction
5 from django.shortcuts import render, redirect
4 from django.shortcuts import render, redirect
6
5
@@ -30,7 +29,7 b' class AllThreadsView(PostMixin, BaseBoar'
30 def get(self, request, page=DEFAULT_PAGE, form=None):
29 def get(self, request, page=DEFAULT_PAGE, form=None):
31 context = self.get_context_data(request=request)
30 context = self.get_context_data(request=request)
32
31
33 self.user = context['user']
32 self.user = utils.get_user(request)
34
33
35 if not form:
34 if not form:
36 form = ThreadForm(error_class=PlainErrorList)
35 form = ThreadForm(error_class=PlainErrorList)
@@ -70,7 +69,8 b' class AllThreadsView(PostMixin, BaseBoar'
70 context[PARAMETER_PAGINATOR] = paginator
69 context[PARAMETER_PAGINATOR] = paginator
71 context[PARAMETER_CURRENT_PAGE] = paginator.page(int(page))
70 context[PARAMETER_CURRENT_PAGE] = paginator.page(int(page))
72
71
73 def parse_tags_string(self, tag_strings):
72 @staticmethod
73 def parse_tags_string(tag_strings):
74 """
74 """
75 Parses tag list string and returns tag object list.
75 Parses tag list string and returns tag object list.
76 """
76 """
@@ -120,7 +120,7 b' class AllThreadsView(PostMixin, BaseBoar'
120
120
121 post = Post.objects.create_post(title=title, text=text, ip=ip,
121 post = Post.objects.create_post(title=title, text=text, ip=ip,
122 image=image, tags=tags,
122 image=image, tags=tags,
123 user=self._get_user(request))
123 user=utils.get_user(request))
124
124
125 if html_response:
125 if html_response:
126 return redirect(post.get_url())
126 return redirect(post.get_url())
@@ -87,7 +87,7 b' def api_add_post(request, opening_post_i'
87 status = STATUS_ERROR
87 status = STATUS_ERROR
88 if form.is_valid():
88 if form.is_valid():
89 post = ThreadView().new_post(request, form, opening_post,
89 post = ThreadView().new_post(request, form, opening_post,
90 html_response=False)
90 html_response=False)
91 if not post:
91 if not post:
92 status = STATUS_ERROR
92 status = STATUS_ERROR
93 else:
93 else:
@@ -153,7 +153,7 b' def api_get_threads(request, count):'
153
153
154 # TODO Add tags, replies and images count
154 # TODO Add tags, replies and images count
155 opening_posts.append(_get_post_data(opening_post.id,
155 opening_posts.append(_get_post_data(opening_post.id,
156 include_last_update=True))
156 include_last_update=True))
157
157
158 return HttpResponse(content=json.dumps(opening_posts))
158 return HttpResponse(content=json.dumps(opening_posts))
159
159
@@ -241,5 +241,5 b' def _get_post_data(post_id, format_type='
241 post_json['image_preview'] = post.image.url_200x150
241 post_json['image_preview'] = post.image.url_200x150
242 if include_last_update:
242 if include_last_update:
243 post_json['bump_time'] = datetime_to_epoch(
243 post_json['bump_time'] = datetime_to_epoch(
244 post.thread_new.bump_time)
244 post.thread_new.bump_time)
245 return post_json
245 return post_json
@@ -1,5 +1,6 b''
1 from django.db import transaction
1 from django.db import transaction
2 from django.shortcuts import get_object_or_404
2 from django.shortcuts import get_object_or_404
3 from boards import utils
3
4
4 from boards.views.base import BaseBoardView
5 from boards.views.base import BaseBoardView
5 from boards.models import Post, Ban
6 from boards.models import Post, Ban
@@ -10,7 +11,7 b' class BanUserView(BaseBoardView, Redirec'
10
11
11 @transaction.atomic
12 @transaction.atomic
12 def get(self, request, post_id):
13 def get(self, request, post_id):
13 user = self._get_user(request)
14 user = utils.get_user(request)
14 post = get_object_or_404(Post, id=post_id)
15 post = get_object_or_404(Post, id=post_id)
15
16
16 if user.is_moderator():
17 if user.is_moderator():
@@ -1,15 +1,10 b''
1 from datetime import datetime, timedelta
2 import hashlib
3 from django.db import transaction
1 from django.db import transaction
4 from django.db.models import Count
5 from django.template import RequestContext
2 from django.template import RequestContext
6 from django.utils import timezone
7 from django.views.generic import View
3 from django.views.generic import View
4
8 from boards import utils
5 from boards import utils
9 from boards.models import User, Post
6 from boards.models.user import Ban
10 from boards.models.post import SETTING_MODERATE
7
11 from boards.models.user import RANK_USER, Ban
12 import neboard
13
8
14 BAN_REASON_SPAM = 'Autoban: spam bot'
9 BAN_REASON_SPAM = 'Autoban: spam bot'
15
10
@@ -20,78 +15,11 b' class BaseBoardView(View):'
20
15
21 def get_context_data(self, **kwargs):
16 def get_context_data(self, **kwargs):
22 request = kwargs['request']
17 request = kwargs['request']
23 context = self._default_context(request)
18 # context = self._default_context(request)
24
25 context['version'] = neboard.settings.VERSION
26 context['site_name'] = neboard.settings.SITE_NAME
27
28 return context
29
30 def _default_context(self, request):
31 """Create context with default values that are used in most views"""
32
33 context = RequestContext(request)
19 context = RequestContext(request)
34
20
35 user = self._get_user(request)
36 context['user'] = user
37 context['tags'] = user.fav_tags.all()
38 context['posts_per_day'] = float(Post.objects.get_posts_per_day())
39
40 theme = self._get_theme(request, user)
41 context['theme'] = theme
42 context['theme_css'] = 'css/' + theme + '/base_page.css'
43
44 # This shows the moderator panel
45 moderate = user.get_setting(SETTING_MODERATE)
46 if moderate == 'True':
47 context['moderator'] = user.is_moderator()
48 else:
49 context['moderator'] = False
50
51 return context
21 return context
52
22
53 def _get_user(self, request):
54 """
55 Get current user from the session. If the user does not exist, create
56 a new one.
57 """
58
59 session = request.session
60 if not 'user_id' in session:
61 request.session.save()
62
63 md5 = hashlib.md5()
64 md5.update(session.session_key)
65 new_id = md5.hexdigest()
66
67 while User.objects.filter(user_id=new_id).exists():
68 md5.update(str(timezone.now()))
69 new_id = md5.hexdigest()
70
71 time_now = timezone.now()
72 user = User.objects.create(user_id=new_id, rank=RANK_USER,
73 registration_time=time_now)
74
75 session['user_id'] = user.id
76 else:
77 user = User.objects.select_related('fav_tags').get(
78 id=session['user_id'])
79
80 return user
81
82 def _get_theme(self, request, user=None):
83 """
84 Get user's CSS theme
85 """
86
87 if not user:
88 user = self._get_user(request)
89 theme = user.get_setting('theme')
90 if not theme:
91 theme = neboard.settings.DEFAULT_THEME
92
93 return theme
94
95 @transaction.atomic
23 @transaction.atomic
96 def _ban_current_user(self, request):
24 def _ban_current_user(self, request):
97 """
25 """
@@ -1,5 +1,6 b''
1 from django.shortcuts import redirect, get_object_or_404
1 from django.shortcuts import redirect, get_object_or_404
2 from django.db import transaction
2 from django.db import transaction
3 from boards import utils
3
4
4 from boards.views.base import BaseBoardView
5 from boards.views.base import BaseBoardView
5 from boards.views.mixins import RedirectNextMixin
6 from boards.views.mixins import RedirectNextMixin
@@ -10,7 +11,7 b' class DeletePostView(BaseBoardView, Redi'
10
11
11 @transaction.atomic
12 @transaction.atomic
12 def get(self, request, post_id):
13 def get(self, request, post_id):
13 user = self._get_user(request)
14 user = utils.get_user(request)
14 post = get_object_or_404(Post, id=post_id)
15 post = get_object_or_404(Post, id=post_id)
15
16
16 opening_post = post.is_opening()
17 opening_post = post.is_opening()
@@ -9,7 +9,7 b' from boards.forms import AddTagForm, Pla'
9 class PostAdminView(BaseBoardView, DispatcherMixin):
9 class PostAdminView(BaseBoardView, DispatcherMixin):
10
10
11 def get(self, request, post_id, form=None):
11 def get(self, request, post_id, form=None):
12 user = self._get_user(request)
12 user = utils.get_user(request)
13 if not user.is_moderator:
13 if not user.is_moderator:
14 redirect('index')
14 redirect('index')
15
15
@@ -30,7 +30,7 b' class PostAdminView(BaseBoardView, Dispa'
30 return render(request, 'boards/post_admin.html', context)
30 return render(request, 'boards/post_admin.html', context)
31
31
32 def post(self, request, post_id):
32 def post(self, request, post_id):
33 user = self._get_user(request)
33 user = utils.get_user(request)
34 if not user.is_moderator:
34 if not user.is_moderator:
35 redirect('index')
35 redirect('index')
36
36
@@ -1,5 +1,6 b''
1 from django.db import transaction
1 from django.db import transaction
2 from django.shortcuts import render, redirect
2 from django.shortcuts import render, redirect
3 from boards import utils
3
4
4 from boards.views.base import BaseBoardView, PARAMETER_FORM
5 from boards.views.base import BaseBoardView, PARAMETER_FORM
5 from boards.forms import SettingsForm, ModeratorSettingsForm, PlainErrorList
6 from boards.forms import SettingsForm, ModeratorSettingsForm, PlainErrorList
@@ -10,15 +11,16 b' class SettingsView(BaseBoardView):'
10
11
11 def get(self, request):
12 def get(self, request):
12 context = self.get_context_data(request=request)
13 context = self.get_context_data(request=request)
13 user = context['user']
14 user = utils.get_user(request)
14 is_moderator = user.is_moderator()
15 is_moderator = user.is_moderator()
15
16
16 selected_theme = context['theme']
17 selected_theme = utils.get_theme(request, user)
17
18
18 if is_moderator:
19 if is_moderator:
19 form = ModeratorSettingsForm(initial={
20 form = ModeratorSettingsForm(initial={
20 'theme': selected_theme,
21 'theme': selected_theme,
21 'moderate': context['moderator']
22 'moderate': user.get_setting(SETTING_MODERATE) and \
23 user.is_moderator()
22 }, error_class=PlainErrorList)
24 }, error_class=PlainErrorList)
23 else:
25 else:
24 form = SettingsForm(initial={'theme': selected_theme},
26 form = SettingsForm(initial={'theme': selected_theme},
@@ -29,8 +31,7 b' class SettingsView(BaseBoardView):'
29 return render(request, 'boards/settings.html', context)
31 return render(request, 'boards/settings.html', context)
30
32
31 def post(self, request):
33 def post(self, request):
32 context = self.get_context_data(request=request)
34 user = utils.get_user(request)
33 user = context['user']
34 is_moderator = user.is_moderator()
35 is_moderator = user.is_moderator()
35
36
36 with transaction.atomic():
37 with transaction.atomic():
@@ -2,6 +2,7 b' from django.shortcuts import render'
2
2
3 from boards.views.base import BaseBoardView
3 from boards.views.base import BaseBoardView
4
4
5
5 class StaticPageView(BaseBoardView):
6 class StaticPageView(BaseBoardView):
6
7
7 def get(self, request, name):
8 def get(self, request, name):
@@ -1,4 +1,5 b''
1 from django.shortcuts import get_object_or_404
1 from django.shortcuts import get_object_or_404
2 from boards import utils
2 from boards.models import Tag, Post
3 from boards.models import Tag, Post
3 from boards.views.all_threads import AllThreadsView, DEFAULT_PAGE
4 from boards.views.all_threads import AllThreadsView, DEFAULT_PAGE
4 from boards.views.mixins import DispatcherMixin, RedirectNextMixin
5 from boards.views.mixins import DispatcherMixin, RedirectNextMixin
@@ -47,7 +48,7 b' class TagView(AllThreadsView, Dispatcher'
47 return self.get(request, tag_name, page, form)
48 return self.get(request, tag_name, page, form)
48
49
49 def subscribe(self, request):
50 def subscribe(self, request):
50 user = self._get_user(request)
51 user = utils.get_user(request)
51 tag = get_object_or_404(Tag, name=self.tag_name)
52 tag = get_object_or_404(Tag, name=self.tag_name)
52
53
53 if not tag in user.fav_tags.all():
54 if not tag in user.fav_tags.all():
@@ -56,7 +57,7 b' class TagView(AllThreadsView, Dispatcher'
56 return self.redirect_to_next(request)
57 return self.redirect_to_next(request)
57
58
58 def unsubscribe(self, request):
59 def unsubscribe(self, request):
59 user = self._get_user(request)
60 user = utils.get_user(request)
60 tag = get_object_or_404(Tag, name=self.tag_name)
61 tag = get_object_or_404(Tag, name=self.tag_name)
61
62
62 if tag in user.fav_tags.all():
63 if tag in user.fav_tags.all():
@@ -70,7 +71,7 b' class TagView(AllThreadsView, Dispatcher'
70 shown.
71 shown.
71 """
72 """
72
73
73 user = self._get_user(request)
74 user = utils.get_user(request)
74 tag = get_object_or_404(Tag, name=self.tag_name)
75 tag = get_object_or_404(Tag, name=self.tag_name)
75
76
76 user.hide_tag(tag)
77 user.hide_tag(tag)
@@ -80,7 +81,7 b' class TagView(AllThreadsView, Dispatcher'
80 Removed tag from user's hidden tags.
81 Removed tag from user's hidden tags.
81 """
82 """
82
83
83 user = self._get_user(request)
84 user = utils.get_user(request)
84 tag = get_object_or_404(Tag, name=self.tag_name)
85 tag = get_object_or_404(Tag, name=self.tag_name)
85
86
86 user.unhide_tag(tag)
87 user.unhide_tag(tag)
@@ -1,17 +1,18 b''
1 import string
2 from django.core.urlresolvers import reverse
1 from django.core.urlresolvers import reverse
3 from django.db import transaction
2 from django.db import transaction
4 from django.http import Http404
3 from django.http import Http404
5 from django.shortcuts import get_object_or_404, render, redirect
4 from django.shortcuts import get_object_or_404, render, redirect
6 from django.views.generic.edit import FormMixin
5 from django.views.generic.edit import FormMixin
6
7 from boards import utils
7 from boards import utils
8 from boards.forms import PostForm, PlainErrorList
8 from boards.forms import PostForm, PlainErrorList
9 from boards.models import Post, Ban, Tag
9 from boards.models import Post, Ban
10 from boards.views.banned import BannedView
10 from boards.views.banned import BannedView
11 from boards.views.base import BaseBoardView, PARAMETER_FORM
11 from boards.views.base import BaseBoardView, PARAMETER_FORM
12 from boards.views.posting_mixin import PostMixin
12 from boards.views.posting_mixin import PostMixin
13 import neboard
13 import neboard
14
14
15
15 MODE_GALLERY = 'gallery'
16 MODE_GALLERY = 'gallery'
16 MODE_NORMAL = 'normal'
17 MODE_NORMAL = 'normal'
17
18
@@ -119,7 +120,7 b' class ThreadView(BaseBoardView, PostMixi'
119 post = Post.objects.create_post(title=title, text=text, ip=ip,
120 post = Post.objects.create_post(title=title, text=text, ip=ip,
120 thread=post_thread, image=image,
121 thread=post_thread, image=image,
121 tags=tags,
122 tags=tags,
122 user=self._get_user(request))
123 user=utils.get_user(request))
123
124
124 thread_to_show = (opening_post.id if opening_post else post.id)
125 thread_to_show = (opening_post.id if opening_post else post.id)
125
126
@@ -105,6 +105,7 b' TEMPLATE_CONTEXT_PROCESSORS = ('
105 'django.core.context_processors.static',
105 'django.core.context_processors.static',
106 'django.core.context_processors.request',
106 'django.core.context_processors.request',
107 'django.contrib.auth.context_processors.auth',
107 'django.contrib.auth.context_processors.auth',
108 'boards.context_processors.user_and_ui_processor',
108 )
109 )
109
110
110 MIDDLEWARE_CLASSES = (
111 MIDDLEWARE_CLASSES = (
@@ -137,15 +138,22 b' INSTALLED_APPS = ('
137 'django.contrib.messages',
138 'django.contrib.messages',
138 'django.contrib.staticfiles',
139 'django.contrib.staticfiles',
139 # Uncomment the next line to enable the admin:
140 # Uncomment the next line to enable the admin:
140 'django.contrib.admin',
141 'django.contrib.admin',
141 # Uncomment the next line to enable admin documentation:
142 # Uncomment the next line to enable admin documentation:
142 # 'django.contrib.admindocs',
143 # 'django.contrib.admindocs',
143 'django.contrib.humanize',
144 'django.contrib.humanize',
144 'django_cleanup',
145 'django_cleanup',
145 'boards',
146
146 'captcha',
147 # Migrations
147 'south',
148 'south',
148 'debug_toolbar',
149 'debug_toolbar',
150
151 'captcha',
152
153 # Search
154 'haystack',
155
156 'boards',
149 )
157 )
150
158
151 DEBUG_TOOLBAR_PANELS = (
159 DEBUG_TOOLBAR_PANELS = (
@@ -201,6 +209,15 b' LOGGING = {'
201 },
209 },
202 }
210 }
203
211
212 HAYSTACK_CONNECTIONS = {
213 'default': {
214 'ENGINE': 'haystack.backends.whoosh_backend.WhooshEngine',
215 'PATH': os.path.join(os.path.dirname(__file__), 'whoosh_index'),
216 },
217 }
218
219 HAYSTACK_SIGNAL_PROCESSOR = 'haystack.signals.RealtimeSignalProcessor'
220
204 MARKUP_FIELD_TYPES = (
221 MARKUP_FIELD_TYPES = (
205 ('markdown', markdown_extended),
222 ('markdown', markdown_extended),
206 )
223 )
General Comments 0
You need to be logged in to leave comments. Login now