Show More
@@ -1,184 +1,193 b'' | |||
|
1 | 1 | from django.core.urlresolvers import reverse |
|
2 | 2 | from django.template import RequestContext |
|
3 | 3 | from boards import forms |
|
4 | 4 | import boards |
|
5 | 5 | from boards.forms import ThreadForm, PostForm, SettingsForm |
|
6 | 6 | from boards.models import Post, Admin, Tag |
|
7 | 7 | from django.shortcuts import render, get_list_or_404, redirect |
|
8 | 8 | from django.http import HttpResponseRedirect, Http404 |
|
9 | 9 | import neboard |
|
10 | 10 | |
|
11 | 11 | |
|
12 | 12 | def index(request, page=0): |
|
13 | 13 | context = RequestContext(request) |
|
14 | 14 | |
|
15 | 15 | if request.method == 'POST': |
|
16 | 16 | return new_post(request) |
|
17 | 17 | else: |
|
18 | 18 | threads = Post.objects.get_threads(page=int(page)) |
|
19 | 19 | |
|
20 | 20 | # TODO Get rid of the duplicate code in index and tag views |
|
21 | 21 | context['threads'] = None if len(threads) == 0 else threads |
|
22 | 22 | context['form'] = forms.ThreadForm() |
|
23 | 23 | context['tags'] = Tag.objects.get_popular_tags() |
|
24 | 24 | context['theme'] = _get_theme(request) |
|
25 | 25 | context['pages'] = range(Post.objects.get_thread_page_count()) |
|
26 | 26 | |
|
27 | 27 | return render(request, 'posting_general.html', |
|
28 | 28 | context) |
|
29 | 29 | |
|
30 | 30 | |
|
31 | 31 | def new_post(request, thread_id=boards.models.NO_PARENT): |
|
32 | 32 | """Add a new post (in thread or as a reply).""" |
|
33 | 33 | |
|
34 | 34 | if thread_id == boards.models.NO_PARENT: |
|
35 | 35 | form = ThreadForm(request.POST, request.FILES) |
|
36 | 36 | else: |
|
37 | 37 | form = PostForm(request.POST, request.FILES) |
|
38 | 38 | |
|
39 | 39 | if form.is_valid(): |
|
40 | 40 | data = form.cleaned_data |
|
41 | 41 | else: |
|
42 | 42 | return redirect(index) |
|
43 | 43 | |
|
44 | 44 | title = data['title'] |
|
45 | 45 | text = data['text'] |
|
46 | 46 | |
|
47 | 47 | if 'image' in data.keys(): |
|
48 | 48 | image = data['image'] |
|
49 | 49 | else: |
|
50 | 50 | image = None |
|
51 | 51 | |
|
52 | ip = request.META['REMOTE_ADDR'] | |
|
52 | ip = _get_client_ip(request) | |
|
53 | 53 | |
|
54 | 54 | tags = [] |
|
55 | 55 | |
|
56 | 56 | new_thread = thread_id == boards.models.NO_PARENT |
|
57 | 57 | if new_thread: |
|
58 | 58 | tag_strings = data['tags'] |
|
59 | 59 | |
|
60 | 60 | if tag_strings: |
|
61 | 61 | tag_strings = tag_strings.split(' ') |
|
62 | 62 | for tag_name in tag_strings: |
|
63 | 63 | tag_name = tag_name.strip() |
|
64 | 64 | if len(tag_name) > 0: |
|
65 | 65 | tag, created = Tag.objects.get_or_create(name=tag_name) |
|
66 | 66 | tags.append(tag) |
|
67 | 67 | |
|
68 | 68 | # TODO Add a possibility to define a link image instead of an image file. |
|
69 | 69 | # If a link is given, download the image automatically. |
|
70 | 70 | |
|
71 | 71 | post = Post.objects.create_post(title=title, text=text, ip=ip, |
|
72 | 72 | parent_id=thread_id, image=image, |
|
73 | 73 | tags=tags) |
|
74 | 74 | |
|
75 | 75 | thread_to_show = (post.id if new_thread else thread_id) |
|
76 | 76 | |
|
77 | 77 | if new_thread: |
|
78 | 78 | return redirect(thread, post_id=thread_to_show) |
|
79 | 79 | else: |
|
80 | 80 | return redirect(reverse(thread, |
|
81 | 81 | kwargs={'post_id': thread_to_show}) + '#' |
|
82 | 82 | + str(post.id)) |
|
83 | 83 | |
|
84 | 84 | |
|
85 | 85 | def tag(request, tag_name, page=0): |
|
86 | 86 | """Get all tag threads (posts without a parent).""" |
|
87 | 87 | |
|
88 | 88 | tag = Tag.objects.get(name=tag_name) |
|
89 | 89 | threads = Post.objects.get_threads(tag=tag, page=int(page)) |
|
90 | 90 | |
|
91 | 91 | if request.method == 'POST': |
|
92 | 92 | return new_post(request) |
|
93 | 93 | else: |
|
94 | 94 | context = RequestContext(request) |
|
95 | 95 | context['threads'] = None if len(threads) == 0 else threads |
|
96 | 96 | context['tag'] = tag_name |
|
97 | 97 | context['tags'] = Tag.objects.get_popular_tags() |
|
98 | 98 | context['theme'] = _get_theme(request) |
|
99 | 99 | context['pages'] = range(Post.objects.get_thread_page_count(tag=tag)) |
|
100 | 100 | |
|
101 | 101 | context['form'] = forms.ThreadForm(initial={'tags': tag_name}) |
|
102 | 102 | |
|
103 | 103 | return render(request, 'posting_general.html', |
|
104 | 104 | context) |
|
105 | 105 | |
|
106 | 106 | |
|
107 | 107 | def thread(request, post_id): |
|
108 | 108 | """Get all thread posts""" |
|
109 | 109 | |
|
110 | 110 | if request.method == 'POST': |
|
111 | 111 | return new_post(request, post_id) |
|
112 | 112 | else: |
|
113 | 113 | # TODO Show 404 if there is no such thread |
|
114 | 114 | posts = Post.objects.get_thread(post_id) |
|
115 | 115 | |
|
116 | 116 | context = RequestContext(request) |
|
117 | 117 | |
|
118 | 118 | context['posts'] = posts |
|
119 | 119 | context['form'] = forms.PostForm() |
|
120 | 120 | context['tags'] = Tag.objects.get_popular_tags() |
|
121 | 121 | context['theme'] = _get_theme(request) |
|
122 | 122 | |
|
123 | 123 | return render(request, 'thread.html', context) |
|
124 | 124 | |
|
125 | 125 | |
|
126 | 126 | def login(request): |
|
127 | 127 | """Log in as admin""" |
|
128 | 128 | |
|
129 | 129 | if 'name' in request.POST and 'password' in request.POST: |
|
130 | 130 | request.session['admin'] = False |
|
131 | 131 | |
|
132 | 132 | isAdmin = len(Admin.objects.filter(name=request.POST['name'], |
|
133 | 133 | password=request.POST[ |
|
134 | 134 | 'password'])) > 0 |
|
135 | 135 | |
|
136 | 136 | if isAdmin: |
|
137 | 137 | request.session['admin'] = True |
|
138 | 138 | |
|
139 | 139 | response = HttpResponseRedirect('/') |
|
140 | 140 | |
|
141 | 141 | else: |
|
142 | 142 | response = render(request, 'login.html', {'error': 'Login error'}) |
|
143 | 143 | else: |
|
144 | 144 | response = render(request, 'login.html', {}) |
|
145 | 145 | |
|
146 | 146 | return response |
|
147 | 147 | |
|
148 | 148 | |
|
149 | 149 | def logout(request): |
|
150 | 150 | request.session['admin'] = False |
|
151 | 151 | return HttpResponseRedirect('/') |
|
152 | 152 | |
|
153 | 153 | |
|
154 | 154 | def settings(request): |
|
155 | 155 | context = RequestContext(request) |
|
156 | 156 | |
|
157 | 157 | if request.method == 'POST': |
|
158 | 158 | form = SettingsForm(request.POST) |
|
159 | 159 | if form.is_valid(): |
|
160 | 160 | selected_theme = form.cleaned_data['theme'] |
|
161 | 161 | request.session['theme'] = selected_theme |
|
162 | 162 | |
|
163 | 163 | return redirect(settings) |
|
164 | 164 | else: |
|
165 | 165 | selected_theme = _get_theme(request) |
|
166 | 166 | form = SettingsForm(initial={'theme': selected_theme}) |
|
167 | 167 | context['form'] = form |
|
168 | 168 | context['tags'] = Tag.objects.get_popular_tags() |
|
169 | 169 | context['theme'] = _get_theme(request) |
|
170 | 170 | |
|
171 | 171 | return render(request, 'settings.html', context) |
|
172 | 172 | |
|
173 | 173 | |
|
174 | 174 | def all_tags(request): |
|
175 | 175 | context = RequestContext(request) |
|
176 | 176 | context['tags'] = Tag.objects.get_popular_tags() |
|
177 | 177 | context['theme'] = _get_theme(request) |
|
178 | 178 | context['all_tags'] = Tag.objects.get_not_empty_tags() |
|
179 | 179 | |
|
180 | 180 | return render(request, 'tags.html', context) |
|
181 | 181 | |
|
182 | 182 | |
|
183 | 183 | def _get_theme(request): |
|
184 | return request.session.get('theme', neboard.settings.DEFAULT_THEME) No newline at end of file | |
|
184 | return request.session.get('theme', neboard.settings.DEFAULT_THEME) | |
|
185 | ||
|
186 | ||
|
187 | def _get_client_ip(request): | |
|
188 | x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR') | |
|
189 | if x_forwarded_for: | |
|
190 | ip = x_forwarded_for.split(',')[-1].strip() | |
|
191 | else: | |
|
192 | ip = request.META.get('REMOTE_ADDR') | |
|
193 | return ip No newline at end of file |
General Comments 0
You need to be logged in to leave comments.
Login now