def theme(request, id=None):
    shop = request.shop
    theme = shop.theme
    edit = request.GET.get('edit', None)
    if id is None:
        template = Template.objects.filter(theme=theme, name='layout').get()
        edit = 'template'
    else:
        if edit == 'template':  
            template = get_object_or_404(Template, pk=id)
            if template.theme.shop != shop:
                raise Http404
        elif edit == 'asset':
            asset = get_object_or_404(Asset, pk=id)
            if asset.theme.shop != shop and asset.is_editable():
                raise Http404
            
    param = {}
    if edit == 'template':
        text = copy.copy(template.text)
        form_template = TemplateForm(request.POST or None, instance=template)
        if form_template.is_valid():
            if text != form_template.cleaned_data['text']:
                shop.last_date_to_change_layout = datetime.datetime.now()
                shop.save()
                form_template.save()
#            request.flash['message'] = unicode(_("Template successfully saved."))
#            request.flash['severity'] = "success"
            return HttpResponse('Template successfully saved.')
            #return HttpResponseRedirect(reverse('web_store_theme', args=[template.id])+"?edit=template")
        version_list = Version.objects.get_for_object(template).order_by('-pk')[:10]
        param = {'form_template': form_template,
                 'version_list': version_list,
                 'template': template}

    else: # edit == 'asset'
    
        try: 
            if request.method == 'POST':
                form_asset = AssetEditForm(shop=shop, data=request.POST)
                if form_asset.is_valid():
                    text = form_asset.cleaned_data['text']
                    if asset.file.storage.exists(asset.file.name):
                        asset.file.storage.delete(asset.file.name)
                    asset.file.save(asset.file.name, ContentFile(str(text)))
                    
                    try:
                        shop.last_date_to_change_layout = datetime.datetime.now()
                        shop.save()
                        asset.save()
                        asset.render()
                    except Exception,e:
                        return HttpResponse(e)
    
                    return HttpResponse('File successfully saved.')
                else:
                    errors = "\n".join(["%s" % (v.as_text()) for k,v in form_asset.errors.iteritems()])
                    return HttpResponse(errors)
            else:
def template_edit(request, id):
    shop = request.shop
    template = get_object_or_404(Template, pk=id)
    if template.theme.shop != shop:
        raise Http404
    text = copy.copy(template.text)
    form = TemplateForm(request.POST or None, instance=template)
    if form.is_valid():
        if text != form.cleaned_data['text']:
            form.save()
        request.flash['message'] = unicode(_("Template successfully edited."))
        request.flash['severity'] = "success"
        return HttpResponseRedirect(reverse('template_edit', args=[id]))
    version_list = Version.objects.get_for_object(template).order_by('-pk')[:10]
    return render_to_response('themes/template_edit.html',
                              {
                               'form': form,
                               'template': template,
                               'templates': shop.theme.template_set.all(),
                               'version_list': version_list,
                               },
                              RequestContext(request))
def template_edit(request, id):
    shop = request.shop
    template = get_object_or_404(Template, pk=id)
    if template.theme.shop != shop:
        raise Http404
    text = copy.copy(template.text)
    form = TemplateForm(request.POST or None, instance=template)
    if form.is_valid():
        if text != form.cleaned_data['text']:
            shop.shop.last_date_to_change_layout = datetime.datetime.now()
            shop.save()
            form.save()
        request.flash['message'] = unicode(_("Template successfully edited."))
        request.flash['severity'] = "success"
        return HttpResponseRedirect(reverse('template_edit', args=[id]))
    version_list = Version.objects.get_for_object(template).order_by(
        '-pk')[:10]
    return render_to_response(
        'themes/template_edit.html', {
            'form': form,
            'template': template,
            'templates': shop.theme.template_set.all(),
            'version_list': version_list,
        }, RequestContext(request))
def theme(request, id=None):
    shop = request.shop
    theme = shop.theme
    edit = request.GET.get('edit', None)
    if id is None:
        template = Template.objects.filter(theme=theme, name='layout').get()
        edit = 'template'
    else:
        if edit == 'template':
            template = get_object_or_404(Template, pk=id)
            if template.theme.shop != shop:
                raise Http404
        elif edit == 'asset':
            asset = get_object_or_404(Asset, pk=id)
            if asset.theme.shop != shop and asset.is_editable():
                raise Http404

    param = {}
    if edit == 'template':
        text = copy.copy(template.text)
        form_template = TemplateForm(request.POST or None, instance=template)
        if form_template.is_valid():
            if text != form_template.cleaned_data['text']:
                shop.last_date_to_change_layout = datetime.datetime.now()
                shop.save()
                form_template.save()


#            request.flash['message'] = unicode(_("Template successfully saved."))
#            request.flash['severity'] = "success"
            return HttpResponse('Template successfully saved.')
            #return HttpResponseRedirect(reverse('web_store_theme', args=[template.id])+"?edit=template")
        version_list = Version.objects.get_for_object(template).order_by(
            '-pk')[:10]
        param = {
            'form_template': form_template,
            'version_list': version_list,
            'template': template
        }

    else:  # edit == 'asset'

        try:
            if request.method == 'POST':
                form_asset = AssetEditForm(shop=shop, data=request.POST)
                if form_asset.is_valid():
                    text = form_asset.cleaned_data['text']
                    if asset.file.storage.exists(asset.file.name):
                        asset.file.storage.delete(asset.file.name)
                    asset.file.save(asset.file.name, ContentFile(str(text)))

                    try:
                        shop.last_date_to_change_layout = datetime.datetime.now(
                        )
                        shop.save()
                        asset.save()
                        asset.render()
                    except Exception, e:
                        return HttpResponse(e)

                    return HttpResponse('File successfully saved.')
                else:
                    errors = "\n".join([
                        "%s" % (v.as_text())
                        for k, v in form_asset.errors.iteritems()
                    ])
                    return HttpResponse(errors)
            else: