-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
executable file
·215 lines (195 loc) · 6.43 KB
/
main.py
File metadata and controls
executable file
·215 lines (195 loc) · 6.43 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import datetime
import hashlib
import os
import threading
from fastapi import FastAPI, HTTPException, Request, Depends
from faunadb import query as q
from faunadb.client import FaunaClient
import pytz
from src.json_to_md import write_to_file
from src.sendemail import sendmail
from starlette.config import Config
from authlib.integrations.starlette_client import OAuth
from auth.oauth import get_current_user
from auth.jwttoken import create_access_token
from auth.model import User
from fastapi.security import OAuth2PasswordRequestForm
app = FastAPI()
FAUNA_DB_KEY = os.getenv("FAUNA_DB_KEY")
client = FaunaClient(secret=FAUNA_DB_KEY)
indexes = client.query(q.paginate(q.indexes()))
config = Config('.env')
oauth = OAuth(config)
@app.get("/")
async def root():
info = {
"availabe_routes": {
"/schema": "shows the JSON schema to POST at /savedata",
"/register": "let's you register to the app",
"/login": "let's you login and get an access token",
"/savedata": "let's you save your data to the database using the access token",
"/resume/{user}": "replace {user} with your registered username to generate your resume"
}
}
return info
@app.get("/schema")
async def send_schema():
schema = {
"user": "foo-bar",
"full_name": "foo-bar",
"address": "foo-bar",
"phone": "foo-bar",
"email": "foo-bar",
"website": "foo-bar",
"summary": "foo-bar",
"skills": "foo-bar",
"education": {
"1": {
"school": "foo-bar",
"start": "foo-bar",
"end": "foo-bar",
"details": "foo-bar"
},
"2": {
"school": "foo-bar",
"start": "foo-bar",
"end": "foo-bar",
"details": "foo-bar"
},
"3": {
"school": "foo-bar",
"start": "foo-bar",
"end": "foo-bar",
"details": "foo-bar"
}
},
"job": {
"1": {
"employer": "foo-bar",
"position": "foo-bar",
"start": "foo-bar",
"end": "foo-bar",
"details": "foo-bar"
},
"2": {
"employer": "foo-bar",
"position": "foo-bar",
"start": "foo-bar",
"end": "foo-bar",
"details": "foo-bar"
},
"3": {
"employer": "foo-bar",
"position": "foo-bar",
"start": "foo-bar",
"end": "foo-bar",
"details": "foo-bar"
}
},
"project": {
"1": {
"name": "foo-bar",
"end": "foo-bar",
"details": "foo-bar"
},
"2": {
"name": "foo-bar",
"end": "foo-bar",
"details": "foo-bar"
},
"3": {
"name": "foo-bar",
"end": "foo-bar",
"details": "foo-bar"
}
},
"references": "foo-bar"
}
return schema
@app.get("/resume/{user}")
async def create_resume(user: str):
try:
is_user_present = client.query(q.get(q.match(q.index("users_index"), user)))
try:
try:
data = client.query(q.get(q.match(q.index("resume_index"), user)))["data"]
except Exception as e:
return {
"status": "FAILED",
"message": e
}
try:
write_to_file(data)
except Exception as e:
return {
"status": "FAILED",
"message": str(e)
}
os.system("./push.sh")
print("sending email")
threading.Timer(120, sendmail(data["email"], f"https://givemyresume.tech/{data['user']}", data["full_name"])).start()
print("email sent")
return {
"status": "SUCCESS",
"message": "We will send you an email with your resume link."
}
except Exception as e:
return {
"status": "FAILED",
"message": str(e)
}
except:
return {
"status": "FAILED",
"message": "User not found with the given username. Have you signed up yet?"
}
@app.post("/register")
def register(request:User):
try:
user = client.query(q.get(q.match(q.index("users_index"), request.username)))
except:
user = False
if user:
raise HTTPException(status_code=400, detail="Username already registered")
hashed_pass = hashlib.sha512(request.password.encode()).hexdigest()
user_object = dict(request)
user_object["password"] = hashed_pass
user_object["date"] = datetime.datetime.now(pytz.UTC)
user_id = client.query(q.create(q.collection("Users"), {
"data": user_object
}))
return {"status":"user created"}
@app.post('/login')
def login(request:OAuth2PasswordRequestForm = Depends()):
try:
user = client.query(q.get(q.match(q.index("users_index"), request.username)))
except:
user = False
if not user:
raise HTTPException(status_code=400, detail="Username not registered")
else:
if not hashlib.sha512(request.password.encode()).hexdigest() == user["data"]["password"]:
raise HTTPException(status_code=400, detail="Incorrect Password")
access_token = create_access_token(data={"sub": user["data"]["username"] })
return {"access_token": access_token, "token_type": "bearer"}
@app.post("/savedata")
async def savedata(signupData : Request, username = Depends(get_current_user)):
data = await signupData.json()
data["user"] = username
try:
resume = client.query(q.get(q.match(q.index("resume_index"), username)))
quiz = client.query(q.update(q.ref(q.collection("Resume_Info"),resume["ref"].id()), {
"data": data
}))
return {
"status" : "SUCCESS",
"message": "Data Updated"
}
except:
quiz = client.query(q.create(q.collection("Resume_Info"), {
"data": data
}))
return {
"status" : "SUCCESS",
"message": "Data Created"
}