Skip to content

Commit

Permalink
Add ability to use custom theme
Browse files Browse the repository at this point in the history
  • Loading branch information
kizniche committed Sep 26, 2024
1 parent d031f16 commit c1c01b9
Show file tree
Hide file tree
Showing 10 changed files with 123 additions and 25 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ This release changes the install directory from ~/Mycodo to /opt/Mycodo. This ne
- Add options for changing title and brand text
- Add ability to edit Input and Input Measurement unique_id
- Add "edge_input" Input option for use in Edge Trigger Functions
- Add ability to use custom theme

### Miscellaneous

Expand Down
31 changes: 31 additions & 0 deletions alembic_db/alembic/versions/435f35958689_update_theme_entry.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
"""update theme entry
Revision ID: 435f35958689
Revises: 9bdb60d2a2cd
Create Date: 2024-09-26 17:18:03.518493
"""
import sys
import os

sys.path.append(os.path.abspath(os.path.join(__file__, "../../../..")))

from alembic_db.alembic_post_utils import write_revision_post_alembic

from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = '435f35958689'
down_revision = '9bdb60d2a2cd'
branch_labels = None
depends_on = None


def upgrade():
write_revision_post_alembic(revision)


def downgrade():
pass
13 changes: 13 additions & 0 deletions alembic_db/alembic_post.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,19 @@ def upgrade_revision(error, revision):
# elif revision == 'REPLACE_WITH_ALEMBIC_REVISION_ID':
# pass # Code goes here

elif revision == '435f35958689':
from mycodo.databases.models import User

try:
with session_scope(MYCODO_DB_PATH) as session:
# Update output widget custom option format for output selection
users = session.query(User).all()
for each_user in users:
each_user.theme = f"/static/css/bootstrap-4-themes/{each_user.theme}.css"
session.commit()
except:
print(f"Exception processing 435f35958689: {traceback.format_exc()}")

elif revision == 'b354722c9b8b':
import json
import os
Expand Down
2 changes: 2 additions & 0 deletions docker/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ services:
- mycodo_custom_user_scripts:/home/mycodo/mycodo/user_scripts
- mycodo_custom_user_css:/home/mycodo/mycodo/mycodo_flask/static/css/user_css
- mycodo_custom_user_js:/home/mycodo/mycodo/mycodo_flask/static/js/user_js
- mycodo_custom_user_fonts:/home/mycodo/mycodo/mycodo_flask/static/fonts/user_fonts
- mycodo_log:/var/log/mycodo
- mycodo_ssl_certs:/home/mycodo/mycodo/mycodo_flask/ssl_certs
- mycodo_influxdb:/var/lib/influxdb
Expand Down Expand Up @@ -123,6 +124,7 @@ volumes:
mycodo_custom_user_scripts:
mycodo_custom_user_css:
mycodo_custom_user_js:
mycodo_custom_user_fonts:
mycodo_databases:
mycodo_ssl_certs:
mycodo_log:
Expand Down
71 changes: 49 additions & 22 deletions mycodo/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# config.py - Global Mycodo settings
#
import binascii
import logging
import sys
from datetime import timedelta

Expand All @@ -13,8 +14,10 @@
sys.path.append(os.path.abspath(os.path.dirname(__file__)))
from config_translations import TRANSLATIONS as T

logger = logging.getLogger("mycodo.config")

MYCODO_VERSION = '8.15.13'
ALEMBIC_VERSION = '9bdb60d2a2cd'
ALEMBIC_VERSION = '435f35958689'

# FORCE UPGRADE MASTER
# Set True to enable upgrading to the master branch of the Mycodo repository.
Expand Down Expand Up @@ -59,6 +62,10 @@
PATH_TEMPLATE_LAYOUT = os.path.join(PATH_TEMPLATE, 'layout.html')
PATH_TEMPLATE_LAYOUT_DEFAULT = os.path.join(PATH_TEMPLATE, 'layout_default.html')
PATH_TEMPLATE_USER = os.path.join(PATH_TEMPLATE, 'user_templates')
PATH_CSS = os.path.join(INSTALL_DIRECTORY, 'mycodo/mycodo_flask/static/css')
PATH_CSS_USER = os.path.join(PATH_CSS, 'user_css')
PATH_JS_USER = os.path.join(INSTALL_DIRECTORY, 'mycodo/mycodo_flask/static/js/user_js')
PATH_FONTS_USER = os.path.join(INSTALL_DIRECTORY, 'mycodo/mycodo_flask/static/fonts/user_fonts')
PATH_USER_SCRIPTS = os.path.join(INSTALL_DIRECTORY, 'mycodo/user_scripts')
PATH_PYTHON_CODE_USER = os.path.join(INSTALL_DIRECTORY, 'mycodo/user_python_code')
PATH_MEASUREMENTS_BACKUP = os.path.join(INSTALL_DIRECTORY, 'mycodo/backup_measurements')
Expand Down Expand Up @@ -482,30 +489,50 @@

