-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbiserver.py
162 lines (135 loc) · 6.2 KB
/
biserver.py
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
from flask import Flask, render_template, flash
from flask import jsonify
from flask import Flask,g,request,session
import requests,pyodbc,urllib2
from urllib2 import Request,urlopen
from embedtoken import EmbedToken
app = Flask(__name__)
clientid=
clientsecret=
username=
password=
groupid=
datasetid=
reportid=
tablename=
def updateData(dist,access_token, dataset_id, table_name):
initial_json="""
{
"rows": ["""
middle_json = ""
end_json = """]
}
"""
each = 0
saleinvoiceid = []
saleorderid = []
productid = []
amount = []
saleorderdate = []
saleinvoicedate = []
distributor_id = []
distributor_name = []
delivery_address = []
product_name = []
orderqty = []
gross_amount = []
net_amount = []
if (dist=="All Distributors"):
cnxn = pyodbc.connect(r'Driver={SQL Server};Server=.\SQLEXPRESS;Database=DELHI;Trusted_Connection=yes;')
cursor = cnxn.cursor()
cursor.execute(" select top(2000) a.SALEINVOICEID,a.SALEORDERID,a.PRODUCTID,a.AMOUNT,CONVERT(nvarchar,a.SALEORDERDATE , 106) as SALEORDERDATE,CONVERT(nvarchar,b.SALEINVOICEDATE , 106) as SALEINVOICEDATE,b.DISTRIBUTORID,b.DISTRIBUTORNAME,b.DELIVERYADDRESS,c.PRODUCTNAME,c.ORDERQTY,d.GROSSAMOUNT,d.NETAMOUNT from DELHI.dbo.T_SALEINVOICE_DETAILS a,DELHI.dbo.T_SALEINVOICE_HEADER b,DELHI.dbo.T_SALEORDER_DETAILS c,DELHI.dbo.T_SALEINVOICE_FOOTER d where a.SALEINVOICEID = b.SALEINVOICEID and a.SALEORDERID = c.SALEORDERID and a.SALEINVOICEID = d.SALEINVOICEID")
for row in cursor:
saleinvoiceid.append(str(row[0]))
saleorderid.append(str(row[1]))
productid.append(str(row[2]))
amount.append(float(row[3]))
saleorderdate.append(str(row[4]))
saleinvoicedate.append(str(row[5]))
distributor_id.append(str(row[6]))
distributor_name.append(str(row[7]))
delivery_address.append(str(row[8]))
product_name.append(unicode(row[9]).encode('utf-8'))
orderqty.append(str(row[10]))
gross_amount.append(float(row[11]))
net_amount.append(float(row[12]))
cnxn.close()
else:
cnxn = pyodbc.connect(r'Driver={SQL Server};Server=.\SQLEXPRESS;Database=DELHI;Trusted_Connection=yes;')
cursor = cnxn.cursor()
cursor.execute(" select top(2000) a.SALEINVOICEID,a.SALEORDERID,a.PRODUCTID,a.AMOUNT,CONVERT(nvarchar,a.SALEORDERDATE , 106) as SALEORDERDATE,CONVERT(nvarchar,b.SALEINVOICEDATE , 106) as SALEINVOICEDATE,b.DISTRIBUTORID,b.DISTRIBUTORNAME,b.DELIVERYADDRESS,c.PRODUCTNAME,c.ORDERQTY,d.GROSSAMOUNT,d.NETAMOUNT from DELHI.dbo.T_SALEINVOICE_DETAILS a,DELHI.dbo.T_SALEINVOICE_HEADER b,DELHI.dbo.T_SALEORDER_DETAILS c,DELHI.dbo.T_SALEINVOICE_FOOTER d where a.SALEINVOICEID = b.SALEINVOICEID and a.SALEORDERID = c.SALEORDERID and a.SALEINVOICEID = d.SALEINVOICEID and b.DISTRIBUTORNAME='"+dist+"'")
for row in cursor:
saleinvoiceid.append(str(row[0]))
saleorderid.append(str(row[1]))
productid.append(str(row[2]))
amount.append(float(row[3]))
saleorderdate.append(str(row[4]))
saleinvoicedate.append(str(row[5]))
distributor_id.append(str(row[6]))
distributor_name.append(str(row[7]))
delivery_address.append(str(row[8]))
product_name.append(unicode(row[9]).encode('utf-8'))
orderqty.append(str(row[10]))
gross_amount.append(float(row[11]))
net_amount.append(float(row[12]))
cnxn.close()
while each<len(amount):
content = """{
"productid": '"""+productid[each]+"""',
"amount": """+str(float(amount[each]))+""",
"saleinvoicedate": '"""+saleinvoicedate[each]+"""',
"distributor_name": '"""+distributor_name[each]+"""',
"delivery_address": '"""+delivery_address[each]+"""',
"product_name": '"""+product_name[each]+"""',
"orderqty": '"""+orderqty[each]+"""',
"gross_amount": """+str(float(gross_amount[each]))+""",
"net_amount": """+str(float(net_amount[each]))+""",
},"""
middle_json = middle_json+content
each=each+1
values = initial_json+middle_json+end_json
headers = {
'Authorization': 'Bearer ' + access_token,
'Content-Type': 'application/json'
}
response = requests.post('https://api.powerbi.com/v1.0/myorg/groups/'+groupid+'/datasets/'+dataset_id+'/tables/'+table_name+'/rows', data=values, headers=headers)
print response
def get_access_token():
data = {
'grant_type': 'password',
'resource': r'https://analysis.windows.net/powerbi/api',
'client_id': clientid ,
'client_secret': clientsecret ,
'username': username,
'password': password
}
response = requests.post('https://login.microsoftonline.com/common/oauth2/token', data=data)
return response.json()
def clear_dataset(access_token, dataset_id, table_name):
headers = {
'Authorization': 'Bearer ' + access_token
}
request = Request(
'https://api.powerbi.com/v1.0/myorg/groups/'+groupid+'/datasets/'+dataset_id+'/tables/'+table_name+'/rows',headers=headers)
request.get_method = lambda: 'DELETE'
response_body = urlopen(request).read()
return response_body
@app.route('/token',methods=['GET','POST'])
def bidemotoken():
data=request.form["key1"]
if data=="sendToken":
a=get_access_token()
conf = EmbedToken(str(a['access_token']),username,password,clientid,clientsecret,reportid,groupid)
token = conf.get_embed_token()
print token
returnData = {'selectedReport':token.get('report_id'),'embedToken':token.get('token')}
return jsonify(returnData)
@app.route('/api',methods=['GET','POST'])
def bidemodata():
data=request.form["key2"]
a=get_access_token() #access_token
clear_dataset(str(a['access_token']), datasetid, tablename)
updateData(data,str(a['access_token']), datasetid, tablename)
return "Report Updated"
if __name__ == '__main__':
app.run()