dd3e333bdf6feea17979bb002d333331682852ed
1 # BiaWeb Website content manager (c) 2010 V.Harishankar
2 # Database handling functions
9 # function to get an article from the database
10 def get_article (dbname
, artid
):
12 conn
= sqlite3
.connect (dbname
)
14 c
.execute ("SELECT * FROM articles WHERE aid=?;", (artid
,))
22 # function to create an article
23 def create_article (dbname
, title
, summary
, keywords
, content
, catid
, stub
, rating
):
25 creattime
= time
.time ()
28 conn
= sqlite3
.connect (dbname
)
30 c
.execute ("INSERT INTO articles (title, summary, keywords, content, cdate, mdate, cid, stub, rating) \
31 VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?);", (title
, summary
, keywords
, content
, creattime
,
32 modtime
, catid
, stub
, rating
))
39 # function to update an article
40 def update_article (dbname
, aid
, title
, summary
, keywords
, content
, catid
, stub
, rating
):
41 # modification time only to be updated
42 modtime
= time
.time ()
44 conn
= sqlite3
.connect (dbname
)
46 c
.execute ("UPDATE articles SET title=?, summary=?, keywords=?, content=?, mdate=?, cid=?, \
47 stub=?, rating=? WHERE aid=?;",
48 (title
, summary
, keywords
, content
, modtime
, catid
, stub
, rating
, aid
))
55 # function to delete an article
56 def delete_article (dbname
, artid
):
58 conn
= sqlite3
.connect (dbname
)
60 c
.execute ("DELETE FROM articles WHERE aid=?;", (artid
,))
67 # function to get a category from the database
68 def get_category (dbname
, catid
):
70 conn
= sqlite3
.connect (dbname
)
72 c
.execute ("SELECT * FROM categories WHERE cid=?", (catid
,))
80 # function to update a category
81 def update_category (dbname
, catid
, cat_name
, cat_desc
, cat_stub
):
83 conn
= sqlite3
.connect (dbname
)
85 c
.execute ("UPDATE categories SET name=?, desc=?, stub=? WHERE cid=?;",
86 (cat_name
, cat_desc
, cat_stub
, catid
))
93 # function to remove a category
94 def remove_category (dbname
, catid
):
96 conn
= sqlite3
.connect (dbname
)
98 c
.execute ("DELETE FROM categories WHERE cid=?;", (catid
,))
99 c
.execute ("DELETE FROM articles WHERE cid=?;", (catid
,))
103 except sqlite3
.Error
:
107 # function to create a category
108 def create_category (dbname
, category_name
, category_desc
, category_stub
):
110 conn
= sqlite3
.connect (dbname
)
112 c
.execute ("INSERT INTO categories (name, desc, stub) VALUES (?, ?, ?);",
113 (category_name
, category_desc
, category_stub
))
117 except sqlite3
.Error
:
120 # function to set the configuration and update the database
121 def set_configuration (dbname
, site_title
, site_url
, keywords
, description
, copyright
,
124 conn
= sqlite3
.connect (dbname
)
126 c
.executemany ("UPDATE config SET config_param=? WHERE config_name=?;",
127 [[site_url
, "Website URL"],
128 [site_title
, "Website Title"],
129 [keywords
, "Keywords"],
130 [description
, "Description"],
131 [num_rss
, "No. of RSS items"],
132 [dest_path
, "Destination path"],
133 [copyright
, "Copyright"]])
137 except sqlite3
.Error
:
140 # function to get the existing site configuration and return it as a tuple
141 def get_configuration (dbname
):
143 conn
= sqlite3
.connect (dbname
)
145 c
.execute ("SELECT * FROM config;")
149 for name
, param
in recs
:
150 if name
== "Website URL":
152 elif name
== "Website Title":
153 website_title
= param
154 elif name
== "Keywords":
156 elif name
== "Description":
158 elif name
== "No. of RSS items":
159 num_rss
= int (param
)
160 elif name
== "Destination path":
162 elif name
== "Copyright":
164 return (website_url
, website_title
, keywords
, description
, num_rss
, destination
, copyright
)
165 except sqlite3
.Error
:
168 # Function to get list of articles (either full list or just in a category
169 def get_articles (dbname
, category_id
=None):
171 conn
= sqlite3
.connect (dbname
)
173 if category_id
== None:
174 c
.execute ("SELECT * FROM articles;")
176 c
.execute ("SELECT * FROM articles WHERE cid=?", (category_id
,))
181 except sqlite3
.Error
:
184 # Function to get list of categories and return a (category_id, category_name) array
185 def get_categories (dbname
):
187 conn
= sqlite3
.connect (dbname
)
189 c
.execute ("SELECT * FROM categories;")
194 except sqlite3
.Error
:
197 # function to create a new site database
198 def create_db (dbname
, site_title
, site_url
, keywords
, description
, copyright
,
201 if os
.path
.exists (dbname
):
207 conn
= sqlite3
.connect (dbname
)
209 c
.execute ("CREATE TABLE IF NOT EXISTS \
210 categories (cid INTEGER PRIMARY KEY, \
211 name TEXT, desc TEXT, \
213 c
.execute ("CREATE TABLE IF NOT EXISTS \
214 articles (aid INTEGER PRIMARY KEY, \
215 title TEXT, summary TEXT, keywords TEXT, \
216 content TEXT, cdate NUMERIC, mdate NUMERIC, cid NUMERIC, \
217 stub TEXT, rating NUMERIC);")
218 c
.execute ("CREATE TABLE IF NOT EXISTS \
219 config (config_name TEXT, config_param TEXT);")
221 c
.execute ("CREATE TABLE IF NOT EXISTS \
222 templates (template_name TEXT, template_content);")
224 template_main
= """<?xml version="1.0" encoding="UTF-8"?>
225 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
226 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
228 <title>${site_title}</title>
229 <base href="${site_url}" />
230 <meta name="generator" content="BiaWeb"/>
231 <meta name="keywords" content="${meta_keywords}"/>
232 <meta name="description" content="${meta_description}"/>
233 <link rel="StyleSheet" type="text/css" href="style.css" />
234 <link rel="alternate" type="application/rss+xml" title="Entries RSS 2.0" href="subscribe.xml" />
238 <h1><a href="${site_url}">${page_title}</a></h1>
239 <div class="headerdesc">${page_desc}</div>
246 ${list_of_categories}
250 <a href="subscribe.xml">Latest articles (RSS)</a>
252 <form action="cgi-bin/search.py" method="post" enctype="multipart/form-data">
253 <p><input type="text" name="query" maxlength="255" style="width:142px;border: 1px inset #5A5A5A; color:#5A5A5A; background-color:#FFFFFF;" value="" /><br />
254 <input type="submit" value="Search" /><br />
255 <input type="hidden" name="fromsearch" value="fromsearch" />
256 <input type="radio" name="criteria" value="1" checked="checked" />All words<br />
257 <input type="radio" name="criteria" value="2" />Any word</p>
260 <div id="footer">${copyright}<br />Site generated by
261 <a href=\"http://harishankar.org/software/biaweb.php\">BiaWeb</a> created by V. Harishankar</div>
265 template_article_bit
= """<h2>${article_title}</h2>
266 <div class="modified">Created: ${article_cdate} | Last modified: ${article_mdate}</div>
267 <div class="rating">Rating: ${rating}</div>
268 <div class="content">${article_contents}</div>
271 template_news_item_bit
= """<h3><a href="${news_link}">${news_title}</a></h3>
272 <div class="modified">${news_datetime}</div>
273 <div class="content">${news_description}</div>
276 template_index_bit
= """<h2>Welcome to ${site_name}</h2>
277 <div class="content">
278 Welcome to my site, ${site_name}.
280 <h2>Latest Articles</h2>
284 template_table_bit
= """<h2>${category_title}</h2>
285 <p>${category_desc}</p>
286 <table class="categorytable">
289 <td style="width:50%">Title</td>
300 template_tablerow_bit
= """<tr>
301 <td style="width:50%"><a href="${article_url}">${title}</a></td>
307 template_style
= """body {
308 font-family: "Bitstream Vera Sans", Verdana, Arial, Sans Serif;
310 background-color: #ffffff;
316 background-color: #efefef;
340 background-color: #efefef;
344 font-variant: small-caps;
357 .categorytable thead {
372 font-family: "Bitstream Vera Serif", Serif;
381 border-bottom: 1px solid;
382 border-color: #888888;
390 h1 a, h1 a:hover, h1 a:visited, h2 a:active {
391 text-decoration: none;
395 background-color: #efefef;
397 h2 a, h2 a:hover, h2 a:visited, h2 a:active {
398 text-decoration: none;
412 c
.executemany ("INSERT INTO templates (template_name, template_content) VALUES (?, ?);",
413 [["main_template", template_main
],
414 ["article_bit", template_article_bit
],
415 ["news_bit", template_news_item_bit
],
416 ["table_bit", template_table_bit
],
417 ["tablerow_bit", template_tablerow_bit
],
418 ["stylesheet", template_style
],
419 ["index_bit", template_index_bit
]])
421 c
.executemany ("INSERT INTO config (config_name, config_param) VALUES (?, ?); ",
422 [["Website URL", site_url
],
423 ["Website Title", site_title
],
424 ["Keywords", keywords
],
425 ["Description", description
],
426 ["No. of RSS items", num_rss
],
427 ["Destination path", dest_path
],
428 ["Copyright", copyright
]])
433 except sqlite3
.Error
: