Beispiel #1
0
 def test_smart_urlquote(self):
     items = (
         ("http://öäü.com/", "http://xn--4ca9at.com/"),
         ("http://öäü.com/öäü/",
          "http://xn--4ca9at.com/%C3%B6%C3%A4%C3%BC/"),
         # Everything unsafe is quoted, !*'();:@&=+$,/?#[]~ is considered
         # safe as per RFC.
         (
             "http://example.com/path/öäü/",
             "http://example.com/path/%C3%B6%C3%A4%C3%BC/",
         ),
         ("http://example.com/%C3%B6/ä/",
          "http://example.com/%C3%B6/%C3%A4/"),
         ("http://example.com/?x=1&y=2+3&z=",
          "http://example.com/?x=1&y=2+3&z="),
         ("http://example.com/?x=<>\"'",
          "http://example.com/?x=%3C%3E%22%27"),
         (
             "http://example.com/?q=http://example.com/?x=1%26q=django",
             "http://example.com/?q=http%3A%2F%2Fexample.com%2F%3Fx%3D1%26q%3D"
             "django",
         ),
         (
             "http://example.com/?q=http%3A%2F%2Fexample.com%2F%3Fx%3D1%26q%3D"
             "django",
             "http://example.com/?q=http%3A%2F%2Fexample.com%2F%3Fx%3D1%26q%3D"
             "django",
         ),
         ("http://.www.f oo.bar/", "http://.www.f%20oo.bar/"),
     )
     # IDNs are properly quoted
     for value, output in items:
         with self.subTest(value=value, output=output):
             self.assertEqual(smart_urlquote(value), output)
Beispiel #2
0
def clean_content_url(url, content_type):
    """
    埋め込みタイプのコンテンツのURLをcleanする
        - remove_query_string:  `?`以降のクエリストリングを削除
        - remove_hash_string:   `#`以降のハッシュストリングを削除
        - remove_last_slash:    URLの最後の`/`を削除
    """
    def remove_query_string(url):
        splitted_url = url.split("?")
        if len(splitted_url) > 1:
            url = splitted_url[0]
        return url

    def remove_hash_string(url):
        splitted_url = url.split("#")
        if len(splitted_url) > 1:
            url = splitted_url[0]
        return url

    def remove_last_slash(url):
        if url[-1] == "/":
            url = url[:-1]
        return url

    if not url:
        return url
    # URLエンコーディング
    url = smart_urlquote(url)
    # 埋め込みタイプなら、不要な文字列を削除(WEBの場合は不要)
    if content_type in EMBED_TYPES and content_type != WEB:
        url = remove_query_string(url)
        url = remove_hash_string(url)
        url = remove_last_slash(url)

    return url
Beispiel #3
0
def gravatar_for_email(email, size=None, rating=None):
    """
    Generates a Gravatar URL for the given email address.

    Syntax::

        {% gravatar_for_email <email> [size] [rating] %}

    Example::

        {% gravatar_for_email [email protected] 48 pg %}
    """
    gravatar_url = "%savatar/%s" % (GRAVATAR_URL_PREFIX,
            _get_gravatar_id(email))

    parameters = [p for p in (
        ('d', GRAVATAR_DEFAULT_IMAGE),
        ('s', size or GRAVATAR_DEFAULT_SIZE),
        ('r', rating or GRAVATAR_DEFAULT_RATING),
    ) if p[1]]

    if parameters:
        gravatar_url += '?' + urllib.urlencode(parameters, doseq=True)

    return smart_urlquote(gravatar_url)
Beispiel #4
0
 def get_context(self, name, value, attrs):
     context = super().get_context(name, value, attrs)
     context['current_label'] = _('Currently:')
     context['change_label'] = _('Change:')
     context['widget']['href'] = smart_urlquote(
         context['widget']['value']) if value else ''
     return context
Beispiel #5
0
 def get_context(self, name, value, attrs):
     context = super().get_context(name, value, attrs)
     context["current_label"] = _("Currently:")
     context["change_label"] = _("Change:")
     context["widget"]["href"] = (smart_urlquote(context["widget"]["value"])
                                  if value else "")
     return context
Beispiel #6
0
 def test_smart_urlquote(self):
     items = (
         ('http://öäü.com/', 'http://xn--4ca9at.com/'),
         ('http://öäü.com/öäü/',
          'http://xn--4ca9at.com/%C3%B6%C3%A4%C3%BC/'),
         # Everything unsafe is quoted, !*'();:@&=+$,/?#[]~ is considered
         # safe as per RFC.
         ('http://example.com/path/öäü/',
          'http://example.com/path/%C3%B6%C3%A4%C3%BC/'),
         ('http://example.com/%C3%B6/ä/',
          'http://example.com/%C3%B6/%C3%A4/'),
         ('http://example.com/?x=1&y=2+3&z=',
          'http://example.com/?x=1&y=2+3&z='),
         ('http://example.com/?x=<>"\'',
          'http://example.com/?x=%3C%3E%22%27'),
         ('http://example.com/?q=http://example.com/?x=1%26q=django',
          'http://example.com/?q=http%3A%2F%2Fexample.com%2F%3Fx%3D1%26q%3Ddjango'
          ),
         ('http://example.com/?q=http%3A%2F%2Fexample.com%2F%3Fx%3D1%26q%3Ddjango',
          'http://example.com/?q=http%3A%2F%2Fexample.com%2F%3Fx%3D1%26q%3Ddjango'
          ),
     )
     # IDNs are properly quoted
     for value, output in items:
         with self.subTest(value=value, output=output):
             self.assertEqual(smart_urlquote(value), output)
