1 # BiaWeb Website content manager (c) 2010 V.Harishankar
2 # Database handling functions
9 # function to get the template text for a particular template
10 def get_template_text (dbname
, template_name
):
12 conn
= sqlite3
.connect (dbname
)
14 c
.execute ("SELECT template_content FROM templates WHERE template_name=?",
23 # function to retrieve the templates from the database
24 def get_templates (dbname
):
26 conn
= sqlite3
.connect (dbname
)
28 c
.execute ("SELECT * FROM templates;")
36 # function to get an article from the database
37 def get_article (dbname
, artid
):
39 conn
= sqlite3
.connect (dbname
)
41 c
.execute ("SELECT * FROM articles WHERE aid=?;", (artid
,))
49 # function to create an article
50 def create_article (dbname
, title
, summary
, keywords
, content
, catid
, stub
, rating
):
52 creattime
= time
.time ()
55 conn
= sqlite3
.connect (dbname
)
57 c
.execute ("INSERT INTO articles (title, summary, keywords, content, cdate, mdate, cid, stub, rating) \
58 VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?);", (title
, summary
, keywords
, content
, creattime
,
59 modtime
, catid
, stub
, rating
))
66 # function to update an article
67 def update_article (dbname
, aid
, title
, summary
, keywords
, content
, catid
, stub
, rating
):
68 # modification time only to be updated
69 modtime
= time
.time ()
71 conn
= sqlite3
.connect (dbname
)
73 c
.execute ("UPDATE articles SET title=?, summary=?, keywords=?, content=?, mdate=?, cid=?, \
74 stub=?, rating=? WHERE aid=?;",
75 (title
, summary
, keywords
, content
, modtime
, catid
, stub
, rating
, aid
))
82 # function to delete an article
83 def delete_article (dbname
, artid
):
85 conn
= sqlite3
.connect (dbname
)
87 c
.execute ("DELETE FROM articles WHERE aid=?;", (artid
,))
94 # function to get a category from the database
95 def get_category (dbname
, catid
):
97 conn
= sqlite3
.connect (dbname
)
99 c
.execute ("SELECT * FROM categories WHERE cid=?", (catid
,))
104 except sqlite3
.Error
:
107 # function to update a category
108 def update_category (dbname
, catid
, cat_name
, cat_desc
, cat_stub
):
110 conn
= sqlite3
.connect (dbname
)
112 c
.execute ("UPDATE categories SET name=?, desc=?, stub=? WHERE cid=?;",
113 (cat_name
, cat_desc
, cat_stub
, catid
))
117 except sqlite3
.Error
:
120 # function to remove a category
121 def remove_category (dbname
, catid
):
123 conn
= sqlite3
.connect (dbname
)
125 c
.execute ("DELETE FROM categories WHERE cid=?;", (catid
,))
126 c
.execute ("DELETE FROM articles WHERE cid=?;", (catid
,))
130 except sqlite3
.Error
:
134 # function to create a category
135 def create_category (dbname
, category_name
, category_desc
, category_stub
):
137 conn
= sqlite3
.connect (dbname
)
139 c
.execute ("INSERT INTO categories (name, desc, stub) VALUES (?, ?, ?);",
140 (category_name
, category_desc
, category_stub
))
144 except sqlite3
.Error
:
147 # function to set the configuration and update the database
148 def set_configuration (dbname
, site_title
, site_url
, keywords
, description
, copyright
,
151 conn
= sqlite3
.connect (dbname
)
153 c
.executemany ("UPDATE config SET config_param=? WHERE config_name=?;",
154 [[site_url
, "Website URL"],
155 [site_title
, "Website Title"],
156 [keywords
, "Keywords"],
157 [description
, "Description"],
158 [num_rss
, "No. of RSS items"],
159 [dest_path
, "Destination path"],
160 [copyright
, "Copyright"]])
164 except sqlite3
.Error
:
167 # function to get the existing site configuration and return it as a tuple
168 def get_configuration (dbname
):
170 conn
= sqlite3
.connect (dbname
)
172 c
.execute ("SELECT * FROM config;")
176 for name
, param
in recs
:
177 if name
== "Website URL":
179 elif name
== "Website Title":
180 website_title
= param
181 elif name
== "Keywords":
183 elif name
== "Description":
185 elif name
== "No. of RSS items":
186 num_rss
= int (param
)
187 elif name
== "Destination path":
189 elif name
== "Copyright":
191 return (website_url
, website_title
, keywords
, description
, num_rss
, destination
, copyright
)
192 except sqlite3
.Error
:
195 # Function to get list of articles (either full list or just in a category
196 def get_articles (dbname
, category_id
=None):
198 conn
= sqlite3
.connect (dbname
)
200 if category_id
== None:
201 c
.execute ("SELECT * FROM articles;")
203 c
.execute ("SELECT * FROM articles WHERE cid=?", (category_id
,))
208 except sqlite3
.Error
:
211 # Function to get list of categories and return a (category_id, category_name) array
212 def get_categories (dbname
):
214 conn
= sqlite3
.connect (dbname
)
216 c
.execute ("SELECT * FROM categories;")
221 except sqlite3
.Error
:
224 # function to create a new site database
225 def create_db (dbname
, site_title
, site_url
, keywords
, description
, copyright
,
228 if os
.path
.exists (dbname
):
234 conn
= sqlite3
.connect (dbname
)
236 c
.execute ("CREATE TABLE IF NOT EXISTS \
237 categories (cid INTEGER PRIMARY KEY, \
238 name TEXT, desc TEXT, \
240 c
.execute ("CREATE TABLE IF NOT EXISTS \
241 articles (aid INTEGER PRIMARY KEY, \
242 title TEXT, summary TEXT, keywords TEXT, \
243 content TEXT, cdate NUMERIC, mdate NUMERIC, cid NUMERIC, \
244 stub TEXT, rating NUMERIC);")
245 c
.execute ("CREATE TABLE IF NOT EXISTS \
246 config (config_name TEXT, config_param TEXT);")
248 c
.execute ("CREATE TABLE IF NOT EXISTS \
249 templates (template_name TEXT, template_content);")
251 template_main
= """<?xml version="1.0" encoding="UTF-8"?>
252 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
253 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
255 <title>${site_title}</title>
256 <base href="${site_url}" />
257 <meta name="generator" content="BiaWeb"/>
258 <meta name="keywords" content="${meta_keywords}"/>
259 <meta name="description" content="${meta_description}"/>
260 <link rel="StyleSheet" type="text/css" href="style.css" />
261 <link rel="alternate" type="application/rss+xml" title="Entries RSS 2.0" href="subscribe.xml" />
265 <h1><a href="${site_url}">${page_title}</a></h1>
266 <div class="headerdesc">${page_desc}</div>
273 ${list_of_categories}
277 <a href="subscribe.xml">Latest articles (RSS)</a>
279 <form action="cgi-bin/search.py" method="post" enctype="multipart/form-data">
280 <p><input type="text" name="query" maxlength="255" style="width:142px;border: 1px inset #5A5A5A; color:#5A5A5A; background-color:#FFFFFF;" value="" /><br />
281 <input type="submit" value="Search" /><br />
282 <input type="hidden" name="fromsearch" value="fromsearch" />
283 <input type="radio" name="criteria" value="1" checked="checked" />All words<br />
284 <input type="radio" name="criteria" value="2" />Any word</p>
287 <div id="footer">${copyright}<br />Site generated by
288 <a href=\"http://harishankar.org/software/biaweb.php\">BiaWeb</a> created by V. Harishankar</div>
292 template_article_bit
= """<h2>${article_title}</h2>
293 <div class="modified">Created: ${article_cdate} | Last modified: ${article_mdate}</div>
294 <div class="rating">Rating: ${rating}</div>
295 <div class="content">${article_contents}</div>
298 template_news_item_bit
= """<h3><a href="${news_link}">${news_title}</a></h3>
299 <div class="modified">${news_datetime}</div>
300 <div class="content">${news_description}</div>
303 template_index_bit
= """<h2>Welcome to ${site_name}</h2>
304 <div class="content">
305 Welcome to my site, ${site_name}.
307 <h2>Latest Articles</h2>
311 template_table_bit
= """<h2>${category_title}</h2>
312 <p>${category_desc}</p>
313 <table class="categorytable">
316 <td style="width:50%">Title</td>
327 template_tablerow_bit
= """<tr>
328 <td style="width:50%"><a href="${article_url}">${title}</a></td>
334 template_style
= """body {
335 font-family: "Bitstream Vera Sans", Verdana, Arial, Sans Serif;
337 background-color: #ffffff;
343 background-color: #efefef;
367 background-color: #efefef;
371 font-variant: small-caps;
384 .categorytable thead {
399 font-family: "Bitstream Vera Serif", Serif;
408 border-bottom: 1px solid;
409 border-color: #888888;
417 h1 a, h1 a:hover, h1 a:visited, h2 a:active {
418 text-decoration: none;
422 background-color: #efefef;
424 h2 a, h2 a:hover, h2 a:visited, h2 a:active {
425 text-decoration: none;
439 c
.executemany ("INSERT INTO templates (template_name, template_content) VALUES (?, ?);",
440 [["main_template", template_main
],
441 ["article_bit", template_article_bit
],
442 ["news_bit", template_news_item_bit
],
443 ["table_bit", template_table_bit
],
444 ["tablerow_bit", template_tablerow_bit
],
445 ["stylesheet", template_style
],
446 ["index_bit", template_index_bit
]])
448 c
.executemany ("INSERT INTO config (config_name, config_param) VALUES (?, ?); ",
449 [["Website URL", site_url
],
450 ["Website Title", site_title
],
451 ["Keywords", keywords
],
452 ["Description", description
],
453 ["No. of RSS items", num_rss
],
454 ["Destination path", dest_path
],
455 ["Copyright", copyright
]])
460 except sqlite3
.Error
: