-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathviews.py
135 lines (96 loc) · 5.14 KB
/
views.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
from django.shortcuts import render
from django.http import Http404, HttpResponseRedirect
from boards import DefaultBoard
from models import Post
from evennia.utils import ansi
from forms import PostForm, ReplyForm
# Create your views here.
def show_boardlist(request):
if not request.user.is_authenticated or request.user.username == "":
return render(request, 'login.html', {})
boards = DefaultBoard.objects.get_all_visible_boards(request.user)
context = {'boards': boards, 'page_title': 'Forums'}
# make the variables in 'context' available to the web page template
return render(request, 'boardlist.html', context)
def show_board(request, board_id):
if not request.user.is_authenticated or request.user.username == "":
return render(request, 'login.html', {})
try:
board = DefaultBoard.objects.get(pk=board_id)
if not board.access(request.user, access_type="read", default=False):
return render(request, 'board_noperm.html', {})
can_post = board.access(request.user, access_type="post", default=False)
threads = board.threads(request.user)
context = {'board': board, 'threads': threads, 'can_post': can_post,
'board_id': board.id, 'page_title': 'Forums - ' + board.name}
return render(request, 'board.html', context)
except (DefaultBoard.DoesNotExist, DefaultBoard.MultipleObjectsReturned):
return render(request, 'board_noperm.html', {})
def show_thread(request, board_id, post_id):
if not request.user.is_authenticated or request.user.username == "":
return render(request, 'login.html', {})
try:
post = Post.objects.get(pk=post_id)
board = post.db_board
if not board.access(request.user, access_type="read", default=False):
return render(request, 'board_noperm.html', {})
can_post = board.access(request.user, access_type="post", default=False)
plaintext = ansi.strip_ansi(post.db_text)
setattr(post, 'plaintext', plaintext)
post.mark_read(request.user, True)
replies = Post.objects.filter(db_parent=post).order_by('db_date_created')
for r in replies:
plaintext = ansi.strip_ansi(r.db_text)
setattr(r, 'plaintext', plaintext)
r.mark_read(request.user, True)
form = ReplyForm()
context = {'board': board, 'post': post, 'replies': replies, 'can_post': can_post,
'board_id': board, 'post_id': post, 'form': form,
'page_title': 'Forums - ' + post.db_subject}
return render(request, 'thread.html', context)
except (Post.DoesNotExist, Post.MultipleObjectsReturned):
return Http404("Error accessing boards.")
def submit_post(request, board_id):
if not request.user.is_authenticated or request.user.username == "":
return render(request, 'login.html', {})
try:
board = DefaultBoard.objects.get(pk=board_id)
if not board.access(request.user, access_type="post", default=False):
return render(request, 'board_noperm.html', {})
if request.method == "POST":
# Actual submission
form = PostForm(request.POST)
if not form.is_valid():
return Http404("Error submitting post.")
text = form.cleaned_data['text']
new_post = board.create_post(subject=form.cleaned_data['subject'], text=text,
author_name=request.user.username, author_player=request.user)
return HttpResponseRedirect("/boards/" + str(board.id) + "/" + str(new_post.id) + "/")
else:
form = PostForm()
context = {'board': board, 'board_id': board.id, 'form': form}
return render(request, 'submit_post.html', context)
except (Board.DoesNotExist, Board.MultipleObjectsReturned):
return Http404("Error accessing boards.")
def submit_reply(request, board_id, post_id):
if not request.user.is_authenticated or request.user.username == "":
return render(request, 'login.html', {})
try:
board = DefaultBoard.objects.get(pk=board_id)
post = Post.objects.get(pk=post_id)
if not board.access(request.user, access_type="post", default=False):
return render(request, 'board_noperm.html', {})
if request.method == "POST":
form = ReplyForm(request.POST)
if not form.is_valid():
return Http404("Error submitting post.")
text = form.cleaned_data['text']
board.create_post(subject="Re: " + post.db_subject, text=text, author_name=request.user.username,
author_player=request.user, parent=post)
return HttpResponseRedirect("/boards/" + str(board.id) + "/" + str(post.id) + "/")
else:
form = ReplyForm()
context = {'board': board, 'board_id': board.id, 'post_id': post.id, 'form': form}
return render(request, 'submit_reply.html', context)
except (Board.DoesNotExist, Board.MultipleObjectsReturned, Post.DoesNotExist, Post.MultipleObjectsReturned):
return Http404("Error accessing boards.")