示例#1
0
文件: utils.py 项目: chendong0444/ams
def handle_uploaded_file(file_path, file_dir):
    file_name = os.path.basename(file_path)
    filecopy = os.path.join(THEME_ROOT, file_dir, file_name)
    dest_path = os.path.join(settings.PROJECT_ROOT, "themes", filecopy)

    shutil.move(file_path, dest_path)

    # copy to s3
    if settings.USE_S3_THEME:
        if os.path.splitext(f.name)[1] == '.html':
            public = False
        else:
            public = True
        dest_path = "/themes/%s" % filecopy
        save_file_to_s3(file_path, dest_path=dest_path, public=public)

        cache_key = ".".join([
            settings.SITE_CACHE_KEY, 'theme',
            file_path[(file_path.find(THEME_ROOT)):]
        ])
        cache.delete(cache_key)

        if hasattr(settings,
                   'REMOTE_DEPLOY_URL') and settings.REMOTE_DEPLOY_URL:
            urllib.urlopen(settings.REMOTE_DEPLOY_URL)
示例#2
0
    def save(self, request, file_relative_path, ROOT_DIR=THEME_ROOT):
        content = self.cleaned_data["content"]
        file_path = (os.path.join(ROOT_DIR,
                                  file_relative_path)).replace("\\", "/")
        if os.path.isfile(file_path) and content <> "":
            archive_file(request, file_relative_path, ROOT_DIR=ROOT_DIR)
            f = codecs.open(file_path, 'w', 'utf-8', 'replace')
            file = File(f)
            file.write(content)
            file.close()

            # copy to s3 storage
            if os.path.splitext(file_path)[1] == '.html':
                public = False
            else:
                public = True
            save_file_to_s3(file_path, public=public)

            if hasattr(settings,
                       'REMOTE_DEPLOY_URL') and settings.REMOTE_DEPLOY_URL:
                urllib.urlopen(settings.REMOTE_DEPLOY_URL)

            return True
        else:
            return False
示例#3
0
文件: utils.py 项目: zaid100/tendenci
def copy_file_to_theme(source_full_path, to_theme, path_to_file, filename):
    """Copies a file and all associated directories into theme
    """
    root_dir = get_theme_root(to_theme)
    try:
        os.makedirs(os.path.join(root_dir, path_to_file))
    except OSError:
        pass
    dest_full_path = os.path.join(root_dir, path_to_file, filename)
    shutil.copy(source_full_path, dest_full_path)

    # copy to s3
    if settings.USE_S3_THEME:
        if os.path.splitext(filename)[1] == '.html':
            public = False
        else:
            public = True
        dest_path = os.path.join(to_theme, path_to_file, filename)
        dest_full_path = os.path.join(settings.THEME_S3_PATH, dest_path)
        save_file_to_s3(source_full_path,
                        dest_path=dest_full_path,
                        public=public)

        cache_key = ".".join([settings.SITE_CACHE_KEY, 'theme', dest_path])
        cache.delete(cache_key)

        if hasattr(settings,
                   'REMOTE_DEPLOY_URL') and settings.REMOTE_DEPLOY_URL:
            urlopen(settings.REMOTE_DEPLOY_URL)
示例#4
0
def handle_uploaded_file(f, file_dir):
    filecopy = os.path.join(THEME_ROOT, file_dir, f.name)
    file_path = os.path.join(settings.PROJECT_ROOT, "themes", filecopy)

    destination = open(file_path, 'wb+')
    for chunk in f.chunks():
        destination.write(chunk)
    destination.close()

    # copy to s3
    if settings.USE_S3_THEME:
        if os.path.splitext(f.name)[1] == '.html':
            public = False
        else:
            public = True
        dest_path = "/themes/%s" % filecopy
        save_file_to_s3(file_path, dest_path=dest_path, public=public)

        cache_key = ".".join([
            settings.SITE_CACHE_KEY, 'theme',
            file_path[(file_path.find(THEME_ROOT)):]
        ])
        cache.delete(cache_key)

        if hasattr(settings,
                   'REMOTE_DEPLOY_URL') and settings.REMOTE_DEPLOY_URL:
            urllib.urlopen(settings.REMOTE_DEPLOY_URL)
示例#5
0
文件: forms.py 项目: kgayle/tendenci
    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=ORIG_ROOT_DIR)

            # Save the file locally no matter the theme location.
            # The save to S3 reads from the local file, so we need to save it first.
            f = codecs.open(file_path, 'w', 'utf-8', 'replace')
            file = File(f)
            file.write(content)
            file.close()

            if settings.USE_S3_THEME:
                # copy to s3 storage
                if os.path.splitext(file_path)[1] == '.html':
                    public = False
                else:
                    public = True
                save_file_to_s3(file_path, public=public)

                cache_key = ".".join([
                    settings.SITE_CACHE_KEY, 'theme',
                    "%s/%s" % (get_theme(), file_relative_path)
                ])
                cache.delete(cache_key)

                if hasattr(settings,
                           'REMOTE_DEPLOY_URL') and settings.REMOTE_DEPLOY_URL:
                    urllib.urlopen(settings.REMOTE_DEPLOY_URL)

            return True
        else:
            return False
示例#6
0
def create_new_template(request, form_class=AddTemplateForm):
    """
    Create a new blank template for a given template name
    """
    form = form_class(request.POST or None)
    ret_dict = {'created': False, 'err': ''}

    if form.is_valid():
        template_name = form.cleaned_data['template_name'].strip()
        template_full_name = 'default-%s.html' % template_name
        existing_templates = [t[0] for t in get_template_list()]
        if not template_full_name in existing_templates:
            # create a new template and assign default content
            use_s3_storage = getattr(settings, 'USE_S3_STORAGE', '')
            if use_s3_storage:
                theme_dir = settings.ORIGINAL_THEMES_DIR
            else:
                theme_dir = settings.THEMES_DIR
            template_dir = os.path.join(theme_dir,
                                get_setting('module', 'theme_editor', 'theme'),
                                'templates')
            template_full_path = os.path.join(template_dir,
                                template_full_name)
            # grab the content from the new-default-template.html
            # first check if there is a customized one on the site
            default_template_name = 'new-default-template.html'
            default_template_path = os.path.join(template_dir,
                                'theme_editor',
                                default_template_name)
            if not os.path.isfile(default_template_path):
                # no customized one found, use the default one
                default_template_path = os.path.join(
                    os.path.abspath(os.path.dirname(__file__)),
                    'templates/theme_editor',
                    default_template_name)
            if os.path.isfile(default_template_path):
                default_content = open(default_template_path).read()
            else:
                default_content = ''
            with open(template_full_path, 'w') as f:
                f.write(default_content)
            if use_s3_storage:
                # django default_storage not set for theme, that's why we cannot use it
                save_file_to_s3(template_full_path)

            ret_dict['created'] = True
            ret_dict['template_name'] = template_full_name
        else:
            ret_dict['err'] = _('Template "%(name)s" already exists' % {'name':template_full_name})


    return HttpResponse(json.dumps(ret_dict))
