Skip to content

Commit b88ec48

Browse files
Fix theming
1 parent 3ca2fda commit b88ec48

File tree

4 files changed

+77
-40
lines changed

4 files changed

+77
-40
lines changed

app/models.py

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -30,32 +30,12 @@ class User(UserMixin, db.Model):
3030
da_password_encrypted = db.Column(db.Text, nullable=True)
3131
da_domain = db.Column(db.String(255), nullable=True)
3232

33+
# User preferences
34+
theme_preference = db.Column(db.String(20), default='light', nullable=True)
35+
3336
# Unique encryption key per user for DA password
3437
encryption_key = db.Column(db.String(255), nullable=True)
3538

36-
def get_theme_preference(self):
37-
"""Get theme preference - uses session storage until database column exists"""
38-
try:
39-
# Try to get from database column if it exists
40-
if hasattr(self, 'theme_preference') and hasattr(self.__class__, 'theme_preference'):
41-
return getattr(self, 'theme_preference', 'light') or 'light'
42-
except:
43-
pass
44-
# Default to light theme
45-
return 'light'
46-
47-
def set_theme_preference(self, theme):
48-
"""Set theme preference - will work once database column exists"""
49-
try:
50-
if theme in ['light', 'dark']:
51-
# Try to set database column if it exists
52-
if hasattr(self.__class__, 'theme_preference'):
53-
setattr(self, 'theme_preference', theme)
54-
return True
55-
except:
56-
pass
57-
return False
58-
5939
def __init__(self, **kwargs):
6040
"""Initialize user with encryption key"""
6141
super(User, self).__init__(**kwargs)

app/settings.py

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def get_da_config():
2222
'da_username': current_user.da_username or '',
2323
'da_domain': current_user.da_domain or '',
2424
'has_password': bool(current_user.da_password_encrypted),
25-
'theme_preference': current_user.get_theme_preference()
25+
'theme_preference': current_user.theme_preference or 'light'
2626
})
2727
except Exception as e:
2828
print(f"Error in GET da-config: {e}")
@@ -137,21 +137,14 @@ def update_theme():
137137
if theme not in ['light', 'dark']:
138138
return jsonify({'error': 'Invalid theme value'}), 400
139139

140-
# Use safe setter method
141-
if current_user.set_theme_preference(theme):
142-
db.session.commit()
143-
return jsonify({
144-
'success': True,
145-
'message': f'Theme updated to {theme}',
146-
'theme': theme
147-
})
148-
else:
149-
# Column doesn't exist yet - still return success for frontend
150-
return jsonify({
151-
'success': True,
152-
'message': f'Theme set to {theme} (database not updated - run migration)',
153-
'theme': theme
154-
})
140+
current_user.theme_preference = theme
141+
db.session.commit()
142+
143+
return jsonify({
144+
'success': True,
145+
'message': f'Theme updated to {theme}',
146+
'theme': theme
147+
})
155148

156149
except Exception as e:
157150
print(f"Error updating theme: {str(e)}")

app/templates/base.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
88
<link rel="icon" type="image/x-icon" href="{{ url_for('static', filename='assets/img/ico_main_25.ico') }}">
99
</head>
10-
<body data-theme="{{ current_user.get_theme_preference() if current_user.is_authenticated else 'light' }}">
10+
<body data-theme="{{ current_user.theme_preference if current_user.is_authenticated else 'light' }}">
1111
<nav>
1212
<div class="nav-brand">
1313
<a href="{{ url_for('dashboard') }}">DirectAdmin 📧 Forwarder</a>

static/style.css

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1033,6 +1033,70 @@ input[type="text"]#totp_code {
10331033
text-decoration: underline;
10341034
}
10351035

1036+
/* Theme Toggle Styles */
1037+
.theme-toggle {
1038+
display: flex;
1039+
align-items: center;
1040+
gap: 1rem;
1041+
margin: 1rem 0;
1042+
}
1043+
1044+
.theme-label {
1045+
font-size: 0.9rem;
1046+
color: var(--text-color);
1047+
}
1048+
1049+
.theme-switch {
1050+
position: relative;
1051+
display: inline-block;
1052+
width: 60px;
1053+
height: 34px;
1054+
}
1055+
1056+
.theme-switch input {
1057+
opacity: 0;
1058+
width: 0;
1059+
height: 0;
1060+
}
1061+
1062+
.theme-slider {
1063+
position: absolute;
1064+
cursor: pointer;
1065+
top: 0;
1066+
left: 0;
1067+
right: 0;
1068+
bottom: 0;
1069+
background-color: var(--text-muted);
1070+
transition: .4s;
1071+
border-radius: 34px;
1072+
}
1073+
1074+
.theme-slider:before {
1075+
position: absolute;
1076+
content: "";
1077+
height: 26px;
1078+
width: 26px;
1079+
left: 4px;
1080+
bottom: 4px;
1081+
background-color: var(--surface-color);
1082+
transition: .4s;
1083+
border-radius: 50%;
1084+
}
1085+
1086+
input:checked + .theme-slider {
1087+
background-color: var(--primary-color);
1088+
}
1089+
1090+
input:checked + .theme-slider:before {
1091+
transform: translateX(26px);
1092+
}
1093+
1094+
/* Fix: Ensure Change Password button is below text in security-item */
1095+
.security-item a.btn-primary {
1096+
display: inline-block;
1097+
margin-top: 0.5rem;
1098+
}
1099+
10361100
/* Theme Toggle Switch */
10371101
.theme-toggle {
10381102
display: flex;

0 commit comments

Comments
 (0)