kelurahan_rpt.py
2.41 KB
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
import os
import uuid
#from ..tools import row2dict, xls_reader
from datetime import datetime
from sqlalchemy import not_, func
from pyramid.view import (
view_config,
)
from pyramid.httpexceptions import (
HTTPFound,
)
import colander
from deform import (
Form,
widget,
ValidationFailure,
)
from datatables import ColumnDT, DataTables
from ..models import (
PbbmDBSession as DBSession,
Kelurahan
)
from ..tools import (
open_rml_row, open_rml_pdf, pdf_response, csv_response,
get_settings)
##########
# RPT #
##########
@view_config(route_name='pbbm-kelurahan-rpt', renderer="json",
permission='pbbm-kelurahan-rpt')
def view_rpt(request):
request = request
ses = request.session
req = request
params = req.params
url_dict = req.matchdict
if url_dict['rpt']=='pdf' :
query = query_register()
_here = os.path.dirname(__file__)
path = os.path.join(os.path.dirname(_here), 'reports')
rml_row = open_rml_row(path+'/kelurahan.row.rml')
rows=[]
for r in query.all():
s = rml_row.format(
kode=r.kode,
nama=r.nama
)
rows.append(s)
pdf, filename = open_rml_pdf(path+'/kelurahan.rml', rows=rows,
company=request.company,
departement = request.departement,
address = request.address)
return pdf_response(request, pdf, filename)
elif url_dict['rpt']=='csv' :
query = query_register()
row = query.first()
header = row.keys()
rows = []
for item in query.all():
rows.append(list(item))
filename = 'kelurahan.csv'
value = {
'header': header,
'rows' : rows,
}
return csv_response(request, value, filename)
def query_register():
return DBSession.query(func.concat(Kelurahan.kd_propinsi, func.concat('.',
func.concat(Kelurahan.kd_dati2, func.concat('-',
func.concat(Kelurahan.kd_kecamatan, func.concat('.',
Kelurahan.kd_kelurahan)))))).label('kode'),
Kelurahan.nm_kelurahan.label('nama')).\
order_by(Kelurahan.kd_kecamatan,Kelurahan.kd_kelurahan)