def search_results(request):
    """ Respond to the "/admin/search/results" URL.

        We display the matching accounts for a given set of search criteria.
    """
    if not auth_controller.is_logged_in(request):
        return auth_controller.redirect_to_login()

    if request.method == "GET":
        params = request.GET
    elif request.method == "POST":
        params = request.POST
    else:
        return HttpResponse("Bad HTTP Method")

    err_msg  = None # initially.
    page     = params.get("page", 1)
    query    = params.get("query", "")
    download = params.get("download", "")

    if request.method == "POST" and request.POST['submit'] == "Search":
        # The user pressed the "Search" button -> reissue the search request.
        query = query.replace("&", "%26")
        query = query.replace("=", "%3D")
        return HttpResponseRedirect("/admin/search/results" +
                                    "?query=" + query)

    if download == "1":
        # The user clicked on our "Download Search Results" button.  Return the
        # search results as a CSV file to download.
        response = HttpResponse(content_type='text/csv')
        response['Content-Disposition'] = 'attachment; filename="results.csv"'

        search_results = _download_search_results(query)

        writer = csv.writer(response)
        for row in search_results:
            writer.writerow(row)

        return response

    response = functions.search(query, page, rpp=15, totals_only=False)
    if not response['success']:
        err_msg = response['error']

    return render(request, "admin/search_results.html",
                  {'menus'       : get_admin_menus(request),
                   'current_url' : "/admin/search",
                   'query'       : query,
                   'num_matches' : response.get('num_matches', 0),
                   'num_pages'   : response.get('num_pages', 1),
                   'err_msg'     : err_msg,
                   'page'        : page,
                   'accounts'    : response.get('accounts', []),
                   'done_url'      : "/admin",
                  })
Example #2
0
    def proxy(self):
        """Return an HttpResponse built based on retrieving self.proxy_url"""
        xff = self.downstream_ip
        headers = self.filter_headers(
            self.headers, self.ignored_request_headers)
        result = request(
            method=self.request.method, url=self.proxy_url, headers=headers,
            data=self.request.body, files=self.request.FILES,
            params=self.query_string)

        response = HttpResponse(result.content, status=result.status_code)
        forwardable_headers = self.filter_headers(
            result.headers, self.ignored_downstream_headers)

        # Attach forwardable headers to response
        for header, value in forwardable_headers.iteritems():
            response[header] = value

        # Update X-Forwarded-For.
        if response.get('x-forwarded-for'):
            xff = '%s, %s' % (response['x-forwarded-for'], xff)

        response['x-forwarded-for'] = xff

        return response
Example #3
0
 def print_msg_if_error(self, response: HttpResponse) -> None:  # nocoverage
     if response.status_code != 200 and response.get('Content-Type') == 'application/json':
         content = ujson.loads(response.content)
         print()
         print("======================================================================")
         print("ERROR: {}".format(content.get('msg')))
         print()
Example #4
0
 def print_msg_if_error(self, response: HttpResponse) -> None:  # nocoverage
     if response.status_code != 200 and response.get('Content-Type') == 'application/json':
         content = ujson.loads(response.content)
         print()
         print("======================================================================")
         print("ERROR: {}".format(content.get('msg')))
         print()
Example #5
0
def build_payload(data: dict, request: WSGIRequest,
                  response: HttpResponse) -> dict:
    payload = {
        "node": platform.node(),
        "content-type": response.get("content-type"),
        "ip": get_ip(request),
        "method": request.method,
        "params": dict(request.GET),
        "path": request.path,
        "status": response.status_code,
        "time": datetime.utcnow(),
    }
    resolver_match = getattr(request, "resolver_match", None)
    if resolver_match:
        payload.update({
            "view": resolver_match.view_name,
        })
    user = getattr(request, "user", None)
    if user:
        payload.update({
            "user_id": user.id,
        })
    payload.update(data)
    payload.pop("start_time", None)
    payload.pop("end_time", None)
    payload.pop("db_record_stacks", None)
    return payload
Example #6
0
        def process_response(self, request, response):
            toolbar = self.debug_toolbars.get(request)
            if not toolbar:
                return response

            settings.DEBUG = toolbar.orig_DEBUG

            if not self._show_toolbar(request):
                return response

            response = HttpResponse(
                '''<html><head></head><body></body><script type="text/javascript">djdt.ready(function(d){
                var $ = d.jQuery;
                d.show_toolbar(false);
                $($("#djDebugPanelList .djDebugPanelButton a")[0]).click();
            });</script></html>''')

            for panel in toolbar.panels:
                panel.process_response(request, response)

            response.content = replace_insensitive(
                smart_unicode(response.content), self.tag,
                smart_unicode(toolbar.render_toolbar() + self.tag))

            if response.get('Content-Length', None):
                response['Content-Length'] = len(response.content)

            del self.debug_toolbars[request]
            return response
Example #7
0
def hello(request):
    response = HttpResponse()
    # response 常见方法
    response.write("Welcome to Django1!")
    response.write("Welcome to Django2!")
    response.writelines(
        ["Welcome to Django3!", "Welcome to Django4!", "Welcome to Django5!"])
    print(response.getvalue())
    print(response.writable())
    response.__setitem__("age", "30")
    print(response.has_header("age"))
    print(response.__getitem__("age"))
    print(response.get("age"))
    response.__delitem__("age")
    response['address'] = "北京"
    print(response.get("address"))
    return response
Example #8
0
def inject_stats(request: WSGIRequest, response: HttpResponse,
                 data: dict) -> None:
    summary_data = summarize_data(data)
    content_type = response.get("content-type", "").lower()
    if content_type.startswith("text/html") and ("charset" not in content_type
                                                 or "utf-8" in content_type):
        inject_html(request, response, data, summary_data)
    response["x-cavalry-data"] = json.dumps(summary_data)
Example #9
0
def main_document_view(request, urn=None, wiki_page=None, wiki_revision=None):
    """Dispatches the appropriate view for a resource/page
    """

    requested_view = request.GET.get('view', None)

    resource_database = get_resource_database()

    if requested_view == 'raw':
        etag = __handle_etag(request, ['raw', urn], weak=False)
        # fixme: we may also want to set last-modified, expires, max-age
        try:
            data_iterator = resource_database[urn]
        except KeyError:
            raise Http404
        response = HttpResponse(list(data_iterator), # see django #6527
                                content_type='application/octet-stream')
        response["ETag"] = etag
        return response

    if request.method == "GET":
        unvaried_etag = [urn, bool(wiki_page),
                         request.META.get("QUERY_STRING", "")]
        varied_etag = unvaried_etag + [request.LANGUAGE_CODE,
                                       bool(request.is_secure()),
                                       request.META.get("HTTP_COOKIE", "")]
        unvaried_etag = __handle_etag(request, unvaried_etag)
        varied_etag = __handle_etag(request, varied_etag)

    try:
        resource = resource_database.get_resource_object(urn)
    except KeyError:
        raise Http404("resource does not exist")
    except UnexpectedHeader as e:
        raise Http404(str(e))
    request.ductus = DuctusRequestInfo(resource, requested_view,
                                       wiki_page, wiki_revision)

    try:
        f = registered_views[resource.fqn][requested_view]
    except KeyError:
        try:
            f = registered_views[None][requested_view]
        except KeyError:
            return query_string_not_found(request)
    if not f.meets_requirements(request.ductus):
        return query_string_not_found(request)
    response = f(request)

    if request.method == "GET" and not response.has_header("ETag"):
        if getattr(response, "_unvarying", False):
            response["ETag"] = unvaried_etag
        else:
            vary_headers = set([h.strip().lower() for h in response.get("Vary", "").split(',') if h])
            if vary_headers.issubset(set(['cookie', 'accept-language'])):
                response["ETag"] = varied_etag
    return response
Example #10
0
 def print_msg_if_error(self, url: str, response: HttpResponse) -> None:  # nocoverage
     if response.status_code == 200:
         return
     print("Error processing URL:", url)
     if response.get("Content-Type") == "application/json":
         content = orjson.loads(response.content)
         print()
         print("======================================================================")
         print("ERROR: {}".format(content.get("msg")))
         print()
Example #11
0
def tweetDownload (request):

    if (not request.user.is_authenticated() or  not request.user.get_profile().is_org_member):
        return HttpResponse(status=403)

    proxy_url = urlsplit(request.get_full_path())
    download_url = "http://" + settings.GEOPS_IP + "?" + proxy_url.query  + settings.GEOPS_DOWNLOAD

    http = httplib2.Http()
    response, content = http.request(
        download_url, request.method)

    response =  HttpResponse(
        content=content,
        status=response.status,
        mimetype=response.get("content-type", "text/plain"))

    response['Content-Disposition'] = response.get('Content-Disposition', 'attachment; filename="tweets"' + request.user.username + '.csv');
    return response
Example #12
0
    def test_p3p_response_header(self):
        request = self.rf.get('/')
        response = HttpResponse("Example response")

        self.assertEqual(
            P3PMiddleware().process_response(request, response),
            response
        )

        self.assertTrue(response.has_header('P3P'))
        self.assertEqual(response.get('P3P'), 'CP=""')
Example #13
0
 def __init__(self, path: _t.Text, method: _t.Text, response: HttpResponse):
     super().__init__(path=path,
                      status=response.status_code,
                      data=self._get_rendered(response),
                      method=method,
                      headers={
                          header: response.headers[header]
                          for header in response.headers
                          if header in response_headers_to_pass
                      })
     self.timing = float(response.get('Response-Time', '0.0'))
Example #14
0
def convert_any_data_to_jsonresponse(response):
    """将不同类型的数据转为 JsonResponse"""
    if type(response) == tuple:
        response = response[0]
    elif type(response) in [str]:
        response = HttpResponse(response)
    elif isinstance(response, dict) and response.get('code', None) is not None:
        response = JsonResponse(**response).render()
    elif type(response) in [list]:
        response = JsonResponse(response).render()
    return response
Example #15
0
    def test_process_response_add_dnt_to_vary(self):
        """
        Test if the ``process_response`` method of the middleware update the ``Vary`` HTTP header.
        """
        request = HttpRequest()
        request.META['HTTP_DNT'] = '1'
        response = HttpResponse()
        self.assertNotIn('DNT', response.get('VARY', []))
        response = DoNotTrackMiddleware().process_response(request, response)

        # Test the result
        self.assertIn('DNT', response['VARY'])
