aboutsummaryrefslogtreecommitdiff
path: root/web/webqtl/schema/ShowSchemaPage.py
blob: e9ced5a1f45c0f97482cea6221b157ac5c068072 (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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# Copyright (C) University of Tennessee Health Science Center, Memphis, TN.
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License
# as published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU Affero General Public License for more details.
#
# This program is available from Source Forge: at GeneNetwork Project
# (sourceforge.net/projects/genenetwork/).
#
# Contact Drs. Robert W. Williams and Xiaodong Zhou (2010)
# at rwilliams@uthsc.edu and xzhou15@uthsc.edu
#
#
#
# This module is used by GeneNetwork project (www.genenetwork.org)
#
# Created by GeneNetwork Core Team 2010/08/10
#
# Last updated by GeneNetwork Core Team 2010/10/20

import sys

from htmlgen import HTMLgen2 as HT

from base.templatePage import templatePage
from base import webqtlConfig
from utility import webqtlUtil



###################################################
#Description: show the schema of webqtl's database#
#Author: Hongqiang Li                             #
#Version: 1.0                                     #
###################################################

class ShowSchemaPage(templatePage):
    def __init__(self,fd):
	cookies = fd.cookies
        sys.stderr = sys.stdout
        templatePage.__init__(self, fd)
        body = HT.SimpleDocument()

        ###############################################################################
        #get user's privilege from cookie, if the user doesn't have enough privilege, #
        #he won't see the update comment icon                                         #
        ###############################################################################
        ShowUpdateIcon = False 

        if not self.openMysql():
           return

        Cursor_WebQtl = Cursor_Comment = self.cursor

        if webqtlConfig.USERDICT[self.privilege] >= webqtlConfig.USERDICT['user']:
			ShowUpdateIcon = True

        ##################
        #show description#
        ##################
        body.append('<BR>')
        body.append('<H1>')
        body.append('Description of Schema')
        if ShowUpdateIcon:
        #{
            # Modified by Hongqiang Li
            # Image_Update = HT.Image('http://web2qtl.utmem.edu/images/modify.gif')
            Image_Update = HT.Image('/images/modify.gif')
            #
            Href_Update = HT.Href(webqtlConfig.CGIDIR+'main.py?FormID=schemaShowComment&TableName=Description_of_Schema', Image_Update)
            body.append(Href_Update)
        #}
        body.append('</H1>')
         
        Cursor_WebQtl.execute('select Comment from TableComments where TableName=\'Description_of_Schema\'')
        Comment = Cursor_WebQtl.fetchone();
        if Comment:
            if str(Comment[0])!='None':
                body.append(Comment[0])

        body.append('<HR WIDTH=100%>')
        body.append('<H2>Tables</H2>')

        ##################
        #show table names#
        ##################
        Cursor_WebQtl.execute('show tables')
        Tables=Cursor_WebQtl.fetchall()
        BlockedTables = ['User', 'TableComments', 'TableFieldAnnotation', 'ProbeSetXRef_TEMP', 'DBList', 'DBType', 'HumanGene', 'LCorr', 'Temp', 'TempData']
        for i in range(0, len(Tables)):
        #{
            TableName = Tables[i][0]
            if TableName in BlockedTables: #skip the table who is blocked
                continue

            HrefTable_Schema = HT.Href(webqtlConfig.CGIDIR+'main.py?FormID=schemaShowPage#'+TableName, TableName)
            body.append(str(HrefTable_Schema)+'<BR>')
        #}
        body.append('<hr width=100%>')
    
        for i in range(0, len(Tables)):
        #{
            TableName = Tables[i][0]
            if TableName in BlockedTables: #skip the table who is blocked
                continue

            #####################
            #get table's comment#
            #####################
            SqlCmd = 'select Comment from TableComments where TableName=\'%s\'' %(TableName)
            Cursor_WebQtl.execute(SqlCmd)
            Comment = Cursor_WebQtl.fetchall()

            ####################################
            #get the content of a table's schma#
            ####################################
            Cursor_WebQtl.execute('desc %s' %(TableName))
            TableDesc = Cursor_WebQtl.fetchall();

            HtmlTR_Schema = []
            for row in range(0, len(TableDesc)):
            #{
                HtmlTD_Schema = []
                for col in range(0, len(TableDesc[row])):
                    if str(TableDesc[row][col])=='None' or str(TableDesc[row][col])=='': #just means I don't want show 'None' *_^
                       HtmlTD_Schema.append('&nbsp;')
                    else:
                       HtmlTD_Schema.append(TableDesc[row][col])

                ##############################
                #get table fileds' annotation#
                ##############################
                TableField = TableName+'.'+TableDesc[row][0]
                Cursor_WebQtl.execute('select Annotation, Foreign_Key from TableFieldAnnotation where TableField=%s', (TableField))
                Annotation = Cursor_WebQtl.fetchone();
                if Annotation:
                #{
                    if str(Annotation[1])=='None' or str(Annotation[1])=='':
                        HtmlTD_Schema.append('&nbsp;')
                    else:
                        HtmlTD_Schema.append(Annotation[1])
                    if str(Annotation[0])=='None' or str(Annotation[0])=='':
                        HtmlTD_Schema.append('&nbsp;')
                    else:
                        HtmlTD_Schema.append(Annotation[0])
                #}
                else:
                #{
                    HtmlTD_Schema.append('&nbsp;')
                    HtmlTD_Schema.append('&nbsp;')
                #}

            	HtmlTR_Schema.append(HtmlTD_Schema)
            #}

            ###############################
            #Html code of a table's schema#
            ###############################
            body.append(HT.NAME(TableName, TableName))
            if ShowUpdateIcon:
            #{
                # Modified by Hongqiang Li
                #Image_Update = HT.Image('http://web2qtl.utmem.edu/images/modify.gif')
                Image_Update = HT.Image('/images/modify.gif')
                #
                Href_Update = HT.Href(webqtlConfig.CGIDIR+'main.py?FormID=schemaShowComment&TableName=%s' %(TableName), Image_Update)
                body.append(Href_Update)
            #}
            body.append('<BR><BR>')

            body.append('<B>Comment:</B><BR>')
            #body.append("<I>")
            if Comment:
                if str(Comment[0][0])!='None':
                    for content in Comment[0][0].split('\n'):
                        body.append(content)
                        body.append('<BR>')
            #body.append("</I>")

            HtmlTable_Schema = HT.Table(width='0%', heading=['Field', 'Type', 'Null', 'Key', 'Default', 'Extra', 'Foreign_Key', 'Annotation'], body = HtmlTR_Schema)
            body.append(HtmlTable_Schema)
            body.append('<hr width=100%>')
        #}

        self.dict['body'] = body