-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathviews.py
111 lines (87 loc) · 3.11 KB
/
views.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
from os.path import isfile
from uuid import uuid4
from datetime import datetime
from app import *
from catalog import get_pics, get_vids, pic_catalog, vid_catalog, build_content_catalog
from client_mon import client_list
from filters import format_time, time_since
####### CONFIG ######
pic_slide_length = 12 #length of picture slideshow
vid_slide_length = 1 #length of video slideshow *** HTML ONLY SUPPORTS 1 ***
build_content_catalog_freq = 500 #rebuild content catalog every N requests
#####################
#counter that counts requests, controls ratio of videos to pictures
counter = 0
#returns index page
@app.route('/')
def index():
return redirect(url_for('media', location = 'common'))
@app.route('/loc/<location>')
def media(location = 'common'):
global counter
global client_list
# if counter is mult of 500, rebuild catalog to catch new files
if counter % build_content_catalog_freq == 0:
build_content_catalog()
if counter % 2 == 0: # If counter is even
counter += 1
content = get_pics(pic_slide_length, location)
response = make_response (render_template('pic.html', content = content))
else: #if counter is odd
counter += 1
content = get_vids(vid_slide_length, location)
response = make_response (render_template('vid.html', content = content))
#get client ID
client_id = request.cookies.get('client_id')
#if None, new client
if not client_id:
client_id = uuid4()
print 'new client:', client_id
response.set_cookie('client_id', bytes(client_id))
else:
print 'repeat_client:', client_id
client_info = [location, request.remote_addr, datetime.now(), True, response, content]
client_list[str(client_id)] = client_info
print 'current clients:', len(client_list)
for client in client_list.iterkeys():
print client
return response
@app.route('/loc/<location>/list')
def list(location = 'common'):
return render_template('list.html', pic_list = pic_catalog[location], vid_list = vid_catalog[location])
@app.route('/dash')
def dash():
global catalog_build_time
return render_template('dash.html', client_list = client_list, client_list_len = len(client_list))
@app.route('/clients/clear', methods = ['GET','POST'])
@app.route('/clients/clear/<client>', methods = ['GET','POST'])
def clients_clear(client = None):
global client_list
if not client:
client_list = {}
else:
del client_list[client]
return redirect(url_for('dash'))
@app.route('/clients/lastcontent_preview/<client>')
def client_last_content_preview(client):
return client_list[client][4]
@app.route('/clients/lastcontent_listing/<client>')
def client_last_content_listing(client):
return render_template('list_last.html', client = client, client_list = client_list)
#returns files
@app.route('/<path:path>')
def file_path(path):
#print "returning file" + path
#if file path is valid file
if isfile(path):
return send_file(path) #return the file
else:
return abort(404)
#forces catalog update
@app.route('/catalog', methods = ['GET','POST'])
def update():
build_content_catalog()
return redirect(url_for('dash'))
@app.errorhandler(Exception)
def handle_exception(e):
return redirect(url_for('media', location = 'common'))