Files
booking/utils.py
T

50 lines
1.6 KiB
Python
Raw Normal View History

2025-01-12 09:57:35 +08:00
import random
import string
import smtplib
from email.mime.text import MIMEText
2025-01-21 20:45:29 +08:00
import os
import uuid
import config
from werkzeug.utils import secure_filename
UPLOAD_ROOT = os.path.join(os.getcwd(), 'uploads')
2025-01-12 09:57:35 +08:00
def generate_password(length=10):
characters = string.ascii_letters + string.digits
return ''.join(random.choice(characters) for _ in range(length))
def send_email(recipient_email, subject, body):
sender_email = "your_email@example.com"
sender_password = "your_email_password"
msg = MIMEText(body)
msg['Subject'] = subject
msg['From'] = sender_email
msg['To'] = recipient_email
try:
with smtplib.SMTP('smtp.gmail.com', 587) as server:
server.starttls()
server.login(sender_email, sender_password)
server.send_message(msg)
except Exception as e:
print(f"Failed to send email: {e}")
2025-01-21 20:45:29 +08:00
def is_file_allowed(file):
return '.' in file.filename and file.filename.rsplit('.', 1)[1].lower() in config.ALLOWED_EXTENSIONS
def save_document(file, subfolder):
if not is_file_allowed(file):
raise ValueError("File type not allowed. Only images are permitted.")
if len(file.read()) > config.MAX_FILE_SIZE:
raise ValueError("File size exceeds the limit of 5 MB.")
file.seek(0)
upload_folder = os.path.join(UPLOAD_ROOT, subfolder)
os.makedirs(upload_folder, exist_ok=True)
filename = secure_filename(f"{uuid.uuid4().hex}.{file.filename.rsplit('.', 1)[1].lower()}")
filepath = os.path.join(upload_folder, filename)
file.save(filepath)
return filename