-
Notifications
You must be signed in to change notification settings - Fork 0
/
ap.py
140 lines (114 loc) · 3.85 KB
/
ap.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
from random import randrange
from typing import Optional
from fastapi import FastAPI , Response , status, HTTPException,Request,Form
from fastapi.params import Body
from fastapi.templating import Jinja2Templates
from pydantic import BaseModel
from fastapi.staticfiles import StaticFiles
import psycopg2
from psycopg2.extras import RealDictCursor
#from apis.general_pages.route_homepage import general_pages_router
'''from . import models
from .database import engine, session_local
models.Base.metadata.create_all(bind=engine)
def get_db():
db = session_local()
try:
yield db
finally:
db.close()'''
app = FastAPI()
templates = Jinja2Templates(directory="html")
student=[{"name":"vansh","rollno":"556","id":1},{"name":"vanshnawander","rollno":"556","id":2}]
class student_registration(BaseModel):
fullname: str
email: str
college : str
domain: str
year: str
dob : str
address : str
username: str
password: str
cpassword: str
#section: Optional[str]=None
try:
conn= psycopg2.connect(host="localhost",database="postgres",user="postgres",password="vansh",cursor_factory=RealDictCursor)
cursor=conn.cursor()
print("databasse connected")
except Exception as e:
print("database failed to connect")
print("error : ",e)
def fin(id):
for i in student:
if i["id"]==id:
return i
def del_ind(id):
for i,j in enumerate(student):
if j["id"]==id:
return i
return 0
app.mount("/static", StaticFiles(directory="html"), name="static")
@app.get("/")
def home(request:Request):
return templates.TemplateResponse("Signin.html",{"request": request})
@app.post("/submit")
def login_check(username:str=Form(...),password:str=Form(...)):
cursor.execute(""" SELECT * FROM user_registration WHERE username=%s and password=%s""",(username,password))
a=cursor.fetchone()
if(a==None):
return {"invalid user"}
else:
return {"user is registered with the details": a}
@app.get("/vansh")
def page():
return "vansh"
@app.get("/posts")
async def root():
return {"message": student}
@app.get("/register")
async def register(request:Request):
return templates.TemplateResponse("Signup.html",{"request": request})
@app.post("/registration")
def create_post(fullname: str=Form(...),
email: str =Form(...),
college : str=Form(...),
domain: str=Form(...),
year: str =Form(...),
dob : str =Form(...),
address : str =Form(...),
username: str =Form(...),
password: str =Form(...),
cpassword: str =Form(...)):
#print(ab.dict())
#dic["id"]=randrange(0,1000000)
#student.append(dic)
if(password!=cpassword):
return {"password didn not match"}
cursor.execute("""INSERT INTO user_registration(fullname, email,college,domain,dob,address,username,password,year) values(%s,%s,%s,%s,%s,%s,%s,%s,%s) """,(fullname, email,college,domain,dob,address,username,password,year))
#a=cursor.fetchone()
conn.commit()
return {"created"}
@app.get("/posts/{id}")
def get_post(id:int,response : Response):
print(id)
a=fin(id)
if not a:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND,
detail="the is not found exception try")
#response.status_code=status.HTTP_404_NOT_FOUND
#return{"response":"the id not found in data base"}
return{"data":a}
@app.delete("/posts/{id}",status_code=status.HTTP_204_NO_CONTENT)
def delete_post(id:int):
x=del_ind(id)
student.pop(x)
return Response(status_code=status.HTTP_204_NO_CONTENT)
@app.put("/posts/{id}",status_code=status.HTTP_205_RESET_CONTENT)
def post_update(id :int,ab:student_registration):
x=del_ind(id)
abc=ab.dict()
student[x]["name"]=abc["fullname"]
student[x]["rollno"]=abc["email"]
student[x]["section"]=abc["year"]
return{"message":ab}