Implemented the category index pages exporting
[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 # to convert a rating number into rating images out of 10 stars
36 def html_format_rating (rating):
37 items = []
38 # if -1 then return unrated as the text
39 if rating == -1:
40 return "unrated"
41 # fill up the number of stars for the rating
42 for i in range (rating):
43 items.append ('<img src="star.gif" alt="*" />')
44 # fill up remaining slots (of 10) with grey stars
45 for i in range (10 - rating):
46 items.append ('<img src="star-grey.gif" alt="-" />')
47
48 rating_str = "".join (items)
49 return rating_str
50
51 # function to generate category directories and indices
52 def generate_category_indices (dbname, conf, templates, category_str, bestrated_str, category_list):
53 # main template
54 tpl_main = string.Template (templates[0][1])
55 # table bit
56 tpl_tablebit = string.Template (templates[3][1])
57 # table row bit
58 tpl_trowbit = string.Template (templates[4][1])
59
60 # run through each category and generate category index page
61 for cat in category_list:
62 try:
63 # create the category directory
64 os.mkdir (os.path.join (conf[5], cat[3]))
65 except IOError, OSError:
66 return False
67
68 # now get the list of articles for the specified category
69 articles_list = biaweb_db.site_articles (dbname, cat[0])
70 if articles_list == False:
71 return False
72
73 tableitems = []
74 # run through the list of articles in category
75 for art in articles_list:
76 url = art[13] + "/" + art[8] + ".html"
77 creattime = time.ctime (art[5])
78 rating_str = html_format_rating (art[9])
79 # now build the table rows for each article
80 tableitem_str = tpl_trowbit.safe_substitute (article_url = url,
81 title = art[1],
82 created = creattime,
83 rating = rating_str)
84 tableitems.append (tableitem_str)
85 # generate the rows as a string
86 tablerows_str = "".join (tableitems)
87
88 # now create the page template
89 table_str = tpl_tablebit.safe_substitute (category_title = cat[1],
90 category_desc = cat[2],
91 table_rows = tablerows_str)
92
93 # now create the index page
94 categoryindex_str = tpl_main.safe_substitute (site_title = conf[1],
95 site_url = "http://" + conf[0],
96 meta_keywords = conf[2],
97 meta_description = cat[2],
98 page_title = conf[1],
99 page_desc = conf[3],
100 contents_bit = table_str,
101 list_of_categories = category_str,
102 list_best_rated = bestrated_str,
103 copyright = conf[6])
104
105 # now write to Category/index.html
106 try:
107 fcatindex = open (os.path.join (conf[5], cat[3], "index.html"), "w+")
108 fcatindex.write (categoryindex_str)
109 fcatindex.close ()
110 except OSError, IOError:
111 return False
112
113 # finally return true
114 return True
115
116 # function to generate main index file and stylesheet
117 def generate_home_page (dbname, conf, templates, category_str, bestrated_str):
118 # main template
119 tpl_main = string.Template (templates[0][1])
120 # index bit
121 tpl_indexbit = string.Template (templates[6][1])
122 # news bits
123 tpl_newsbit = string.Template (templates[2][1])
124
125 # get the latest articles - conf[4] is num of rss entries to be used also
126 latest_arts = biaweb_db.site_latest_articles (dbname, conf[4])
127 if latest_arts == False:
128 return False
129
130 news_items = []
131
132 # Run through the latest articles
133 # for the num of latest news items on index
134 for art in latest_arts:
135 # url is Category/Article.html
136 url = art[13] + "/" + art[8] + ".html"
137 # art[5] is creation time
138 strdate = time.ctime (art[5])
139 # now populate the template variables. art[1] is title, art[2] is summary
140 strnews = tpl_newsbit.safe_substitute (news_title = art[1],
141 news_link = url,
142 news_datetime = strdate,
143 news_description = art[2]
144 )
145 news_items.append (strnews)
146 # now convert it into a string
147 newsbit_str = "".join (news_items)
148
149 # now populate the index template
150 indexbit_str = tpl_indexbit.safe_substitute (site_name = conf[1],
151 news_updates = newsbit_str
152 )
153 # now populate the main page template
154 main_str = tpl_main.safe_substitute (site_title = conf[1],
155 site_url = "http://" + conf[0],
156 meta_keywords = conf[2],
157 meta_description = conf[3],
158 page_title = conf[1],
159 page_desc = conf[3],
160 contents_bit = indexbit_str,
161 list_of_categories = category_str,
162 list_best_rated = bestrated_str,
163 copyright = conf[6])
164
165 # write the index.html file in the destination directory
166 try:
167 findex = open (os.path.join (conf[5], "index.html"), "w+")
168 findex.write (main_str)
169 findex.close ()
170 except IOError, OSError:
171 return False
172
173 # write the style.css file in the destination directory
174 try:
175 fstyle = open (os.path.join (conf[5], "style.css"), "w+")
176 fstyle.write (templates[5][1])
177 fstyle.close ()
178 except IOError, OSError:
179 return False
180
181 return True
182
183 # superfunction to generate the site
184 def generate_site (dbname, files_to_copy, folders_to_copy, search_type_full=True):
185 # get the configuration
186 conf = biaweb_db.get_configuration (dbname)
187 # get the templates
188 tpls = biaweb_db.get_templates (dbname)
189
190 # get the list of categories
191 cats = biaweb_db.get_categories (dbname)
192 # cannot get categories return false
193 if cats == False:
194 return False
195
196 # format the categories as a html bulleted list
197 cats_str = html_format_categories (cats)
198
199 # get the best rated articles
200 best_rated = biaweb_db.site_get_bestrated (dbname)
201 # if cannot retrieve
202 if best_rated == False:
203 return False
204 # format the best rated articles as a html bulleted list
205 best_rated_str = html_format_best_rated (best_rated)
206
207 # remove the destination tree and recreate it
208 try:
209 if os.path.exists (conf[5]):
210 shutil.rmtree (conf[5])
211 os.mkdir (conf[5])
212 except OSError:
213 return False
214
215 # generate the index page including style sheet
216 ret = generate_home_page (dbname, conf, tpls, cats_str, best_rated_str)
217 if ret == False:
218 return False
219
220 ret = generate_category_indices (dbname, conf, tpls, cats_str, best_rated_str, cats)
221 if ret == False:
222 return False
223
224 # finally when all is successfully done return true
225 return True