1 # BiaWeb Website content manager (c) 2010 V.Harishankar
2 # Database handling functions
9 # function to create an article
10 def create_article (dbname
, title
, summary
, keywords
, content
, catid
, stub
, rating
):
12 creattime
= time
.time ()
15 conn
= sqlite3
.connect (dbname
)
17 c
.execute ("INSERT INTO articles (title, summary, keywords, content, cdate, mdate, cid, stub, rating) \
18 VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?);", (title
, summary
, keywords
, content
, creattime
,
19 modtime
, catid
, stub
, rating
))
26 # function to delete an article
27 def delete_article (dbname
, artid
):
29 conn
= sqlite3
.connect (dbname
)
31 c
.execute ("DELETE FROM articles WHERE aid=?;", (artid
,))
38 # function to get a category from the database
39 def get_category (dbname
, catid
):
41 conn
= sqlite3
.connect (dbname
)
43 c
.execute ("SELECT * FROM categories WHERE cid=?", (catid
,))
51 # function to update a category
52 def update_category (dbname
, catid
, cat_name
, cat_desc
, cat_stub
):
54 conn
= sqlite3
.connect (dbname
)
56 c
.execute ("UPDATE categories SET name=?, desc=?, stub=? WHERE cid=?;",
57 (cat_name
, cat_desc
, cat_stub
, catid
))
64 # function to remove a category
65 def remove_category (dbname
, catid
):
67 conn
= sqlite3
.connect (dbname
)
69 c
.execute ("DELETE FROM categories WHERE cid=?;", (catid
,))
70 c
.execute ("DELETE FROM articles WHERE cid=?;", (catid
,))
78 # function to create a category
79 def create_category (dbname
, category_name
, category_desc
, category_stub
):
81 conn
= sqlite3
.connect (dbname
)
83 c
.execute ("INSERT INTO categories (name, desc, stub) VALUES (?, ?, ?);",
84 (category_name
, category_desc
, category_stub
))
91 # function to set the configuration and update the database
92 def set_configuration (dbname
, site_title
, site_url
, keywords
, description
, copyright
,
95 conn
= sqlite3
.connect (dbname
)
97 c
.executemany ("UPDATE config SET config_param=? WHERE config_name=?;",
98 [[site_url
, "Website URL"],
99 [site_title
, "Website Title"],
100 [keywords
, "Keywords"],
101 [description
, "Description"],
102 [num_rss
, "No. of RSS items"],
103 [dest_path
, "Destination path"],
104 [copyright
, "Copyright"]])
108 except sqlite3
.Error
:
111 # function to get the existing site configuration and return it as a tuple
112 def get_configuration (dbname
):
114 conn
= sqlite3
.connect (dbname
)
116 c
.execute ("SELECT * FROM config;")
120 for name
, param
in recs
:
121 if name
== "Website URL":
123 elif name
== "Website Title":
124 website_title
= param
125 elif name
== "Keywords":
127 elif name
== "Description":
129 elif name
== "No. of RSS items":
130 num_rss
= int (param
)
131 elif name
== "Destination path":
133 elif name
== "Copyright":
135 return (website_url
, website_title
, keywords
, description
, num_rss
, destination
, copyright
)
136 except sqlite3
.Error
:
139 # Function to get list of articles (either full list or just in a category
140 def get_articles (dbname
, category_id
=None):
142 conn
= sqlite3
.connect (dbname
)
144 if category_id
== None:
145 c
.execute ("SELECT * FROM articles;")
147 c
.execute ("SELECT * FROM articles WHERE cid=?", (category_id
,))
152 except sqlite3
.Error
:
155 # Function to get list of categories and return a (category_id, category_name) array
156 def get_categories (dbname
):
158 conn
= sqlite3
.connect (dbname
)
160 c
.execute ("SELECT * FROM categories;")
165 except sqlite3
.Error
:
168 # function to create a new site database
169 def create_db (dbname
, site_title
, site_url
, keywords
, description
, copyright
,
172 if os
.path
.exists (dbname
):
178 conn
= sqlite3
.connect (dbname
)
180 c
.execute ("CREATE TABLE IF NOT EXISTS \
181 categories (cid INTEGER PRIMARY KEY, \
182 name TEXT, desc TEXT, \
184 c
.execute ("CREATE TABLE IF NOT EXISTS \
185 articles (aid INTEGER PRIMARY KEY, \
186 title TEXT, summary TEXT, keywords TEXT, \
187 content TEXT, cdate NUMERIC, mdate NUMERIC, cid NUMERIC, \
188 stub TEXT, rating NUMERIC);")
189 c
.execute ("CREATE TABLE IF NOT EXISTS \
190 config (config_name TEXT, config_param TEXT);")
192 c
.execute ("CREATE TABLE IF NOT EXISTS \
193 templates (template_name TEXT, template_content);")
195 template_main
= """<?xml version="1.0" encoding="UTF-8"?>
196 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
197 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
199 <title>${site_title}</title>
200 <base href="${site_url}" />
201 <meta name="generator" content="BiaWeb"/>
202 <meta name="keywords" content="${meta_keywords}"/>
203 <meta name="description" content="${meta_description}"/>
204 <link rel="StyleSheet" type="text/css" href="style.css" />
205 <link rel="alternate" type="application/rss+xml" title="Entries RSS 2.0" href="subscribe.xml" />
209 <h1><a href="${site_url}">${page_title}</a></h1>
210 <div class="headerdesc">${page_desc}</div>
217 ${list_of_categories}
221 <a href="subscribe.xml">Latest articles (RSS)</a>
223 <form action="cgi-bin/search.py" method="post" enctype="multipart/form-data">
224 <p><input type="text" name="query" maxlength="255" style="width:142px;border: 1px inset #5A5A5A; color:#5A5A5A; background-color:#FFFFFF;" value="" /><br />
225 <input type="submit" value="Search" /><br />
226 <input type="hidden" name="fromsearch" value="fromsearch" />
227 <input type="radio" name="criteria" value="1" checked="checked" />All words<br />
228 <input type="radio" name="criteria" value="2" />Any word</p>
231 <div id="footer">${copyright}<br />Site generated by
232 <a href=\"http://harishankar.org/software/biaweb.php\">BiaWeb</a> created by V. Harishankar</div>
236 template_article_bit
= """<h2>${article_title}</h2>
237 <div class="modified">Created: ${article_cdate} | Last modified: ${article_mdate}</div>
238 <div class="rating">Rating: ${rating}</div>
239 <div class="content">${article_contents}</div>
242 template_news_item_bit
= """<h3><a href="${news_link}">${news_title}</a></h3>
243 <div class="modified">${news_datetime}</div>
244 <div class="content">${news_description}</div>
247 template_index_bit
= """<h2>Welcome to ${site_name}</h2>
248 <div class="content">
249 Welcome to my site, ${site_name}.
251 <h2>Latest Articles</h2>
255 template_table_bit
= """<h2>${category_title}</h2>
256 <p>${category_desc}</p>
257 <table class="categorytable">
260 <td style="width:50%">Title</td>
271 template_tablerow_bit
= """<tr>
272 <td style="width:50%"><a href="${article_url}">${title}</a></td>
278 template_style
= """body {
279 font-family: "Bitstream Vera Sans", Verdana, Arial, Sans Serif;
281 background-color: #ffffff;
287 background-color: #efefef;
311 background-color: #efefef;
315 font-variant: small-caps;
328 .categorytable thead {
343 font-family: "Bitstream Vera Serif", Serif;
352 border-bottom: 1px solid;
353 border-color: #888888;
361 h1 a, h1 a:hover, h1 a:visited, h2 a:active {
362 text-decoration: none;
366 background-color: #efefef;
368 h2 a, h2 a:hover, h2 a:visited, h2 a:active {
369 text-decoration: none;
383 c
.executemany ("INSERT INTO templates (template_name, template_content) VALUES (?, ?);",
384 [["main_template", template_main
],
385 ["article_bit", template_article_bit
],
386 ["news_bit", template_news_item_bit
],
387 ["table_bit", template_table_bit
],
388 ["tablerow_bit", template_tablerow_bit
],
389 ["stylesheet", template_style
],
390 ["index_bit", template_index_bit
]])
392 c
.executemany ("INSERT INTO config (config_name, config_param) VALUES (?, ?); ",
393 [["Website URL", site_url
],
394 ["Website Title", site_title
],
395 ["Keywords", keywords
],
396 ["Description", description
],
397 ["No. of RSS items", num_rss
],
398 ["Destination path", dest_path
],
399 ["Copyright", copyright
]])
404 except sqlite3
.Error
: