-
Notifications
You must be signed in to change notification settings - Fork 67
/
organize_templates.py
182 lines (161 loc) · 6.16 KB
/
organize_templates.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import os
from pathlib import Path
from collections import defaultdict
import shutil
from datetime import datetime
def get_template_mappings():
return {
# App-specific templates
'security': 'capabilities/security',
'talent': 'capabilities/talent',
'portal': 'portal',
# Product Management templates
'product_management': {
'patterns': [
'product_detail.html',
'product_tree/*',
'products/*',
'product_area_detail/*',
'product_area_detail_helper/*',
'unauthenticated_tree/*'
],
'target': 'capabilities/product_management'
},
# Shared components and layouts
'common': {
'patterns': [
'base.html',
'footer.html',
'header.html',
'navbar.html',
'404.html',
'about.html',
'home.html',
'privacy_policy.html',
'terms_of_use.html',
'toast.html',
'logo.html',
'main.html',
'base_html.html',
'header_full.html',
'header_without_discord.html',
'video_player_modal.html',
'complete_profile_pop_up.html',
'selectable_dropdown.html'
],
'target': 'common'
},
# Forms
'forms': {
'patterns': [
'forms/*',
'attachments.html'
],
'target': 'common/forms'
},
# Components that should be moved to a shared components app
'components': {
'patterns': [
'components/*',
'partials/*',
'macros/*',
'helper/*',
'filters/*',
'buttons/*',
'bounty/*',
'bounties/*',
'challenges/*',
'agreements/*',
'work/*',
'users/*',
'sub_header/*',
'header/*'
],
'target': 'common/components'
}
}
def organize_templates():
source_dir = Path('apps/templates')
backup_dir = Path(f'template_backup_{datetime.now().strftime("%Y%m%d_%H%M%S")}')
print("Starting template organization...")
# Create backup
print("\nCreating backup...")
shutil.copytree(source_dir, backup_dir / 'templates')
mappings = get_template_mappings()
moved_files = defaultdict(list)
skipped_files = []
# Process each file
for template_path in source_dir.rglob('*.html'):
relative_path = template_path.relative_to(source_dir)
first_dir = str(relative_path).split('/')[0]
matched = False
# Check direct app mappings
if first_dir in ['security', 'talent', 'portal']:
target_app = mappings[first_dir]
target_path = Path(f'apps/{target_app}/templates/{relative_path}')
matched = True
else:
# Check pattern-based mappings
for app, config in mappings.items():
if isinstance(config, dict) and 'patterns' in config:
for pattern in config['patterns']:
if pattern.endswith('/*'):
if str(relative_path).startswith(pattern[:-2]):
target_app = config['target']
target_path = Path(f'apps/{target_app}/templates/{relative_path}')
matched = True
break
elif str(relative_path) == pattern:
target_app = config['target']
target_path = Path(f'apps/{target_app}/templates/{relative_path}')
matched = True
break
if matched:
break
if matched:
print(f"Moving {relative_path} to {target_path}")
target_path.parent.mkdir(parents=True, exist_ok=True)
shutil.copy2(template_path, target_path)
moved_files[target_app].append(str(relative_path))
# Delete the original file after successful copy
template_path.unlink()
else:
print(f"Skipping {relative_path} - no mapping found")
skipped_files.append(str(relative_path))
# Clean up empty directories
for path in sorted([p for p in source_dir.rglob('*') if p.is_dir()], reverse=True):
if path.exists() and not any(path.iterdir()):
path.rmdir()
# Write report
with open(backup_dir / 'migration_report.txt', 'w') as f:
f.write("Template Migration Report\n")
f.write("======================\n\n")
for app, files in moved_files.items():
f.write(f"\n{app}:\n")
f.write(f"Moved {len(files)} files:\n")
for file in sorted(files):
f.write(f" - {file}\n")
f.write("\nSkipped files:\n")
for file in sorted(skipped_files):
f.write(f" - {file}\n")
print("\nMigration complete!")
print(f"Backup created at: {backup_dir}")
print("See migration_report.txt for details")
if skipped_files:
print("\nSkipped files that can be deleted:")
for file in sorted(skipped_files):
print(f" - {file}")
if input("\nDelete skipped files? (y/n): ").lower() == 'y':
for file in skipped_files:
file_path = source_dir / file
if file_path.exists():
file_path.unlink()
print(f"Deleted: {file}")
if __name__ == '__main__':
print("This script will organize templates into their respective apps.")
print("A backup will be created before any changes are made.")
confirm = input("\nProceed? (y/n): ")
if confirm.lower() == 'y':
organize_templates()
else:
print("Operation cancelled.")