Skip to content

Commit b60df96

Browse files
committed
WIP
1 parent fe42380 commit b60df96

File tree

2 files changed

+178
-0
lines changed

2 files changed

+178
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from django.db import migrations
2+
from customstyling.utils import find_and_handle_custom_stylesheets
3+
4+
5+
def forward_migration(apps, schema_editor):
6+
changes_to_make = [
7+
('#content aside', '#content .side-info'),
8+
('#content .side-info h3', '#content .side-info h2'),
9+
]
10+
find_and_handle_custom_stylesheets(apps, schema_editor, changes_to_make, reverse=False)
11+
12+
13+
def reverse_migration(apps, schema_editor):
14+
changes_to_make = [
15+
('#content aside', '#content .side-info'),
16+
('#content aside h3', '#content aside h2'),
17+
]
18+
find_and_handle_custom_stylesheets(apps, schema_editor, changes_to_make, reverse=True)
19+
20+
21+
class Migration(migrations.Migration):
22+
23+
dependencies = [
24+
('customstyling', '0002_upgrade_materialize'),
25+
]
26+
27+
operations = [
28+
migrations.RunPython(
29+
forward_migration,
30+
reverse_code=reverse_migration,
31+
)
32+
]

utils.py

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
import os
2+
import sys
3+
import difflib
4+
5+
from django.conf import settings
6+
from customstyling import plugin_settings
7+
8+
from utils.logger import get_logger
9+
logger = get_logger(__name__)
10+
11+
12+
def change_css_selectors(file_paths, changes_to_make, reverse=False):
13+
"""
14+
Modifies custom stylesheets to update specific CSS selectors.
15+
16+
Args:
17+
file_paths: List of file paths to process
18+
changes_to_make: List of tuples with (old_selector, new_selector)
19+
reverse: If True, reverses the changes (new_selector -> old_selector)
20+
"""
21+
for file_path in file_paths:
22+
try:
23+
css = []
24+
new_css = []
25+
with open(file_path, 'r', encoding="utf-8") as file_ref:
26+
css = file_ref.readlines()
27+
for line in css:
28+
for change in changes_to_make:
29+
if reverse:
30+
new, old = change
31+
else:
32+
old, new = change
33+
line = line.replace(old, new)
34+
new_css.append(line)
35+
if css != new_css:
36+
backup_file_folder = os.path.join(
37+
os.path.dirname(file_path),
38+
'temp',
39+
)
40+
if not os.path.exists(backup_file_folder):
41+
os.mkdir(backup_file_folder)
42+
backup_file_path = os.path.join(
43+
backup_file_folder,
44+
os.path.basename(file_path),
45+
)
46+
with open(backup_file_path, 'w', encoding="utf-8") as file_ref:
47+
file_ref.writelines(css)
48+
logger.info(f'Original backed up to {backup_file_path}')
49+
50+
with open(file_path, 'w', encoding="utf-8") as file_ref:
51+
file_ref.writelines(new_css)
52+
logger.info(f'Stylesheet modified at {file_path}')
53+
diff = difflib.unified_diff(css, new_css)
54+
sys.stdout.writelines(diff)
55+
else:
56+
pass
57+
# logger.info(f'No changes needed at {file_path}')
58+
59+
except FileNotFoundError:
60+
pass
61+
# logger.info(f'No sheet found at {file_path}')
62+
63+
64+
def get_custom_stylesheet_paths(apps, schema_editor):
65+
"""
66+
Gets all custom stylesheet paths for journals and press.
67+
68+
Returns:
69+
list: List of file paths to custom stylesheets
70+
"""
71+
Journal = apps.get_model('journal', 'Journal')
72+
Press = apps.get_model('press', 'Press')
73+
SettingValue = apps.get_model('core', 'SettingValue')
74+
CrossJournalStylesheet = apps.get_model('customstyling', 'CrossJournalStylesheet')
75+
76+
stylesheet_paths = []
77+
78+
# Get journal stylesheets
79+
for journal in Journal.objects.all():
80+
journal_id = journal.id
81+
try:
82+
theme = SettingValue.objects.get(
83+
setting__group__name='general',
84+
setting__name='journal_theme',
85+
journal__id=journal_id,
86+
)
87+
except SettingValue.DoesNotExist:
88+
continue
89+
if theme.value == 'material':
90+
stylesheet_paths.extend([
91+
os.path.join(
92+
settings.BASE_DIR,
93+
'static',
94+
'material',
95+
'css',
96+
f'journal{journal_id}_override.css',
97+
),
98+
os.path.join(
99+
plugin_settings.BASE_CSS_PATH,
100+
str(journal_id),
101+
'custom.css',
102+
)
103+
])
104+
105+
for sheet in CrossJournalStylesheet.objects.filter(journals=journal):
106+
stylesheet_paths.append(
107+
os.path.join(
108+
plugin_settings.BASE_CSS_PATH,
109+
'press',
110+
sheet.stylesheet_name,
111+
)
112+
)
113+
114+
# Get press stylesheets
115+
press = Press.objects.first()
116+
if press and press.theme == 'material':
117+
stylesheet_paths.extend([
118+
os.path.join(
119+
settings.BASE_DIR,
120+
'static',
121+
'material',
122+
'css',
123+
'press_override.css',
124+
),
125+
os.path.join(
126+
plugin_settings.BASE_CSS_PATH,
127+
'press',
128+
'custom.css',
129+
)
130+
])
131+
132+
return stylesheet_paths
133+
134+
135+
def find_and_handle_custom_stylesheets(apps, schema_editor, changes_to_make, reverse=False):
136+
"""
137+
Main function to find and process custom stylesheets with specified changes.
138+
139+
Args:
140+
apps: Django apps registry
141+
schema_editor: Django schema editor
142+
changes_to_make: List of tuples with (old_selector, new_selector)
143+
reverse: If True, reverses the changes
144+
"""
145+
stylesheet_paths = get_custom_stylesheet_paths(apps, schema_editor)
146+
change_css_selectors(stylesheet_paths, changes_to_make, reverse=reverse)

0 commit comments

Comments
 (0)