Example #1
0
class FS(View):
    pagination_by = 18
    page_sign = 'page'
    path_sign = 'path'
    format_sign = 'format'


    def get_fs(self):
        self.model = FsUrlModel()
        self.model.into(self.path)
        return self.model

    def get_context_data(self, **kwargs):
        fs = self.get_fs()
        paginator = Paginator(fs.dir_url, self.pagination_by)
        page = paginator.page(self.page)
        return dict(dir=page.object_list, page=page, back_path=fs.back_path)

    def get_arg(self, request, sign, kwargs, to_type=None, default=None):
        arg = kwargs.get(sign, None) or request.GET.get(sign, None)
        if to_type and not isinstance(arg, to_type) and arg:
            arg = to_type(arg)
        return arg or default

    def get_args(self, request, *args, **kwargs):
        self.page = self.get_arg(request, self.page_sign, kwargs, to_type=int, default=1)
        self.path = self.get_arg(request, self.path_sign, kwargs, default="")
        self.format = self.get_arg(request, self.format_sign, kwargs, default="html")

    def page_to_dict(self, page):
        next_page = page.has_next() and page.next_page_number() or False
        previous_page = page.has_previous() and page.previous_page_number() or False
        current = page.number
        total = page.paginator.num_pages
        return dict(next=next_page, previous=previous_page, current=current, all=total)

    def render_to_json(self):
        context = self.get_context_data()
        context['dir'] = map(lambda i: i.to_dict(), context['dir'])
        context['page'] = self.page_to_dict(context['page'])
        return HttpResponse(dumps(context),
                            content_type='application/json')

    def ishtml(self):
        return self.format == "html"

    def get(self, request, *args, **kwargs):
        self.get_args(request, *args, **kwargs)
        return self.render_to_json()
Example #2
0
 def get_fs(self):
     self.model = FsUrlModel()
     self.model.into(self.path)
     return self.model