-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
129 lines (95 loc) · 3.88 KB
/
Copy pathmain.py
File metadata and controls
129 lines (95 loc) · 3.88 KB
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
from fastapi.templating import Jinja2Templates
from fastapi.responses import HTMLResponse
from fastapi import FastAPI, Form, Request
from fastapi.security import OAuth2PasswordBearer
import credential_handler
import drive
app = FastAPI()
templates = Jinja2Templates(directory="templates")
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")
credential_handler.get_creds()
folder_1_id = "1ha3wKJKEyZVWv1o6qhhWCzYdUj-5krUt"
folder_2_id = "1V4PRKhXkhpUvL4Hae2ES7wBvM0TgaRMM"
file_id = "1RWGTSrK0MQGdI8TydxJVngYd_BX0sma1"
@app.get('/', response_class=HTMLResponse)
async def home(request: Request):
"""
Displays a home page with available views.
"""
return templates.TemplateResponse('home.html', {'request': request})
@app.get("/search-files")
async def search_files():
"""
Displays all user files and folders.
"""
return drive.search_files()
@app.get('/edit-file', response_class=HTMLResponse)
async def edit_file(request: Request):
"""
Renders a form with one field - file id, user must enter a file id and a new content will be uploaded to this file.
"""
return templates.TemplateResponse('edit_file.html', {'request': request})
@app.post('/edit-file')
async def edit_file(request: Request, object_id: str = Form(...)):
"""
Edits the file and shows a success page.
"""
drive.edit_file(object_id)
return templates.TemplateResponse('success.html', {'request': request})
@app.get('/delete-object', response_class=HTMLResponse)
async def delete_object(request: Request):
"""
Renders a form with one field - object id, user must enter an object id to delete.
"""
return templates.TemplateResponse('delete_object.html', {'request': request})
@app.post('/delete-object')
async def delete_object(request: Request, object_id: str = Form(...)):
"""
Trashes the file and shows a success page.
"""
drive.delete_object(object_id)
return templates.TemplateResponse('success.html', {'request': request})
@app.get('/move-file', response_class=HTMLResponse)
async def move_file(request: Request):
"""
Renders a form with two fields: file_id and folder_id,
user must enter an id of a file to move and an id of a folder to move it to.
"""
return templates.TemplateResponse('move_file.html', {'request': request})
@app.post('/move-file')
async def move_file(request: Request, folder_id: str = Form(...), file_id: str = Form(...)):
"""
Moves the file and shows a success page.
"""
drive.move_file(file_id, folder_id)
return templates.TemplateResponse('success.html', {'request': request})
@app.get('/upload-file', response_class=HTMLResponse)
async def upload_file_to_folder(request: Request):
"""
Renders the form with one field: folder id, user must enter the form id and the file 'test_file.txt'
will be uploaded to it.
"""
return templates.TemplateResponse('upload_file.html', {'request': request})
@app.post('/upload-file')
async def upload_file_to_folder(request: Request, folder_id: str = Form(...), file_name: str = Form(...)):
"""
Uploads the file and shows a success page.
"""
drive.upload_file_to_folder(file_name, folder_id)
return templates.TemplateResponse('success.html', {'request': request})
@app.get('/create-folder', response_class=HTMLResponse)
async def create_folder(request: Request):
"""
Renders a form with 1 field: folder name, user must enter the folder name and it will be created in g-drive.
"""
return templates.TemplateResponse('create_folder.html', {'request': request})
@app.post('/create-folder')
async def create_folder(request: Request, name: str = Form(...)):
"""
Creates the folder and shows a success page.
"""
drive.create_folder(name)
return templates.TemplateResponse('success.html', {'request': request})
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8000)