Example #16
0
    def test_response_returns_json_dump_file(self):

        """Assert equals file has been downloaded"""

        file_path = os.path.join(settings.MEDIA_ROOT, "data.json")
        with open(file_path, 'r') as f:
            response = HttpResponse(f.read(), content_type='text/json')
            response['Content-Disposition'] = 'attachment; filename=' + '"' + os.path.basename(file_path)
            self.assertEquals(
                response.get('Content-Disposition'),
                'attachment; filename=' + '"' + os.path.basename(file_path)
            )
Example #17
0
def tweetDownload(request):

    if (not request.user.is_authenticated()
            or not request.user.get_profile().is_org_member):
        return HttpResponse(status=403)

    proxy_url = urlsplit(request.get_full_path())
    download_url = "http://" + settings.GEOPS_IP + "?" + proxy_url.query + settings.GEOPS_DOWNLOAD

    http = httplib2.Http()
    response, content = http.request(download_url, request.method)

    response = HttpResponse(content=content,
                            status=response.status,
                            mimetype=response.get("content-type",
                                                  "text/plain"))

    response['Content-Disposition'] = response.get(
        'Content-Disposition',
        'attachment; filename="tweets"' + request.user.username + '.csv')
    return response
Example #18
0
    def generate_response(self, request, response, asked_url):
        final_response = HttpResponse()

        final_response = self.sso_client.fill_response(
            response, final_response, self.application.get_redirect_uri())
        final_response.status_code = response.status_code
        """ We want to immediately return the result of the SSO Forward POST Request """
        if self.application.sso_forward_return_post:
            matched = None
            if self.application.sso_capture_content_enabled and self.application.sso_capture_content:
                ##TODO: MAKE THE CAPTURE/REPLACE/ADDITIONNAL REQUEST
                #  ON ALL THE RESPONSE (HEADERS INCLUDED !)
                # response_raw = ""
                # for key,item in response.headers:
                #     response_raw += str(key)+": "+str(item)+"\r\n"
                # response_raw += response.text

                matched = re_search(self.application.sso_capture_content,
                                    response.content)  # response_raw

            final_response_body = self.sso_client.advanced_sso_perform(
                matched, self.application, response)

            if "gzip" in final_response.get('Content-Encoding', ""):
                final_response = create_gzip_response(request,
                                                      final_response_body)
                final_response = self.sso_client.fill_response(
                    response, final_response,
                    self.application.get_redirect_uri())
                final_response.status_code = response.status_code
            else:
                final_response.content = final_response_body
            final_response['Content-Length'] = len(final_response.content)
            logger.debug(
                "SSOForward::generate_response: sso_forward_return_post activated - final response generated"
            )

        elif response.status_code not in (
                301, 302,
                303) or not self.application.sso_forward_follow_redirect:
            """ simply redirect to the default Application entry point """
            final_response.status_code = 302
            # redirect user to url redirected
            final_response['Location'] = asked_url
            del final_response['Content-Length']
            del final_response['Content-Encoding']
            logger.debug(
                "SSOForward::generate_response: Generated response redirects to '{}'"
                .format(asked_url))

        return final_response
    def test_ignoring_marked_for_delete_tokens(self):
        token = Token.objects.create(user=self.user)
        now = timezone.now()
        Token.objects.update(expire_in=now - timedelta(seconds=5),
                             dead_in=now + timedelta(seconds=5),
                             marked_for_delete=True)

        request = Request(request=self.request_factory.get(
            '/', HTTP_AUTHORIZATION='Token %s' % token),
                          authenticators=[SecureTokenAuthentication()])
        request._authenticate()
        response = HttpResponse()

        middleware = UpdateTokenMiddleware()
        response = middleware.process_response(request._request, response)

        new_token = response.get('X-Token', None)
        self.assertIsNone(new_token)
Example #20
0
 def error_response(self, error, mimetype='application/json', status=400,
         **kwargs):
     """
     Return an error response to the client with default status code of
     *400* stating the error as outlined in :draft:`5.2`.
     """
     request = kwargs.get('request')
     if request:
         del kwargs['request']
     response = HttpResponse(json.dumps(error), mimetype=mimetype,
             status=status, **kwargs)
     if request:
         set_login_response_header(request, response)
         login_type = response.get('login_type', '')
         error_dict = {
             'login_type': login_type,
             'error': error
         }
         error_dict['error'].update({'msg': error.get('error', u'登录失败')})
         server_track(request, 'api.user.login_failure', error)
     return response
Example #21
0
    def test_token_updating(self):
        token = Token.objects.create(user=self.user)
        now = timezone.now()
        Token.objects.update(expire_in=now - timedelta(seconds=5), dead_in=now + timedelta(seconds=5))

        request = Request(
            request=self.request_factory.get('/', HTTP_AUTHORIZATION='Token {0}'.format(token)),
            authenticators=[SecureTokenAuthentication()],
        )
        request._authenticate()
        response = HttpResponse()

        middleware = UpdateTokenMiddleware()
        response = middleware.process_response(request._request, response)

        new_token = response.get('X-Token', None)
        self.assertIsNotNone(new_token)

        new_token = Token.objects.get(key=new_token)
        self.assertEqual(new_token.user, self.user)

        old_token = Token.objects.get(id=token.id)
        self.assertTrue(old_token.marked_for_delete)
Example #22
0
 def test_dict_behavior(self):
     """
     Test for bug #14020: Make HttpResponse.get work like dict.get
     """
     r = HttpResponse()
     self.assertIsNone(r.get("test"))
