Permalink
Browse files

Theme Editor: Added customize more templates functionality to the the…

…me editor. Works with S3 or locally. Theme is now read from the proper place.
  • Loading branch information...
1 parent 6f9aceb commit c27e0a287f91e6186fb356bc54e4595fd7cf535f @jmoswalt jmoswalt committed Nov 1, 2012
@@ -36,9 +36,23 @@ class FileForm(forms.Form):
)
rf_path = forms.CharField(widget=forms.HiddenInput())
- def save(self, request, file_relative_path, ROOT_DIR=THEME_ROOT):
+ def save(self, request, file_relative_path, ROOT_DIR=THEME_ROOT, ORIG_ROOT_DIR=THEME_ROOT):
content = self.cleaned_data["content"]
file_path = (os.path.join(ROOT_DIR, file_relative_path)).replace("\\", "/")
+
+ if settings.USE_S3_THEME:
+ file_path = (os.path.join(ORIG_ROOT_DIR, file_relative_path)).replace("\\", "/")
+
+ # write the theme file locally in case it was wiped by a restart
+ if settings.USE_S3_THEME and not os.path.isfile(file_path):
+ file_dir = os.path.dirname(file_path)
+ if not os.path.isdir(file_dir):
+ # if directory does not exist, create it
+ os.makedirs(file_dir)
+ new_file = open(file_path, 'w')
+ new_file.write('')
+ new_file.close()
+
if os.path.isfile(file_path) and content != "":
archive_file(request, file_relative_path, ROOT_DIR=ROOT_DIR)
@@ -144,11 +144,9 @@ <h1 id="to-editor" class="file-editor-header">{% trans "Theme Editor" %} : <a hr
</div>
<div class="well well-small">
<h4>Customize more templates:</h4>
- This functionality is temporarily unavailable.
- {% comment %}
Copy down one of Tendenci's built-in app templates and customize it.
<a href="{% url theme_editor.app_list %}" class="btn btn-mini">{% trans "Customize more templates" %}</a>
- {% endcomment %}
+
</div>
</div>
@@ -1,6 +1,7 @@
import os
import shutil
import sys
+import boto
import urllib
from django.conf import settings
@@ -56,7 +57,8 @@ def copy(filename, path_to_file, full_filename, TO_ROOT=THEME_ROOT):
public = False
else:
public = True
- save_file_to_s3(filecopy, public=public)
+ dest_path = "/themes/%s" % filecopy
+ save_file_to_s3(full_filename, dest_path=dest_path, public=public)
def qstr_is_dir(query_string, ROOT_DIR=THEME_ROOT):
@@ -71,6 +73,11 @@ def qstr_is_file(query_string, ROOT_DIR=THEME_ROOT):
"""
Check to see if the query string is a directory or not
"""
+ if settings.USE_S3_THEME:
+ content = get_file_content(query_string)
+ if content:
+ return True
+
current_file = os.path.join(ROOT_DIR, query_string)
return os.path.isfile(current_file)
@@ -138,6 +145,64 @@ def get_all_files_list(ROOT_DIR=THEME_ROOT):
for parent in files_folders:
subdir['contents'].append({'folder_path': path})
+ if settings.USE_S3_THEME:
+ s3_files_folders = {'contents': []}
+ theme_folder = "%s/%s" % (settings.THEME_S3_PATH, get_theme())
+ conn = boto.connect_s3(settings.AWS_ACCESS_KEY_ID,
+ settings.AWS_SECRET_ACCESS_KEY)
+ bucket = conn.get_bucket(settings.AWS_STORAGE_BUCKET_NAME)
+
+ for item in bucket.list(prefix=theme_folder):
+
+ editable = False
+ if os.path.splitext(item.name)[1] in ALLOWED_EXTENSIONS:
+ editable = True
+
+ file_path = item.name.replace(theme_folder, '').lstrip('/')
+ path_split = file_path.split('/')
+ splits = len(path_split)
+
+ if splits == 1:
+ s3_files_folders['contents'].append({
+ 'name': path_split[0],
+ 'path': file_path,
+ 'editable': editable})
+ elif splits == 2:
+ if not path_split[0] in s3_files_folders:
+ s3_files_folders[path_split[0]] = {'contents': []}
+
+ s3_files_folders[path_split[0]]['contents'].append({
+ 'name': path_split[1],
+ 'path': file_path,
+ 'editable': editable})
+ elif splits == 3:
+ if not path_split[0] in s3_files_folders:
+ s3_files_folders[path_split[0]] = {'contents': []}
+
+ if not path_split[1] in s3_files_folders[path_split[0]]:
+ s3_files_folders[path_split[0]][path_split[1]] = {'contents': []}
+
+ s3_files_folders[path_split[0]][path_split[1]]['contents'].append({
+ 'name': path_split[2],
+ 'path': file_path,
+ 'editable': editable})
+ elif splits == 4:
+ if not path_split[0] in s3_files_folders:
+ s3_files_folders[path_split[0]] = {'contents': []}
+
+ if not path_split[1] in s3_files_folders[path_split[0]]:
+ s3_files_folders[path_split[0]][path_split[1]] = {'contents': []}
+
+ if not path_split[2] in s3_files_folders[path_split[0]][path_split[1]]:
+ s3_files_folders[path_split[0]][path_split[1]][path_split[2]] = {'contents': []}
+
+ s3_files_folders[path_split[0]][path_split[1]][path_split[2]]['contents'].append({
+ 'name': path_split[3],
+ 'path': file_path,
+ 'editable': editable})
+
+ return {get_theme(): s3_files_folders}
+
return files_folders
@@ -29,10 +29,11 @@ def edit_file(request, form_class=FileForm, template_name="theme_editor/index.ht
raise Http403
selected_theme = request.GET.get("theme_edit", get_theme())
- if settings.USE_S3_STORAGE:
- theme_root = os.path.join(settings.ORIGINAL_THEMES_DIR, selected_theme)
+ original_theme_root = os.path.join(settings.ORIGINAL_THEMES_DIR, selected_theme)
+ if settings.USE_S3_THEME:
+ theme_root = os.path.join(settings.THEME_S3_PATH, selected_theme)
else:
- theme_root = os.path.join(settings.THEMES_DIR, selected_theme)
+ theme_root = os.path.join(settings.ORIGINAL_THEMES_DIR, selected_theme)
# get the default file and clean up any input
default_file = request.GET.get("file", DEFAULT_FILE)
@@ -100,19 +101,11 @@ def edit_file(request, form_class=FileForm, template_name="theme_editor/index.ht
if request.method == "POST":
file_form = form_class(request.POST)
if file_form.is_valid():
- if file_form.save(request, default_file, ROOT_DIR=theme_root):
+ if file_form.save(request, default_file, ROOT_DIR=theme_root, ORIG_ROOT_DIR=original_theme_root):
message = "Successfully updated %s" % current_file
message_status = messages.SUCCESS
- log_defaults = {
- 'event_id': 1110000,
- 'event_data': '%s updated by %s' % (current_file, request.user),
- 'description': 'theme file edited',
- 'user': request.user,
- 'request': request,
- 'source': 'theme_editor',
- }
- EventLog.objects.log(**log_defaults)
+ EventLog.objects.log()
else:
message = "Cannot update"
message_status = messages.WARNING
@@ -244,7 +237,7 @@ def copy_to_theme(request, app=None):
'source': 'theme_editor',
}
EventLog.objects.log(**log_defaults)
- return redirect('theme_editor.original_templates')
+ return redirect('theme_editor.editor')
def delete_file(request):
@@ -46,7 +46,7 @@ def read_theme_file_from_s3(file_path):
return content
-def save_file_to_s3(file_path, dirpath=None, public=False):
+def save_file_to_s3(file_path, dirpath=None, public=False, dest_path=None):
"""
Save the file to S3.
"""
@@ -61,8 +61,10 @@ def save_file_to_s3(file_path, dirpath=None, public=False):
if not dirpath:
dirpath = settings.ORIGINAL_THEMES_DIR
- key = '%s%s' % (settings.AWS_LOCATION,
- file_path.replace(os.path.dirname(dirpath), ''))
+ if not dest_path:
+ dest_path = file_path.replace(os.path.dirname(dirpath), '')
+
+ key = '%s%s' % (settings.AWS_LOCATION, dest_path)
k.key = key
if os.path.splitext(filename)[1] == '.less':
content_type = 'text/css'

0 comments on commit c27e0a2

Please sign in to comment.