134 lines
4.3 KiB
Python
134 lines
4.3 KiB
Python
from flask import Flask, render_template, request, session, redirect, url_for, make_response
|
|
from flask_assets import Environment, Bundle
|
|
from translation_manager import init_app, translate, get_current_language, create_language_selector, set_language
|
|
import os
|
|
from datetime import datetime
|
|
from xml.etree import ElementTree as ET
|
|
|
|
app = Flask(__name__)
|
|
app.secret_key = os.environ.get('SECRET_KEY', 'your-secret-key-change-in-production')
|
|
|
|
# Initialize Flask-Assets
|
|
assets = Environment(app)
|
|
assets.url = app.static_url_path
|
|
|
|
# Configure assets for production
|
|
if not app.debug:
|
|
assets.auto_build = False
|
|
assets.cache = True
|
|
assets.manifest = 'file'
|
|
|
|
# Register asset bundles
|
|
css_bundle = assets.register('css_all', Bundle(
|
|
'css/main.css',
|
|
filters='cssmin',
|
|
output='css/main.min.css'
|
|
))
|
|
|
|
js_bundle = assets.register('js_all', Bundle(
|
|
'js/main.js',
|
|
filters='jsmin',
|
|
output='js/main.min.js'
|
|
))
|
|
|
|
# Initialize the translation system
|
|
init_app(app)
|
|
|
|
# Context processor to make variables available in templates
|
|
@app.context_processor
|
|
def inject_conf_var():
|
|
return dict(
|
|
translate=translate,
|
|
get_current_language=get_current_language,
|
|
create_language_selector=create_language_selector,
|
|
t=translate, # Short alias
|
|
current_year=datetime.now().year
|
|
)
|
|
|
|
def generate_sitemap():
|
|
"""Generate XML sitemap for the website"""
|
|
root = ET.Element("urlset")
|
|
root.set("xmlns", "http://www.sitemaps.org/schemas/sitemap/0.9")
|
|
|
|
# Define the main pages with their priorities and change frequencies
|
|
pages = [
|
|
{'url': '/', 'priority': '1.0', 'changefreq': 'weekly'},
|
|
{'url': '/services', 'priority': '0.9', 'changefreq': 'monthly'},
|
|
{'url': '/about', 'priority': '0.8', 'changefreq': 'monthly'},
|
|
{'url': '/contact', 'priority': '0.7', 'changefreq': 'monthly'},
|
|
{'url': '/portfolio', 'priority': '0.8', 'changefreq': 'weekly'},
|
|
]
|
|
|
|
base_url = "https://kobelly.com" # Change this to your actual domain
|
|
|
|
for page in pages:
|
|
url_elem = ET.SubElement(root, "url")
|
|
loc = ET.SubElement(url_elem, "loc")
|
|
loc.text = base_url + page['url']
|
|
|
|
lastmod = ET.SubElement(url_elem, "lastmod")
|
|
lastmod.text = datetime.now().strftime("%Y-%m-%d")
|
|
|
|
changefreq = ET.SubElement(url_elem, "changefreq")
|
|
changefreq.text = page['changefreq']
|
|
|
|
priority = ET.SubElement(url_elem, "priority")
|
|
priority.text = page['priority']
|
|
|
|
return ET.tostring(root, encoding='unicode', method='xml')
|
|
|
|
@app.route('/sitemap.xml')
|
|
def sitemap():
|
|
"""Serve the XML sitemap"""
|
|
sitemap_xml = generate_sitemap()
|
|
response = make_response(sitemap_xml)
|
|
response.headers["Content-Type"] = "application/xml"
|
|
return response
|
|
|
|
@app.route('/')
|
|
def index():
|
|
print("Current language in index:", get_current_language())
|
|
return render_template('index.html')
|
|
|
|
@app.route('/services')
|
|
def services():
|
|
return render_template('services.html')
|
|
|
|
@app.route('/contact')
|
|
def contact():
|
|
return render_template('contact.html')
|
|
|
|
@app.route('/about')
|
|
def about():
|
|
return render_template('about.html')
|
|
|
|
@app.route('/portfolio')
|
|
def portfolio():
|
|
return render_template('portfolio.html')
|
|
|
|
@app.route('/set_language/<language>')
|
|
def set_language_route(language):
|
|
"""Set the language and redirect back to the previous page"""
|
|
if set_language(language):
|
|
# Redirect back to the referring page or home
|
|
return redirect(request.referrer or url_for('index'))
|
|
return redirect(url_for('index'))
|
|
|
|
@app.route('/build-assets')
|
|
def build_assets():
|
|
"""Build minified assets with cache busting"""
|
|
if app.debug:
|
|
try:
|
|
# Build assets
|
|
css_bundle.build()
|
|
js_bundle.build()
|
|
return {'status': 'success', 'message': 'Assets built successfully'}
|
|
except Exception as e:
|
|
return {'status': 'error', 'message': str(e)}, 500
|
|
else:
|
|
return {'status': 'error', 'message': 'Asset building only available in debug mode'}, 403
|
|
|
|
if __name__ == '__main__':
|
|
# Check environment variables for debug mode
|
|
debug_mode = os.environ.get('FLASK_DEBUG', '1') == '1' and os.environ.get('FLASK_ENV') != 'production'
|
|
app.run(debug=debug_mode, host='0.0.0.0', port=5000) |