##// END OF EJS Templates
Added checkinf form fields when creating a new thread.
Ilyas -
r18:174f8766 default
parent child Browse files
Show More
1 NO CONTENT: modified file, binary diff hidden
NO CONTENT: modified file, binary diff hidden
1 NO CONTENT: modified file, binary diff hidden
NO CONTENT: modified file, binary diff hidden
@@ -1,8 +1,8 b''
1 from django import forms
1 from django import forms
2
2
3
3
4 class NewThreadForm(forms.Form):
4 class NewThreadForm(forms.Form):
5 title = forms.CharField(max_length = 100)
5 title = forms.CharField(max_length = 100)
6 text = forms.CharField(widget = forms.Textarea)
6 text = forms.CharField(widget = forms.Textarea)
7 image = forms.ImageField()
7 image = forms.ImageField(required=False)
8 tags = forms.CharField(max_length=100)
8 tags = forms.CharField(max_length=100)
1 NO CONTENT: modified file, binary diff hidden
NO CONTENT: modified file, binary diff hidden
@@ -1,30 +1,43 b''
1 html {
1 html {
2 background: #555555;
2 background: #555555;
3 }
3 }
4
4
5 #admin_panel {
5 .admin_panel {
6 background: #FF0000;
6 background: #FF0000;
7 color: #00FF00
7 color: #00FF00
8 }
8 }
9
9
10 .input_field {
11
12 }
13
14 .input_field_name {
15
16 }
17
18 .input_field_error {
19 color: #FF0000;
20 }
21
22
10 .title {
23 .title {
11 font-weight: bold;
24 font-weight: bold;
12 color: #ffcc00;
25 color: #ffcc00;
13 }
26 }
14
27
15 .text {
28 .text {
16 color: #ffffff;
29 color: #ffffff;
17 }
30 }
18
31
19 .post-form {
32 .post-form {
20 text-align: left;
33 text-align: left;
21 color: #ffffff;
34 color: #ffffff;
22 }
35 }
23
36
24 .link {
37 .link {
25 color: #33bb33
38 color: #33bb33
26 }
39 }
27
40
28 .link:hover {
41 .link:hover {
29 color: #00ff00;
42 color: #00ff00;
30 } No newline at end of file
43 }
1 NO CONTENT: modified file, binary diff hidden
NO CONTENT: modified file, binary diff hidden
@@ -1,85 +1,95 b''
1 from django.template import RequestContext
1 from django.template import RequestContext
2 from boards import forms
2 from boards import forms
3 from boards.forms import NewThreadForm
3 from boards.models import Post, Admin
4 from boards.models import Post, Admin
4 from django.shortcuts import render, get_list_or_404
5 from django.shortcuts import render, get_list_or_404
5 from django.http import HttpResponseRedirect, Http404
6 from django.http import HttpResponseRedirect
6
7
7 def index(request):
8 def index(request):
8 context = RequestContext(request)
9 context = RequestContext(request)
9
10
10 if request.method == 'POST':
11 if request.method == 'POST':
11 Post.objects.create_post(request.POST['title'],
12
12 request.POST['text'], ip = request.META['REMOTE_ADDR'])
13 form = NewThreadForm(request.POST)
14 if form.is_valid():
15
16 Post.objects.create_post(form.cleaned_data['title'],
17 form.cleaned_data['text'], ip = request.META['REMOTE_ADDR'])
18 else:
19 context['form'] = form
20
13
21
14 threads = Post.objects.get_threads()
22 threads = Post.objects.get_threads()
15
23
16 context['threads'] = None if len(threads) == 0 else threads
24 context['threads'] = None if len(threads) == 0 else threads
17 context['form'] = forms.NewThreadForm()
25
26 if 'form' not in context:
27 context['form'] = forms.NewThreadForm()
18
28
19 return render(request, 'posting_general.html',
29 return render(request, 'posting_general.html',
20 context)
30 context)
21
31
22 def new_post(request):
32 def new_post(request):
23 """Add a new post (in thread or as a reply)."""
33 """Add a new post (in thread or as a reply)."""
24
34
25 title = request.POST['title']
35 title = request.POST['title']
26 text = request.POST['text']
36 text = request.POST['text']
27
37
28 image = request.POST['image']
38 image = request.POST['image']
29
39
30 # TODO Get tags list, download image (if link is given)
40 # TODO Get tags list, download image (if link is given)
31
41
32 post = Post.objects.create_post(title = title, text = text, image = image)
42 post = Post.objects.create_post(title = title, text = text, image = image)
33
43
34 # TODO Show the thread with a newly created post
44 # TODO Show the thread with a newly created post
35
45
36 def tag(request):
46 def tag(request):
37 """Get all tag threads (posts without a parent)."""
47 """Get all tag threads (posts without a parent)."""
38
48
39 tag_name = request.GET['tag']
49 tag_name = request.GET['tag']
40
50
41 threads = get_list_or_404(Post, tag = tag_name)
51 threads = get_list_or_404(Post, tag = tag_name)
42
52
43 context = RequestContext(request)
53 context = RequestContext(request)
44 context['threads'] = None if len(threads) == 0 else threads
54 context['threads'] = None if len(threads) == 0 else threads
45 context['tag'] = tag_name
55 context['tag'] = tag_name
46
56
47 return render(request, 'posting_general.html',
57 return render(request, 'posting_general.html',
48 context)
58 context)
49
59
50 def thread(request, id):
60 def thread(request, id):
51 """Get all thread posts"""
61 """Get all thread posts"""
52
62
53 # TODO Show 404 if there is no such thread
63 # TODO Show 404 if there is no such thread
54
64
55 posts = Post.objects.get_thread(id)
65 posts = Post.objects.get_thread(id)
56
66
57 context = RequestContext(request)
67 context = RequestContext(request)
58 context['posts'] = posts
68 context['posts'] = posts
59
69
60 return render(request, 'thread.html', context)
70 return render(request, 'thread.html', context)
61
71
62 def login(request):
72 def login(request):
63 """Log in as admin"""
73 """Log in as admin"""
64
74
65 if 'name' in request.POST and 'password' in request.POST:
75 if 'name' in request.POST and 'password' in request.POST:
66 request.session['admin'] = False
76 request.session['admin'] = False
67
77
68 isAdmin = len(Admin.objects.filter(name = request.POST['name'],
78 isAdmin = len(Admin.objects.filter(name = request.POST['name'],
69 password = request.POST['password'])) > 0
79 password = request.POST['password'])) > 0
70
80
71 if isAdmin :
81 if isAdmin :
72 request.session['admin'] = True
82 request.session['admin'] = True
73
83
74 response = HttpResponseRedirect('/boards')
84 response = HttpResponseRedirect('/boards')
75
85
76 else:
86 else:
77 response = render(request, 'login.html', {'error' : 'Login error'})
87 response = render(request, 'login.html', {'error' : 'Login error'})
78 else:
88 else:
79 response = render(request, 'login.html', {})
89 response = render(request, 'login.html', {})
80
90
81 return response
91 return response
82
92
83 def logout(request):
93 def logout(request):
84 request.session['admin'] = False
94 request.session['admin'] = False
85 return HttpResponseRedirect('/boards')
95 return HttpResponseRedirect('/boards')
1 NO CONTENT: modified file, binary diff hidden
NO CONTENT: modified file, binary diff hidden
@@ -1,20 +1,20 b''
1 <!DOCTYPE html>
1 <!DOCTYPE html>
2 <html>
2 <html>
3 <head>
3 <head>
4 <link rel="stylesheet" type="text/css" href="{{ STATIC_URL }}css/base_page.css" media="all"/>
4 <link rel="stylesheet" type="text/css" href="{{ STATIC_URL }}css/base_page.css" media="all"/>
5 {% block head %}{% endblock %}
5 {% block head %}{% endblock %}
6 </head>
6 </head>
7 <body>
7 <body>
8 <div id="admin_panel">
8 <div class="admin_panel">
9
9
10 {% if request.session.admin == True %}
10 {% if request.session.admin == True %}
11 Admin panel TODO: Need to implement <BR />
11 Admin panel TODO: Need to implement <BR />
12 {% endif %}
12 {% endif %}
13
13
14
14
15 </div>
15 </div>
16
16
17 {% block content %}{% endblock %}
17 {% block content %}{% endblock %}
18
18
19 </body>
19 </body>
20 </html> No newline at end of file
20 </html>
@@ -1,28 +1,52 b''
1 {% extends "base.html" %}
1 {% extends "base.html" %}
2
2
3 {% block content %}
3 {% block content %}
4
4
5 {% if threads %}
5 {% if threads %}
6 {% for thread in threads %}
6 {% for thread in threads %}
7 <span class="title">{{ thread.title }}</span><br />
7 <span class="title">{{ thread.title }}</span><br />
8 <span class="text">{{ thread.text }}</span><br />
8 <span class="text">{{ thread.text }}</span><br />
9 <a class="link" href="/boards/post/{{ thread.id }}/">View
9 <a class="link" href="/boards/post/{{ thread.id }}/">View
10 thread</a>
10 thread</a>
11 <hr />
11 <hr />
12 {% endfor %}
12 {% endfor %}
13 {% else %}
13 {% else %}
14 No threads found.
14 No threads found.
15 <hr />
15 <hr />
16 {% endif %}
16 {% endif %}
17
17
18 <div class="post-form">
18 <div class="post-form">
19 <form action="/boards/" method="post">{% csrf_token %}
19 <form action="/boards/" method="post">{% csrf_token %}
20 {{ form.as_p }}
20 <div class="input_field_name">
21 <input type="submit" value="Post!" />
21 <span class=input_field_error>{{ form.title.errors }}</span>
22
23 <label class=input_field_name for="title">Title</label>
24 {{ form.title }}
25 </div>
26
27 <div class="input_field_name">
28 <span class=input_field_error>{{ form.text.errors }}</span>
29
30 <label class=input_field_name for="text">Text</label>
31 {{ form.text }}
32 </div>
33
34 <div class="input_field_name">
35 <label class=input_field_name for="image">Avatar</label>
36 {{ form.image }}
37 </div>
38
39 <div class="input_field_name">
40 <span class=input_field_error>{{ form.tags.errors}}</span>
41 <label class=input_field_name for="tags">Name</label>
42 {{ form.tags }}
43 </div>
44
45 <input type="submit" value="Create" />
22 </form>
46 </form>
23 </div>
47 </div>
24
48
25 <HR />
49 <HR />
26
50
27 {% endblock %}
51 {% endblock %}
28
52
General Comments 0
You need to be logged in to leave comments. Login now