temp_helper / app.py
Junaidb's picture
Update app.py
26cc895 verified
from fastapi import FastAPI,Request,HTTPException,Response
from typing import Optional
from fastapi.responses import JSONResponse
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
from components.dbconnection import provideClient
from fastapi.responses import HTMLResponse
origins=["*"]
app=FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
client = provideClient()
db = client["pahalgam_pd"]
coll = db["ponyworkers"]
class PPD(BaseModel):
name:str
registration_number:str
phone_number:str
address:str
aadhar:str
parentage:str
total_ponies:str
stand:str
license:bool
renewal:bool
@app.post("/dataingestion")
async def Ingest(request:PPD):
payload={
"name":request.name,
"registration_number":request.registration_number,
"phone_number":request.phone_number,
"address":request.address,
"aadhar":request.aadhar,
"parentage":request.parentage,
"total_ponies":request.total_ponies,
"stand":request.stand,
"license":request.license,
"renewal":request.renewal
}
result = coll.insert_one(payload)
print(f"Inserted ID: {result.inserted_id}")
return {"success":True}
@app.get("/", response_class=HTMLResponse)
async def static_page():
return """
<!DOCTYPE html5>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Pony Registration - Official Form</title>
<style>
/* */
:root {
--primary-blue: #003366; /* Navy Blue */
--secondary-red: #d22027; /* Accent Red */
--bg-gray: #f0f0f0;
--border-gray: #5b616b;
--text-dark: #1b1b1b;
}
body {
font-family: 'Source Sans Pro', 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif;
background: var(--bg-gray);
display: flex;
justify-content: center;
padding: 40px 20px;
color: var(--text-dark);
line-height: 1.5;
}
.form-container {
background: white;
padding: 40px;
border-top: 8px solid var(--primary-blue);
box-shadow: 0 4px 8px rgba(0,0,0,0.1);
width: 100%;
max-width: 650px;
height:100%;
}
.header-section {
border-bottom: 2px solid #efefef;
margin-bottom: 25px;
padding-bottom: 15px;
}
h2 {
color: var(--primary-blue);
margin: 0;
font-size: 28px;
text-transform: uppercase;
letter-spacing: 1px;
}
.instruction-text {
font-size: 14px;
color: #555;
margin-top: 5px;
}
.grid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 20px;
}
.field { margin-bottom: 20px; }
.full { grid-column: span 2; }
label {
display: block;
font-size: 14px;
font-weight: 700;
margin-bottom: 8px;
color: var(--text-dark);
}
input[type="text"],
input[type="number"] {
width: 100%;
padding: 12px;
border: 1px solid var(--border-gray);
border-radius: 0; /* Govt forms often use sharp corners */
box-sizing: border-box;
font-size: 16px;
}
input:focus {
outline: 3px solid #2491ff;
outline-offset: 0;
}
/* Checkbox styling */
.checkbox-group {
display: flex;
align-items: center;
gap: 10px;
cursor: pointer;
}
input[type="checkbox"] {
width: 20px;
height: 20px;
cursor: pointer;
}
/* Buttons */
.button-row {
display: flex;
gap: 15px;
margin-top: 30px;
border-top: 1px solid #ddd;
padding-top: 20px;
}
button {
flex: 2;
padding: 15px;
font-size: 16px;
font-weight: bold;
border: none;
cursor: pointer;
transition: background 0.2s;
}
.btn-submit {
background: var(--primary-blue);
color: white;
}
.btn-submit:hover { background: #002244; }
.btn-clear {
flex: 1;
background: #e1e1e1;
color: #333;
border: 1px solid #ccc;
}
.btn-clear:hover { background: #d0d0d0; }
.required-star { color: var(--secondary-red); }
</style>
</head>
<body>
<div class="form-container">
<div class="header-section">
<h2>Pony Registration</h2>
<div class="instruction-text">Official Registration Form for Domesticated Equines. Please complete all required fields.</div>
</div>
<form id="registrationForm" onsubmit="submitData(event)">
<div class="grid">
<div class="field full">
<label for="name">Full Legal Name <span class="required-star">*</span></label>
<input id="name" name="name" type="text" placeholder="e.g. John Doe" required>
</div>
<div class="field">
<label for="registration_number">Registration Number</label>
<input id="registration_number" name="registration_number" type="text" required>
</div>
<div class="field">
<label for="phone_number">Phone Number</label>
<input id="phone_number" name="phone_number" type="text" placeholder="(555) 000-0000" required>
</div>
<div class="field full">
<label for="address">Address</label>
<input id="address" name="address" type="text" required>
</div>
<div class="field">
<label for="aadhar">Aadhar / ID Number</label>
<input id="aadhar" name="aadhar" type="text" required>
</div>
<div class="field">
<label for="parentage">Parentage / Lineage</label>
<input id="parentage" name="parentage" type="text" required>
</div>
<div class="field">
<label for="total_ponies">Total Quantity of Ponies</label>
<input type="number" id="total_ponies" name="total_ponies" required>
</div>
<div class="field">
<label for="stand">Assigned Stand</label>
<input id="stand" name="stand" type="text" required>
</div>
<div class="field">
<label for="license">License Held</label>
<input type="checkbox" id="license" name="license">
</div>
<div class="field">
<label for="renewal">Applied for Renewal</label>
<input type="checkbox" id="renewal" name="renewal">
</div>
</div>
<div class="button-row">
<button type="button" class="btn-clear" onclick="clearForm()">Clear Form</button>
<button type="submit" class="btn-submit">Submit Application</button>
</div>
</form>
</div>
<script>
async function submitData(e) {
e.preventDefault()
const form = document.getElementById('registrationForm');
const formData = new FormData(form);
const payload = Object.fromEntries(formData.entries());
try {
const res = await fetch('/dataingestion', {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify(payload)
});
const result = await res.json();
if(result.success === true){
alert("Submission Successful: Data has been transmitted.");
} else {
alert("Error: The server could not process your application.");
}
} catch (error) {
console.error("Transmission error:", error);
alert("Network Error: Could not connect to official servers.");
}
}
function clearForm() {
if(confirm("Are you sure you want to clear all entered data?")) {
document.getElementById("registrationForm").reset();
}
}
</script>
</body>
</html>
"""