# Web UI themes
THEMES = [
('cerulean', 'Cerulean'),
('cosmo', 'Cosmo'),
('cyborg', 'Cyborg'),
('darkly', 'Darkly'),
('flatly', 'Flatly'),
('journal', 'Journal'),
('literia', 'Literia'),
('lumen', 'Lumen'),
('lux', 'Lux'),
('materia', 'Materia'),
('minty', 'Minty'),
('pulse', 'Pulse'),
('sandstone', 'Sandstone'),
('simplex', 'Simplex'),
('slate', 'Slate'),
('solar', 'Solar'),
('spacelab', 'Spacelab'),
('superhero', 'Superhero'),
('united', 'United'),
('yeti', 'Yeti')
('/static/css/bootstrap-4-themes/cerulean.css', 'Cerulean'),
('/static/css/bootstrap-4-themes/cosmo.css', 'Cosmo'),
('/static/css/bootstrap-4-themes/cyborg.css', 'Cyborg'),
('/static/css/bootstrap-4-themes/darkly.css', 'Darkly'),
('/static/css/bootstrap-4-themes/flatly.css', 'Flatly'),
('/static/css/bootstrap-4-themes/journal.css', 'Journal'),
('/static/css/bootstrap-4-themes/literia.css', 'Literia'),
('/static/css/bootstrap-4-themes/lumen.css', 'Lumen'),
('/static/css/bootstrap-4-themes/lux.css', 'Lux'),
('/static/css/bootstrap-4-themes/materia.css', 'Materia'),
('/static/css/bootstrap-4-themes/minty.css', 'Minty'),
('/static/css/bootstrap-4-themes/pulse.css', 'Pulse'),
('/static/css/bootstrap-4-themes/sandstone.css', 'Sandstone'),
('/static/css/bootstrap-4-themes/simplex.css', 'Simplex'),
('/static/css/bootstrap-4-themes/slate.css', 'Slate'),
('/static/css/bootstrap-4-themes/solar.css', 'Solar'),
('/static/css/bootstrap-4-themes/spacelab.css', 'Spacelab'),
('/static/css/bootstrap-4-themes/superhero.css', 'Superhero'),
('/static/css/bootstrap-4-themes/united.css', 'United'),
('/static/css/bootstrap-4-themes/yeti.css', 'Yeti')
]

THEMES_DARK = ['cyborg', 'darkly', 'slate', 'solar', 'superhero']
THEMES_DARK = [
'/static/css/bootstrap-4-themes/cyborg.css',
'/static/css/bootstrap-4-themes/darkly.css',
'/static/css/bootstrap-4-themes/slate.css',
'/static/css/bootstrap-4-themes/solar.css',
'/static/css/bootstrap-4-themes/superhero.css'
]

try:
user_themes = os.path.join(PATH_CSS_USER, "bootstrap-4-themes")
for file in os.listdir(os.fsencode(user_themes)):
filename = os.fsdecode(file)
if filename.endswith(".css"):
theme_location = f"/static/css/user_css/bootstrap-4-themes/{filename}"
logger.info(f"Adding custom theme: {theme_location}")
theme_display = filename.split('.')[0].replace('-', ' ').replace('_', ' ').title()
THEMES.append((theme_location, theme_display))

if 'dark' in filename.lower():
THEMES_DARK.append(theme_location)
except:
pass

class ProdConfig(object):
"""Production Configuration."""
Expand Down
2 changes: 1 addition & 1 deletion mycodo/mycodo_flask/templates/layout-remote.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<script src="/static/js/popper.min.js"></script>

{% if current_user.theme != '' %}
<link href="/static/css/bootstrap-4-themes/{{current_user.theme}}.css" rel="stylesheet">
<link href="{{current_user.theme}}" rel="stylesheet">
{% endif %}