Beispiel #7
0
def urlize_quoted_hrefs(text, trim_url_limit=None, nofollow=True, autoescape=True):
    """
    Converts href in text into clickable links. If trim_url_limit is not None, the URLs in link
    text longer than this limit will truncated to trim_url_limit-3 characters and appended with
    an ellipsis. If nofollow is True, the URLs in link text will get a rel="nofollow" attribute.

    """
    def trim_url(x, limit=trim_url_limit):
        return limit is not None and (len(x) > limit and ('%s...' % x[:max(0, limit - 3)])) or x

    safe_input = isinstance(text, SafeData)
    words = word_split_re.split(force_text(text))
    for i, word in enumerate(words):
        if '/pulp/api/v3/' in word:
            # Deal with punctuation.
            lead, middle, trail = '', word, ''
            for punctuation in TRAILING_PUNCTUATION:
                if middle.endswith(punctuation):
                    middle = middle[:-len(punctuation)]
                    trail = punctuation + trail
            for opening, closing in WRAPPING_PUNCTUATION:
                if middle.startswith(opening):
                    middle = middle[len(opening):]
                    lead = lead + opening
                # Keep parentheses at the end only if they're balanced.
                if middle.endswith(closing) and \
                   middle.count(closing) == middle.count(opening) + 1:
                    middle = middle[:-len(closing)]
                    trail = closing + trail

            # Make URL we want to point to.
            url = None
            nofollow_attr = ' rel="nofollow"' if nofollow else ''

            if href_re.match(middle):
                url = smart_urlquote(middle)

            # Check if it's a real URL
            if url and ("{" in url or "%7B" in url):
                url = None

            # Make link.
            if url:
                trimmed = trim_url(middle)
                if autoescape and not safe_input:
                    lead, trail = escape(lead), escape(trail)
                    url, trimmed = escape(url), escape(trimmed)
                middle = '<a href="%s"%s>%s</a>' % (url, nofollow_attr, trimmed)
                words[i] = mark_safe('%s%s%s' % (lead, middle, trail))
            else:
                if safe_input:
                    words[i] = mark_safe(word)
                elif autoescape:
                    words[i] = escape(word)
        elif safe_input:
            words[i] = mark_safe(word)
        elif autoescape:
            words[i] = escape(word)
    return ''.join(words)
Beispiel #8
0
 def append(value):
     if value:
         output = '<a href="{href}" class="add-on" target="_blank"><i class="icon-globe"></i></a>'
         href = force_str(value)
         return format_html(output, href=smart_urlquote(href))
     else:
         output = '<span class="add-on"><i class="icon-globe"></i></span>'
         return format_html(output)
Beispiel #9
0
 def get_unityroom_embed_html(self):
     # プロジェクトURLのHTML取得
     html = self.get_bs4_html()
     # aタグからWebGLのURLを取得
     a_tag = html.select_one("div.spec-games-center a[target='_blank']")
     safe_webgl_url = smart_urlquote(a_tag.get("href"))
     # 埋め込み用のHTMLを生成
     embed_html = f'<iframe src="{safe_webgl_url}" width="560" height="358" scrolling="no" frameborder="0" allowfullscreen></iframe>'  # noqa: E501
     return embed_html
Beispiel #10
0
 def render(self, name, value, attrs=None):
     html = super(AdminURLFieldWidget, self).render(name, value, attrs)
     if value:
         value = force_text(self._format_value(value))
         final_attrs = {'href': mark_safe(smart_urlquote(value))}
         html = format_html(
             '<p class="url"><a {0} target="_blank">Visit this site</a><br />{1}</p>',
             flatatt(final_attrs), html)
     return html
Beispiel #11
0
 def render(self, name, value, attrs=None):
     html = super(AdminURLFieldWidget, self).render(name, value, attrs)
     if value:
         value = force_text(self._format_value(value))
         final_attrs = {'href': smart_urlquote(value)}
         html = format_html('<p class="url">{} <a{}>{}</a><br />{} {}</p>',
                            _('Currently:'), flatatt(final_attrs), value,
                            _('Change:'), html)
     return html
Beispiel #12
0
def smart_urlquote_wrapper(matched_url):
    """
    Simple wrapper for smart_urlquote. ValueError("Invalid IPv6 URL") can
    be raised here, see issue #1386
    """
    try:
        return smart_urlquote(matched_url)
    except ValueError:
        return None
Beispiel #13
0
 def render(self, name, value, attrs=None):
     html = super(AdminURLFieldWidget, self).render(name, value, attrs)
     if value:
         value = force_text(self._format_value(value))
         final_attrs = {"href": mark_safe(smart_urlquote(value))}
         html = format_html(
             '<p class="url"><a {0} target="_blank">Visit this site</a><br />{1}</p>', flatatt(final_attrs), html
         )
     return html
def smart_urlquote_wrapper(matched_url):
    """
    Simple wrapper for smart_urlquote. ValueError("Invalid IPv6 URL") can
    be raised here, see issue #1386
    """
    try:
        return smart_urlquote(matched_url)
    except ValueError:
        return None
Beispiel #15
0
 def render(self, name, value, attrs=None):
     html = super().render(name, value, attrs)
     if value:
         url = '{}{}'.format(settings.ISSUE_TRACKER_URL, value)
         final_attrs = {'href': smart_urlquote(url), 'target': '_blank'}
         html = format_html(
             '<div class="ticket-url">{}<a{}>{}</a></div>',
             html, flatatt(final_attrs), url,
         )
     return html
Beispiel #16
0
 def render(self, name, value, attrs=None):
     markup = super(widgets.AdminURLFieldWidget, self).render(
         name, value, attrs)
     if value:
         value = force_text(self.format_value(value))
         final_attrs = {'href': html.smart_urlquote(value)}
         markup = html.format_html(
             '<p class="url">{}<br />{} <a{}>{}</a></p>',
             markup, _('Currently:'), flatatt(final_attrs), value,
         )
     return markup
