1 # BiaWeb Website content manager (c) 2010 V.Harishankar
2 # Database handling functions
9 # function to retrieve the templates from the database
10 def get_templates (dbname
):
12 conn
= sqlite3
.connect (dbname
)
14 c
.execute ("SELECT * FROM templates;")
22 # function to get an article from the database
23 def get_article (dbname
, artid
):
25 conn
= sqlite3
.connect (dbname
)
27 c
.execute ("SELECT * FROM articles WHERE aid=?;", (artid
,))
35 # function to create an article
36 def create_article (dbname
, title
, summary
, keywords
, content
, catid
, stub
, rating
):
38 creattime
= time
.time ()
41 conn
= sqlite3
.connect (dbname
)
43 c
.execute ("INSERT INTO articles (title, summary, keywords, content, cdate, mdate, cid, stub, rating) \
44 VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?);", (title
, summary
, keywords
, content
, creattime
,
45 modtime
, catid
, stub
, rating
))
52 # function to update an article
53 def update_article (dbname
, aid
, title
, summary
, keywords
, content
, catid
, stub
, rating
):
54 # modification time only to be updated
55 modtime
= time
.time ()
57 conn
= sqlite3
.connect (dbname
)
59 c
.execute ("UPDATE articles SET title=?, summary=?, keywords=?, content=?, mdate=?, cid=?, \
60 stub=?, rating=? WHERE aid=?;",
61 (title
, summary
, keywords
, content
, modtime
, catid
, stub
, rating
, aid
))
68 # function to delete an article
69 def delete_article (dbname
, artid
):
71 conn
= sqlite3
.connect (dbname
)
73 c
.execute ("DELETE FROM articles WHERE aid=?;", (artid
,))
80 # function to get a category from the database
81 def get_category (dbname
, catid
):
83 conn
= sqlite3
.connect (dbname
)
85 c
.execute ("SELECT * FROM categories WHERE cid=?", (catid
,))
93 # function to update a category
94 def update_category (dbname
, catid
, cat_name
, cat_desc
, cat_stub
):
96 conn
= sqlite3
.connect (dbname
)
98 c
.execute ("UPDATE categories SET name=?, desc=?, stub=? WHERE cid=?;",
99 (cat_name
, cat_desc
, cat_stub
, catid
))
103 except sqlite3
.Error
:
106 # function to remove a category
107 def remove_category (dbname
, catid
):
109 conn
= sqlite3
.connect (dbname
)
111 c
.execute ("DELETE FROM categories WHERE cid=?;", (catid
,))
112 c
.execute ("DELETE FROM articles WHERE cid=?;", (catid
,))
116 except sqlite3
.Error
:
120 # function to create a category
121 def create_category (dbname
, category_name
, category_desc
, category_stub
):
123 conn
= sqlite3
.connect (dbname
)
125 c
.execute ("INSERT INTO categories (name, desc, stub) VALUES (?, ?, ?);",
126 (category_name
, category_desc
, category_stub
))
130 except sqlite3
.Error
:
133 # function to set the configuration and update the database
134 def set_configuration (dbname
, site_title
, site_url
, keywords
, description
, copyright
,
137 conn
= sqlite3
.connect (dbname
)
139 c
.executemany ("UPDATE config SET config_param=? WHERE config_name=?;",
140 [[site_url
, "Website URL"],
141 [site_title
, "Website Title"],
142 [keywords
, "Keywords"],
143 [description
, "Description"],
144 [num_rss
, "No. of RSS items"],
145 [dest_path
, "Destination path"],
146 [copyright
, "Copyright"]])
150 except sqlite3
.Error
:
153 # function to get the existing site configuration and return it as a tuple
154 def get_configuration (dbname
):
156 conn
= sqlite3
.connect (dbname
)
158 c
.execute ("SELECT * FROM config;")
162 for name
, param
in recs
:
163 if name
== "Website URL":
165 elif name
== "Website Title":
166 website_title
= param
167 elif name
== "Keywords":
169 elif name
== "Description":
171 elif name
== "No. of RSS items":
172 num_rss
= int (param
)
173 elif name
== "Destination path":
175 elif name
== "Copyright":
177 return (website_url
, website_title
, keywords
, description
, num_rss
, destination
, copyright
)
178 except sqlite3
.Error
:
181 # Function to get list of articles (either full list or just in a category
182 def get_articles (dbname
, category_id
=None):
184 conn
= sqlite3
.connect (dbname
)
186 if category_id
== None:
187 c
.execute ("SELECT * FROM articles;")
189 c
.execute ("SELECT * FROM articles WHERE cid=?", (category_id
,))
194 except sqlite3
.Error
:
197 # Function to get list of categories and return a (category_id, category_name) array
198 def get_categories (dbname
):
200 conn
= sqlite3
.connect (dbname
)
202 c
.execute ("SELECT * FROM categories;")
207 except sqlite3
.Error
:
210 # function to create a new site database
211 def create_db (dbname
, site_title
, site_url
, keywords
, description
, copyright
,
214 if os
.path
.exists (dbname
):
220 conn
= sqlite3
.connect (dbname
)
222 c
.execute ("CREATE TABLE IF NOT EXISTS \
223 categories (cid INTEGER PRIMARY KEY, \
224 name TEXT, desc TEXT, \
226 c
.execute ("CREATE TABLE IF NOT EXISTS \
227 articles (aid INTEGER PRIMARY KEY, \
228 title TEXT, summary TEXT, keywords TEXT, \
229 content TEXT, cdate NUMERIC, mdate NUMERIC, cid NUMERIC, \
230 stub TEXT, rating NUMERIC);")
231 c
.execute ("CREATE TABLE IF NOT EXISTS \
232 config (config_name TEXT, config_param TEXT);")
234 c
.execute ("CREATE TABLE IF NOT EXISTS \
235 templates (template_name TEXT, template_content);")
237 template_main
= """<?xml version="1.0" encoding="UTF-8"?>
238 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
239 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
241 <title>${site_title}</title>
242 <base href="${site_url}" />
243 <meta name="generator" content="BiaWeb"/>
244 <meta name="keywords" content="${meta_keywords}"/>
245 <meta name="description" content="${meta_description}"/>
246 <link rel="StyleSheet" type="text/css" href="style.css" />
247 <link rel="alternate" type="application/rss+xml" title="Entries RSS 2.0" href="subscribe.xml" />
251 <h1><a href="${site_url}">${page_title}</a></h1>
252 <div class="headerdesc">${page_desc}</div>
259 ${list_of_categories}
263 <a href="subscribe.xml">Latest articles (RSS)</a>
265 <form action="cgi-bin/search.py" method="post" enctype="multipart/form-data">
266 <p><input type="text" name="query" maxlength="255" style="width:142px;border: 1px inset #5A5A5A; color:#5A5A5A; background-color:#FFFFFF;" value="" /><br />
267 <input type="submit" value="Search" /><br />
268 <input type="hidden" name="fromsearch" value="fromsearch" />
269 <input type="radio" name="criteria" value="1" checked="checked" />All words<br />
270 <input type="radio" name="criteria" value="2" />Any word</p>
273 <div id="footer">${copyright}<br />Site generated by
274 <a href=\"http://harishankar.org/software/biaweb.php\">BiaWeb</a> created by V. Harishankar</div>
278 template_article_bit
= """<h2>${article_title}</h2>
279 <div class="modified">Created: ${article_cdate} | Last modified: ${article_mdate}</div>
280 <div class="rating">Rating: ${rating}</div>
281 <div class="content">${article_contents}</div>
284 template_news_item_bit
= """<h3><a href="${news_link}">${news_title}</a></h3>
285 <div class="modified">${news_datetime}</div>
286 <div class="content">${news_description}</div>
289 template_index_bit
= """<h2>Welcome to ${site_name}</h2>
290 <div class="content">
291 Welcome to my site, ${site_name}.
293 <h2>Latest Articles</h2>
297 template_table_bit
= """<h2>${category_title}</h2>
298 <p>${category_desc}</p>
299 <table class="categorytable">
302 <td style="width:50%">Title</td>
313 template_tablerow_bit
= """<tr>
314 <td style="width:50%"><a href="${article_url}">${title}</a></td>
320 template_style
= """body {
321 font-family: "Bitstream Vera Sans", Verdana, Arial, Sans Serif;
323 background-color: #ffffff;
329 background-color: #efefef;
353 background-color: #efefef;
357 font-variant: small-caps;
370 .categorytable thead {
385 font-family: "Bitstream Vera Serif", Serif;
394 border-bottom: 1px solid;
395 border-color: #888888;
403 h1 a, h1 a:hover, h1 a:visited, h2 a:active {
404 text-decoration: none;
408 background-color: #efefef;
410 h2 a, h2 a:hover, h2 a:visited, h2 a:active {
411 text-decoration: none;
425 c
.executemany ("INSERT INTO templates (template_name, template_content) VALUES (?, ?);",
426 [["main_template", template_main
],
427 ["article_bit", template_article_bit
],
428 ["news_bit", template_news_item_bit
],
429 ["table_bit", template_table_bit
],
430 ["tablerow_bit", template_tablerow_bit
],
431 ["stylesheet", template_style
],
432 ["index_bit", template_index_bit
]])
434 c
.executemany ("INSERT INTO config (config_name, config_param) VALUES (?, ?); ",
435 [["Website URL", site_url
],
436 ["Website Title", site_title
],
437 ["Keywords", keywords
],
438 ["Description", description
],
439 ["No. of RSS items", num_rss
],
440 ["Destination path", dest_path
],
441 ["Copyright", copyright
]])
446 except sqlite3
.Error
: