dynamic colours in website settings
This commit is contained in:
Binary file not shown.
130
routes/main.py
130
routes/main.py
@@ -1,6 +1,6 @@
|
||||
from flask import render_template, Blueprint, redirect, url_for, request, flash
|
||||
from flask import render_template, Blueprint, redirect, url_for, request, flash, Response
|
||||
from flask_login import current_user, login_required
|
||||
from models import User, db, Room, RoomFile, RoomMemberPermission
|
||||
from models import User, db, Room, RoomFile, RoomMemberPermission, SiteSettings
|
||||
import os
|
||||
from werkzeug.utils import secure_filename
|
||||
from sqlalchemy import func, case, literal_column, text
|
||||
@@ -326,4 +326,128 @@ def init_routes(main_bp):
|
||||
@main_bp.route('/settings')
|
||||
@login_required
|
||||
def settings():
|
||||
return render_template('settings/settings.html')
|
||||
if not current_user.is_admin:
|
||||
flash('Only administrators can access settings.', 'error')
|
||||
return redirect(url_for('main.dashboard'))
|
||||
|
||||
site_settings = SiteSettings.get_settings()
|
||||
return render_template('settings/settings.html',
|
||||
primary_color=site_settings.primary_color,
|
||||
secondary_color=site_settings.secondary_color)
|
||||
|
||||
@main_bp.route('/settings/colors', methods=['POST'])
|
||||
@login_required
|
||||
def update_colors():
|
||||
if not current_user.is_admin:
|
||||
flash('Only administrators can update settings.', 'error')
|
||||
return redirect(url_for('main.dashboard'))
|
||||
|
||||
primary_color = request.form.get('primary_color')
|
||||
secondary_color = request.form.get('secondary_color')
|
||||
|
||||
if not primary_color or not secondary_color:
|
||||
flash('Both primary and secondary colors are required.', 'error')
|
||||
return redirect(url_for('main.settings'))
|
||||
|
||||
site_settings = SiteSettings.get_settings()
|
||||
site_settings.primary_color = primary_color
|
||||
site_settings.secondary_color = secondary_color
|
||||
|
||||
try:
|
||||
db.session.commit()
|
||||
flash('Color settings updated successfully!', 'success')
|
||||
except Exception as e:
|
||||
db.session.rollback()
|
||||
flash('An error occurred while updating color settings.', 'error')
|
||||
|
||||
return redirect(url_for('main.settings'))
|
||||
|
||||
@main_bp.route('/settings/colors/reset', methods=['POST'])
|
||||
@login_required
|
||||
def reset_colors():
|
||||
if not current_user.is_admin:
|
||||
flash('Only administrators can update settings.', 'error')
|
||||
return redirect(url_for('main.dashboard'))
|
||||
|
||||
site_settings = SiteSettings.get_settings()
|
||||
site_settings.primary_color = '#16767b' # Default from colors.css
|
||||
site_settings.secondary_color = '#741b5f' # Default from colors.css
|
||||
|
||||
try:
|
||||
db.session.commit()
|
||||
flash('Colors reset to defaults successfully!', 'success')
|
||||
except Exception as e:
|
||||
db.session.rollback()
|
||||
flash('An error occurred while resetting colors.', 'error')
|
||||
|
||||
return redirect(url_for('main.settings'))
|
||||
|
||||
@main_bp.route('/dynamic-colors.css')
|
||||
def dynamic_colors():
|
||||
site_settings = SiteSettings.get_settings()
|
||||
|
||||
# Calculate derived colors
|
||||
def hex_to_rgb(hex_color):
|
||||
hex_color = hex_color.lstrip('#')
|
||||
return tuple(int(hex_color[i:i+2], 16) for i in (0, 2, 4))
|
||||
|
||||
def rgb_to_hex(rgb):
|
||||
return '#{:02x}{:02x}{:02x}'.format(rgb[0], rgb[1], rgb[2])
|
||||
|
||||
def lighten_color(hex_color, amount):
|
||||
rgb = hex_to_rgb(hex_color)
|
||||
return rgb_to_hex(tuple(min(255, int(c + (255 - c) * amount)) for c in rgb))
|
||||
|
||||
# Calculate all color variants
|
||||
primary_light = lighten_color(site_settings.primary_color, 0.15)
|
||||
primary_bg_light = lighten_color(site_settings.primary_color, 0.9)
|
||||
primary_opacity_15 = site_settings.primary_color + '26'
|
||||
|
||||
secondary_light = lighten_color(site_settings.secondary_color, 0.15)
|
||||
secondary_bg_light = lighten_color(site_settings.secondary_color, 0.9)
|
||||
secondary_opacity_15 = site_settings.secondary_color + '26'
|
||||
|
||||
# Calculate chart colors
|
||||
primary_chart_light = lighten_color(site_settings.primary_color, 0.2)
|
||||
primary_chart_lighter = lighten_color(site_settings.primary_color, 0.4)
|
||||
primary_chart_lightest = lighten_color(site_settings.primary_color, 0.6)
|
||||
primary_chart_pale = lighten_color(site_settings.primary_color, 0.8)
|
||||
|
||||
secondary_chart_light = lighten_color(site_settings.secondary_color, 0.2)
|
||||
secondary_chart_lighter = lighten_color(site_settings.secondary_color, 0.4)
|
||||
secondary_chart_lightest = lighten_color(site_settings.secondary_color, 0.6)
|
||||
secondary_chart_pale = lighten_color(site_settings.secondary_color, 0.8)
|
||||
|
||||
css = f"""
|
||||
:root {{
|
||||
/* Primary Colors */
|
||||
--primary-color: {site_settings.primary_color};
|
||||
--primary-light: {primary_light};
|
||||
--primary-bg-light: {primary_bg_light};
|
||||
--primary-opacity-15: {primary_opacity_15};
|
||||
|
||||
/* Secondary Colors */
|
||||
--secondary-color: {site_settings.secondary_color};
|
||||
--secondary-light: {secondary_light};
|
||||
--secondary-bg-light: {secondary_bg_light};
|
||||
--secondary-opacity-15: {secondary_opacity_15};
|
||||
|
||||
/* Chart Colors */
|
||||
--chart-primary: {site_settings.primary_color};
|
||||
--chart-secondary: {site_settings.secondary_color};
|
||||
--chart-warning: #ffd700;
|
||||
|
||||
/* Primary Chart Colors */
|
||||
--chart-primary-light: {primary_chart_light};
|
||||
--chart-primary-lighter: {primary_chart_lighter};
|
||||
--chart-primary-lightest: {primary_chart_lightest};
|
||||
--chart-primary-pale: {primary_chart_pale};
|
||||
|
||||
/* Secondary Chart Colors */
|
||||
--chart-secondary-light: {secondary_chart_light};
|
||||
--chart-secondary-lighter: {secondary_chart_lighter};
|
||||
--chart-secondary-lightest: {secondary_chart_lightest};
|
||||
--chart-secondary-pale: {secondary_chart_pale};
|
||||
}}
|
||||
"""
|
||||
return Response(css, mimetype='text/css')
|
||||
Reference in New Issue
Block a user