{% block head %}{% endblock %}
Expand Down
2 changes: 1 addition & 1 deletion mycodo/mycodo_flask/templates/layout_default.html
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
{% endif %}

{% if current_user.theme != '' %}
<link href="/static/css/bootstrap-4-themes/{{current_user.theme}}.css" rel="stylesheet">
<link href="{{current_user.theme}}" rel="stylesheet">
{% endif %}

<link href="/static/css/custom.css" rel="stylesheet">
Expand Down
2 changes: 2 additions & 0 deletions mycodo/scripts/upgrade_commands.sh
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ case "${1:-''}" in
mkdir -p "${MYCODO_PATH}"/mycodo/mycodo_flask/ssl_certs
mkdir -p "${MYCODO_PATH}"/mycodo/mycodo_flask/static/js/user_js
mkdir -p "${MYCODO_PATH}"/mycodo/mycodo_flask/static/css/user_css
mkdir -p "${MYCODO_PATH}"/mycodo/mycodo_flask/static/fonts/user_fonts

if [[ ! -e /var/log/mycodo/mycodo.log ]]; then
touch /var/log/mycodo/mycodo.log
Expand Down Expand Up @@ -735,6 +736,7 @@ case "${1:-''}" in
mkdir -p "${MYCODO_PATH}"/mycodo/scripts
mkdir -p "${MYCODO_PATH}"/mycodo/mycodo_flask/static/js/user_js
mkdir -p "${MYCODO_PATH}"/mycodo/mycodo_flask/static/css/user_css
mkdir -p "${MYCODO_PATH}"/mycodo/mycodo_flask/static/fonts/user_fonts

if [[ ! -e /var/log/mycodo/mycodo.log ]]; then
touch /var/log/mycodo/mycodo.log
Expand Down
18 changes: 18 additions & 0 deletions mycodo/scripts/upgrade_install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,24 @@ runSelfUpgrade() {
printf "Done.\n"
fi

if [ -d "${CURRENT_MYCODO_DIRECTORY}"/mycodo/mycodo_flask/static/fonts/user_fonts ] ; then
printf "Copying mycodo/mycodo_flask/static/fonts/user_fonts..."
if ! cp -r "${CURRENT_MYCODO_DIRECTORY}"/mycodo/mycodo_flask/static/fonts/user_fonts "${THIS_MYCODO_DIRECTORY}"/mycodo/mycodo_flask/static/fonts/ ; then
printf "Failed: Error while trying to copy mycodo/mycodo_flask/static/fonts/user_fonts"
error_found
fi
printf "Done.\n"
fi

if [ -d "${CURRENT_MYCODO_DIRECTORY}"/mycodo/user_scripts ] ; then
printf "Copying mycodo/user_scripts..."
if ! cp -r "${CURRENT_MYCODO_DIRECTORY}"/mycodo/user_scripts "${THIS_MYCODO_DIRECTORY}"/mycodo/user_scripts/ ; then
printf "Failed: Error while trying to copy mycodo/user_scripts"
error_found
fi
printf "Done.\n"
fi

if [ -d "${CURRENT_MYCODO_DIRECTORY}"/output_usage_reports ] ; then
printf "Moving output_usage_reports directory..."
if ! mv "${CURRENT_MYCODO_DIRECTORY}"/output_usage_reports "${THIS_MYCODO_DIRECTORY}" ; then
Expand Down
6 changes: 5 additions & 1 deletion mycodo/utils/widget_generate_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ def assure_path_exists(path):

def generate_widget_html():
"""Generate all HTML files for all widgets."""
error = []
dict_widgets = parse_widget_information()
assure_path_exists(PATH_TEMPLATE_USER)

Expand Down Expand Up @@ -111,8 +112,11 @@ def generate_widget_html():
fw.write(html_js_ready_end)
fw.close()
set_user_grp(path_js_ready_end, 'mycodo', 'mycodo')
except Exception:
except Exception as err:
logger.exception(f"Generating widget HTML for widget: {widget_name}")
error.append(f"Exception generating widget HTML for '{widget_name}' (see Web Log for full traceback): {err}")

return error


if __name__ == "__main__":
Expand Down

0 comments on commit c1c01b9

Please sign in to comment.