Beispiel #17
0
 def render(self, name, value, attrs=None):
     html = super(AdminURLFieldWidget, self).render(name, value, attrs)
     if value:
         value = force_text(self._format_value(value))
         final_attrs = {'href': mark_safe(smart_urlquote(value))}
         html = format_html(
             '<p class="url">{0} <a {1}>{2}</a><br />{3} {4}</p>',
             _('Currently:'), flatatt(final_attrs), value,
             _('Change:'), html
         )
     return html
Beispiel #18
0
 def get_context(self, name, value, attrs):
     try:
         self.validator(value if value else '')
         url_valid = True
     except ValidationError:
         url_valid = False
     context = super().get_context(name, value, attrs)
     context['current_label'] = _('Currently:')
     context['change_label'] = _('Change:')
     context['widget']['href'] = smart_urlquote(context['widget']['value']) if value else ''
     context['url_valid'] = url_valid
     return context
Beispiel #19
0
 def get_context(self, name, value, attrs):
     try:
         self.validator(value if value else '')
         url_valid = True
     except ValidationError:
         url_valid = False
     context = super().get_context(name, value, attrs)
     context['current_label'] = _('Currently:')
     context['change_label'] = _('Change:')
     context['widget']['href'] = smart_urlquote(context['widget']['value']) if value else ''
     context['url_valid'] = url_valid
     return context
Beispiel #20
0
    def render(self, name, value, attrs=None):
        if value is None:
            return None

        final_attrs = self.build_attrs(attrs)
        href = smart_urlquote(value)
        text = self.text or href
        return format_html(
            '<a href="{href}" {attrs}>{text}</a>',
            href=href,
            attrs=flatatt(final_attrs),
            text=force_str(text),
        )
Beispiel #21
0
    def get_context(self, name, value, attrs):
        try:
            self.validator(value if value else '')
            url_valid = True
        except ValidationError:
            url_valid = False
        context = super().get_context(name, value, attrs)
        context['current_label'] = _('Currently:')
        context['change_label'] = _('Change:')
        context['url_valid'] = url_valid

        if url_valid is True:
            http_urls = s3_to_http_schema(value, True)

            if http_urls["distributed"] is not None:
                context['distributed_label'] = _('Distributed:')
                context['distributed_value'] = smart_urlquote(
                    http_urls["distributed"])

            context['widget']['href'] = smart_urlquote(http_urls["pre_signed"])

        return context
Beispiel #22
0
def parse_url(word):
    """
    If word is url, return a parsed version of it.
    :param word: string
    :return: None or parsed url
    """
    url = None
    if simple_url_re.match(word):
        url = smart_urlquote(word)
    elif simple_url_2_re.match(word):
        url = smart_urlquote('http://%s' % word)
    elif not ':' in word and simple_email_re.match(word):
        local, domain = word.rsplit('@', 1)
        try:
            domain = domain.encode('idna').decode('ascii')
        except UnicodeError:
            return

        url = 'mailto:%s@%s' % (local, domain)

    if url:
        return urlparse.urlparse(url)
Beispiel #23
0
 def get_context(self, name, value, attrs):
     try:
         self.validator(value if value else "")
         url_valid = True
     except ValidationError:
         url_valid = False
     context = super().get_context(name, value, attrs)
     context["current_label"] = _("Currently:")
     context["change_label"] = _("Change:")
     context["widget"]["href"] = (smart_urlquote(context["widget"]["value"])
                                  if value else "")
     context["url_valid"] = url_valid
     return context
Beispiel #24
0
 def render(self, name, value, attrs=None):
     if value:
         value = self.get_url(value)
     html = super(ForumToolsIDFieldWidget, self).render(name, value, attrs)
     if value:
         value = force_text(value)
         final_attrs = {'href': smart_urlquote(value)}
         html = format_html(
             '<p class="url">{0} <a{1}>{2}</a><br />{3} {4}</p>',
             _('Currently:'), flatatt(final_attrs), value,
             _('Change:'), html
         )
     return html
Beispiel #25
0
def parse_url(word):
    """
    If word is url, return a parsed version of it.
    :param word: string
    :return: None or parsed url
    """
    url = None
    if simple_url_re.match(word):
        url = smart_urlquote(word)
    elif simple_url_2_re.match(word):
        url = smart_urlquote('http://%s' % word)
    elif not ':' in word and simple_email_re.match(word):
        local, domain = word.rsplit('@', 1)
        try:
            domain = domain.encode('idna').decode('ascii')
        except UnicodeError:
            return

        url = 'mailto:%s@%s' % (local, domain)

    if url:
        return urlparse.urlparse(url)
Beispiel #26
0
 def render(self, name, value, attrs=None):
     markup = super(widgets.AdminURLFieldWidget,
                    self).render(name, value, attrs)
     if value:
         value = force_text(self.format_value(value))
         final_attrs = {'href': html.smart_urlquote(value)}
         markup = html.format_html(
             '<p class="url">{}<br />{} <a{}>{}</a></p>',
             markup,
             _('Currently:'),
             flatatt(final_attrs),
             value,
         )
     return markup
Beispiel #27
0
 def render(self, name, value, attrs=None):
     html = super(AdminURLFieldWidget, self).render(name, value, attrs)
     if value:
         value = force_text(self._format_value(value))
         final_attrs = {"href": smart_urlquote(value)}
         html = format_html(
             '<p class="url">{} <a{}>{}</a><br />{} {}</p>',
             _("Currently:"),
             flatatt(final_attrs),
             value,
             _("Change:"),
             html,
         )
     return html
