def test_valid_unicode_removed(self):
        q = '/?page=2345&q=%E6%9F%A5%E8%AF%A2'
        key = 'page'
        value = '2345'
        removed_key = 'q'

        assert key in remove_query_param(q, removed_key)
        assert value in remove_query_param(q, removed_key)
        assert '%' not in remove_query_param(q, removed_key)
Esempio n. 2
0
    def page_number_query(self, url, page_number):
        """
        Builds uri and adds page param.
        """
        url = remove_query_param(self.request.build_absolute_uri(url), '_')
        paginated_url = replace_query_param(url, self.page_query_param, page_number)

        if page_number == 1:
            return remove_query_param(paginated_url, self.page_query_param)

        return paginated_url
Esempio n. 3
0
 def get_first_link(self, display_page_query_param=True):
     url = self.request.build_absolute_uri()
     if display_page_query_param:
         page_number = self.page.paginator.validate_number(1)
         return replace_query_param(url, self.page_query_param, page_number)
     else:
         return remove_query_param(url, self.page_query_param)
 def get_previous_link(self):
     if not self.page.has_previous():
         return None
     url = self.request.build_absolute_uri()
     page_number = self.page.previous_page_number()
     if page_number == 1:
         return remove_query_param(url, self.page_query_param)
     return replace_query_param(url, self.page_query_param, page_number)
Esempio n. 5
0
    def get_first_link(self):
        """ Return the URL of the first paginated page """

        if not self.get_previous_link():
            return None

        return remove_query_param(
            self.request.build_absolute_uri(),
            self.offset_query_param
        )
Esempio n. 6
0
    def get_previous_link(self):
        if self.offset <= 0:
            return None

        url = self.request.build_absolute_uri()

        if self.offset - self.limit <= 0:
            return remove_query_param(url, self.offset_query_param)

        offset = self.offset - self.limit
        return replace_query_param(url, self.offset_query_param, offset)
Esempio n. 7
0
    def get_last_link(self):
        if self.count == 0:
            return None

        url = self.request.build_absolute_uri()
        url = replace_query_param(url, self.limit_query_param, self.limit)

        offset = self.count - self.limit

        if offset <= 0:
            return remove_query_param(url, self.offset_query_param)

        return replace_query_param(url, self.offset_query_param, offset)
Esempio n. 8
0
    def _get_paginate_link(self, page_number) -> str:
        """
        Args:
            page_number: int

        Returns:
            string: URL of the requested page

        """
        try:
            self.pagination.validate_number(page_number)
        except EmptyPage:
            return ''

        url = self.request.build_absolute_uri()
        if page_number == 1:
            return remove_query_param(url, 'page')
        return replace_query_param(url, 'page', page_number)
Esempio n. 9
0
 def get_all_links(self):
     paginator = self.page.paginator
     links = []
     base_url = self.request.build_absolute_uri()
     for page_number in paginator.page_range:
         s_link = ""
         if page_number == 1:
             s_link = remove_query_param(
                 base_url,
                 self.page_query_param
             )
         else:
             s_link = replace_query_param(
                 base_url,
                 self.page_query_param,
                 page_number
             )
         links.append({
             'page': page_number,
             'url': s_link
         })
     return links
Esempio n. 10
0
 def page_number_to_url(page_number):
     if page_number == 1:
         return remove_query_param(base_url, self.offset_query_param)
     else:
         offset = self.offset + ((page_number - current) * self.limit)
         return replace_query_param(base_url, self.offset_query_param, offset)
Esempio n. 11
0
 def get_first_link(self):
     if self.offset <= 0:
         return None
     url = self.request.get_full_path()
     return remove_query_param(url, self.offset_query_param)
Esempio n. 12
0
 def get_previous_link(self):
     url = self.request.build_absolute_uri()
     page_number = self.page_number - 1
     if page_number == 1:
         return remove_query_param(url, self.page_query_param)
     return replace_query_param(url, self.page_query_param, page_number)
Esempio n. 13
0
 def get_first_link(self):
     if not self.page.has_previous():
         return None
     else:
         url = self.request.build_absolute_uri()
         return remove_query_param(url, self.page_query_param)
Esempio n. 14
0
    def test_invalid_unicode(self):
        q = '/?from=login&page=2&%FF%FE%3C%73%63%72%69%70%74%3E%61%6C%65%72%74%28%33%31%33%29%3C%2F%73%63%72%69%70%74%3E=1'
        key = 'from'
        removed_key = 'page'

        assert key in remove_query_param(q, removed_key)