示例#7
0
文件: views.py 项目: BIGGANI/tendenci
def create_new_template(request, form_class=AddTemplateForm):
    """
    Create a new blank template for a given template name
    """
    form = form_class(request.POST or None)
    ret_dict = {'created': False, 'err': ''}

    if form.is_valid():
        template_name = form.cleaned_data['template_name'].strip()
        template_full_name = 'default-%s.html' % template_name
        existing_templates = [t[0] for t in get_template_list()]
        if not template_full_name in existing_templates:
            # create a new template and assign default content
            use_s3_storage = getattr(settings, 'USE_S3_STORAGE', '')
            if use_s3_storage:
                theme_dir = settings.ORIGINAL_THEMES_DIR
            else:
                theme_dir = settings.THEMES_DIR
            template_dir = os.path.join(theme_dir,
                                get_setting('module', 'theme_editor', 'theme'),
                                'templates')
            template_full_path = os.path.join(template_dir,
                                template_full_name)
            # grab the content from the new-default-template.html
            # first check if there is a customized one on the site
            default_template_name = 'new-default-template.html'
            default_template_path = os.path.join(template_dir,
                                'theme_editor',
                                default_template_name)
            if not os.path.isfile(default_template_path):
                # no customized one found, use the default one
                default_template_path = os.path.join(
                    os.path.abspath(os.path.dirname(__file__)),
                    'templates/theme_editor',
                    default_template_name)
            if os.path.isfile(default_template_path):
                default_content = open(default_template_path).read()
            else:
                default_content = ''
            with open(template_full_path, 'w') as f:
                f.write(default_content)
            if use_s3_storage:
                # django default_storage not set for theme, that's why we cannot use it
                save_file_to_s3(template_full_path)

            ret_dict['created'] = True
            ret_dict['template_name'] = template_full_name
        else:
            ret_dict['err'] = _('Template "%(name)s" already exists' % {'name':template_full_name})


    return HttpResponse(json.dumps(ret_dict))
示例#8
0
def create_new_template(request, form_class=AddTemplateForm):
    """
    Create a new blank template for a given template name
    """
    selected_theme = request.GET.get("theme_edit", get_theme())
    if not is_valid_theme(selected_theme):
        raise Http404(_('Specified theme does not exist'))
    if is_theme_read_only(selected_theme):
        raise Http403

    form = form_class(request.POST or None)
    ret_dict = {'created': False, 'err': ''}

    if form.is_valid():
        template_name = form.cleaned_data['template_name'].strip()
        template_full_name = 'default-%s.html' % template_name
        existing_templates = [t[0] for t in get_template_list()]
        if template_full_name not in existing_templates:
            # create a new template and assign default content
            theme_root = get_theme_root(selected_theme)
            template_dir = os.path.join(theme_root, 'templates')
            template_full_path = os.path.join(template_dir, template_full_name)
            # grab the content from the new-default-template.html
            # first check if there is a customized one on the site
            default_template_name = 'new-default-template.html'
            default_template_path = os.path.join(template_dir, 'theme_editor',
                                                 default_template_name)
            if not os.path.isfile(default_template_path):
                # no customized one found, use the default one
                default_template_path = os.path.join(
                    os.path.abspath(os.path.dirname(__file__)),
                    'templates/theme_editor', default_template_name)
            if os.path.isfile(default_template_path):
                default_content = open(default_template_path).read()
            else:
                default_content = ''
            with open(template_full_path, 'w') as f:
                f.write(default_content)
            if settings.USE_S3_STORAGE:
                s3_path = os.path.join(settings.THEME_S3_PATH, selected_theme,
                                       'templates', template_full_name)
                save_file_to_s3(template_full_path,
                                dest_path=s3_path,
                                public=False)
            ret_dict['created'] = True
            ret_dict['template_name'] = template_full_name
        else:
            ret_dict['err'] = _('Template "%(name)s" already exists' %
                                {'name': template_full_name})

    return HttpResponse(json.dumps(ret_dict))
示例#9
0
def handle_uploaded_file(f, file_dir):
    file_path = os.path.join(file_dir, f.name)
    destination = open(file_path, 'wb+')
    for chunk in f.chunks():
        destination.write(chunk)
    destination.close()
    
    # copy to s3
    if settings.USE_S3_STORAGE:
        if os.path.splitext(f.name)[1] == '.html':
            public = False
        else:
            public = True
        save_file_to_s3(file_path, public=public)
示例#10
0
文件: utils.py 项目: eloyz/tendenci
def handle_uploaded_file(f, file_dir):
    file_path = os.path.join(file_dir, f.name)
    destination = open(file_path, 'wb+')
    for chunk in f.chunks():
        destination.write(chunk)
    destination.close()

    # copy to s3
    if settings.USE_S3_STORAGE:
        if os.path.splitext(f.name)[1] == '.html':
            public = False
        else:
            public = True
        save_file_to_s3(file_path, public=public)
示例#11
0
    def save(self, root_dir, theme, file_relative_path, request):
        content = self.cleaned_data["content"]
        file_path = os.path.join(root_dir, file_relative_path)

        # 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(root_dir, file_relative_path, request)

            # Save the file locally no matter the theme location.
            # The save to S3 reads from the local file, so we need to save it first.
            f = codecs.open(file_path, 'w', 'utf-8', 'replace')
            file = File(f)
            file.write(content)
            file.close()

            if settings.USE_S3_THEME:
                # copy to s3 storage
                if os.path.splitext(file_relative_path)[1] == '.html':
                    public = False
                else:
                    public = True
                dest_path = os.path.join(theme, file_relative_path)
                dest_full_path = os.path.join(settings.THEME_S3_PATH,
                                              dest_path)
                save_file_to_s3(file_path,
                                dest_path=dest_full_path,
                                public=public)

                cache_key = '.'.join(
                    [settings.SITE_CACHE_KEY, 'theme', dest_path])
                cache.delete(cache_key)

                if hasattr(settings,
                           'REMOTE_DEPLOY_URL') and settings.REMOTE_DEPLOY_URL:
                    urlopen(settings.REMOTE_DEPLOY_URL)

            return True
        else:
            return False
示例#12
0
文件: utils.py 项目: eloyz/tendenci
def copy(filename, path_to_file, full_filename, TO_ROOT=THEME_ROOT):
    """Copies a file and all associated directories into TO_ROOT
    """
    try:
        os.makedirs(os.path.join(TO_ROOT, "templates", path_to_file))
    except OSError:
        pass

    filecopy = os.path.join(TO_ROOT, "templates", path_to_file, filename)
    shutil.copy(full_filename, filecopy)

    # copy to s3
    if hasattr(settings, 'USE_S3_STORAGE') and settings.USE_S3_STORAGE:
        if os.path.splitext(filename)[1] == '.html':
            public = False
        else:
            public = True
        save_file_to_s3(filecopy, public=public)
示例#13
0
def copy(filename, path_to_file, full_filename, TO_ROOT=THEME_ROOT):
    """Copies a file and all associated directories into TO_ROOT
    """
    try:
        os.makedirs(os.path.join(TO_ROOT, "templates", path_to_file))
    except OSError:
        pass
    
    filecopy = os.path.join(TO_ROOT, "templates", path_to_file, filename)
    shutil.copy(full_filename, filecopy)
    
    # copy to s3
    if hasattr(settings, 'USE_S3_STORAGE') and settings.USE_S3_STORAGE:
        if os.path.splitext(filename)[1] == '.html':
            public = False
        else:
            public = True
        save_file_to_s3(filecopy, public=public)
示例#14
0
    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=ORIG_ROOT_DIR)

            # Save the file locally no matter the theme location.
            # The save to S3 reads from the local file, so we need to save it first.
            f = codecs.open(file_path, "w", "utf-8", "replace")
            file = File(f)
            file.write(content)
            file.close()

            if settings.USE_S3_THEME:
                # copy to s3 storage
                if os.path.splitext(file_path)[1] == ".html":
                    public = False
                else:
                    public = True
                save_file_to_s3(file_path, public=public)

                cache_key = ".".join([settings.SITE_CACHE_KEY, "theme", "%s/%s" % (get_theme(), file_relative_path)])
                cache.delete(cache_key)

                if hasattr(settings, "REMOTE_DEPLOY_URL") and settings.REMOTE_DEPLOY_URL:
                    urllib.urlopen(settings.REMOTE_DEPLOY_URL)

            return True
        else:
            return False
