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