from rest_framework.utils.urls import remove_query_param, replace_query_param

url = 'http://www.google.com/abcd/?name=junaid&location=palakkal'

new_url = replace_query_param(url, 'name', 'javas')
print(new_url)

new_url = replace_query_param(new_url, 'location', 'thalappara')
print(new_url)

new_url = remove_query_param(new_url, 'location')
print(new_url)
Esempio n. 16
0
    def list(self, request, parent_lookup_question__flow=None):
        try:
            flow = Flow.objects.get(id=parent_lookup_question__flow)
        except (Flow.DoesNotExist, ValidationError):
            raise NotFound()

        answers = FlowResponse.objects.filter(flow=flow).order_by("timestamp")

        try:
            start_filter = request.query_params["filter[start-timestamp]"]
            start_filter = dateparse.parse_datetime(start_filter)
            assert start_filter is not None
            answers = answers.filter(timestamp__gt=start_filter)
        except (KeyError, ValueError, AssertionError):
            pass

        try:
            end_filter = request.query_params["filter[end-timestamp]"]
            end_filter = dateparse.parse_datetime(end_filter)
            assert end_filter is not None
            answers = answers.filter(timestamp__lte=end_filter)
        except (KeyError, ValueError, AssertionError):
            pass

        page_size = settings.REST_FRAMEWORK["PAGE_SIZE"]
        try:
            page_size = min(page_size, int(request.query_params["page[size]"]))
        except (KeyError, ValueError):
            pass

        has_next = False
        has_previous = False
        reversed = False
        try:
            after = request.query_params["page[afterCursor]"]
            after_answer = FlowResponse.objects.get(row_id_value=after,
                                                    flow=flow)
            answers = answers.filter(timestamp__gt=after_answer.timestamp)
            answers = answers.order_by("timestamp")
            has_previous = True
        except (KeyError, FlowResponse.DoesNotExist):
            pass

        try:
            before = request.query_params["page[beforeCursor]"]
            before_answer = FlowResponse.objects.get(row_id_value=before,
                                                     flow=flow)
            answers = answers.filter(timestamp__lt=before_answer.timestamp)
            answers = answers.order_by("-timestamp")
            reversed = True
            has_next = True
        except (KeyError, FlowResponse.DoesNotExist):
            pass

        answers = answers[:page_size + 1].values_list(
            "timestamp",
            "row_id_value",
            "row_id_type",
            "contact_id_value",
            "session_id_value",
            "question__id",
            "response_value",
            "response_metadata",
        )

        if len(answers) > page_size:
            answers = answers[:page_size]
            if reversed:
                has_previous = True
            else:
                has_next = True

        if reversed:
            answers = answers[::-1]

        if has_next:
            next_ = replace_query_param(request.build_absolute_uri(),
                                        "page[afterCursor]", answers[-1][1])
            next_ = remove_query_param(next_, "page[beforeCursor]")
        else:
            next_ = None

        if has_previous:
            previous = replace_query_param(request.build_absolute_uri(),
                                           "page[beforeCursor]", answers[0][1])
            previous = remove_query_param(previous, "page[afterCursor]")
        else:
            previous = None

        return Response({
            "data": {
                "type": "flow-results-data",
                "id": flow.id,
                "attributes": {
                    "responses": [[
                        a[0],
                        int(a[1])
                        if a[2] == FlowResponse.Type.INTEGER else a[1],
                        *a[3:],
                    ] for a in answers]
                },
                "relationships": {
                    "descriptor": {
                        "links": {
                            "self": request.build_absolute_uri()
                        }
                    },
                    "links": {
                        "self": request.build_absolute_uri(),
                        "next": next_,
                        "previous": previous,
                    },
                },
            }
        })
Esempio n. 17
0
 def _get_previous_link(self):
     url = self.request.build_absolute_uri()
     if self.page_num == 1:
         return remove_query_param(url, self.page_query_param)
     return replace_query_param(url, self.page_query_param,
                                self.page_num - 1)
Esempio n. 18
0
 def get_first_link(self):
     if self.offset <= 0:
         return None
     url = self.request.build_absolute_uri()
     return remove_query_param(url, self.offset_query_param)
Esempio n. 19
0
 def page_number_to_url(page_number):
     if page_number == 1:
         return remove_query_param(base_url, self.page_query_param)
     else:
         return replace_query_param(base_url, self.page_query_param, page_number)
Esempio n. 20
0
 def page_number_to_url(page_number):
     if page_number == 1:
         return remove_query_param(base_url, self.page_query_param)
     else:
         return replace_query_param(base_url, self.page_query_param,
                                    page_number)
Esempio n. 21
0
 def get_last_link(self):
     url = self.request.build_absolute_uri()
     page_number = self.page.paginator.page_range[-1]
     if page_number == 1:
         return remove_query_param(url, self.page_query_param)
     return replace_query_param(url, self.page_query_param, page_number)
Esempio n. 22
0
 def get_first_link(self):
     if self.count == 0:
         return None
     
     url = self.request.build_absolute_uri()
     return remove_query_param(url, self.offset_query_param)
    def test_invalid_unicode(self):
        q = '/?from=login&page=2&%FF%FE%3C%73%63%72%69%70%74%3E%61%6C%65%72%74%28%33%31%33%29%3C%2F%73%63%72%69%70%74%3E=1'
        key = 'from'
        removed_key = 'page'

        assert key in remove_query_param(q, removed_key)
Esempio n. 24
0
 def get_first_link(self):
     url = self.request.build_absolute_uri()
     return remove_query_param(url, self.page_query_param)
Esempio n. 25
0
 def get_first_link(self):
     if not self.page.has_previous():
         return None
     url = self.request.build_absolute_uri()
     return remove_query_param(url, self.page_query_param)
Esempio n. 26
0
 def page_number_to_url(page_number):
     if page_number == 1:
         return remove_query_param(base_url, self.offset_query_param)
     else:
         offset = self.offset + ((page_number - current) * self.limit)
         return replace_query_param(base_url, self.offset_query_param, offset)
Esempio n. 27
0
    def get(self, request, *args, **kwargs):
        """获取分享目录网页"""
        lang_code = request.GET.get(LANGUAGE_QUERY_PARAMETER)
        if lang_code:
            try:
                lang_code = translation.get_supported_language_variant(lang_code)
                translation.activate(lang_code)
                next_url = self.request.build_absolute_uri()
                next_url = remove_query_param(next_url, LANGUAGE_QUERY_PARAMETER)
                return set_language_redirect(lang_code=lang_code, next_url=next_url)
            except LookupError:
                pass

        share_base = kwargs.get(self.lookup_field, '')

        pp = PathParser(filepath=share_base)
        bucket_name, dir_base = pp.get_bucket_and_dirpath()
        if not bucket_name:
            return render(request, 'info.html', context={'code': 400, 'code_text': _('分享路径无效')})

        # 存储桶验证和获取桶对象
        h_manager = HarborManager()
        try:
            bucket = h_manager.get_bucket(bucket_name=bucket_name)
            if not bucket:
                return render(request, 'info.html', context={'code': 404, 'code_text': _('存储桶不存在')})

            if dir_base:
                base_obj = h_manager.get_metadata_obj(table_name=bucket.get_bucket_table_name(), path=dir_base)
                if not base_obj:
                    return render(request, 'info.html', context={'code': 404, 'code_text': _('分享根目录不存在')})
            else:
                base_obj = None

            # 是否有文件对象的访问权限
            if not self.has_access_permission(bucket=bucket, base_dir_obj=base_obj):
                return render(request, 'info.html', context={'code': 403, 'code_text': _('您没有访问权限')})
        except HarborError as e:
            return render(request, 'info.html', context=e.err_data_old())

        # 无分享密码
        if (not base_obj) or (not base_obj.has_share_password()):
            return render(request, 'share.html', context={
                'share_base': share_base, 'share_user': bucket.user.username, 'share_code': None})

        # 验证分享密码
        p = request.GET.get('p', None)
        if p:
            data = QueryDict(mutable=True)
            data.setlist('password', [p])
            form = SharePasswordForm(data)  # 模拟post请求数据
            if base_obj.check_share_password(p):
                return render(request, 'share.html', context={
                    'share_base': share_base, 'share_user': bucket.user.username, 'share_code': p})
            else:
                form.is_valid()
                form.add_error('password', error=_('分享密码有误'))
        else:
            form = SharePasswordForm()

        content = {
            'form_title': _('验证分享密码'),
            'submit_text': _('确定'),
            'form': form,
            'share_base': share_base,
            'share_user': bucket.user.username
        }
        return render(request, 'share_form.html', context=content)