Created
February 2, 2017 19:41
-
-
Save edwindotcom/6785b5b24111ab4b5e31feb6d2e57030 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from bottle import route, run, get, post, request, redirect, static_file | |
import json | |
import os | |
import sys | |
from os import listdir | |
from os.path import isfile, join | |
# if not os.environ.has_key("IMG_ROOT"): | |
# print 'need IMG_ROOT env set' | |
# sys.exit(1) | |
# img_root = os.environ['IMG_ROOT'] | |
employees = [] | |
out = "" | |
out_json = {} | |
reportsTo = "" | |
reportsToId = "" | |
index_html = """<h1>TPM Services:</h1> | |
<p><a href='/images'>Images Service</a> - upload and host images</p> | |
<p><a href='/orgchart'>Org Chart</a> - data is from december, can't get data from workday</p>""" | |
header = "<html><head><title>TPM Services</title></head><body style='font-family:Arial,sans-serif'>" | |
footer = "</body></html>" | |
search_form = """<form action="/search" method="get"> | |
Search Org Chart: <input type="text" name="user" value=""> | |
<input type="submit" value="search"> | |
</form>""" | |
upload_form = """ | |
<form method="POST" enctype="multipart/form-data" action="/upload"> | |
<label>Upload file</label><br> | |
<p><input type="file" name="uploadfile" /></p> | |
<p><input type="submit" value="Upload" /></p> | |
<p><a href='/img'>Images List</a>""" | |
def loadJson(json_file): | |
with open(json_file) as jdata: | |
data = json.load(jdata) | |
return data | |
def search(node, user): | |
global reportsToId | |
global employees | |
global out | |
global out_json | |
for i in range(len(node)): | |
node_name = str(node[i]['name']) | |
job_title = node[i]['data']['jInfo']['job_title'] | |
user = str(user) | |
if job_title.lower().find(user.lower()) != -1: | |
employees.append((node_name, job_title)) | |
out += "|--- <img src='%s' height='60' width='60' align='middle' />%s - %s <br />" % (node[i]['data']['img'], node_name, job_title) | |
if node[i]['children']: | |
for j in range(len(node[i]['children'])): | |
out += " |--- <img src='%s' height='60' width='60' align='middle' /> <a href='search?user=%s'>%s</a> - %s<br />" % (node[i]['children'][j]['data']['img'], node[i]['children'][j]['name'], node[i]['children'][j]['name'], node[i]['children'][j]['data']['jInfo']['job_title']) | |
reportsToId = node[i]['data']['reportsTo'] | |
if node_name.lower().find(user.lower()) != -1: | |
out_json = node[i] | |
employees.append((node_name, job_title)) | |
out += "|--- <img src='%s' height='60' width='60' align='middle' />%s - %s <br />" % (node[i]['data']['img'], node_name, job_title) | |
if node[i]['children']: | |
for j in range(len(node[i]['children'])): | |
out += " |--- <img src='%s' height='60' width='60' align='middle' /> <a href='search?user=%s'>%s</a> - %s<br />" % (node[i]['children'][j]['data']['img'], node[i]['children'][j]['name'], node[i]['children'][j]['name'], node[i]['children'][j]['data']['jInfo']['job_title']) | |
reportsToId = node[i]['data']['reportsTo'] | |
if node[i]['children']: | |
search(node[i]['children'], user) | |
def find_by_id(node, id): | |
global out | |
global reportsTo | |
if id: | |
for i in range(len(node)): | |
if int(node[i]['id']) == int(id): | |
reportsTo = "<img src='%s' height='60' width='60' align='middle' /> <a href='search?user=%s'>%s</a> - %s<br />" % (node[i]['data']['img'], node[i]['name'], node[i]['name'], node[i]['data']['jInfo']['job_title']) | |
break | |
return | |
if node[i]['children']: | |
find_by_id(node[i]['children'], id) | |
else: | |
print 'id not found' | |
return | |
@route('/search') | |
def main(): | |
global out | |
global employees | |
global reportsTo | |
out = "" | |
data = loadJson('data_20161221.json') | |
if not request.query.user: | |
redirect('/') | |
user = request.query.user | |
user.replace('%20', ' ') | |
print 'searching for %s in %s' % (user, "name") | |
search(data['children'], user) | |
if len(employees) > 1: | |
out = 'Found %s:<br />' % len(employees) | |
for emp in employees: | |
out += "<a href='search?user=%s'>%s</a> - %s<br />" % (emp[0], emp[0], emp[1]) | |
del employees[:] | |
return header + search_form + out + footer | |
elif(len(employees) == 0): | |
del employees[:] | |
return header + search_form + 'No one found by that name or title' + footer | |
else: | |
# print out_json | |
find_by_id(data['children'], reportsToId) | |
if user == 'Kenneth Lin': | |
reportsTo = "" | |
if out: | |
# render_str = reportsTo + out | |
del employees[:] | |
return header + search_form + reportsTo + out + footer | |
@route('/upload', method='POST') | |
def upload(): | |
"""Handle file upload form""" | |
# get the 'newfile' field from the form | |
newfile = request.files.get('uploadfile') | |
# only allow upload of text files | |
# if newfile.content_type != 'image/gif, image/jpg, image/png': | |
# return "Only images allowed" | |
save_path = os.path.join(img_root, newfile.filename) | |
newfile.save(save_path) | |
# redirect to home page if it all works ok | |
return redirect('/img') | |
@route('/img') | |
def print_files(): | |
onlyfiles = [f for f in listdir(img_root) if isfile(join(img_root, f))] | |
out = "" | |
for item in onlyfiles: | |
out += "<a href='img/%s'>%s</a><br />" % (item, item) | |
return out | |
@route("/img/<filename>", name="static") | |
def static(filename): | |
return static_file(filename, root=img_root) | |
@route('/images') | |
def index(): | |
return header + upload_form + footer | |
@route('/orgchart') | |
def index(): | |
return header + search_form + footer | |
@route('/') | |
def index(): | |
return header + index_html + footer | |
run(host='0.0.0.0', port=80, debug=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment