Show More
@@ -1,192 +1,192 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 | 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 | posts = Post.objects.get_thread(post_id) |
|
114 | 114 | |
|
115 | 115 | context = RequestContext(request) |
|
116 | 116 | |
|
117 | 117 | context['posts'] = posts |
|
118 | 118 | context['form'] = forms.PostForm() |
|
119 | 119 | context['tags'] = Tag.objects.get_popular_tags() |
|
120 | 120 | context['theme'] = _get_theme(request) |
|
121 | 121 | |
|
122 | 122 | return render(request, 'thread.html', context) |
|
123 | 123 | |
|
124 | 124 | |
|
125 | 125 | def login(request): |
|
126 | 126 | """Log in as admin""" |
|
127 | 127 | |
|
128 | 128 | if 'name' in request.POST and 'password' in request.POST: |
|
129 | 129 | request.session['admin'] = False |
|
130 | 130 | |
|
131 | 131 | isAdmin = len(Admin.objects.filter(name=request.POST['name'], |
|
132 | 132 | password=request.POST[ |
|
133 | 133 | 'password'])) > 0 |
|
134 | 134 | |
|
135 | 135 | if isAdmin: |
|
136 | 136 | request.session['admin'] = True |
|
137 | 137 | |
|
138 | 138 | response = HttpResponseRedirect('/') |
|
139 | 139 | |
|
140 | 140 | else: |
|
141 | 141 | response = render(request, 'login.html', {'error': 'Login error'}) |
|
142 | 142 | else: |
|
143 | 143 | response = render(request, 'login.html', {}) |
|
144 | 144 | |
|
145 | 145 | return response |
|
146 | 146 | |
|
147 | 147 | |
|
148 | 148 | def logout(request): |
|
149 | 149 | request.session['admin'] = False |
|
150 | 150 | return HttpResponseRedirect('/') |
|
151 | 151 | |
|
152 | 152 | |
|
153 | 153 | def settings(request): |
|
154 | 154 | context = RequestContext(request) |
|
155 | 155 | |
|
156 | 156 | if request.method == 'POST': |
|
157 | 157 | form = SettingsForm(request.POST) |
|
158 | 158 | if form.is_valid(): |
|
159 | 159 | selected_theme = form.cleaned_data['theme'] |
|
160 | 160 | request.session['theme'] = selected_theme |
|
161 | 161 | |
|
162 | 162 | return redirect(settings) |
|
163 | 163 | else: |
|
164 | 164 | selected_theme = _get_theme(request) |
|
165 | 165 | form = SettingsForm(initial={'theme': selected_theme}) |
|
166 | 166 | context['form'] = form |
|
167 | 167 | context['tags'] = Tag.objects.get_popular_tags() |
|
168 | 168 | context['theme'] = _get_theme(request) |
|
169 | 169 | |
|
170 | 170 | return render(request, 'settings.html', context) |
|
171 | 171 | |
|
172 | 172 | |
|
173 | 173 | def all_tags(request): |
|
174 | 174 | context = RequestContext(request) |
|
175 | 175 | context['tags'] = Tag.objects.get_popular_tags() |
|
176 | 176 | context['theme'] = _get_theme(request) |
|
177 | 177 | context['all_tags'] = Tag.objects.get_not_empty_tags() |
|
178 | 178 | |
|
179 | 179 | return render(request, 'tags.html', context) |
|
180 | 180 | |
|
181 | 181 | |
|
182 | 182 | def _get_theme(request): |
|
183 | 183 | return request.session.get('theme', neboard.settings.DEFAULT_THEME) |
|
184 | 184 | |
|
185 | 185 | |
|
186 | 186 | def _get_client_ip(request): |
|
187 | 187 | x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR') |
|
188 | 188 | if x_forwarded_for: |
|
189 | 189 | ip = x_forwarded_for.split(',')[-1].strip() |
|
190 | 190 | else: |
|
191 | 191 | ip = request.META.get('REMOTE_ADDR') |
|
192 | return ip No newline at end of file | |
|
192 | return ip |
@@ -1,144 +1,154 b'' | |||
|
1 | 1 | {% extends "base.html" %} |
|
2 | 2 | |
|
3 | 3 | {% load i18n %} |
|
4 | 4 | {% load markup %} |
|
5 | 5 | |
|
6 | 6 | {% block head %} |
|
7 | <title>Neboard</title> | |
|
7 | {% if tag %} | |
|
8 | <title>Neboard - {{ tag }}</title> | |
|
9 | {% else %} | |
|
10 | <title>Neboard</title> | |
|
11 | {% endif %} | |
|
8 | 12 | {% endblock %} |
|
9 | 13 | |
|
10 | 14 | {% block content %} |
|
11 | 15 | |
|
16 | {% if tag %} | |
|
17 | <div class="tag_info"> | |
|
18 | <h2>{{ tag }}</h2> | |
|
19 | </div> | |
|
20 | {% endif %} | |
|
21 | ||
|
12 | 22 | {% if threads %} |
|
13 | 23 | {% for thread in threads %} |
|
14 | 24 | <div class="thread"> |
|
15 | 25 | {% if thread.can_bump %} |
|
16 | 26 | <div class="post"> |
|
17 | 27 | {% else %} |
|
18 | 28 | <div class="post dead_post"> |
|
19 | 29 | {% endif %} |
|
20 | 30 | {% if thread.image %} |
|
21 | 31 | <div class="image"> |
|
22 | 32 | <a class="fancy" |
|
23 | 33 | href="{{ thread.image.url }}"><img |
|
24 | 34 | src="{{ thread.image.url_200x150 }}" /> |
|
25 | 35 | </a> |
|
26 | 36 | </div> |
|
27 | 37 | {% endif %} |
|
28 | 38 | <div class="message"> |
|
29 | 39 | <div class="post-info"> |
|
30 | 40 | <span class="title">{{ thread.title }}</span> |
|
31 | 41 | <a class="post_id" href="{% url 'thread' thread.id %}"> |
|
32 | 42 | (#{{ thread.id }})</a> |
|
33 | 43 | [{{ thread.pub_time }}] |
|
34 | 44 | [<a class="link" href="{% url 'thread' thread.id %}#form" |
|
35 | 45 | >{% trans "Reply" %}</a>] |
|
36 | 46 | </div> |
|
37 | 47 | {% autoescape off %} |
|
38 | 48 | {{ thread.text.rendered|truncatewords_html:50 }} |
|
39 | 49 | {% endautoescape %} |
|
40 | 50 | </div> |
|
41 | 51 | <div class="metadata"> |
|
42 | 52 | {{ thread.get_reply_count }} {% trans 'replies' %}, |
|
43 | 53 | {{ thread.get_images_count }} {% trans 'images' %}. |
|
44 | 54 | {% if thread.tags.all %} |
|
45 | 55 | <span class="tags">{% trans 'Tags' %}: |
|
46 | 56 | {% for tag in thread.tags.all %} |
|
47 | 57 | <a class="tag" href=" |
|
48 | 58 | {% url 'tag' tag_name=tag.name %}"> |
|
49 | 59 | {{ tag.name }}</a> |
|
50 | 60 | {% endfor %} |
|
51 | 61 | </span> |
|
52 | 62 | {% endif %} |
|
53 | 63 | </div> |
|
54 | 64 | </div> |
|
55 | 65 | {% if thread.get_last_replies %} |
|
56 | 66 | <div class="last-replies"> |
|
57 | 67 | {% for post in thread.get_last_replies %} |
|
58 | 68 | {% if thread.can_bump %} |
|
59 | 69 | <div class="post"> |
|
60 | 70 | {% else %} |
|
61 | 71 | <div class="post dead_post"> |
|
62 | 72 | {% endif %} |
|
63 | 73 | {% if post.image %} |
|
64 | 74 | <div class="image"> |
|
65 | 75 | <a class="fancy" |
|
66 | 76 | href="{{ post.image.url }}"><img |
|
67 | 77 | src="{{ post.image.url_200x150 }}" /> |
|
68 | 78 | </a> |
|
69 | 79 | </div> |
|
70 | 80 | {% endif %} |
|
71 | 81 | <div class="message"> |
|
72 | 82 | <div class="post-info"> |
|
73 | 83 | <span class="title">{{ post.title }}</span> |
|
74 | 84 | <a class="post_id" href=" |
|
75 | 85 | {% url 'thread' thread.id %}#{{ post.id }}"> |
|
76 | 86 | (#{{ post.id }})</a> |
|
77 | 87 | [{{ post.pub_time }}] |
|
78 | 88 | </div> |
|
79 | 89 | {% autoescape off %} |
|
80 | 90 | {{ post.text.rendered|truncatewords_html:50 }} |
|
81 | 91 | {% endautoescape %} |
|
82 | 92 | </div> |
|
83 | 93 | </div> |
|
84 | 94 | {% endfor %} |
|
85 | 95 | </div> |
|
86 | 96 | {% endif %} |
|
87 | 97 | </div> |
|
88 | 98 | {% endfor %} |
|
89 | 99 | {% else %} |
|
90 | 100 | No threads found. |
|
91 | 101 | <hr /> |
|
92 | 102 | {% endif %} |
|
93 | 103 | |
|
94 | 104 | <form enctype="multipart/form-data" method="post">{% csrf_token %} |
|
95 | 105 | <div class="post-form-w"> |
|
96 | 106 | <div class="form-title">{% trans "Create new thread" %}</div> |
|
97 | 107 | <div class="post-form"> |
|
98 | 108 | <div class="form-row"> |
|
99 | 109 | <div class="form-label">{% trans 'Title' %}</div> |
|
100 | 110 | <div class="form-input">{{ form.title }}</div> |
|
101 | 111 | </div> |
|
102 | 112 | <div class="form-row"> |
|
103 | 113 | <div class="form-label">{% trans 'Text' %}</div> |
|
104 | 114 | <div class="form-input">{{ form.text }}</div> |
|
105 | 115 | </div> |
|
106 | 116 | <div class="form-row"> |
|
107 | 117 | <div class="form-label">{% trans 'Image' %}</div> |
|
108 | 118 | <div class="form-input">{{ form.image }}</div> |
|
109 | 119 | </div> |
|
110 | 120 | <div class="form-row"> |
|
111 | 121 | <div class="form-label">{% trans 'Tags' %}</div> |
|
112 | 122 | <div class="form-input">{{ form.tags }}</div> |
|
113 | 123 | </div> |
|
114 | 124 | </div> |
|
115 | 125 | <div class="form-submit"><input type="submit" |
|
116 | 126 | value="{% trans "Post" %}"/></div> |
|
117 | 127 | <div>Tags must be delimited by spaces. Text or image is required |
|
118 | 128 | </div> |
|
119 | 129 | <div>Use <a |
|
120 | 130 | href="http://daringfireball.net/projects/markdown/basics"> |
|
121 | 131 | markdown</a> syntax for posting.</div> |
|
122 | 132 | </div> |
|
123 | 133 | </form> |
|
124 | 134 | |
|
125 | 135 | {% endblock %} |
|
126 | 136 | |
|
127 | 137 | {% block metapanel %} |
|
128 | 138 | |
|
129 | 139 | <span class="metapanel"> |
|
130 | 140 | <b><a href="https://bitbucket.org/neko259/neboard/">Neboard</a> |
|
131 | 141 | 2013-05 (dev)</b> |
|
132 | 142 | {% trans "Pages:" %} |
|
133 | 143 | {% for page in pages %} |
|
134 | 144 | [<a href=" |
|
135 | 145 | {% if tag %} |
|
136 | 146 | {% url "tag" tag_name=tag page=page %} |
|
137 | 147 | {% else %} |
|
138 | 148 | {% url "index" page=page %} |
|
139 | 149 | {% endif %} |
|
140 | 150 | ">{{ page }}</a>] |
|
141 | 151 | {% endfor %} |
|
142 | 152 | </span> |
|
143 | 153 | |
|
144 | {% endblock %} No newline at end of file | |
|
154 | {% endblock %} |
General Comments 0
You need to be logged in to leave comments.
Login now