Beispiel #1
0
def save_categories(request, slug):
    if request.method == 'POST':
        post = get_object_or_404(request.site['posts'], slug=slug)
        categories = request.POST['value']
        post['categories'].delete()
        for category in categories.split(","):
            category, created = Category.query().get_or_create(
                name=category.strip().lower())
            post['categories'].get_or_create(category=category)
    return HttpResponse(post.get_categories())
Beispiel #2
0
    def request_response(self, request):
        """
        Function to handle client requests, returning a HttpResponse object.
        """

        # Calling middleware methods for request processing before to call the view function
        request._response_middleware = []
        for middleware in self._middleware_instances:
            if hasattr(middleware, 'process_response'):
                request._response_middleware.insert(0, middleware.process_response)

            if hasattr(middleware, 'process_request'):
                resp = middleware.process_request(request)
                if resp is not None:
                    resp.request = request
                    return resp

        try:
            url, args, kwargs = resolve_url(request.path_info[1:], self.urls)

            kwargs.update(url.params)
            #kwargs['request'] = request # Commented to let request argument to be the first no-named arg

            args, kwargs = resolve_lazy_params(request, args, kwargs)
            args = (request,) + args

            resp = url.view(*args, **kwargs)

            if not isinstance(resp, HttpResponse):
                resp = HttpResponse(resp)
        except URLNotFound as e:
            raise Http404(request)

        # Attach the request to the response to keep it in memory, joined
        resp.request = request

        return resp
    def process_request(self, request):
        # Only for debug mode?
        if settings.DEBUG and not app_settings.ONLY_IN_DEBUG:
            return

        # Mockup views
        for mockup in MockupView.query().filter(is_active=True).order_by('creation_date'):
            match = re.match(mockup['url_pattern'], request.path_info)
            if not match:
                continue

            # Default response
            response = HttpResponse()
            response.content = mockup['content']
            response.status_code = mockup['status_code']
            response.headers.update(dict([l.split(':',1) for l in mockup['headers']]))
            response.mime_type = mockup['mime_type']

            # For given Python code
            if mockup['python_code']:
                code = []
                code.append('def func(request, *args, **kwargs):')
                code.extend(['    '+l for l in mockup['python_code'].split('\n')])
                code.append('response = func(request, *args, **kwargs)')
                global_vars = {'request': request, 'args':match.groups(), 'kwargs':match.groupdict()}
                local_vars = {} # This dictionary receives the response instance
                eval(compile('\n'.join(code), '<string>', 'exec'), global_vars, local_vars)

                if isinstance(local_vars['response'], HttpResponse):
                    response = local_vars['response']
                else:
                    response.content = local_vars['response']

            # For a raw uploaded file
            elif mockup['raw_file']:
                response.content = mockup['raw_file'].read()

            if response.status_code != 404:
                return response
Beispiel #4
0
def save_name(request, slug):
    post = get_object_or_404(request.site['posts'], slug=slug)
    post['name'] = request.POST['value']
    post.save()
    return HttpResponse(post['name'])
Beispiel #5
0
def get_markdown(request, slug):
    post = get_object_or_404(request.site['posts'], slug=slug)
    source = post['source'] if post['source'] is not None else ''
    return HttpResponse(source)