Example #23
0
    def get(self, request, apiname, *k, **ks):
        import time
        from utils import Data
        from defs import raiseApiEx as err
        from defs import ApiException, CheckException
        self.me = None
        self.meid = None
        starttime = time.time()
        try:
            apiname = apiname.strip('/')
            sys_debug = self.site.setting.get('sys_debug')
            if sys_debug and (apiname == 'doc.html'
                              or not apiname) and self.session_get('xme'):
                return self.doc(request)
            if not sys_debug and request.method != 'POST':
                err("Not Allow!")
            if not apiname:
                err("接口不能为空")
            apifun = None
            try:
                apifun = getattr(self, apiname)
                needlogin = getattr(apifun, 'logined')
            except:
                err("不存在接口 %s" % apiname)

            needlogin = getattr(apifun, 'logined')
            userrole = getattr(apifun, 'userrole')
            operation = getattr(apifun, 'operation')
            checker = getattr(apifun, 'checker')
            timegap = getattr(apifun, 'timegap')
            cachetime = getattr(apifun, 'cachetime')
            if timegap:
                from django.core.cache import cache
                cachekey = 'nexttime_%s_%s_%s' % (
                    self.get_clientkey(), self.__class__.__name__, apiname)
                nexttime = cache.get(cachekey)
                if nexttime and time.time() < nexttime:
                    # err(u'调用太频繁:%s' % (cachekey if sys_debug else apiname))
                    err(u'调用太频繁')
                cache.set(cachekey, time.time() + timegap)
            if needlogin and not self.get_meid():
                err(u'用户未登录', c=2001)

            if userrole and userrole not in self.get_roles():
                err(u'用户权限不足')

            if operation:
                self.checkOperation(operation)

            if checker and not checker(self, request):
                err(u'检查不通过')

            kvargs = {}
            param = {}
            param.update(dict(request.GET.items()))
            param.update(dict(request.POST.items()))

            missargs = []
            arginfos = getattr(apifun, 'arginfos')
            for p in arginfos:
                ap = p['info']
                name = p['name']
                #                 print name, ap.default, ap.empty, ap.hasdefault
                if name != '_param' and ap.empty and name not in param:
                    param[name] = ap.default if ap.hasdefault else ''
                if name == '_param':
                    kvargs[name] = param
                elif not ap.hasdefault and name not in param:
                    missargs.append(name)
                elif not ap.hasdefault and not ap.empty and not param.get(
                        name):
                    missargs.append(name)
                else:
                    kvargs[name] = param.get(name, ap.default)
            if missargs:
                err(u'缺失参数: %s' % (', '.join(missargs)))

            def doapi():
                data = apifun(**kvargs)
                if isinstance(data, Data):
                    data = data.data
                return data

            if cachetime:
                from base.utils import get_or_set_cache, md5
                paramkey = u'&'.join([
                    u'%s=%s' % (k, v) for k, v in param.items()
                    if not k.startswith('_')
                ])
                apikey = 'api_cache_%s_%s' % (apiname, md5(paramkey))

                def responsejson():
                    data = get_or_set_cache(apikey, doapi, cachetime)
                    res = {
                        "code": 0,
                        "data": data
                    } if (not data or type(data) != dict
                          or '_msg' not in data) else {
                              "code": 0,
                              "msg": data['_msg'],
                              "data": data.get('data')
                          }
                    res['timestamp'] = int(time.time())
                    return json.dumps(res, default=json_filed_default)

                data = HttpResponse(get_or_set_cache(apikey + '_json',
                                                     responsejson, cachetime),
                                    content_type="text/json")
            else:
                data = doapi()
            from django.http.response import HttpResponseBase
            if isinstance(data, HttpResponseBase):
                return data
            res = {
                "code": 0,
                "data": data
            } if (not data or type(data) != dict or '_msg' not in data) else {
                "code": 0,
                "msg": data['_msg'],
                "data": data.get('data')
            }
            res['time'] = int((time.time() - starttime) * 1000)
            return self.api_response(res)

        except ApiException, e:
            res = e.args[0]
            if 'msg' in res:
                res['msg'] = self.translate(res['msg'])
            return self.api_response(res)
Example #24
0
def is_json_content(response: HttpResponse) -> bool:
    return bool(
        response.get('Content-Type') == 'application/json' and
        response.content
    )