示例#15
0
文件: utils.py 项目: chendong0444/ams
def copy(filename, path_to_file, full_filename, TO_ROOT=THEME_ROOT):
    """Copies a file and all associated directories into TO_ROOT
    """
    try:
        os.makedirs(os.path.join(TO_ROOT, "templates", path_to_file))
    except OSError:
        pass

    filecopy = os.path.join(TO_ROOT, "templates", path_to_file, filename)
    shutil.copy(full_filename, filecopy)

    # copy to s3
    if settings.USE_S3_THEME:
        if os.path.splitext(filename)[1] == '.html':
            public = False
        else:
            public = True
        dest_path = "/themes/%s" % filecopy
        save_file_to_s3(full_filename, dest_path=dest_path, public=public)
示例#16
0
def copy(filename, path_to_file, full_filename, TO_ROOT=THEME_ROOT):
    """Copies a file and all associated directories into TO_ROOT
    """
    try:
        os.makedirs(os.path.join(TO_ROOT, "templates", path_to_file))
    except OSError:
        pass

    filecopy = os.path.join(TO_ROOT, "templates", path_to_file, filename)
    shutil.copy(full_filename, filecopy)

    # copy to s3
    if settings.USE_S3_THEME:
        if os.path.splitext(filename)[1] == '.html':
            public = False
        else:
            public = True
        dest_path = "/themes/%s" % filecopy
        save_file_to_s3(full_filename, dest_path=dest_path, public=public)
示例#17
0
 def save(self, request, file_relative_path, ROOT_DIR=THEME_ROOT):
     content = self.cleaned_data["content"]
     file_path = (os.path.join(ROOT_DIR, file_relative_path)).replace("\\", "/")
     if os.path.isfile(file_path) and content <> "":
         archive_file(request, file_relative_path, ROOT_DIR=ROOT_DIR)
         f = codecs.open(file_path, 'w', 'utf-8', 'replace')
         file = File(f)
         file.write(content)
         file.close()
         
         # copy to s3 storage
         if os.path.splitext(file_path)[1] == '.html':
             public = False
         else:
             public = True
         save_file_to_s3(file_path, public=public)
         
         return True
     else:
         return False
示例#18
0
def handle_uploaded_file(file_path, file_dir):
    file_name = os.path.basename(file_path)
    filecopy = os.path.join(THEME_ROOT, file_dir, file_name)
    dest_path = os.path.join(settings.PROJECT_ROOT, "themes", filecopy)

    shutil.move(file_path, dest_path)

    # copy to s3
    if settings.USE_S3_THEME:
        if os.path.splitext(f.name)[1] == '.html':
            public = False
        else:
            public = True
        dest_path = "/themes/%s" % filecopy
        save_file_to_s3(file_path, dest_path=dest_path, public=public)

        cache_key = ".".join([settings.SITE_CACHE_KEY, 'theme', file_path[(file_path.find(THEME_ROOT)):]])
        cache.delete(cache_key)

        if hasattr(settings, 'REMOTE_DEPLOY_URL') and settings.REMOTE_DEPLOY_URL:
            urllib.urlopen(settings.REMOTE_DEPLOY_URL)
示例#19
0
文件: utils.py 项目: a2call/tendenci
def handle_uploaded_file(f, file_dir):
    filecopy = os.path.join(THEME_ROOT, file_dir, f.name)
    file_path = os.path.join(settings.PROJECT_ROOT, "themes", filecopy)

    destination = open(file_path, 'wb+')
    for chunk in f.chunks():
        destination.write(chunk)
    destination.close()

    # copy to s3
    if settings.USE_S3_THEME:
        if os.path.splitext(f.name)[1] == '.html':
            public = False
        else:
            public = True
        dest_path = "/themes/%s" % filecopy
        save_file_to_s3(file_path, dest_path=dest_path, public=public)

        cache_key = ".".join([settings.SITE_CACHE_KEY, 'theme', file_path[(file_path.find(THEME_ROOT)):]])
        cache.delete(cache_key)

        if hasattr(settings, 'REMOTE_DEPLOY_URL') and settings.REMOTE_DEPLOY_URL:
            urllib.urlopen(settings.REMOTE_DEPLOY_URL)