From a8271c418902a220f161c0c0d2d43a660de60913 Mon Sep 17 00:00:00 2001 From: Peter Pfeufer Date: Wed, 14 Aug 2024 12:58:01 +0200 Subject: [PATCH] [CHANGE] Remove custom CSS file when it will be empty --- allianceauth/custom_css/models.py | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/allianceauth/custom_css/models.py b/allianceauth/custom_css/models.py index 27c22f67..d3c3cebd 100644 --- a/allianceauth/custom_css/models.py +++ b/allianceauth/custom_css/models.py @@ -3,6 +3,7 @@ Models for the custom_css app """ import hashlib +import os import re import sys @@ -61,11 +62,19 @@ class CustomCSS(SingletonModel): self.pk = 1 - custom_css_file = open( - f"{settings.STATIC_ROOT}allianceauth/custom-styles.css", "w+" - ) - custom_css_file.write(self.compress_css()) - custom_css_file.close() + if len(self.css.replace(" ", "")) > 0: + # Write the custom CSS to a file + custom_css_file = open( + f"{settings.STATIC_ROOT}allianceauth/custom-styles.css", "w+" + ) + custom_css_file.write(self.compress_css()) + custom_css_file.close() + else: + # Remove the custom CSS file + try: + os.remove(f"{settings.STATIC_ROOT}allianceauth/custom-styles.css") + except FileNotFoundError: + pass super().save(*args, **kwargs)