Example #25
0
def gzip_compressor(request):
    plugins = split_commas(request.GET.get("plugins", ""))
    languages = split_commas(request.GET.get("languages", ""))
    themes = split_commas(request.GET.get("themes", ""))
    files = split_commas(request.GET.get("files", ""))
    source = request.GET.get("src", "") == "true"
    isJS = request.GET.get("js", "") == "true"
    compress = request.GET.get("compress", "true") == "true"
    content = []

    response = HttpResponse()
    response["Content-Type"] = "text/javascript"

    if not isJS:
        response.write(
            render_to_string("tinymce/tiny_mce_gzip.js",
                             {"base_url": tinymce.settings.JS_BASE_URL}))
        return response

    patch_vary_headers(response, ["Accept-Encoding"])

    now = datetime.utcnow()
    response["Date"] = now.strftime("%a, %d %b %Y %H:%M:%S GMT")

    cacheKey = "|".join(plugins + languages + themes)
    cacheData = cache.get(cacheKey)

    if cacheData is not None:
        if "ETag" in cacheData:
            if_none_match = request.META.get("HTTP_IF_NONE_MATCH")
            if if_none_match == cacheData["ETag"]:
                response.status_code = 304
                response.content = ""
                response["Content-Length"] = "0"
                return response

        if "Last-Modified" in cacheData:
            if_modified_since = request.META.get("HTTP_IF_MODIFIED_SINCE")
            if if_modified_since == cacheData["Last-Modified"]:
                response.status_code = 304
                response.content = ""
                response["Content-Length"] = "0"
                return response

    tinyMCEPreInit = {
        "base": tinymce.settings.JS_BASE_URL,
        "suffix": "",
    }
    content.append("var tinyMCEPreInit={};".format(json.dumps(tinyMCEPreInit)))

    # Add core
    files = ["tinymce"]

    # Add core languages
    for lang in languages:
        files.append("langs/{}".format(lang))

    # Add plugins
    for plugin in plugins:
        files.append("plugins/{}/plugin".format(plugin))

        for lang in languages:
            files.append("plugins/{}/langs/{}".format(plugin, lang))

    # Add themes
    for theme in themes:
        files.append("themes/{}/theme".format(theme))

        for lang in languages:
            files.append("themes/{}/langs/{}".format(theme, lang))

    for f in files:
        # Check for unsafe characters
        if not safe_filename_re.match(f):
            continue
        content.append(get_file_contents(f, source=source))

    # Restore loading functions
    content.append(
        'tinymce.each("{}".split(",")'.format(",".join(files)) +
        ', function(f){tinymce.ScriptLoader.markDone(tinyMCE.baseURL+"/"+f+".js");});'
    )

    unicode_content = []
    for i, c in enumerate(content):
        try:
            unicode_content.append(c.decode("latin-1"))
        except AttributeError:
            # python 3 way
            unicode_content.append(smart_text(c))
        except UnicodeDecodeError:
            try:
                unicode_content.append(c.decode("utf-8"))
            except Exception:
                print("{} is nor latin-1 nor utf-8.".format(files[i]))
                raise

    # Compress
    if compress:
        content = compress_string(b"".join(
            [c.encode("utf-8") for c in unicode_content]))
        response["Content-Encoding"] = "gzip"
        response["Content-Length"] = str(len(content))

    response.write(content)
    timeout = 3600 * 24 * 10
    patch_response_headers(response, timeout)
    if not response.has_header("Last-Modified"):
        response["Last-Modified"] = http_date()
    cache.set(
        cacheKey,
        {
            "Last-Modified": response["Last-Modified"],
            "ETag": response.get("ETag", "")
        },
    )
    return response
Example #26
0
 def __init__(self, path: _t.Text, method: _t.Text, response: HttpResponse):
     super().__init__(path=path,
                      status=response.status_code,
                      data=self._get_rendered(response),
                      method=method)
     self.timing = float(response.get('Response-Time', '0.0'))
Example #27
0
 def test_dict_behavior(self):
     """
     Test for bug #14020: Make HttpResponse.get work like dict.get
     """
     r = HttpResponse()
     self.assertEqual(r.get("test"), None)
Example #28
0
def gzip_compressor(request):
    plugins = split_commas(request.GET.get("plugins", ""))
    languages = split_commas(request.GET.get("languages", ""))
    themes = split_commas(request.GET.get("themes", ""))
    isJS = request.GET.get("js", "") == "true"
    compress = request.GET.get("compress", "true") == "true"
    suffix = request.GET.get("suffix", "") == "_src" and "_src" or ""
    content = []

    response = HttpResponse()
    response["Content-Type"] = "text/javascript"

    if not isJS:
        response.write(render_to_string('tinymce/tiny_mce_gzip.js', {
            'base_url': tinymce_settings.JS_BASE_URL,
        }, context_instance=RequestContext(request)))
        return response

    patch_vary_headers(response, ['Accept-Encoding'])

    now = datetime.utcnow()
    response['Date'] = now.strftime('%a, %d %b %Y %H:%M:%S GMT')

    cacheKey = '|'.join(plugins + languages + themes)
    cacheData = cache.get(cacheKey)

    if not cacheData is None:
        if 'ETag' in cacheData:
            if_none_match = request.META.get('HTTP_IF_NONE_MATCH', None)
            if if_none_match == cacheData['ETag']:
                response.status_code = 304
                response.content = ''
                response['Content-Length'] = '0'
                return response

        if 'Last-Modified' in cacheData:
            if_modified_since = request.META.get('HTTP_IF_MODIFIED_SINCE', None)
            if if_modified_since == cacheData['Last-Modified']:
                response.status_code = 304
                response.content = ''
                response['Content-Length'] = '0'
                return response

    content.append("var tinyMCEPreInit={base:'%s',suffix:''};" % tinymce_settings.JS_BASE_URL);

    # Add core
    files = ["tiny_mce"]

    # Add core languages
    for lang in languages:
        files.append("langs/%s" % lang)

    # Add plugins
    for plugin in plugins:
        files.append("plugins/%s/editor_plugin%s" % (plugin, suffix))

        for lang in languages:
            files.append("plugins/%s/langs/%s" % (plugin, lang))

    # Add themes
    for theme in themes:
        files.append("themes/%s/editor_template%s" % (theme, suffix))

        for lang in languages:
            files.append("themes/%s/langs/%s" % (theme, lang))

    for f in files:
        # Check for unsafe characters
        if not safe_filename_re.match(f):
            continue
        content.append(get_file_contents("%s.js" % f))

    # Restore loading functions
    content.append('tinymce.each("%s".split(","), function(f){'
                   'tinymce.ScriptLoader.markDone(tinyMCE.baseURL+'
                   '"/"+f+".js");});' % ",".join(files))

    unicode_content = []
    for i, c in enumerate(content):
        try:
            unicode_content.append(c.decode('latin-1'))
        except UnicodeDecodeError:
            try:
                unicode_content.append(c.decode('utf-8'))
            except:
                print("%s is nor latin-1 nor utf-8." % files[i])
                raise

    # Compress
    if compress:
        content = compress_string(''.join([c.encode('utf-8')
                                           for c in unicode_content]))
        response['Content-Encoding'] = 'gzip'
        response['Content-Length'] = str(len(content))

    response.write(content)
    timeout = 3600 * 24 * 10
    patch_response_headers(response, timeout)
    cache.set(cacheKey, {
        'Last-Modified': response['Last-Modified'],
        'ETag': response.get('ETag', ''),
    })
    return response
