1 # BiaWeb Website content manager (c) 2010 V.Harishankar
2 # Database handling functions
8 # function to get a category from the database
9 def get_category (dbname
, catid
):
11 conn
= sqlite3
.connect (dbname
)
13 c
.execute ("SELECT * FROM categories WHERE cid=?", (catid
,))
21 # function to update a category
22 def update_category (dbname
, catid
, cat_name
, cat_desc
, cat_stub
):
24 conn
= sqlite3
.connect (dbname
)
26 c
.execute ("UPDATE categories SET name=?, desc=?, stub=? WHERE cid=?;",
27 (cat_name
, cat_desc
, cat_stub
, catid
))
34 # function to remove a category
35 def remove_category (dbname
, catid
):
37 conn
= sqlite3
.connect (dbname
)
39 c
.execute ("DELETE FROM categories WHERE cid=?;", (catid
,))
40 c
.execute ("DELETE FROM articles WHERE cid=?;", (catid
,))
47 # function to set the configuration and update the database
48 def set_configuration (dbname
, site_title
, site_url
, keywords
, description
, copyright
,
51 conn
= sqlite3
.connect (dbname
)
53 c
.executemany ("UPDATE config SET config_param=? WHERE config_name=?;",
54 [[site_url
, "Website URL"],
55 [site_title
, "Website Title"],
56 [keywords
, "Keywords"],
57 [description
, "Description"],
58 [num_rss
, "No. of RSS items"],
59 [dest_path
, "Destination path"],
60 [copyright
, "Copyright"]])
67 # function to get the existing site configuration and return it as a tuple
68 def get_configuration (dbname
):
70 conn
= sqlite3
.connect (dbname
)
72 c
.execute ("SELECT * FROM config;")
76 for name
, param
in recs
:
77 if name
== "Website URL":
79 elif name
== "Website Title":
81 elif name
== "Keywords":
83 elif name
== "Description":
85 elif name
== "No. of RSS items":
87 elif name
== "Destination path":
89 elif name
== "Copyright":
91 return (website_url
, website_title
, keywords
, description
, num_rss
, destination
, copyright
)
96 # function to create a category
97 def create_category (dbname
, category_name
, category_desc
, category_stub
):
99 conn
= sqlite3
.connect (dbname
)
101 c
.execute ("INSERT INTO categories (name, desc, stub) VALUES (?, ?, ?);",
102 (category_name
, category_desc
, category_stub
))
106 except sqlite3
.Error
:
109 # Function to get list of articles (either full list or just in a category
110 def get_articles (dbname
, category_id
=None):
112 conn
= sqlite3
.connect (dbname
)
114 if category_id
== None:
115 c
.execute ("SELECT * FROM articles;")
117 c
.execute ("SELECT * FROM articles WHERE cid=?", (category_id
,))
122 except sqlite3
.Error
:
125 # Function to get list of categories and return a (category_id, category_name) array
126 def get_categories (dbname
):
128 conn
= sqlite3
.connect (dbname
)
130 c
.execute ("SELECT * FROM categories;")
135 except sqlite3
.Error
:
138 # function to create a new site database
139 def create_db (dbname
, site_title
, site_url
, keywords
, description
, copyright
,
142 if os
.path
.exists (dbname
):
148 conn
= sqlite3
.connect (dbname
)
150 c
.execute ("CREATE TABLE IF NOT EXISTS \
151 categories (cid INTEGER PRIMARY KEY, \
152 name TEXT, desc TEXT, \
154 c
.execute ("CREATE TABLE IF NOT EXISTS \
155 articles (aid INTEGER PRIMARY KEY, \
156 title TEXT, summary TEXT, keywords TEXT, \
157 content TEXT, cdate NUMERIC, mdate NUMERIC, cid NUMERIC, \
158 stub TEXT, rating NUMERIC);")
159 c
.execute ("CREATE TABLE IF NOT EXISTS \
160 config (config_name TEXT, config_param TEXT);")
162 c
.execute ("CREATE TABLE IF NOT EXISTS \
163 templates (template_name TEXT, template_content);")
165 template_main
= """<?xml version="1.0" encoding="UTF-8"?>
166 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
167 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
169 <title>${site_title}</title>
170 <base href="${site_url}" />
171 <meta name="generator" content="BiaWeb"/>
172 <meta name="keywords" content="${meta_keywords}"/>
173 <meta name="description" content="${meta_description}"/>
174 <link rel="StyleSheet" type="text/css" href="style.css" />
175 <link rel="alternate" type="application/rss+xml" title="Entries RSS 2.0" href="subscribe.xml" />
179 <h1><a href="${site_url}">${page_title}</a></h1>
180 <div class="headerdesc">${page_desc}</div>
187 ${list_of_categories}
191 <a href="subscribe.xml">Latest articles (RSS)</a>
193 <form action="cgi-bin/search.py" method="post" enctype="multipart/form-data">
194 <p><input type="text" name="query" maxlength="255" style="width:142px;border: 1px inset #5A5A5A; color:#5A5A5A; background-color:#FFFFFF;" value="" /><br />
195 <input type="submit" value="Search" /><br />
196 <input type="hidden" name="fromsearch" value="fromsearch" />
197 <input type="radio" name="criteria" value="1" checked="checked" />All words<br />
198 <input type="radio" name="criteria" value="2" />Any word</p>
201 <div id="footer">${copyright}<br />Site generated by
202 <a href=\"http://harishankar.org/software/biaweb.php\">BiaWeb</a> created by V. Harishankar</div>
206 template_article_bit
= """<h2>${article_title}</h2>
207 <div class="modified">Created: ${article_cdate} | Last modified: ${article_mdate}</div>
208 <div class="rating">Rating: ${rating}</div>
209 <div class="content">${article_contents}</div>
212 template_news_item_bit
= """<h3><a href="${news_link}">${news_title}</a></h3>
213 <div class="modified">${news_datetime}</div>
214 <div class="content">${news_description}</div>
217 template_index_bit
= """<h2>Welcome to ${site_name}</h2>
218 <div class="content">
219 Welcome to my site, ${site_name}.
221 <h2>Latest Articles</h2>
225 template_table_bit
= """<h2>${category_title}</h2>
226 <p>${category_desc}</p>
227 <table class="categorytable">
230 <td style="width:50%">Title</td>
241 template_tablerow_bit
= """<tr>
242 <td style="width:50%"><a href="${article_url}">${title}</a></td>
248 template_style
= """body {
249 font-family: "Bitstream Vera Sans", Verdana, Arial, Sans Serif;
251 background-color: #ffffff;
257 background-color: #efefef;
281 background-color: #efefef;
285 font-variant: small-caps;
298 .categorytable thead {
313 font-family: "Bitstream Vera Serif", Serif;
322 border-bottom: 1px solid;
323 border-color: #888888;
331 h1 a, h1 a:hover, h1 a:visited, h2 a:active {
332 text-decoration: none;
336 background-color: #efefef;
338 h2 a, h2 a:hover, h2 a:visited, h2 a:active {
339 text-decoration: none;
353 c
.executemany ("INSERT INTO templates (template_name, template_content) VALUES (?, ?);",
354 [["main_template", template_main
],
355 ["article_bit", template_article_bit
],
356 ["news_bit", template_news_item_bit
],
357 ["table_bit", template_table_bit
],
358 ["tablerow_bit", template_tablerow_bit
],
359 ["stylesheet", template_style
],
360 ["index_bit", template_index_bit
]])
362 c
.executemany ("INSERT INTO config (config_name, config_param) VALUES (?, ?); ",
363 [["Website URL", site_url
],
364 ["Website Title", site_title
],
365 ["Keywords", keywords
],
366 ["Description", description
],
367 ["No. of RSS items", num_rss
],
368 ["Destination path", dest_path
],
369 ["Copyright", copyright
]])
374 except sqlite3
.Error
: