updates
This commit is contained in:
@@ -3,17 +3,13 @@ from sqlalchemy.orm import Session
|
||||
from pydantic import BaseModel, EmailStr
|
||||
from typing import Optional
|
||||
import logging
|
||||
|
||||
from ..config.database import get_db
|
||||
from ..models.user import User
|
||||
from ..models.role import Role
|
||||
from ..models.system_settings import SystemSettings
|
||||
from ..utils.mailer import send_email
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
router = APIRouter(prefix="/contact", tags=["contact"])
|
||||
|
||||
router = APIRouter(prefix='/contact', tags=['contact'])
|
||||
|
||||
class ContactForm(BaseModel):
|
||||
name: str
|
||||
@@ -22,182 +18,35 @@ class ContactForm(BaseModel):
|
||||
message: str
|
||||
phone: Optional[str] = None
|
||||
|
||||
|
||||
def get_admin_email(db: Session) -> str:
|
||||
"""Get admin email from system settings or find admin user"""
|
||||
# First, try to get from company_email (company settings)
|
||||
company_email_setting = db.query(SystemSettings).filter(
|
||||
SystemSettings.key == "company_email"
|
||||
).first()
|
||||
|
||||
company_email_setting = db.query(SystemSettings).filter(SystemSettings.key == 'company_email').first()
|
||||
if company_email_setting and company_email_setting.value:
|
||||
return company_email_setting.value
|
||||
|
||||
# Second, try to get from admin_email (legacy setting)
|
||||
admin_email_setting = db.query(SystemSettings).filter(
|
||||
SystemSettings.key == "admin_email"
|
||||
).first()
|
||||
|
||||
admin_email_setting = db.query(SystemSettings).filter(SystemSettings.key == 'admin_email').first()
|
||||
if admin_email_setting and admin_email_setting.value:
|
||||
return admin_email_setting.value
|
||||
|
||||
# If not found in settings, find the first admin user
|
||||
admin_role = db.query(Role).filter(Role.name == "admin").first()
|
||||
admin_role = db.query(Role).filter(Role.name == 'admin').first()
|
||||
if admin_role:
|
||||
admin_user = db.query(User).filter(
|
||||
User.role_id == admin_role.id,
|
||||
User.is_active == True
|
||||
).first()
|
||||
|
||||
admin_user = db.query(User).filter(User.role_id == admin_role.id, User.is_active == True).first()
|
||||
if admin_user:
|
||||
return admin_user.email
|
||||
|
||||
# Fallback to SMTP_FROM_EMAIL if configured
|
||||
from ..config.settings import settings
|
||||
if settings.SMTP_FROM_EMAIL:
|
||||
return settings.SMTP_FROM_EMAIL
|
||||
|
||||
# Last resort: raise error
|
||||
raise HTTPException(
|
||||
status_code=500,
|
||||
detail="Admin email not configured. Please set company_email in system settings or ensure an admin user exists."
|
||||
)
|
||||
raise HTTPException(status_code=500, detail='Admin email not configured. Please set company_email in system settings or ensure an admin user exists.')
|
||||
|
||||
|
||||
@router.post("/submit")
|
||||
async def submit_contact_form(
|
||||
contact_data: ContactForm,
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
"""Submit contact form and send email to admin"""
|
||||
@router.post('/submit')
|
||||
async def submit_contact_form(contact_data: ContactForm, db: Session=Depends(get_db)):
|
||||
try:
|
||||
# Get admin email
|
||||
admin_email = get_admin_email(db)
|
||||
|
||||
# Create email subject
|
||||
subject = f"Contact Form: {contact_data.subject}"
|
||||
|
||||
# Create email body (HTML)
|
||||
html_body = f"""
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<style>
|
||||
body {{
|
||||
font-family: Arial, sans-serif;
|
||||
line-height: 1.6;
|
||||
color: #333;
|
||||
}}
|
||||
.container {{
|
||||
max-width: 600px;
|
||||
margin: 0 auto;
|
||||
padding: 20px;
|
||||
background-color: #f9f9f9;
|
||||
}}
|
||||
.header {{
|
||||
background: linear-gradient(135deg, #d4af37 0%, #c9a227 100%);
|
||||
color: #0f0f0f;
|
||||
padding: 20px;
|
||||
text-align: center;
|
||||
border-radius: 8px 8px 0 0;
|
||||
}}
|
||||
.content {{
|
||||
background-color: #ffffff;
|
||||
padding: 30px;
|
||||
border-radius: 0 0 8px 8px;
|
||||
}}
|
||||
.field {{
|
||||
margin-bottom: 20px;
|
||||
}}
|
||||
.label {{
|
||||
font-weight: bold;
|
||||
color: #d4af37;
|
||||
display: block;
|
||||
margin-bottom: 5px;
|
||||
}}
|
||||
.value {{
|
||||
color: #333;
|
||||
padding: 10px;
|
||||
background-color: #f5f5f5;
|
||||
border-radius: 4px;
|
||||
}}
|
||||
.footer {{
|
||||
text-align: center;
|
||||
margin-top: 20px;
|
||||
padding-top: 20px;
|
||||
border-top: 1px solid #ddd;
|
||||
color: #666;
|
||||
font-size: 12px;
|
||||
}}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="container">
|
||||
<div class="header">
|
||||
<h2>New Contact Form Submission</h2>
|
||||
</div>
|
||||
<div class="content">
|
||||
<div class="field">
|
||||
<span class="label">Name:</span>
|
||||
<div class="value">{contact_data.name}</div>
|
||||
</div>
|
||||
<div class="field">
|
||||
<span class="label">Email:</span>
|
||||
<div class="value">{contact_data.email}</div>
|
||||
</div>
|
||||
{f'<div class="field"><span class="label">Phone:</span><div class="value">{contact_data.phone}</div></div>' if contact_data.phone else ''}
|
||||
<div class="field">
|
||||
<span class="label">Subject:</span>
|
||||
<div class="value">{contact_data.subject}</div>
|
||||
</div>
|
||||
<div class="field">
|
||||
<span class="label">Message:</span>
|
||||
<div class="value" style="white-space: pre-wrap;">{contact_data.message}</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="footer">
|
||||
<p>This email was sent from the hotel booking contact form.</p>
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
"""
|
||||
|
||||
# Create plain text version
|
||||
text_body = f"""
|
||||
New Contact Form Submission
|
||||
|
||||
Name: {contact_data.name}
|
||||
Email: {contact_data.email}
|
||||
{f'Phone: {contact_data.phone}' if contact_data.phone else ''}
|
||||
Subject: {contact_data.subject}
|
||||
|
||||
Message:
|
||||
{contact_data.message}
|
||||
"""
|
||||
|
||||
# Send email to admin
|
||||
await send_email(
|
||||
to=admin_email,
|
||||
subject=subject,
|
||||
html=html_body,
|
||||
text=text_body
|
||||
)
|
||||
|
||||
logger.info(f"Contact form submitted successfully. Email sent to {admin_email}")
|
||||
|
||||
return {
|
||||
"status": "success",
|
||||
"message": "Thank you for contacting us! We will get back to you soon."
|
||||
}
|
||||
|
||||
subject = f'Contact Form: {contact_data.subject}'
|
||||
html_body = f
|
||||
text_body = f
|
||||
await send_email(to=admin_email, subject=subject, html=html_body, text=text_body)
|
||||
logger.info(f'Contact form submitted successfully. Email sent to {admin_email}')
|
||||
return {'status': 'success', 'message': 'Thank you for contacting us! We will get back to you soon.'}
|
||||
except HTTPException:
|
||||
raise
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to submit contact form: {type(e).__name__}: {str(e)}", exc_info=True)
|
||||
raise HTTPException(
|
||||
status_code=500,
|
||||
detail="Failed to submit contact form. Please try again later."
|
||||
)
|
||||
|
||||
logger.error(f'Failed to submit contact form: {type(e).__name__}: {str(e)}', exc_info=True)
|
||||
raise HTTPException(status_code=500, detail='Failed to submit contact form. Please try again later.')
|
||||
Reference in New Issue
Block a user