Example #29
0
def gzip_compressor(request):
    plugins = split_commas(request.GET.get("plugins", ""))
    languages = split_commas(request.GET.get("languages", ""))
    themes = split_commas(request.GET.get("themes", ""))
    isJS = request.GET.get("js", "") == "true"
    compress = request.GET.get("compress", "true") == "true"
    suffix = request.GET.get("suffix", "") == "_src" and "_src" or ""
    content = []

    response = HttpResponse()
    response["Content-Type"] = "text/javascript"

    if not isJS:
        response.write(render_to_string(template_name='tinymce/tiny_mce_gzip.js', context={
            'base_url': tinymce_settings.JS_BASE_URL,
        }, request=request))
        return response

    patch_vary_headers(response, ['Accept-Encoding'])

    now = datetime.utcnow()
    response['Date'] = now.strftime('%a, %d %b %Y %H:%M:%S GMT')

    cacheKey = '|'.join(plugins + languages + themes)
    cacheData = cache.get(cacheKey)

    if cacheData is not None:
        if 'ETag' in cacheData:
            if_none_match = request.META.get('HTTP_IF_NONE_MATCH', None)
            if if_none_match == cacheData['ETag']:
                response.status_code = 304
                response.content = ''
                response['Content-Length'] = '0'
                return response

        if 'Last-Modified' in cacheData:
            if_modified_since = request.META.get('HTTP_IF_MODIFIED_SINCE', None)
            if if_modified_since == cacheData['Last-Modified']:
                response.status_code = 304
                response.content = ''
                response['Content-Length'] = '0'
                return response

    content.append("var tinyMCEPreInit={base:'%s',suffix:''};" % tinymce_settings.JS_BASE_URL)

    # Add core
    files = ["tiny_mce"]

    # Add core languages
    for lang in languages:
        files.append("langs/%s" % lang)

    # Add plugins
    for plugin in plugins:
        files.append("plugins/%s/editor_plugin%s" % (plugin, suffix))

        for lang in languages:
            files.append("plugins/%s/langs/%s" % (plugin, lang))

    # Add themes
    for theme in themes:
        files.append("themes/%s/editor_template%s" % (theme, suffix))

        for lang in languages:
            files.append("themes/%s/langs/%s" % (theme, lang))

    for f in files:
        # Check for unsafe characters
        if not safe_filename_re.match(f):
            continue
        content.append(get_file_contents("%s.js" % f))

    # Restore loading functions
    content.append('tinymce.each("%s".split(","), function(f){'
                   'tinymce.ScriptLoader.markDone(tinyMCE.baseURL+'
                   '"/"+f+".js");});' % ",".join(files))

    unicode_content = []
    for i, c in enumerate(content):
        try:
            unicode_content.append(c.decode('latin-1'))
        except UnicodeDecodeError:
            try:
                unicode_content.append(c.decode('utf-8'))
            except:
                print("%s is nor latin-1 nor utf-8." % files[i])
                raise

    # Compress
    if compress:
        content = compress_string(''.join([c.encode('utf-8')
                                           for c in unicode_content]))
        response['Content-Encoding'] = 'gzip'
        response['Content-Length'] = str(len(content))

    response.write(content)
    timeout = 3600 * 24 * 10
    patch_response_headers(response, timeout)
    cache.set(cacheKey, {
        'Last-Modified': response['Last-Modified'],
        'ETag': response.get('ETag', ''),
    })
    return response
Example #30
0
 def test_dict_behavior(self):
     """
     Test for bug #14020: Make HttpResponse.get work like dict.get
     """
     r = HttpResponse()
     self.assertEqual(r.get('test'), None)