Beispiel #28
0
    def render(self, name, value, attrs=None):
        if value is None:
            return format_html(
                '<p class="text-error">{text}</p>',
                text=_('Populate form fields above'),
            )

        final_attrs = self.build_attrs(attrs)
        href = smart_urlquote(value)
        text = self.text or href
        return format_html(
            '<p><a href="{href}" {attrs}>{text}</a></p>',
            href=href,
            attrs=flatatt(final_attrs),
            text=force_str(text),
        )
Beispiel #29
0
    def get_sketchfab_embed_html(self):
        # プロジェクトURLのHTML取得
        html = self.get_bs4_html()
        # プロジェクト投稿ユーザーのSketchfabURL取得
        safe_user_link = smart_urlquote(
            html.select_one("a.user-name__link").get("href"))
        # プロジェクトURLの32文字のID取得
        project_id = re.search(r"[a-f0-9]{32}", self.encoded_url).group(0)

        iframe_html = 'main/sketchfab_iframe.html'
        context = {
            "project_id": project_id,
            "encoded_url": self.encoded_url,
            "user_link": safe_user_link
        }
        embed_html = render_to_string(iframe_html, context)
        return embed_html
    def _subpage_crawler(self):
        """Create the list of products by finding the link of each product page"""
        while True:
            try:
                family = self.product_families.pop()
            except IndexError:
                break

            with closing(urlopen(self.base_url + family)) as product_list_page:
                product_list_soup = BeautifulSoup(product_list_page,
                                                  'html.parser')
                product_list = product_list_soup.find_all('table',
                                                          bgcolor='#EFEEE4')
                for product in product_list:
                    product_url = product.find('a').get('href')
                    product_url = '/' + product_url.split('/')[1]
                    product_url = smart_urlquote(product_url)
                    self.product_links[product_url] = family
Beispiel #31
0
 def test_smart_urlquote(self):
     items = (
         ('http://öäü.com/', 'http://xn--4ca9at.com/'),
         ('http://öäü.com/öäü/', 'http://xn--4ca9at.com/%C3%B6%C3%A4%C3%BC/'),
         # Everything unsafe is quoted, !*'();:@&=+$,/?#[]~ is considered
         # safe as per RFC.
         ('http://example.com/path/öäü/', 'http://example.com/path/%C3%B6%C3%A4%C3%BC/'),
         ('http://example.com/%C3%B6/ä/', 'http://example.com/%C3%B6/%C3%A4/'),
         ('http://example.com/?x=1&y=2+3&z=', 'http://example.com/?x=1&y=2+3&z='),
         ('http://example.com/?x=<>"\'', 'http://example.com/?x=%3C%3E%22%27'),
         ('http://example.com/?q=http://example.com/?x=1%26q=django',
          'http://example.com/?q=http%3A%2F%2Fexample.com%2F%3Fx%3D1%26q%3Ddjango'),
         ('http://example.com/?q=http%3A%2F%2Fexample.com%2F%3Fx%3D1%26q%3Ddjango',
          'http://example.com/?q=http%3A%2F%2Fexample.com%2F%3Fx%3D1%26q%3Ddjango'),
     )
     # IDNs are properly quoted
     for value, output in items:
         with self.subTest(value=value, output=output):
             self.assertEqual(smart_urlquote(value), output)
Beispiel #32
0
 def get_context(self, name, value, attrs):
     context = super(AdminURLFieldWidget, self).get_context(name, value, attrs)
     context['current_label'] = _('Currently:')
     context['change_label'] = _('Change:')
     context['widget']['href'] = smart_urlquote(context['widget']['value']) if value else ''
     return context
Beispiel #33
0
    def _product_crawler(self):
        from products.models import Product, Picture, Price

        # Return the price in the right format
        def _to_decimal(s):
            from decimal import Decimal as D
            return D(s.strip().replace(u'€', '').replace(',',
                                                         '.').replace(' ', ''))

        while True:
            try:
                product_url, category = self.product_links.popitem()
            except KeyError:
                break
            try:
                with closing(urlopen(product_url)) as product_page:
                    product_soup = BeautifulSoup(product_page, 'html.parser')
            except HTTPError:
                print 'error loading page for object at url', self.base_url + product_url

            # Get the image
            if product_soup.find('img', class_='alignleft wp-post-image'):
                image_url = product_soup.find(
                    'img', class_='alignleft wp-post-image').get('src')
            elif product_soup.find('a', class_='lightbox'):
                image_url = product_soup.find('a',
                                              class_='lightbox').get('href')
            else:
                image_url = 'http://www.axxlocations.fr/wp-content/themes/axxlocations/img/logo.jpg'

            image_url = smart_urlquote(image_url)

            # Get the title
            infosProduits = product_soup.find(
                'article', class_=re.compile('post[0-9]*')).find('h1').text

            # Get the description
            try:
                description = product_soup.find('div',
                                                id='produit-avantages').text
            except:
                description = ''
                pass

            try:
                description += product_soup.find(
                    'div', id='produits-carateristiques').text
            except:
                pass

            # Format the title
            summary = infosProduits

            deposit_amount = 0.0

            # print 'url:%s \n img:%s \n infos:%s \n description:%s \n summary:%s' % (product_url, image_url, infosProduits, description, summary)

            # Create the product
            from products.models import Category, Price
            from products.choices import UNIT

            try:
                product = Product.objects.create(
                    summary=summary,
                    description=description,
                    deposit_amount=deposit_amount,
                    address=self.address,
                    owner=self.patron,
                    category=Category.objects.get(
                        slug=category_mapping[category]))
                try:
                    with closing(urlopen(image_url)) as image:
                        product.pictures.add(
                            Picture.objects.create(
                                image=uploadedfile.SimpleUploadedFile(
                                    name='img', content=image.read())))
                except HTTPError as e:
                    print '\nerror loading image for object at url:', self.base_url + product_url
                    pass
            except:
                print 'CANNOT CREATE PRODUCT : %s' % summary
                pass
Beispiel #34
0
def _wrap_img_tag(url, info, size):
    return '<img src="%s"%s alt="Avatar for %s" height="%s" width="%s"/>' % \
            (smart_urlquote(url), _imgclass_attr(), info, size, size)
Beispiel #35
0
        if attrs is not None:
            final_attrs.update(attrs)
<<<<<<< HEAD
        super(AdminURLFieldWidget, self).__init__(attrs=final_attrs)

    def get_context(self, name, value, attrs):
        context = super(AdminURLFieldWidget, self).get_context(name, value, attrs)
=======
        super().__init__(attrs=final_attrs)

    def get_context(self, name, value, attrs):
        context = super().get_context(name, value, attrs)
>>>>>>> 37c99181c9a6b95433d60f8c8ef9af5731096435
        context['current_label'] = _('Currently:')
        context['change_label'] = _('Change:')
        context['widget']['href'] = smart_urlquote(context['widget']['value']) if value else ''
        return context


class AdminIntegerFieldWidget(forms.NumberInput):
    class_name = 'vIntegerField'

    def __init__(self, attrs=None):
        final_attrs = {'class': self.class_name}
        if attrs is not None:
            final_attrs.update(attrs)
<<<<<<< HEAD
        super(AdminIntegerFieldWidget, self).__init__(attrs=final_attrs)
=======
        super().__init__(attrs=final_attrs)
>>>>>>> 37c99181c9a6b95433d60f8c8ef9af5731096435
Beispiel #36
0
def test_smart_urlquote(inp):
    smart_urlquote(inp)
Beispiel #37
0
 def get_context(self, name, value, attrs):
     context = super(AdminURLFieldWidget, self).get_context(name, value, attrs)
     context['current_label'] = _('Currently:')
     context['change_label'] = _('Change:')
     context['widget']['href'] = smart_urlquote(context['widget']['value'])
     return context
Beispiel #38
0
def convert_links(text, trim_url_limit=None, nofollow=False, autoescape=False):
    """
    Finds URLs in text and attempts to handle correctly.
    Heavily based on django.utils.html.urlize
    With the additions of attempting to embed media links, particularly images.

    Works on http://, https://, www. links, and also on links ending in one of
    the original seven gTLDs (.com, .edu, .gov, .int, .mil, .net, and .org).

    Links can have trailing punctuation (periods, commas, close-parens) and
    leading punctuation (opening parens) and it'll still do the right thing.
    
    TO-DO: refactor to better leverage existing django.utils.html

    """

    safe_input = isinstance(text, SafeData)
    words = word_split_re.split(force_text(text))
    for i, word in enumerate(words):
        if '.' in word or ':' in word:
            # Deal with punctuation.
            lead, middle, trail = '', word, ''
            stripped = middle.rstrip(TRAILING_PUNCTUATION_CHARS)
            if middle != stripped:
                trail = middle[len(stripped):] + trail
                middle = stripped
            for opening, closing in WRAPPING_PUNCTUATION:
                if middle.startswith(opening):
                    middle = middle[len(opening):]
                    lead = lead + opening
                # Keep parentheses at the end only if they're balanced.
                if (middle.endswith(closing)
                    and middle.count(closing) == middle.count(opening) + 1):
                    middle = middle[:-len(closing)]
                    trail = closing + trail

            # Make URL we want to point to.
            url = None
            if simple_url_re.match(middle):
                url = smart_urlquote(middle)
            elif simple_url_2_re.match(middle):
                url = smart_urlquote('http://%s' % middle)
            elif ':' not in middle and is_email_simple(middle):
                local, domain = middle.rsplit('@', 1)
                try:
                    domain = domain.encode('idna').decode('ascii')
                except UnicodeError:
                    continue
            if url:
                u = url.lower()
                if autoescape and not safe_input:
                    lead, trail = escape(lead), escape(trail)
                    url = escape(url)

                # Photos
                if u.endswith('.jpg') or u.endswith('.gif') or u.endswith('.png'):
                    middle = '<img src="%s">' % url

                # Youtube
                #'https://www.youtube.com/watch?v=gkqXgaUuxZg'
                elif 'youtube.com/watch' in url:
                    parsed = urlparse.urlsplit(url)
                    query  = urlparse.parse_qs(parsed.query)
                    token  = query.get('v')
                    if token and len(token) > 0:
                        middle = '<iframe src="http://www.youtube.com/embed/%s" height="320" width="100%%"></iframe>' % token[0]
                    else:
                        middle = url
                elif 'youtu.be/' in url:
                    try:
                        token = url.rsplit('/', 1)[1]
                        middle = '<iframe src="http://www.youtube.com/embed/%s" height="320" width="100%%"></iframe>' % token
                    except IndexError:
                        middle = six.u(url)

                words[i] = mark_safe('%s%s%s' % (lead, middle, trail))
            else:
                if safe_input:
                    words[i] = mark_safe(word)
                elif autoescape:
                    words[i] = escape(word)
        elif safe_input:
            words[i] = mark_safe(word)
        elif autoescape:
            words[i] = escape(word)
    return ''.join(words)
Beispiel #39
0
 def get_context(self, name, value, attrs):
     context = super().get_context(name, value, attrs)
     context['current_label'] = _('Currently:')
     context['change_label'] = _('Change:')
     context['widget']['href'] = smart_urlquote(context['widget']['value'])
     return context
Beispiel #40
0
def urlize_quoted_hrefs(text,
                        trim_url_limit=None,
                        nofollow=True,
                        autoescape=True):
    """
    Converts href in text into clickable links. If trim_url_limit is not None, the URLs in link
    text longer than this limit will truncated to trim_url_limit-3 characters and appended with
    an ellipsis. If nofollow is True, the URLs in link text will get a rel="nofollow" attribute.

    """
    def trim_url(x, limit=trim_url_limit):
        return limit is not None and (len(x) > limit and
                                      ('%s...' % x[:max(0, limit - 3)])) or x

    safe_input = isinstance(text, SafeData)
    words = word_split_re.split(force_text(text))
    for i, word in enumerate(words):
        if '/pulp/api/v3/' in word:
            # Deal with punctuation.
            lead, middle, trail = '', word, ''
            for punctuation in TRAILING_PUNCTUATION:
                if middle.endswith(punctuation):
                    middle = middle[:-len(punctuation)]
                    trail = punctuation + trail
            for opening, closing in WRAPPING_PUNCTUATION:
                if middle.startswith(opening):
                    middle = middle[len(opening):]
                    lead = lead + opening
                # Keep parentheses at the end only if they're balanced.
                if (middle.endswith(closing) and middle.count(closing)
                        == middle.count(opening) + 1):
                    middle = middle[:-len(closing)]
                    trail = closing + trail

            # Make URL we want to point to.
            url = None
            nofollow_attr = ' rel="nofollow"' if nofollow else ''

            if href_re.match(middle):
                url = smart_urlquote(middle)

            # Check if it's a real URL
            if url and ("{" in url or "%7B" in url):
                url = None

            # Make link.
            if url:
                trimmed = trim_url(middle)
                if autoescape and not safe_input:
                    lead, trail = escape(lead), escape(trail)
                    url, trimmed = escape(url), escape(trimmed)
                middle = '<a href="%s"%s>%s</a>' % (url, nofollow_attr,
                                                    trimmed)
                words[i] = mark_safe('%s%s%s' % (lead, middle, trail))
            else:
                if safe_input:
                    words[i] = mark_safe(word)
                elif autoescape:
                    words[i] = escape(word)
        elif safe_input:
            words[i] = mark_safe(word)
        elif autoescape:
            words[i] = escape(word)
    return ''.join(words)
"""
Beispiel #42
0
def urlize_impl(text, trim_url_limit=None, nofollow=False, autoescape=False):
    """
    Converts any URLs in text into clickable links.

    Works on http://, https://, www. links, and also on links ending in one of
    the original seven gTLDs (.com, .edu, .gov, .int, .mil, .net, and .org).
    Links can have trailing punctuation (periods, commas, close-parens) and
    leading punctuation (opening parens) and it'll still do the right thing.

    If trim_url_limit is not None, the URLs in link text longer than this limit
    will truncated to trim_url_limit-3 characters and appended with an elipsis.

    If nofollow is True, the URLs in link text will get a rel="nofollow"
    attribute.

    If autoescape is True, the link text and URLs will get autoescaped.
    """
    trim_url = lambda x, limit=trim_url_limit: limit is not None and (len(x) > limit and ('%s...' % x[:max(0, limit - 3)])) or x
    safe_input = isinstance(text, SafeData)
    words = word_split_re.split(force_text(text))
    for i, word in enumerate(words):
        match = None
        if '.' in word or '@' in word or ':' in word:
            # Deal with punctuation.
            lead, middle, trail = '', word, ''
            for punctuation in TRAILING_PUNCTUATION:
                if middle.endswith(punctuation):
                    middle = middle[:-len(punctuation)]
                    trail = punctuation + trail
            for opening, closing in WRAPPING_PUNCTUATION:
                if middle.startswith(opening):
                    middle = middle[len(opening):]
                    lead = lead + opening
                # Keep parentheses at the end only if they're balanced.
                if (middle.endswith(closing)
                    and middle.count(closing) == middle.count(opening) + 1):
                    middle = middle[:-len(closing)]
                    trail = closing + trail

            # Make URL we want to point to.
            url = None
            nofollow_attr = ' rel="nofollow"' if nofollow else ''
            if simple_url_re.match(middle):
                url = smart_urlquote(middle)
            elif simple_url_2_re.match(middle):
                url = smart_urlquote('http://%s' % middle)
            elif not ':' in middle and simple_email_re.match(middle):
                local, domain = middle.rsplit('@', 1)
                try:
                    domain = domain.encode('idna').decode('ascii')
                except UnicodeError:
                    continue
                url = 'mailto:%s@%s' % (local, domain)
                nofollow_attr = ''

            # Make link.
            if url:
                trimmed = trim_url(middle)
                if autoescape and not safe_input:
                    lead, trail = escape(lead), escape(trail)
                    url, trimmed = escape(url), escape(trimmed)
                #
                # Custom stuff for us
                #
                lowered = url.lower()
                is_image = (lowered.endswith('.jpg') or lowered.endswith('.gif')
                            or lowered.endswith('.png'))
                class_attr = is_image and ' class="image"' or ''
                middle = '<a href="%s"%s%s>%s</a>' % (url, nofollow_attr,
                                                      class_attr, trimmed)
                #
                # End custom stuff
                #
                words[i] = mark_safe('%s%s%s' % (lead, middle, trail))
            else:
                if safe_input:
                    words[i] = mark_safe(word)
                elif autoescape:
                    words[i] = escape(word)
        elif safe_input:
            words[i] = mark_safe(word)
        elif autoescape:
            words[i] = escape(word)
    return ''.join(words)
Beispiel #43
0
def convert_links(text, trim_url_limit=None, nofollow=False, autoescape=False):
    """
    Finds URLs in text and attempts to handle correctly.
    Heavily based on django.utils.html.urlize
    With the additions of attempting to embed media links, particularly images.

    Works on http://, https://, www. links, and also on links ending in one of
    the original seven gTLDs (.com, .edu, .gov, .int, .mil, .net, and .org).

    Links can have trailing punctuation (periods, commas, close-parens) and
    leading punctuation (opening parens) and it'll still do the right thing.

    """

    safe_input = isinstance(text, SafeData)
    words = word_split_re.split(force_text(text))
    for i, word in enumerate(words):
        if '.' in word or ':' in word:
            # Deal with punctuation.
            lead, middle, trail = '', word, ''
            for punctuation in TRAILING_PUNCTUATION:
                if middle.endswith(punctuation):
                    middle = middle[:-len(punctuation)]
                    trail = punctuation + trail
            for opening, closing in WRAPPING_PUNCTUATION:
                if middle.startswith(opening):
                    middle = middle[len(opening):]
                    lead = lead + opening
                # Keep parentheses at the end only if they're balanced.
                if (middle.endswith(closing)
                    and middle.count(closing) == middle.count(opening) + 1):
                    middle = middle[:-len(closing)]
                    trail = closing + trail

            # Make URL we want to point to.
            url = None
            if simple_url_re.match(middle):
                url = smart_urlquote(middle)
            elif simple_url_2_re.match(middle):
                url = smart_urlquote('http://%s' % middle)
            elif not ':' in middle and simple_email_re.match(middle):
                local, domain = middle.rsplit('@', 1)
                try:
                    domain = domain.encode('idna').decode('ascii')
                except UnicodeError:
                    continue
            if url:
                u = url.lower()
                if autoescape and not safe_input:
                    lead, trail = escape(lead), escape(trail)
                    url = escape(url)

                # Photos
                if u.endswith('.jpg') or u.endswith('.gif') or u.endswith('.png'):
                    middle = '<img src="%s">' % url

                # Youtube
                #'https://www.youtube.com/watch?v=gkqXgaUuxZg'
                elif 'youtube.com/watch' in url:
                    parsed = urlparse.urlsplit(url)
                    query  = urlparse.parse_qs(parsed.query)
                    token  = query.get('v')
                    if token and len(token) > 0:
                        middle = '<iframe src="http://www.youtube.com/embed/%s" height="320" width="100%%"></iframe>' % token[0]
                    else:
                        middle = url
                elif 'youtu.be/' in url:
                    try:
                        token = url.rsplit('/', 1)[1]
                        middle = '<iframe src="http://www.youtube.com/embed/' + token + '" height="320" width="100%%"></iframe>'
                    except IndexError:
                        middle = url

                words[i] = mark_safe('%s%s%s' % (lead, middle, trail))
            else:
                if safe_input:
                    words[i] = mark_safe(word)
                elif autoescape:
                    words[i] = escape(word)
        elif safe_input:
            words[i] = mark_safe(word)
        elif autoescape:
            words[i] = escape(word)
    return ''.join(words)
Beispiel #44
0
 def __init__(self, content_type, project_url):
     self.type = content_type
     # HTMLのhref用
     self.encoded_url = smart_urlquote(project_url)
     # HTMLのinnerText用
     self.safe_url = escape(unquote(project_url))
def _urlize_all_text(text,
                     trim_url_limit=None,
                     nofollow=False,
                     autoescape=False):
    """
    Convert any URLs in text into clickable links.

    Works on http://, https://, www. links, and also on links ending in one of
    the original seven gTLDs (.com, .edu, .gov, .int, .mil, .net, and .org).
    Links can have trailing punctuation (periods, commas, close-parens) and
    leading punctuation (opening parens) and it'll still do the right thing.

    If trim_url_limit is not None, truncate the URLs in the link text longer
    than this limit to trim_url_limit - 1 characters and append an ellipsis.

    If nofollow is True, give the links a rel="nofollow" attribute.

    If autoescape is True, autoescape the link text and URLs.
    """
    safe_input = isinstance(text, SafeData)

    def trim_url(x, limit=trim_url_limit):
        if limit is None or len(x) <= limit:
            return x
        return '%s…' % x[:max(0, limit - 1)]

    def trim_punctuation(lead, middle, trail):
        """
        Trim trailing and wrapping punctuation from `middle`. Return the items
        of the new state.
        """
        # Continue trimming until middle remains unchanged.
        trimmed_something = True
        while trimmed_something:
            trimmed_something = False
            # Trim wrapping punctuation.
            for opening, closing in WRAPPING_PUNCTUATION:
                if middle.startswith(opening):
                    middle = middle[len(opening):]
                    lead += opening
                    trimmed_something = True
                # Keep parentheses at the end only if they're balanced.
                if middle.endswith(closing) and middle.count(
                        closing) == middle.count(opening) + 1:
                    middle = middle[:-len(closing)]
                    trail = closing + trail
                    trimmed_something = True
            # Trim trailing punctuation (after trimming wrapping punctuation,
            # as encoded entities contain ';'). Unescape entities to avoid
            # breaking them by removing ';'.
            middle_unescaped = html.unescape(middle)
            stripped = middle_unescaped.rstrip(TRAILING_PUNCTUATION_CHARS)
            if middle_unescaped != stripped:
                trail = middle[len(stripped):] + trail
                middle = middle[:len(stripped) - len(middle_unescaped)]
                trimmed_something = True
        return lead, middle, trail

    def is_email_simple(value):
        """Return True if value looks like an email address."""
        # An @ must be in the middle of the value.
        if '@' not in value or value.startswith('@') or value.endswith('@'):
            return False
        try:
            p1, p2 = value.split('@')
        except ValueError:
            # value contains more than one @.
            return False
        # Dot must be in p2 (e.g. example.com)
        if '.' not in p2 or p2.startswith('.'):
            return False
        return True

    words = word_split_re.split(str(text))
    for i, word in enumerate(words):
        if '.' in word or '@' in word or ':' in word:
            # lead: Current punctuation trimmed from the beginning of the word.
            # middle: Current state of the word.
            # trail: Current punctuation trimmed from the end of the word.
            lead, middle, trail = '', word, ''
            # Deal with punctuation.
            lead, middle, trail = trim_punctuation(lead, middle, trail)

            # Make URL we want to point to.
            url = None
            nofollow_attr = ' rel="noopener noreferrer nofollow"' if nofollow else ''
            if simple_url_re.match(middle):
                url = smart_urlquote(html.unescape(middle))
            elif simple_url_2_re.match(middle):
                url = smart_urlquote('http://%s' % html.unescape(middle))
            elif ':' not in middle and is_email_simple(middle):
                local, domain = middle.rsplit('@', 1)
                try:
                    domain = punycode(domain)  # type: ignore
                except UnicodeError:
                    continue
                url = 'mailto:%s@%s' % (local, domain)
                nofollow_attr = ''

            # Make link.
            if url:
                trimmed = trim_url(middle)
                if autoescape and not safe_input:
                    lead, trail = html.escape(lead), html.escape(trail)
                    trimmed = html.escape(trimmed)
                middle = '<a href="%s"%s>%s</a>' % (html.escape(url),
                                                    nofollow_attr, trimmed)
                words[i] = mark_safe('%s%s%s' % (lead, middle, trail))
            else:
                if safe_input:
                    words[i] = mark_safe(word)
                elif autoescape:
                    words[i] = html.escape(word)
        elif safe_input:
            words[i] = mark_safe(word)
        elif autoescape:
            words[i] = html.escape(word)
    return ''.join(words)
Beispiel #46
0
def link_to(title, url, attrs):
    final_attrs = {'href': mark_safe(smart_urlquote(url))}
    if attrs:
        final_attrs.update(attrs)
    return format_html('<a {1}>{0}</a>', force_text(title),
                       flatatt(final_attrs))
Beispiel #47
0
def generate_xml():

    root = ElementTree.Element('yml_catalog')
    root.set('date', datetime.now().strftime("%Y-%m-%d %H:%M"))

    shop = ElementTree.SubElement(root, 'shop')
    ElementTree.SubElement(shop, 'name').text = settings.SITE_TITLE
    ElementTree.SubElement(shop, 'company').text = u'ИП Морозов Денис Владимирович'
    ElementTree.SubElement(shop, 'url').text = 'azbuka-kamnya.ru'
    ElementTree.SubElement(shop, 'platform').text = 'Mezzanine'
    ElementTree.SubElement(shop, 'version').text = '3.1.5'
    ElementTree.SubElement(shop, 'email').text = '*****@*****.**'

    currencies = ElementTree.SubElement(shop, 'currencies')
    ElementTree.SubElement(currencies, 'currency', id = 'RUR', rate='1')
    ElementTree.SubElement(currencies, 'currency', id = 'USD', rate='CBRF', plus='2')
    ElementTree.SubElement(currencies, 'currency', id = 'EUR', rate='CBRF', plus='2')

    categories = ElementTree.SubElement(shop, 'categories')
    for category in Page.objects.published().filter(content_model='category'):
        if category.parent_id:
            ElementTree.SubElement(categories,
                             'category',
                             id=str(category.id),
                             parentId=str(category.parent_id)
            ).text = category.title
        else:
            ElementTree.SubElement(categories,
                             str('category'),
                             id=str(category.id)
            ).text = category.title

    ElementTree.SubElement(shop, 'local_delivery_cost').text = '0'

    offers = ElementTree.SubElement(shop, 'offers')
    for product in Product.objects.published():
        if not product.unit_price or product.content_model == 'productstone':
            continue
        offer = ElementTree.SubElement(
            offers,
            'offer',
            id=str(product.id),
            type='vendor.model',
            available='false',
            bid='10',
            cbid='10'
        )
        ElementTree.SubElement(offer, 'url').text = 'http://azbuka-kamnya.ru' + product.get_absolute_url()
        ElementTree.SubElement(offer, 'price').text = str(product.unit_price)
        ElementTree.SubElement(offer, 'currencyId').text = CURRENCIES[product.currency]
        ElementTree.SubElement(offer, 'categoryId').text = str(product.categories.all()[0].id)
        if product.image:
            ElementTree.SubElement(offer, 'picture').text = \
                'http://azbuka-kamnya.ru' + settings.MEDIA_URL + smart_urlquote(product.image)
        ElementTree.SubElement(offer, 'store').text = 'true'
        ElementTree.SubElement(offer, 'pickup').text = 'true'
        ElementTree.SubElement(offer, 'delivery').text = 'true'
        ElementTree.SubElement(offer, 'typePrefix').text = \
            getattr(product, product.content_model)._meta.verbose_name.title()
        ElementTree.SubElement(offer, 'vendor').text = product.get_manufacturer_display()
        ElementTree.SubElement(offer, 'model').text = \
            product.title.replace(product.get_manufacturer_display(), u'')
        ElementTree.SubElement(offer, 'description').text = product.description
        ElementTree.SubElement(offer, 'sales_notes').text = u'Необходима предоплата'

        for name, value in getattr(product, product.content_model).get_characteristics().items():
            if not value:
                continue
            unit = None
            name = unicode(name)
            if len(name.split(',')) > 1:
                name, unit = name.split(',')
            if unit:
                ElementTree.SubElement(offer, 'param', name=name, unit=unit.strip()).text = unicode(value)
            else:
                ElementTree.SubElement(offer, 'param', name=name).text = unicode(value)

    tree = ElementTree.ElementTree(root).getroot()
    doctype = """<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE yml_catalog SYSTEM "shops.dtd">"""
    xml_string = '\n'.join([doctype, tostring(tree, 'utf-8')])
    if settings.DEBUG:
        open('products.xml', 'w').write(xml_string)
    else:
        open('/home/users/9/9252095267/domains/azbuka-kamnya.ru/products.xml', 'w').write(xml_string)