-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
98 lines (74 loc) · 3.45 KB
/
app.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
from fastapi import FastAPI, HTTPException
from fastapi.responses import FileResponse, JSONResponse
from pydantic import BaseModel
from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import letter
from reportlab.lib import colors
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
from reportlab.platypus import SimpleDocTemplate, Table, TableStyle, Paragraph
from pymongo import MongoClient
# uvicorn app:app --reload
app = FastAPI()
client = MongoClient("mongodb://localhost:27017/")
db = client['mydatabase']
collection = db['appointments']
def create_invoice(file_name, name, age, phone, date, time):
pdf = SimpleDocTemplate(filename=file_name, pagesize=letter)
content = []
# logo_path = "path/to/your/company_logo.png"
company_name = "Your Company Name"
company_address = "123 Company Street, City, Country"
# content.append(Image(logo_path, width=200, height=50))
content.append(Paragraph(company_name, style=ParagraphStyle('Title')))
content.append(Paragraph(company_address, style=getSampleStyleSheet()['BodyText']))
content.append(Paragraph("", style=getSampleStyleSheet()['BodyText']))
styles = getSampleStyleSheet()
style_normal = styles['Normal']
style_bold = ParagraphStyle('Bold', parent=styles['Normal'], fontName='Helvetica-Bold')
content.append(Paragraph("Invoice", style_bold))
content.append(Paragraph(f"Date: {date}", style_normal))
content.append(Paragraph(f"Name: {name}", style_normal))
content.append(Paragraph(f"Age: {age}", style_normal))
content.append(Paragraph(f"Phone: {phone}", style_normal))
content.append(Paragraph(f"Date: {date}", style_normal))
content.append(Paragraph(f"Time: {time}", style_normal))
data = [
["Description", "Details"],
["Name", name],
["Age", age],
["Phone", phone],
["Date", date],
["Time", time],
]
table_style = TableStyle([('BACKGROUND', (0, 0), (-1, 0), colors.grey),
('TEXTCOLOR', (0, 0), (-1, 0), colors.whitesmoke),
('ALIGN', (0, 0), (-1, -1), 'CENTER'),
('FONTNAME', (0, 0), (-1, 0), 'Helvetica-Bold'),
('BOTTOMPADDING', (0, 0), (-1, 0), 12),
('BACKGROUND', (0, 1), (-1, -1), colors.beige),
('GRID', (0, 0), (-1, -1), 1, colors.black)])
content.append(Table(data, style=table_style))
pdf.build(content)
class AppointmentRequest(BaseModel):
name: str
outlet_name: str
@app.post("/book-appointment")
async def book_appointment(appointment_data: AppointmentRequest):
try:
result = await collection.insert_one(appointment_data.model_dump())
appointment_id = str(result.inserted_id)
pdf_name=f"appointment_1.pdf"
pdf_path = f"/Users/rewatsachdeva/Desktop/web dev/clinic api-v2/{pdf_name}"
create_invoice(pdf_path,
appointment_id,
appointment_data.name,
appointment_data.age,
appointment_data.phone,
appointment_data.date,
appointment_data.time)
return JSONResponse(content="Booking successfull")
except Exception as e:
print(e)
return JSONResponse(content="Booking failed", status_code=404)
class SlotRequest(BaseModel):
appointment_date: str