Example #31
0
def gzip_compressor(request):
    plugins = split_commas(request.GET.get("plugins", ""))
    languages = split_commas(request.GET.get("languages", ""))
    themes = split_commas(request.GET.get("themes", ""))
    isJS = request.GET.get("js", "") == "true"
    compress = request.GET.get("compress", "true") == "true"
    suffix = request.GET.get("suffix", "") == "_src" and "_src" or ""
    content = []

    response = HttpResponse()
    response["Content-Type"] = "text/javascript"

    if not isJS:
        mce_config = tinymce.settings.DEFAULT_CONFIG
        compressor_config = {
                'plugins': mce_config.get('plugins', ''),
                'themes': mce_config.get('theme', 'advanced'),
                'languages': mce_config.get('language', 'en'),
                'diskcache': "true",
                'debug': "false",
            }
        response.write(render_to_string('tinymce/tiny_mce_gzip.js', {
            'base_url': tinymce.settings.JS_BASE_URL,
            'compressor_config' : compressor_config,
        }, context_instance=RequestContext(request)))
        return response

    patch_vary_headers(response, ['Accept-Encoding'])

    now = datetime.utcnow()
    response['Date'] = now.strftime('%a, %d %b %Y %H:%M:%S GMT')

    cacheKey = '|'.join(plugins + languages + themes)
    cacheData = cache.get(cacheKey)

    if not cacheData is None:
        if cacheData.has_key('ETag'):
            if_none_match = request.META.get('HTTP_IF_NONE_MATCH', None)
            if if_none_match == cacheData['ETag']:
                response.status_code = 304
                response.content = ''
                response['Content-Length'] = '0'
                return response

        if cacheData.has_key('Last-Modified'):
            if_modified_since = request.META.get('HTTP_IF_MODIFIED_SINCE', None)
            if if_modified_since == cacheData['Last-Modified']:
                response.status_code = 304
                response.content = ''
                response['Content-Length'] = '0'
                return response

    # Add core, with baseURL added
    content.append(get_file_contents("tiny_mce%s.js" % suffix))
    content.append('tinyMCE.baseURL="%s"' % tinymce.settings.JS_BASE_URL)

    # Patch loading functions
    content.append("tinyMCE_GZ.start();")

    # Add core languages
    for lang in languages:
        content.append(get_file_contents("langs/%s.js" % lang))

    # Add themes
    for theme in themes:
        content.append(get_file_contents("themes/%s/editor_template%s.js"
                % (theme, suffix)))

        for lang in languages:
            content.append(get_file_contents("themes/%s/langs/%s.js"
                    % (theme, lang)))

    # Add plugins
    for plugin in plugins:
        content.append(get_file_contents("plugins/%s/editor_plugin%s.js"
                % (plugin, suffix)))

        for lang in languages:
            content.append(get_file_contents("plugins/%s/langs/%s.js"
                    % (plugin, lang)))

    # Add filebrowser
    if tinymce.settings.USE_FILEBROWSER:
        content.append(render_to_string('tinymce/filebrowser.js', {},
            context_instance=RequestContext(request)).encode("utf-8"))

    # Restore loading functions
    content.append("tinyMCE_GZ.end();")

    print compress
    content = '\n'.join(content)
    # Compress
    if compress:
        content = compress_string(content)
        response['Content-Encoding'] = 'gzip'
        
    response['Content-Length'] = str(len(content))

    response.write(content)
    timeout = 3600 * 24 * 10
    patch_response_headers(response, timeout)
    cache.set(cacheKey, {
        'Last-Modified': response['Last-Modified'],
        'ETag': response.get('ETag', ''),
    })
    return response
Example #32
0
def gzip_compressor(request):
    plugins = split_commas(request.GET.get("plugins", ""))
    languages = split_commas(request.GET.get("languages", ""))
    themes = split_commas(request.GET.get("themes", ""))
    isJS = request.GET.get("js", "") == "true"
    compress = request.GET.get("compress", "true") == "true"
    suffix = request.GET.get("suffix", "") == "_src" and "_src" or ""
    content = []

    response = HttpResponse()
    response["Content-Type"] = "text/javascript"

    if not isJS:
        mce_config = tinymce.settings.DEFAULT_CONFIG
        compressor_config = {
            'plugins': mce_config.get('plugins', ''),
            'themes': mce_config.get('theme', 'advanced'),
            'languages': mce_config.get('language', 'en'),
            'diskcache': "true",
            'debug': "false",
        }
        response.write(
            render_to_string('tinymce/tiny_mce_gzip.js', {
                'base_url': tinymce.settings.JS_BASE_URL,
                'compressor_config': compressor_config,
            },
                             context_instance=RequestContext(request)))
        return response

    patch_vary_headers(response, ['Accept-Encoding'])

    now = datetime.utcnow()
    response['Date'] = now.strftime('%a, %d %b %Y %H:%M:%S GMT')

    cacheKey = '|'.join(plugins + languages + themes)
    cacheData = cache.get(cacheKey)

    if not cacheData is None:
        if cacheData.has_key('ETag'):
            if_none_match = request.META.get('HTTP_IF_NONE_MATCH', None)
            if if_none_match == cacheData['ETag']:
                response.status_code = 304
                response.content = ''
                response['Content-Length'] = '0'
                return response

        if cacheData.has_key('Last-Modified'):
            if_modified_since = request.META.get('HTTP_IF_MODIFIED_SINCE',
                                                 None)
            if if_modified_since == cacheData['Last-Modified']:
                response.status_code = 304
                response.content = ''
                response['Content-Length'] = '0'
                return response

    # Add core, with baseURL added
    content.append(get_file_contents("tiny_mce%s.js" % suffix))
    content.append('tinyMCE.baseURL="%s"' % tinymce.settings.JS_BASE_URL)

    # Patch loading functions
    content.append("tinyMCE_GZ.start();")

    # Add core languages
    for lang in languages:
        content.append(get_file_contents("langs/%s.js" % lang))

    # Add themes
    for theme in themes:
        content.append(
            get_file_contents("themes/%s/editor_template%s.js" %
                              (theme, suffix)))

        for lang in languages:
            content.append(
                get_file_contents("themes/%s/langs/%s.js" % (theme, lang)))

    # Add plugins
    for plugin in plugins:
        content.append(
            get_file_contents("plugins/%s/editor_plugin%s.js" %
                              (plugin, suffix)))

        for lang in languages:
            content.append(
                get_file_contents("plugins/%s/langs/%s.js" % (plugin, lang)))

    # Add filebrowser
    if tinymce.settings.USE_FILEBROWSER:
        content.append(
            render_to_string(
                'tinymce/filebrowser.js', {},
                context_instance=RequestContext(request)).encode("utf-8"))

    # Restore loading functions
    content.append("tinyMCE_GZ.end();")

    print compress
    content = '\n'.join(content)
    # Compress
    if compress:
        content = compress_string(content)
        response['Content-Encoding'] = 'gzip'

    response['Content-Length'] = str(len(content))

    response.write(content)
    timeout = 3600 * 24 * 10
    patch_response_headers(response, timeout)
    cache.set(
        cacheKey, {
            'Last-Modified': response['Last-Modified'],
            'ETag': response.get('ETag', ''),
        })
    return response
