-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
133 lines (105 loc) · 4.2 KB
/
main.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
import logging
import webapp2
import os
import jinja2
# LocalFiles
from secretlist import CreateDecorator
from hortator import GetChromeManifest
from hortator import BuildChromeManifest
from hortator import StatsFromManifest
# Determine run location
global RUNLOCATION
if os.environ['SERVER_SOFTWARE'].startswith('Dev'):
RUNLOCATION = 'debug'
debug = True
else:
RUNLOCATION = 'online'
debug = False
#
# OAuth Token using list unpacking from secret files
#
decorator = (CreateDecorator(RUNLOCATION))
# Jinja Stuff Goes Here
jinja_environment = jinja2.Environment(autoescape=True,
loader=jinja2.FileSystemLoader(os.path.join(os.path.dirname(__file__), 'pagetemplates')))
class BaseHandler(webapp2.RequestHandler):
'All your Error Handling are belong to us'
def handle_exception(self, exception, debug):
# Log the error
logging.exception(exception)
template = jinja_environment.get_template('error.html')
self.response.out.write(template.render(messageobj=exception._get_reason()))
class Main(BaseHandler):
def get(self):
template = jinja_environment.get_template('index.html')
self.response.out.write(template.render())
class SetupPage(BaseHandler):
def get(self):
template = jinja_environment.get_template('setup.html')
self.response.out.write(template.render())
class AboutPage(BaseHandler):
def get(self):
template = jinja_environment.get_template('about.html')
self.response.out.write(template.render())
class StatsPage(BaseHandler):
@decorator.oauth_required
def get(self):
response = GetChromeManifest(decorator)
manifestTemplate = {
'annotatedUser': u'', 'lastEnrollmentTime': u'','lastSync': u'',
'notes': u'','orgUnitPath': u'','osVersion': u'',
'platformVersion': u'','serialNumber': u''
}
stats = StatsFromManifest(response)
response = BuildChromeManifest(manifestTemplate, response)
readableList = ['User', 'First Enrollment', 'Serial Number', 'Last Sync', 'Platform Version', 'Notes', 'OS Version', 'OU Path']
template = jinja_environment.get_template('statspage.html')
# Calculate percentages
total = len(response)
VProunded = 100*round(float(stats['VersionTotal']) / float(total),2)
CProunded = 100*round(float(stats['ChannelTotal']) / float(total),2)
AProunded = 100*round(float(stats['RecentSync']) / float(total),2)
self.response.out.write(template.render(
header_list=readableList, device_page=response,
Channel=stats['Channel'], OUPath=stats['OUPath'],
Version=stats['Version'], active=stats['RecentSync'],
VersionTotal=stats['VersionTotal'], VersionPercent=VProunded,
ChannelTotal=stats['ChannelTotal'], ChannelPercent=CProunded,
Status=stats['Status'], enroll=stats['RecentEnroll'],
ActivePercentage=AProunded ,total=total
)
)
class MakeCSV(BaseHandler):
@decorator.oauth_required
def get(self):
response = GetChromeManifest(decorator)
manifest = {
'annotatedLocation': u'','annotatedUser': u'','bootMode': u'','deviceId': u'',
'firmwareVersion': u'','lastEnrollmentTime': u'','lastSync': u'',
'macAddress': u'','meid': u'','model': u'','notes': u'','orderNumber': u'',
'orgUnitPath': u'','osVersion': u'','platformVersion': u'','serialNumber': u'',
'status': u'','supportEndDate': u'','willAutoRenew': u''
}
response = BuildChromeManifest(manifest, response)
# Specify headers for a CSV download
self.response.headers['Content-Type'] = 'application/csv'
self.response.headers.add_header('content-disposition', 'attachment', filename='devicelist.csv')
# Write the CSV Header entries
line = ""
for _ in manifest.keys():
line += ( "\"" + _ + "\"" + ",")
self.response.write(line[:-1] + "\n")
# Begin writing the device info lines
for row in response:
line = ""
for _ in row:
line += ("\"" + str(_) + "\"" + ",")
self.response.write(line[:-1] + "\n")
app = webapp2.WSGIApplication( [
( '/', Main),
( '/csv', MakeCSV),
( '/stats', StatsPage),
( '/about', AboutPage),
( '/setup', SetupPage),
(decorator.callback_path, decorator.callback_handler())
], debug=debug )