5bbe32b906b484f746f0eca72f072686a8f2a516
[biaweb_qt.git] / biaweb_exporter.py
1 # BiaWeb Website content manager (c) 2010 V.Harishankar
2 # Site exporter/generator class
3
4 import os
5 import os.path
6 import time
7 import sqlite3
8 import string
9 import shutil
10 import biaweb_db
11
12 # to format the best rated articles in a HTML link list
13 def html_format_best_rated (best_rated):
14 items = [ "<ul>\n", ]
15 for art in best_rated:
16 # art[13] is category stub, art[8] is article stub
17 # thus forming the relative url as Category/Article.html
18 str_art = '<li><a href="' + art[13] + '/' + art[8] + '.html">' + art[1] + '</a></li>\n'
19 items.append (str_art)
20 items.append ("</ul>\n")
21 str_items = "".join (items)
22 return str_items
23
24 # to format categories in a HTML link list
25 def html_format_categories (cats):
26 items = [ "<ul>\n", ]
27 for cat in cats:
28 # cat[3] is category stub and cat[1] is category name
29 str_cat = '<li><a href="' + cat[3] + '/">' + cat[1] + '</a></li>\n'
30 items.append (str_cat)
31 items.append ("</ul>\n")
32 str_items = "".join (items)
33 return str_items
34
35 # function to generate main index file and stylesheet
36 def generate_home_page (dbname, conf, templates, category_str, bestrated_str):
37 # main template
38 tpl_main = string.Template (templates[0][1])
39 # index bit
40 tpl_indexbit = string.Template (templates[6][1])
41 # news bits
42 tpl_newsbit = string.Template (templates[2][1])
43
44 # get the latest articles - conf[4] is num of rss entries to be used also
45 latest_arts = biaweb_db.site_latest_articles (dbname, conf[4])
46 if latest_arts == False:
47 return False
48
49 news_items = []
50
51 # Run through the latest articles
52 # for the num of latest news items on index
53 for art in latest_arts:
54 # url is Category/Article.html
55 url = art[13] + "/" + art[8] + ".html"
56 # art[5] is creation time
57 strdate = time.ctime (art[5])
58 # now populate the template variables. art[1] is title, art[2] is summary
59 strnews = tpl_newsbit.safe_substitute (news_title = art[1],
60 news_link = url,
61 news_datetime = strdate,
62 news_description = art[2]
63 )
64 news_items.append (strnews)
65 # now convert it into a string
66 newsbit_str = "".join (news_items)
67
68 # now populate the index template
69 indexbit_str = tpl_indexbit.safe_substitute (site_name = conf[1],
70 news_updates = newsbit_str
71 )
72 # now populate the main page template
73 main_str = tpl_main.safe_substitute (site_title = conf[1],
74 site_url = "http://" + conf[0],
75 meta_keywords = conf[2],
76 meta_description = conf[3],
77 page_title = conf[1],
78 page_desc = conf[3],
79 contents_bit = indexbit_str,
80 list_of_categories = category_str,
81 list_best_rated = bestrated_str,
82 copyright = conf[6])
83
84 # write the index.html file in the destination directory
85 try:
86 findex = open (os.path.join (conf[5], "index.html"), "w+")
87 findex.write (main_str)
88 findex.close ()
89 except IOError, OSError:
90 return False
91
92 # write the style.css file in the destination directory
93 try:
94 fstyle = open (os.path.join (conf[5], "style.css"), "w+")
95 fstyle.write (templates[5][1])
96 fstyle.close ()
97 print "error"
98 except IOError, OSError:
99 return False
100
101 return True
102
103 # superfunction to generate the site
104 def generate_site (dbname, files_to_copy, folders_to_copy, search_type_full=True):
105 # get the configuration
106 conf = biaweb_db.get_configuration (dbname)
107 # get the templates
108 tpls = biaweb_db.get_templates (dbname)
109
110 # get the list of categories
111 cats = biaweb_db.get_categories (dbname)
112 # cannot get categories return false
113 if cats == False:
114 return False
115
116 # format the categories as a html bulleted list
117 cats_str = html_format_categories (cats)
118
119 # get the best rated articles
120 best_rated = biaweb_db.site_get_bestrated (dbname)
121 # if cannot retrieve
122 if best_rated == False:
123 return False
124 # format the best rated articles as a html bulleted list
125 best_rated_str = html_format_best_rated (best_rated)
126
127 # remove the destination tree and recreate it
128 try:
129 if os.path.exists (conf[5]):
130 shutil.rmtree (conf[5])
131 os.mkdir (conf[5])
132 except OSError:
133 return False
134
135 # generate the index page including style sheet
136 ret = generate_home_page (dbname, conf, tpls, cats_str, best_rated_str)
137 if ret == False:
138 return False
139
140 # finally when all is successfully done return true
141 return True