-
Notifications
You must be signed in to change notification settings - Fork 0
/
articles.py
200 lines (172 loc) · 8.78 KB
/
articles.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
# articles.py
# Article 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 ArticlesPage:
def index (self, article_slug=None):
# If article_slug is None, display main article table of contents.
# Else, display specified article.
# Available to all, logged in or not.
if (article_slug == None):
article_slug = "map"
description = None
results = None
comments_description = None
comments_results = None
author_results = []
author_description = None
# Get article, comment, and comment-author data from database.
try:
# Try to connect to the database.
dbconnection = pgdb.connect (database_connect_fields)
dbcursor = dbconnection.cursor()
dbcursor.execute ("SELECT * FROM articles WHERE slug=%s", [article_slug])
# Get the cursor description and results from the query.
description = dbcursor.description
results = dbcursor.fetchone()
# Get any comments for the article.
if (results <> None):
dbcursor.execute ("SELECT * FROM articles WHERE refers_to=%s",
[str(results[sqlutils.getfieldindex ("article_id", description)])])
comments_description = dbcursor.description
comments_results = dbcursor.fetchall()
# Store the user info for the author of the comment, for use when we display the comment.
for result in comments_results:
dbcursor.execute ("SELECT * FROM users WHERE user_id=%s",
[str(result[sqlutils.getfieldindex ("author_id", comments_description)])])
author_description = dbcursor.description
author_results.append (dbcursor.fetchone())
# Close the database cursor and connection.
dbcursor.close()
dbconnection.close()
except:
pass
if (results == None):
# The "welcome" article is the main front page. We need to have this page.
if (article_slug == "welcome"):
return pageutils.generate_page ("Welcome",
"You will need to create an article with the slug: welcome")
# We also need a "map" page.
elif (article_slug == "map"):
return pageutils.generate_page ("Map",
"You will need to create an article with the slug: map")
else:
raise cherrypy.HTTPRedirect ("/404")
# Obtain the article title from the database results.
pagetitle = ""
try:
pagetitle += results[sqlutils.getfieldindex ("title", description)]
except:
pagetitle = "Database Error."
# Obtain the article body from the database results.
pagetext = ""
try:
pagetext += results[sqlutils.getfieldindex ("body", description)]
except:
pagetext += "<p>Database Error.</p>"
# Do we want to show comments on this page?
try:
if (int(results[sqlutils.getfieldindex ("display", description)]) > 1):
pagetext += "<hr><h3>User Comments</h3>"
# Do we have any comments to show?
if (comments_results <> None):
for result in comments_results:
pagetext += "<p>"
pagetext += result[sqlutils.getfieldindex ("body", comments_description)]
for author in author_results:
if author == None:
continue
# Find the author info to display.
if author[0] == result[sqlutils.getfieldindex ("author_id", comments_description)]:
pagetext += "<p><i>posted by " + author[sqlutils.getfieldindex ("name", author_description)]
pagetext += " on " + result[sqlutils.getfieldindex ("creation_date", comments_description)] + "</i></p>\n"
break
# If the user is admin, post link to delete the comment.
if (pageutils.is_admin_p()):
pagetext += ("<p>[<a href=\"/admin/articles/delete/" +
str(result[sqlutils.getfieldindex ("article_id", comments_description)]) +
"\">Delete Comment</a>]</p>\n")
pagetext += "</p>"
pagetext += "<hr width=50%>\n"
# If user is logged in, post link to add a comment.
if (pageutils.is_logged_in_p()):
pagetext += "<p><a href=\"/articles/comment/" + article_slug + "\">Add a comment</a></p>\n"
else:
pagetext += "<p><a href=\"/login\">Log in</a> to add a comment</a></p>\n"
except:
pass
# Build the whole page and return it.
return pageutils.generate_page (pagetitle, pagetext)
index.exposed = True
# We want to be able to have URLs like /articles/foo-article
# The CherryPy index methods can't accept parameters like that, but
# instead we route through the default method. QED.
def default(self, article_slug=None):
return self.index (article_slug)
default.exposed = True
def comment (self, article_slug=None):
# Verify user is logged in.
if (not pageutils.is_logged_in_p()):
raise cherrypy.HTTPRedirect ("/login")
if (article_slug == None):
return pageutils.generate_page ("No Article Specified", "Unable to add comment.")
# Form to add a comment.
pagecontents = ""
pagecontents += "<form action=\"/articles/addcomment\" method=\"post\">"
pagecontents += "<textarea cols=80 rows=10 name=\"body\"></textarea>\n"
pagecontents += "<br><br>"
pagecontents += "<input type=\"hidden\" name=\"slug\" value=\"" + str(article_slug) + "\">"
pagecontents += "<input type=\"submit\" value=\"Add Comment\">"
pagecontents += "</form>"
pagecontents += pageutils.generate_disclaimer()
return pageutils.generate_page ("Add a comment", pagecontents)
comment.exposed = True
def addcomment (self, body, slug):
# Verify user is logged in.
if (not pageutils.is_logged_in_p()):
raise cherrypy.HTTPRedirect ("/login")
# Remove any leading or trailing spaces from comment text.
body = string.strip(body)
user_id = pageutils.get_user_id()
if (user_id == None):
raise cherrypy.HTTPRedirect ("/login")
try:
# Connect to the database and insert the values.
dbconnection = pgdb.connect (database_connect_fields)
dbcursor = dbconnection.cursor()
dbcursor.execute ("SELECT * FROM articles WHERE slug=%s", [slug])
results = dbcursor.fetchone()
if (results == None):
return pageutils.generate_page ("Invalid Article Specified", "Unable to post comment.")
article_id = results[0] # article_id is the first column in the table.
dbcursor.execute ("INSERT INTO articles (author_id, body, display, refers_to, creation_date) " +
"VALUES (%s, %s, %s, %s, current_timestamp)",
[str(user_id), body, "1", str(article_id)])
dbconnection.commit()
# Close the database cursor and connection.
dbcursor.close()
dbconnection.close()
except:
return pageutils.generate_page ("Invalid SQL Query", "Unable to add comment.")
addcomment.exposed = True