/
server.py
152 lines (128 loc) · 4.98 KB
/
server.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
from bottle import template, redirect, request
from oauth2client.contrib.multistore_file import get_credential_storage
from beaker.middleware import SessionMiddleware
from google_api import flow, scope
from apiclient.discovery import build
from database import Document, sql_alchemy_plugin, User, create_db
import httplib2
import bottle
import arrow
import logging
import funcy
import log as log_module
log_module.configure_logging()
log = logging.getLogger("server")
app = bottle.Bottle()
app.install(sql_alchemy_plugin)
# Configure the SessionMiddleware
session_opts = {
'session.type': 'ext:database',
'session.url': 'sqlite:///session.db',
}
app_with_session = SessionMiddleware(app, session_opts)
db = create_db()
auth_uri = flow.step1_get_authorize_url()
def get_user_info(credentials):
"""Get user info from credentials.
Arguments:
credentials -- to access the Drive API
"""
http_auth = credentials.authorize(httplib2.Http())
people_service = build('people', 'v1', http=http_auth)
people_resource = people_service.people()
info = people_resource.get(resourceName='people/me').execute()
return info['names'][0]['metadata']['source']['id']
def get_user(db, google_id):
user = db.query(User).filter_by(google_id=google_id).first()
if not user:
user = User(google_id=google_id, is_download_data=False)
db.add(user)
db.commit()
log.info(
('add new user with google_id %s '
'in User table (is_download_data = %s)'),
google_id,
user.is_download_data)
return user
@app.route('/auth_result')
def auth_result(db):
session = bottle.request.environ.get('beaker.session')
auth_code = request.query.code
credentials = flow.step2_exchange(auth_code)
google_id = get_user_info(credentials)
session['google_id'] = google_id
session.save()
storage = get_credential_storage('test_storage.txt',
google_id,
'user_agent',
scope)
storage.put(credentials)
get_user(db, google_id)
log.info('put credentials in storage file %s', credentials)
return redirect('/')
@funcy.once
def set_trace():
import pdb
pdb.set_trace()
@app.route('/', method=['POST', 'GET'])
def index(db):
session = bottle.request.environ.get('beaker.session')
user_name = session.get('google_id')
if user_name is None:
return redirect(auth_uri)
log.info(
'don''t authorized user_name (google_id = %s)',
str(user_name))
else:
log.info(
'authorized user_name (google_id = %s)',
str(user_name))
user_info = get_user(db, user_name)
is_download_data = False
if user_info.is_download_data:
is_download_data = user_info.is_download_data
files_info = db.query(Document).filter(
Document.google_code_id == user_name).all()
filtr_owner = ''
modification_time_str = ''
creation_time_str = ''
filtr_header = ''
link_str = ''
last_modification_author_str = ''
is_public_access_str = ''
type_access_str = ''
permission_access_str = ''
if request.method == 'POST':
log.info('filtering files...')
filtr_header = request.POST.dict['header'][0]
parser = arrow.parser.DateTimeParser()
creation_time_str = request.POST.dict['creation_time'][0]
if creation_time_str:
creation_time = parser.parse_iso(creation_time_str)
log.info(
'filtering files creation_time > %s',
creation_time_str)
files_info = db.query(Document).filter(
Document.creation_time > creation_time,
Document.google_code_id == user_name)
if filtr_header:
log.info(
'filtering files header include substring = %s',
filtr_header)
files_info = db.query(Document).filter(Document.header.like(
'%' + filtr_header.decode('utf-8') + '%'),
Document.google_code_id == user_name)
return template('file_listing', is_download_data=is_download_data,
files_info=files_info,
owner=filtr_owner,
creation_time=creation_time_str,
modification_time=modification_time_str,
header=filtr_header,
link=link_str,
last_modification_author=last_modification_author_str,
is_public_access=is_public_access_str,
type_access=type_access_str,
permission_access=permission_access_str,
user_name=str(user_name))
if __name__ == '__main__':
bottle.run(host='0.0.0.0', port=80, app=app_with_session)