-
Notifications
You must be signed in to change notification settings - Fork 0
/
discussions.py
301 lines (259 loc) · 13.8 KB
/
discussions.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# discussions.py
# Discussion pages for CedarRapidsOnline web service
# Copyright (C) 2009 Clear Perception Solutions, LLC.
# Written by Trevis J. Rothwell <tjr@gnu.org>
#
# This file is part of the CedarRapidsOnline web service.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import cherrypy
import pgdb
import string
import sqlutils
import pageutils
database_connect_fields = sqlutils.database_connect_fields
class DiscussionsPage:
def default(self, discussion_id=None):
# We want to be able to have URLs like /discussions/discussion-id
# The CherryPy index methods can't accept parameters like that, but
# instead we route through the default method. QED.
return self.index (discussion_id)
default.exposed = True
def index (self, discussion_id=None):
# If discussion_id is None, display main discussion table of contents.
# Else, display specified discussion.
# Available to all, logged in or not.
# Toplevel index.
if (discussion_id == None):
description = None
results = None
author_description = None
author_results = []
# Get discussion listing from database
try:
# Try to connect to the database.
dbconnection = pgdb.connect (database_connect_fields)
dbcursor = dbconnection.cursor()
dbcursor.execute ("SELECT * FROM discussions WHERE refers_to IS null ORDER BY creation_date")
# Get the cursor description and results from the query.
description = dbcursor.description
results = dbcursor.fetchall()
# Get and store the user (author) data.
for result in results:
dbcursor.execute ("SELECT * FROM users WHERE user_id=%s",
[str(result[sqlutils.getfieldindex("author_id", description)])])
if (dbcursor.description <> None):
author_description = dbcursor.description
author_results.append (dbcursor.fetchone())
# Close the database cursor and connection.
dbcursor.close()
dbconnection.close()
except:
return pageutils.generate_page ("Database Error",
"<div class=\"error\">Can't get discussion data.</div>\n")
# Build the page.
pagetext = ""
if (pageutils.is_logged_in_p()):
pagetext += "<a href=\"/discussions/new\">Start New Discussion</a>\n"
else:
pagetext += "<a href=\"/login\">Log In</a> to start a new discussion</a>\n"
pagetext += "<ul>\n"
for result in results:
pagetext += "<li>\n"
pagetext += ("<a href=\"/discussions/" +
str(result[sqlutils.getfieldindex ("discussion_id", description)]) +
"\">" + result[sqlutils.getfieldindex ("subject", description)] + "</a> (posted by ")
for author in author_results:
if author == None:
continue
if author[0] == result[sqlutils.getfieldindex ("author_id", description)]:
pagetext += author[sqlutils.getfieldindex ("name", author_description)]
pagetext += " on " + result[sqlutils.getfieldindex ("creation_date", description)] + ")\n"
pagetext += "</li>\n"
pagetext += "</ul>\n"
return pageutils.generate_page ("Discussions", pagetext)
# Display a specific discussion.
else:
# Make sure we have a potentially-valid discussion id.
try:
discussion_id = str(int(discussion_id))
except:
return pageutils.generate_page ("Invalid Discussion",
"<div class=\"error\">Can't present the requested discussion.</div>\n")
description = None
results = None
reply_results = []
author_description = None
author_results = []
# Get discussion listing from database
try:
# Try to connect to the database.
dbconnection = pgdb.connect (database_connect_fields)
dbcursor = dbconnection.cursor()
dbcursor.execute ("SELECT * FROM discussions WHERE discussion_id=%s", [discussion_id])
description = dbcursor.description
results = dbcursor.fetchone()
# Get the user (author) data for the main discussion.
dbcursor.execute ("SELECT * FROM users WHERE user_id=%s",
[str(results[sqlutils.getfieldindex("author_id", description)])])
author_description = dbcursor.description
author_results.append (dbcursor.fetchone())
# Get any comments/replies for this discussion thread.
dbcursor.execute ("SELECT * FROM discussions WHERE refers_to=%s", [discussion_id])
reply_results = dbcursor.fetchall()
# Get and store the user (author) data.
for result in reply_results:
dbcursor.execute ("SELECT * FROM users WHERE user_id=%s",
[str(result[sqlutils.getfieldindex("author_id", description)])])
author_results.append (dbcursor.fetchone())
# Close the database cursor and connection.
dbcursor.close()
dbconnection.close()
except:
return pageutils.generate_page ("Database Error",
"<div class=\"error\">Can't get discussion data.</div>\n")
# Build page.
pagetitle = results[sqlutils.getfieldindex("subject", description)]
pagetext = "<p>" + results[sqlutils.getfieldindex("body", description)] + "</p>\n"
for author in author_results:
if author == None:
continue
# Find the author info to display.
if author[0] == results[sqlutils.getfieldindex ("author_id", description)]:
pagetext += "<p><i>posted by " + author[sqlutils.getfieldindex ("name", author_description)]
pagetext += (" on " + results[sqlutils.getfieldindex ("creation_date", description)] +
"</i></p>\n")
break
if (pageutils.is_admin_p()):
pagetext += ("<p>[<a href=\"/admin/discussions/delete/" +
str(results[sqlutils.getfieldindex("discussion_id", description)]) +
"\">Delete Discussion</a>]</p>")
pagetext += "<hr width=\"50%\">\n"
pagetext += "<h3>Replies</h3>\n"
# Do we have any replies to show?
if (reply_results <> None):
for result in reply_results:
pagetext += "<p>"
pagetext += result[sqlutils.getfieldindex ("body", description)]
for author in author_results:
if author == None:
continue
# Find the author info to display.
if author[0] == result[sqlutils.getfieldindex ("author_id", description)]:
pagetext += "<p><i>posted by " + author[sqlutils.getfieldindex ("name", author_description)]
pagetext += (" on " +
result[sqlutils.getfieldindex ("creation_date", description)] +
"</i></p>\n")
break
pagetext += "</p>\n"
# If the user is admin, post link to delete the reply.
if (pageutils.is_admin_p()):
pagetext += ("<p>[<a href=\"/admin/discussions/delete/" +
str(result[sqlutils.getfieldindex ("discussion_id", description)]) +
"\">Delete Reply</a>]</p>\n")
pagetext += "<hr width=50%>\n"
# If user is logged in, post link to add a reply.
if (pageutils.is_logged_in_p()):
pagetext += "<p><a href=\"/discussions/reply/" + discussion_id + "\">Add a reply</a></p>\n"
else:
pagetext += "<p><a href=\"/login\">Log in</a> to add a reply</a></p>\n"
# Generate page
return pageutils.generate_page (pagetitle, pagetext)
index.exposed = True
def reply (self, discussion_id=None):
# Verify user is logged in.
if (not pageutils.is_logged_in_p()):
raise cherrypy.HTTPRedirect ("/login")
# Verify a discussion_id was supplied, so we know where this comment goes.
if (discussion_id == None):
return pageutils.generate_page ("Discussion Reference Missing",
"<div class=\"error\"><p>Unable to add comment.</p></div>")
pagetext = ""
pagetitle = "Add a Reply"
pagetext += "<form action=\"/discussions/process\" method=\"post\">"
pagetext += "<textarea cols=80 rows=10 name=\"body\"></textarea>\n"
pagetext += "<br><br>"
pagetext += "<input type=\"hidden\" name=\"refers_to\" value=\"" + (str(discussion_id)) + "\">\n"
pagetext += "<input type=\"submit\" value=\"Add Comment\">"
pagetext += "</form>"
pagetext += pageutils.generate_disclaimer()
return pageutils.generate_page (pagetitle, pagetext)
reply.exposed = True
def new (self):
# Verify user is logged in.
if (not pageutils.is_logged_in_p()):
raise cherrypy.HTTPRedirect ("/login/access")
pagetext = ""
pagetitle = "Start a New Discussion"
pagetext += "<form action=\"/discussions/process\" method=\"post\">"
pagetext += "<b>Subject</b>:"
pagetext += "<br>"
pagetext += "<input type=\"text\" name=\"subject\">\n"
pagetext += "<br><br>"
pagetext += "<b>Message</b>:\n<br>\n"
pagetext += "<textarea cols=80 rows=10 name=\"body\"></textarea>\n"
pagetext += "<br><br>"
pagetext += "<input type=\"submit\" value=\"Start New Discussion\">"
pagetext += "</form>"
pagetext += pageutils.generate_disclaimer()
return pageutils.generate_page (pagetitle, pagetext)
new.exposed = True
def process (self, body=None, subject=None, refers_to=None):
# Verify user is logged in.
if (not pageutils.is_logged_in_p()):
raise cherrypy.HTTPRedirect ("/login/access")
# FIXME: Make sure we have all of the data we need in the form.
# Remove any leading or trailing spaces from comment text.
if (body <> None):
body = string.strip(body)
if (subject <> None):
subject = string.strip(subject)
# Make sure refers_to, if it exists, is an integer.
if (refers_to <> None):
try:
refers_to = str(int(refers_to))
except:
return pageutils.generate_page ("Invalid Reference",
"<div class=\"error\">Unable to add discussion element.</div>\n")
user_id = pageutils.get_user_id()
if (user_id == None):
raise cherrypy.HTTPRedirect ("/login/access")
# Insert the discussion / comment into the database.
try:
# Connect to the database and insert the values.
dbconnection = pgdb.connect (database_connect_fields)
dbcursor = dbconnection.cursor()
# Category value currently unused; default to 0.
# Is this a top-level discussion?
if (refers_to == None):
dbcursor.execute ("INSERT INTO discussions (category, author_id, subject, body, display, creation_date) " +
"VALUES (%s, %s, %s, %s, %s, current_timestamp)",
["0", str(user_id), subject, body, "1"])
# Or is this a reply?
else:
dbcursor.execute ("INSERT INTO discussions (refers_to, category, author_id, subject, body, display, creation_date) " +
"VALUES (%s, %s, %s, %s, %s, %s, current_timestamp)",
[str(refers_to), "0", str(user_id), subject, body, "1"])
dbconnection.commit()
# Close the database cursor and connection.
dbcursor.close()
dbconnection.close()
except:
return pageutils.generate_page ("Database Error",
"<div class=\"error\">Unable to add discussion element.</div>\n")
if (refers_to == None):
raise cherrypy.HTTPRedirect ("/discussions/")
else:
raise cherrypy.HTTPRedirect ("/discussions/" + str(refers_to))
process.exposed = True