Ejemplo n.º 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)