Example #33
0
 def test_dict_behavior(self):
     """
     Test for bug #14020: Make HttpResponse.get work like dict.get
     """
     r = HttpResponse()
     self.assertIsNone(r.get('test'))
Example #34
0
def gzip_compressor(request):
    plugins = split_commas(request.GET.get('plugins', ''))
    languages = split_commas(request.GET.get('languages', ''))
    themes = split_commas(request.GET.get('themes', ''))
    isJS = request.GET.get('js', '') == 'true'
    compress = request.GET.get('compress', 'true') == 'true'
    suffix = request.GET.get('suffix', '') == '_src' and '_src' or ''
    content = []

    response = HttpResponse()
    response['Content-Type'] = 'text/javascript'

    if not isJS:
        response.write(render_to_string('tinymce/tiny_mce_gzip.js', {
            'base_url': tinymce.settings.JS_BASE_URL,
        }))
        return response

    patch_vary_headers(response, ['Accept-Encoding'])

    now = datetime.utcnow()
    response['Date'] = now.strftime('%a, %d %b %Y %H:%M:%S GMT')

    cacheKey = '|'.join(plugins + languages + themes)
    cacheData = cache.get(cacheKey)

    if cacheData is not None:
        if 'ETag' in cacheData:
            if_none_match = request.META.get('HTTP_IF_NONE_MATCH')
            if if_none_match == cacheData['ETag']:
                response.status_code = 304
                response.content = ''
                response['Content-Length'] = '0'
                return response

        if 'Last-Modified' in cacheData:
            if_modified_since = request.META.get('HTTP_IF_MODIFIED_SINCE')
            if if_modified_since == cacheData['Last-Modified']:
                response.status_code = 304
                response.content = ''
                response['Content-Length'] = '0'
                return response

    tinyMCEPreInit = {
        'base': tinymce.settings.JS_BASE_URL,
        'suffix': '',
    }
    content.append('var tinyMCEPreInit={!s};'.format(
        json.dumps(tinyMCEPreInit)
    ))

    # Add core
    files = ['tiny_mce']

    # Add core languages
    for lang in languages:
        files.append('langs/{!s}'.format(lang))

    # Add plugins
    for plugin in plugins:
        files.append('plugins/{!s}/editor_plugin{!s}'.format(plugin, suffix))

        for lang in languages:
            files.append('plugins/{!s}/langs/{!s}'.format(plugin, lang))

    # Add themes
    for theme in themes:
        files.append('themes/{!s}/editor_template{!s}'.format(theme, suffix))

        for lang in languages:
            files.append('themes/{!s}/langs/{!s}'.format(theme, lang))

    for f in files:
        # Check for unsafe characters
        if not safe_filename_re.match(f):
            continue
        content.append(get_file_contents('{!s}.js'.format(f)))

    # Restore loading functions
    content.append('tinymce.each("{!s}".split(","), function(f){{'
                   'tinymce.ScriptLoader.markDone(tinyMCE.baseURL+'
                   '"/"+f+".js");}});'.format(','.join(files)))

    unicode_content = []
    for i, c in enumerate(content):
        try:
            unicode_content.append(c.decode('latin-1'))
        except AttributeError:
            # python 3 way
            unicode_content.append(smart_text(c))
        except UnicodeDecodeError:
            try:
                unicode_content.append(c.decode('utf-8'))
            except Exception:
                print('{!s} is nor latin-1 nor utf-8.'.format(files[i]))
                raise

    # Compress
    if compress:
        content = compress_string(b''.join([c.encode('utf-8')
                                           for c in unicode_content]))
        response['Content-Encoding'] = 'gzip'
        response['Content-Length'] = str(len(content))

    response.write(content)
    timeout = 3600 * 24 * 10
    patch_response_headers(response, timeout)
    if not response.has_header('Last-Modified'):
        # Last-Modified not set since Django 1.11
        response['Last-Modified'] = http_date()
    cache.set(cacheKey, {
        'Last-Modified': response['Last-Modified'],
        'ETag': response.get('ETag', ''),
    })
    return response