Skip to content

Commit 53cc480

Browse files
committed
bug fixed
1 parent dc98bdf commit 53cc480

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed

django_query_to_table/DjangoQtt.py

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
from django.template import Template, Context
2+
3+
import traceback
4+
5+
def generateFromSql(cursor, title, sqltext, footerCols= None, htmlClass="", direction="ltr", font="Tahoma", totalText = "Total", rowIndex = False, headerRowColor ='#eeeeee' ,evenRowColor = '#ffffff', oddRowColor="#ffffff") :
6+
sumCols=[]
7+
try :
8+
if(len(sqltext) < 8 or ("select" not in sqltext.lower())) :
9+
return ('Not Valid SQL')
10+
11+
sql_query = sqltext
12+
sumCols=footerCols
13+
# execute sql query and retrieve data from db
14+
cursor.execute(sql_query)
15+
16+
# retrieve columns of the data
17+
desc = cursor.description
18+
19+
result_as_list = [
20+
dict(zip([col[0] for col in desc ], row)) for row in cursor.fetchall()
21+
]
22+
23+
columns = [col[0] for col in desc ] #result.keys()
24+
data = result_as_list
25+
26+
27+
28+
29+
30+
sumOfColumn = None
31+
if(sumCols != None) :
32+
sumOfColumn={}
33+
34+
# initiating footer aggrigation values
35+
for c in columns :
36+
if(c in sumCols):
37+
sumOfColumn[c]=0
38+
else:
39+
sumOfColumn[c]="-"
40+
41+
# travers in rows and aggrigate the values of columns those are in footerCols
42+
for d in data :
43+
for attr, value in dict(d).items() :
44+
if(attr in sumCols):
45+
sumOfColumn[attr]=sumOfColumn[attr]+int(str(value).replace(",", ""))
46+
47+
totalColumnSet = False
48+
if(sumCols != None) :
49+
for col, val in sumOfColumn.items() :
50+
if(val!="-") :
51+
sumOfColumn[col] = format(int(str(val)),",")
52+
elif (totalColumnSet == False) :
53+
sumOfColumn[col] = totalText
54+
totalColumnSet = True
55+
56+
# template to generate data from data retrieved from database
57+
template= "<center><table dir=\"{{direction}}\" border=\"1\" class=\"table table-striped {{htmlClass}}\" style=\"width:93%;font-family:'{{font}}'\"> <thead> <tr> <th colspan='{{columns|length|add:'1'}}' style=\"font-family:'{{font}}';font-weight: bold;\" > {{title}} </th> </tr> <tr style='background-color:{{headerRowColor}}'>{% if rowIndex == True %} <td align=\"center\"> </td> {% endif %} {% for c in columns %} <th>{{ c }}</th> {% endfor %} </tr> </thead> <tbody> {% for d in data %} <tr style='background-color:{% if forloop.counter0|divisibleby:'2' %} {{evenRowColor}} {% else %} {{oddRowColor}} {% endif %} ' > {% if rowIndex == True %} <td align=\"center\">{{ loop.index }}</td> {% endif %} {% for attr, value in d.items %} <td align=\"center\">{{ value }}</td> {% endfor %} </tr> {% endfor %} {% if sumOfColumn != None %} <tr style='background-color:#eee;font-weight: bold;'> <td></td> {% for a,v in sumOfColumn.items %} <td align=\"center\">{{ v }}</td> {% endfor %} </tr> {% endif %}</tbody> </table></center>"
58+
59+
60+
61+
c = Context({
62+
'title':title,
63+
'data':data,
64+
'columns':columns,
65+
'sumOfColumn':sumOfColumn,
66+
'direction':direction,
67+
'font':font,
68+
'totalText':totalText,
69+
'rowIndex' : rowIndex,
70+
'headerRowColor' :headerRowColor ,
71+
'evenRowColor' : evenRowColor,
72+
'oddRowColor': oddRowColor,
73+
'htmlClass' : htmlClass
74+
})
75+
76+
return Template(template).render(c)
77+
except BaseException as e :
78+
#print exception trace to console
79+
print(traceback.format_exc())
80+
return ("Error :" + str(e))

django_query_to_table/___init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from . import DjangoQtt
2+
3+
4+
__all__ = ['DjangoQtt']

0 commit comments

Comments
 (0)