summaryrefslogtreecommitdiff
path: root/cgi-bin/page_builder.py
blob: 096fda88fb953d52f033da87a666a3d219a6f3ef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!C:\Python364\python3.exe
# -*- coding: utf-8 -*-
import sys, codecs
import cgi, cgitb
import pymysql
import issuedb
import config 
from issuedb import IssueDBFactory
from config import Config

sys.stdout = codecs.getwriter('utf8')(sys.stdout.buffer)

class Page_Builder(object):
    def build(pagen): 
        db = IssueDBFactory.produce()
        cursor = db.cursor() 

        tag_unit_html_file = open("tag_unit.html", 'r', encoding='utf8')
        page_html_file = open("page.html", 'r', encoding= 'utf8')
        issue_unit_html_file = open("issue_unit.html", 'r', encoding = 'utf8')
        page_html = page_html_file.read()
        issue_unit_html = issue_unit_html_file.read()
        tag_unit_html = tag_unit_html_file.read() 

        issue_unit_html_file.close() 
        page_html_file.close()
        tag_unit_html_file.close()

        issues_list = ""
        issues_count = Config.get("config", "issues_count")
        issues_count = int(issues_count)
        if pagen == None:
            pagen = 0 
        pageprev = int(pagen) - 1 
        if pageprev < 0: 
            pageprev = 0 
        pagenext = int(pagen) + 1 
        try:
            cursor.execute("select * from issue ORDER BY issueId DESC LIMIT {0}, {1}".format(pagen*issues_count, issues_count)) 
            issues_rows = cursor.fetchall()
            odd = True
            for row in issues_rows:
                tagslist = "" 
                cursor.execute("select * from tag where tagId in (select tagId from \
                        relation where issueId = {0})".format(row[0]))
                tags_rows = cursor.fetchall() 
                for tag_row in tags_rows: 
                    tag = tag_unit_html.format(\
                        tagid = tag_row[0], 
                        tag_name = tag_row[1],
                        tag_count = '' 
                    )
                    tagslist += tag
                issue = issue_unit_html.format(\
                    issueid = row[0], \
                    title = row[1], \
                    description = row[2], \
                    tags = tagslist, \
                    oddoreven = ((odd == True) and "odd" or "even")
                )
                odd = not odd
                issues_list += issue
        except: 
            print("Error: unable to fetch data") 

        page = page_html.format(\
            issues = issues_list, \
            page_prev = pageprev, \
            page_next = pagenext,\
        )
        db.commit() 
        cursor.close() 
        db.close()
        return page