示例#1
0
class GadgetCodeEntry(Resource):

    def read(self, request, vendor, name, version, user_name=None):
        #user = user_authentication(request, user_name)
        gadget = get_object_or_404(Gadget, vendor=vendor, name=name, version=version)
        xhtml = get_object_or_404(gadget.xhtml, id=gadget.xhtml.id)

        content_type = gadget.xhtml.content_type
        if not content_type:
            content_type = 'text/html'

        if not xhtml.cacheable:
            try:
                if xhtml.url.startswith('/deployment/gadgets/'):
                    xhtml.code = get_xhtml_content(xhtml.url)
                else:
                    xhtml.code = download_http_content(gadget.get_resource_url(xhtml.url, request),
                                                       user=request.user)
                xhtml.save()
            except Exception, e:
                # FIXME: Send the error or use the cached original code?
                msg = _("XHTML code is not accessible")

        xhtml_code = xhtml.code
        if (content_type != 'text/html') and (content_type != 'application/xml+html'):
            return HttpResponse(xhtml_code, mimetype='%s; charset=UTF-8' % content_type)
        else:
            try:
                xhtml_code = includeTagBase(xhtml_code, xhtml.url, request)
                xhtml_code = fix_ezweb_scripts(xhtml_code, request)
                return HttpResponse(xhtml_code, mimetype='%s; charset=UTF-8' % content_type)
            except HTMLParseError, e:
                msg = _("Error when the code was parsed: %(errorMsg)s") % {'errorMsg': e.msg}
                return HttpResponse(get_xml_error(msg), mimetype='application/xml; charset=UTF-8')
示例#2
0
    def read(self, request, vendor, name, version, user_name=None):
        user = user_authentication(request, user_name)
        gadget = get_object_or_404(Gadget, vendor=vendor, name=name, version=version, users__id=user.id)
        xhtml = gadget.xhtml

        content_type = xhtml.content_type
        if not content_type:
            content_type = 'text/html'

        code = xhtml.code
        if not xhtml.cacheable or code == '':
            try:
                if xhtml.url.startswith('/deployment/gadgets/'):
                    code = get_xhtml_content(xhtml.url)
                    code = includeTagBase(code, xhtml.url, request)
                else:
                    code = download_http_content(gadget.get_resource_url(xhtml.url, request), user=request.user)
                code = fix_ezweb_scripts(code, request)
            except Exception, e:
                # FIXME: Send the error or use the cached original code?
                msg = _("XHTML code is not accessible: %(errorMsg)s") % {'errorMsg': e.message}
                return HttpResponse(get_xml_error(msg), mimetype='application/xml; charset=UTF-8')
            raise TemplateParseException(_("'%(file)s' is not a file") %
                                         {'file': local_path})

        f = open(local_path, 'r')
        code = f.read()
        f.close()

    else:
        if url.scheme == '':
            fetch_uri = urlparse.urljoin(main_uri, code_uri)
        else:
            fetch_uri = code_uri

        try:
            code = http_utils.download_http_content(fetch_uri, user=user)
        except HTTPError, e:
            msg = _("Error opening URL: code %(errorCode)s(%(errorMsg)s)") % {
                'errorCode': e.code, 'errorMsg': e.msg,
                }
            raise TemplateParseException(msg)
        except URLError, e:
            msg = _("Error opening URL: %(errorMsg)s") % {'errorMsg': e.reason}
            raise TemplateParseException(msg)

    code = includeTagBase(code, code_uri, request)
    code = fix_ezweb_scripts(code, request)

    return XHTML.objects.create(uri=gadget_uri + "/xhtml", code=code,
                                url=code_uri, content_type=content_type,
                                cacheable=cacheable)