Exemple #1
0
def theme_save_as(request, theme):
    if request.method == 'POST':
        # Theme
        new_theme = Theme()
        new_theme['verbose_name'] = request.POST['name']
        new_name = slugify(new_theme['verbose_name'])
        counter = 0
        while Theme.query().filter(name=new_name).exclude(pk=theme['pk']).count():
            counter += 1
            new_name = '%s-%s'%(slugify(theme['verbose_name']), counter)
        new_theme['name'] = new_name
        new_theme.save()

        # Templates
        for tpl in theme['templates']:
            new_tpl, new = new_theme['templates'].get_or_create(name=tpl['name'])
            new_tpl['notes'] = tpl['notes']
            new_tpl['content'] = tpl['content']
            new_tpl['engine'] = tpl['engine']
            new_tpl.save()

        """
        # FIXME
        # Static files
        for sf in theme['static_files']:
            try:
                new_sf, new = new_theme['static_files'].get_or_create(name=sf['name'])
                new_sf.url = sf.url
                new_sf.mimetype = sf.mimetype
                new_sf.save()

                if sf.file:
                    # Finds a name for the new file
                    root = sf.file.path.replace(sf.file.name, '')
                    name, ext = os.path.splitext(sf.file.name)
                    while os.path.exists(root + name + ext):
                        name += '_'

                    # Reads the old file to make a ContentFile instance
                    fp = file(sf.file.path)
                    content = ContentFile(fp.read())
                    fp.close()
                    
                    # Saves the new file for the new static file object
                    new_sf.file.save(name+ext, content)
            except BaseException as e:
                print(sf, e.__class__, e)
                raise
        """
        ret = {'new_url': reverse('themes_theme', kwargs={'name': new_theme['name']})}
    else:
        ret = {'result': 'error'}
    
    return JsonResponse(ret)
Exemple #2
0
def choose_theme(request):
    """
    Function used by setting THEMES_THEME_CHOOSING to get the current theme by the middleware.
    This function is simple but supports returning the default theme or the one set by a cookie,
    used by "Preview" function. But a similar function can be made to do more than that.
    """
    # Check first about a cookie with current theme
    if request.COOKIES.get(app_settings.CURRENT_THEME_COOKIE, None):
        try:
            return Theme.query().get(name=request.COOKIES[app_settings.CURRENT_THEME_COOKIE].value)
        except Theme.DoesNotExist:
            pass

    # Returns the default theme
    try:
        return Theme.query().get(is_default=True)
    except Theme.DoesNotExist:
        return None
Exemple #3
0
def home(request):
    themes = Theme.query().order_by('verbose_name')
    previewing = request.COOKIES.get(app_settings.CURRENT_THEME_COOKIE, None)

    if request.POST.get('name', None):
        verbose_name = request.POST['name']
        name = slugify(request.POST['name'])
        counter = 0
        while Theme.query().filter(name=name).count():
            counter += 1
            name = '%s-%s'%(slugify(request.POST['name']), counter)
        theme = Theme.query().create(name=name, verbose_name=verbose_name)

        request.warning(_('New theme "%s" created.'))
        return HttpResponseRedirect(reverse('themes_theme', kwargs={'name': theme['name']}))
    return render_to_response(request,
            'themes/home.html',
            {'themes':themes, 'previewing':previewing},
            )
Exemple #4
0
def theme_rename(request, theme):
    if request.method == 'POST':
        # Sets a new verbose name and its slugified name
        theme['verbose_name'] = request.POST['name']
        new_name = slugify(theme['verbose_name'])
        counter = 0
        while Theme.query().filter(name=new_name).exclude(pk=theme['pk']).count():
            counter += 1
            new_name = '%s-%s'%(slugify(theme['verbose_name']), counter)
        theme['name'] = new_name
        theme.save()

        ret = {'new_url': reverse('themes_theme', kwargs={'name': theme['name']})}
    else:
        ret = {'result': 'error'}
    
    # Returns a JSON with new URL to redirect it
    return JsonResponse(ret)
    def _tag_theme_static_file(self, name, caller):
        if ':' in name:
            theme_name, name = name.split(':')
            try:
                theme = Theme.query().get(name=theme_name)
            except Theme.DoesNotExist:
                theme = None
        else:
            theme = getattr(self.environment, 'theme', None)

        if not theme:
            return ''

        try:
            static = theme['static_files'].get(name=name)
        except ThemeStaticFile.DoesNotExist:
            return ''

        return static.get_url()