Exemplo n.º 1
0
def theme_create_static_file(request, theme):
    ret = {}

    if request.method == 'POST':
        name = request.POST['name']
        if theme['static_files'].filter(name=name).count():
            ret = {'result':'error', 'message':'Static File already exists.'}
        else:
            sf = theme['static_files'].create(name=name)

            if request.POST.get('url', None):
                sf['url'] = request.POST['url']
                sf['mime_type'] = mimetypes.guess_type(sf['url'])[0] or ''
                sf.save()
            else:
                # Saves an empty file as a starting point
                content_file = ContentFile('')
                db_field = ThemeStaticFile._meta.fields['file']
                file_name = '%s-%s-%s'%(theme['pk'], sf['pk'], name)
                upload_name = db_field.generate_filename(sf, file_name)
                upload_name = db_field.storage.save(upload_name, content_file)
                sf['file'] = FieldFile(sf, db_field, upload_name)

                # Detects the mime_type for the given name
                sf['mime_type'] = mimetypes.guess_type(file_name)[0] or ''
                sf.save()

                # Calls the signal after static files changed
                theme_static_files_changed.send(sender=request.site, theme=theme, request=request, items=[sf])

            ret = {'result':'ok', 'info':{'pk':str(sf['pk']), 'url':sf.get_url()}}

    return JsonResponse(ret)
Exemplo n.º 2
0
def theme_up_file(request, theme):
    new_static_files = []

    if request.method == 'POST':
        db_field = ThemeStaticFile._meta.fields['file']
        for up_file in request.FILES.getlist('dragupload[]'):
            name = up_file.name
            while theme['static_files'].filter(name=name).count():
                name += '_'

            sf = ThemeStaticFile(theme=theme, name=name, mime_type=up_file.content_type)
            upload_name = db_field.generate_filename(sf, up_file.name)
            upload_name = db_field.storage.save(upload_name, up_file)
            sf['file'] = FieldFile(sf, db_field, upload_name)
            sf.save()

            new_static_files.append(sf)

        # Calls the signal after static files changed
        theme_static_files_changed.send(sender=request.site, theme=theme, request=request, items=new_static_files)

    return render_to_response(request,
            'themes/theme_up_file.html',
            {'theme': theme, 'new_static_files': new_static_files},
            )
Exemplo n.º 3
0
def theme_upload(request, theme):
    if request.method == 'POST':
        up_file = request.FILES.getlist('dragupload[]')[0]
        try:
            import_theme(up_file, theme=theme)

            # Calls the signal after static files changed
            theme_static_files_changed.send(sender=request.site, theme=theme, request=request,
                    items=list(theme['static_files'].exclude(file=None)))

            request.info(_('File imported with success!'))
        except ValueError as e:
            request.info(e)
        return {'theme':theme, 'uploaded':True}
    else:
        return {'theme':theme}
Exemplo n.º 4
0
def theme_import(request):
    if request.method == 'POST':
        try:
            # Calls the theme importing function to save theme, templates and static files in the right places
            theme = import_theme(request.FILES['file'])

            # Calls the signal after static files changed
            theme_static_files_changed.send(sender=request.site, theme=theme, request=request,
                    items=list(theme['static_files'].exclude(file=None)))

            # Final message and redirecting
            request.info(_('File imported with success!'))
            url_redirect = reverse('themes_theme', kwargs={'name': theme['name']})
        except ValueError as e:
            request.info(e)
            url_redirect = reverse('themes_home')
    return HttpResponseRedirect(url_redirect)
Exemplo n.º 5
0
def theme_edit_child(request, theme):
    if request.method == 'POST':
        if request.POST['type'] == 'template':
            item = theme['templates'].get(name=request.POST['name'])
            item['content'] = request.POST.get('content', '')
            item.save()
            return HttpResponse('ok')

        elif request.POST['type'] == 'static-file':
            item = theme['static_files'].get(name=request.POST['name'])
            file_path = item['file'].path
            fp = file(file_path, 'w')
            fp.write(request.POST.get('content', '').encode('utf-8'))
            fp.close()

            # Calls the signal after static files changed
            theme_static_files_changed.send(sender=request.site, theme=theme, request=request, items=[item])

            return HttpResponse('ok')

    else:
        rel = request.GET['rel']
        typ, pk = rel.rsplit('-',1)

        if typ == 'template':
            item = theme['templates'].get(pk=pk)
            content = item['content'] if item['content'] != None else ''
            return HttpResponse(u'type(html)'+content)
        elif typ in ('static-file', 'static-url'):
            item = theme['static_files'].get(pk=pk)
            ext = os.path.splitext(item['file'].name)[-1][1:]

            if item['mime_type'].startswith('text/'):
                try:
                    content = u'type(%s)%s'%(ext, item['file'].read().decode('utf-8'))
                    return HttpResponse(content)
                except ValueError:
                    pass

            try:
                url = item.get_url()
            except ValueError:
                url = item['url']

            ret = {'type':item.get_type(), 'url':url, 'mime_type':item['mime_type']}
            return JsonResponse(ret)