Ejemplo n.º 1
0
def urlquote_plus(url, safe='/'):
    """
    A version of Python's ``urllib.quote_plus()`` function that can operate on
    unicode strings. The url is first UTF-8 encoded before quoting. The
    returned string can safely be used as part of an argument to a subsequent
    ``iri_to_uri()`` call without double-quoting occurring.
    """
    if six.PY3:
        return quote_plus(url, safe)
    else:
        return latin_to_unicode(quote_plus(force_bytes(url), force_bytes(safe)))
Ejemplo n.º 2
0
    def _build_project_api_url(self, repository, rest_parts, query=None):
        """Return an API URL for the Gerrit projects API.

        Args:
            repository (reviewboard.scmtools.models.Repository):
                The repository configured to use Gerrit.

            rest_parts (iterable):
                The rest of the URL parts.

            **query (dict, optional):
                The query parameters to append to the URL.

        Returns:
            unicode:
            The full URL.
        """
        parts = [
            'a',
            'projects',
            quote_plus(repository.extra_data['gerrit_project_name']),
        ]
        parts.extend(rest_parts)

        url = urljoin(repository.extra_data['gerrit_url'], '/'.join(parts))

        if query:
            url = '%s/?%s' % (url, urlencode(query))
        else:
            url = '%s/' % url

        return url
Ejemplo n.º 3
0
    def _created_proxy_response(self, request, path):
        request_payload = request.body

        self.log.debug("Request headers: %s", self.request_headers)

        path = quote_plus(path.encode('utf8'), QUOTE_SAFE)

        request_url = self.get_upstream(path) + path
        self.log.debug("Request URL: %s", request_url)

        if request.GET:
            get_data = encode_items(request.GET.lists())
            request_url += '?' + urlencode(get_data)
            self.log.debug("Request URL: %s", request_url)

        try:
            proxy_response = self.http.urlopen(request.method,
                                               request_url,
                                               redirect=False,
                                               retries=self.retries,
                                               headers=self.request_headers,
                                               body=request_payload,
                                               decode_content=False,
                                               preload_content=False)
            self.log.debug("Proxy response header: %s",
                           proxy_response.getheaders())
        except urllib3.exceptions.HTTPError as error:
            self.log.exception(error)
            raise

        return proxy_response
Ejemplo n.º 4
0
    def _build_project_api_url(self, repository, rest_parts, query=None):
        """Return an API URL for the Gerrit projects API.

        Args:
            repository (reviewboard.scmtools.models.Repository):
                The repository configured to use Gerrit.

            rest_parts (iterable):
                The rest of the URL parts.

            **query (dict, optional):
                The query parameters to append to the URL.

        Returns:
            unicode:
            The full URL.
        """
        parts = [
            'a',
            'projects',
            quote_plus(repository.extra_data['gerrit_project_name']),
        ]
        parts.extend(rest_parts)

        url = urljoin(repository.extra_data['gerrit_url'], '/'.join(parts))

        if query:
            url = '%s/?%s' % (url, urlencode(query))
        else:
            url = '%s/' % url

        return url
Ejemplo n.º 5
0
def test_custom_user_profile_tab():
    setting_name = 'ASKBOT_CUSTOM_USER_PROFILE_TAB'
    tab_settings = getattr(django_settings, setting_name, None)
    if tab_settings:
        if not isinstance(tab_settings, dict):
            print("Setting %s must be a dictionary!!!" % setting_name)

        name = tab_settings.get('NAME', None)
        slug = tab_settings.get('SLUG', None)
        func_name = tab_settings.get('CONTENT_GENERATOR', None)

        errors = list()
        if (name is None) or (not isinstance(name, six.string_types)):
            errors.append("%s['NAME'] must be a string" % setting_name)
        if (slug is None) or (not isinstance(slug, six.string_types)):
            errors.append("%s['SLUG'] must be an ASCII string" % setting_name)

        if quote_plus(slug) != slug:
            errors.append(
                "%s['SLUG'] must be url safe, make it simple" % setting_name
            )

        try:
            func = load_module(func_name)  # noqa
        except ImportError:
            errors.append("%s['CONTENT_GENERATOR'] must be a dotted path to a function" % setting_name)

        header = 'Custom user profile tab is configured incorrectly in your settings.py file'
        footer = 'Please carefully read about adding a custom user profile tab.'
        print_errors(errors, header=header, footer=footer)
Ejemplo n.º 6
0
    def process_request(self, request):
        """when askbot is in the closed mode
        it will let through only authenticated users.
        All others will be redirected to the login url.
        """
        if (askbot_settings.ASKBOT_CLOSED_FORUM_MODE
                and request.user.is_anonymous()):
            resolver_match = ResolverMatch(resolve(request.path))

            internal_ips = getattr(settings, 'ASKBOT_INTERNAL_IPS', None)
            if internal_ips and request.META.get(
                    'REMOTE_ADDR') in internal_ips:
                return None

            if is_view_allowed(resolver_match.func):
                return

            if is_view_protected(resolver_match.func):
                # request.user.message_set.create(_('Please log in to use %s') % askbot_settings.APP_SHORT_NAME)
                django_messages.info(
                    request,
                    _('Please log in to use %s') %
                    askbot_settings.APP_SHORT_NAME)
                redirect_url = '%s?next=%s' % (
                    settings.LOGIN_URL, quote_plus(request.get_full_path()))
                return HttpResponseRedirect(redirect_url)
        return None
Ejemplo n.º 7
0
def test_custom_user_profile_tab():
    setting_name = 'ASKBOT_CUSTOM_USER_PROFILE_TAB'
    tab_settings = getattr(django_settings, setting_name, None)
    if tab_settings:
        if not isinstance(tab_settings, dict):
            print("Setting %s must be a dictionary!!!" % setting_name)

        name = tab_settings.get('NAME', None)
        slug = tab_settings.get('SLUG', None)
        func_name = tab_settings.get('CONTENT_GENERATOR', None)

        errors = list()
        if (name is None) or (not isinstance(name, six.string_types)):
            errors.append("%s['NAME'] must be a string" % setting_name)
        if (slug is None) or (not isinstance(slug, six.string_types)):
            errors.append("%s['SLUG'] must be an ASCII string" % setting_name)

        if quote_plus(slug) != slug:
            errors.append("%s['SLUG'] must be url safe, make it simple" %
                          setting_name)

        try:
            func = load_module(func_name)  # noqa
        except ImportError:
            errors.append(
                "%s['CONTENT_GENERATOR'] must be a dotted path to a function" %
                setting_name)

        header = 'Custom user profile tab is configured incorrectly in your settings.py file'
        footer = 'Please carefully read about adding a custom user profile tab.'
        print_errors(errors, header=header, footer=footer)
Ejemplo n.º 8
0
    def _created_proxy_response(self, request, path):
        request_payload = request.body

        self.log.debug("Request headers: %s", self.request_headers)

        path = quote_plus(path.encode('utf8'), QUOTE_SAFE)

        request_url = self.get_upstream(path) + path
        self.log.debug("Request URL: %s", request_url)

        if request.GET:
            get_data = encode_items(request.GET.lists())
            request_url += '?' + urlencode(get_data)
            self.log.debug("Request URL: %s", request_url)

        try:
            proxy_response = self.http.urlopen(request.method,
                                               request_url,
                                               redirect=False,
                                               retries=self.retries,
                                               headers=self.request_headers,
                                               body=request_payload,
                                               decode_content=False,
                                               preload_content=False)
            self.log.debug("Proxy response header: %s",
                           proxy_response.getheaders())
        except urllib3.exceptions.HTTPError as error:
            self.log.exception(error)
            raise

        return proxy_response
Ejemplo n.º 9
0
def job_detail(request, slug):
    job = get_object_or_404(Job, slug=slug)
    share_content = quote_plus("Achei esta #vaga de %s. Veja mais #vagas em www.vagastirecife.com.br" % (job.title))
    context = {
        'share_content': share_content,
        'job': job
    }
    return render(request, 'jobs/job_detail.html', context)
Ejemplo n.º 10
0
def urlquote_plus(url, safe=''):
    """
    A version of Python's urllib.quote_plus() function that can operate on
    unicode strings. The url is first UTF-8 encoded before quoting. The
    returned string can safely be used as part of an argument to a subsequent
    iri_to_uri() call without double-quoting occurring.
    """
    return force_text(quote_plus(force_str(url), force_str(safe)))
Ejemplo n.º 11
0
def urlquote_plus(url, safe=''):
    """
    A version of Python's urllib.quote_plus() function that can operate on
    unicode strings. The url is first UTF-8 encoded before quoting. The
    returned string can safely be used as part of an argument to a subsequent
    iri_to_uri() call without double-quoting occurring.
    """
    return force_text(quote_plus(force_str(url), force_str(safe)))
Ejemplo n.º 12
0
    def setUp(self):
        mock_param = NonCallableMock()
        self._dict = {'next': '/a&b/', 'x': mock_param}
        self._query_dict = QueryDict('', mutable=True)
        self._query_dict.update(self._dict)

        part_a = 'next=%2Fa%26b%2F'
        part_b = 'x=' + quote_plus(str(mock_param))
        self.expected = (part_a + '&' + part_b, part_b + '&' + part_a)
Ejemplo n.º 13
0
def post_detail(request, id=None):
	instance = get_object_or_404(Post, id=id)
	share_string = quote_plus(instance.content)
	context = {
		"title": "Detail",
		"instance": instance, 
		"share_string": share_string,
		
	}
	return render(request, 'post_detail.html', context)
Ejemplo n.º 14
0
 def __init__(self, staging_folder, filename=None, encoded_filename=None):
     self.staging_folder = staging_folder
     if encoded_filename:
         self.encoded_filename = str(encoded_filename)
         self.filename = base64.urlsafe_b64decode(
             unquote_plus(self.encoded_filename)).decode('utf8')
     else:
         self.filename = filename
         self.encoded_filename = quote_plus(
             base64.urlsafe_b64encode(filename.encode('utf8')))
Ejemplo n.º 15
0
def job_detail(request, slug):
    job = get_object_or_404(Job, slug=slug)
    if job.published:
        share_content = quote_plus("Achei esta #vaga de %s. Veja mais #vagas em www.vagastirecife.com.br" % (job.title.encode('utf8')))
        context = {
            'share_content': share_content,
            'job': job
        }
        return render(request, 'jobs/job_detail.html', context)
    else:
        messages.info(request, "Esta vaga ainda não está disponível. Tente novamente mais tarde.")
        return redirect('index')
Ejemplo n.º 16
0
 def __init__(self, staging_folder, filename=None, encoded_filename=None):
     self.staging_folder = staging_folder
     if encoded_filename:
         self.encoded_filename = str(encoded_filename)
         self.filename = base64.urlsafe_b64decode(
             unquote_plus(self.encoded_filename)
         ).decode('utf8')
     else:
         self.filename = filename
         self.encoded_filename = quote_plus(base64.urlsafe_b64encode(
             filename.encode('utf8')
         ))
Ejemplo n.º 17
0
def post_detail(request, id):
    #instance = Post.objects.get(id=1)
    instance = get_object_or_404(Post, id=id)
    if instance.publish > timezone.now() or instance.draft:
        if not request.user.is_staff or not request.user.is_superuser:
            raise Http404
    share_string = quote_plus(instance.content)
    context = {
        "title": "Detail",
        "instance": instance,
        "share_string": share_string,
    }
    return render(request, 'blog/post_detail.html', context)
Ejemplo n.º 18
0
def post_detail(request, slug=None):
	#instance = Post.objects.get(slug=1)
	instance = get_object_or_404(Post,slug=slug)
	if instance.publish > timezone.now().date() or instance.draft:
		if not request.user.is_staff or not request.user.is_superuser:
			raise Http404

	share_string= quote_plus(instance.titulo)
	context={
	"titulo":instance.titulo,
	"instance":instance,
	"share_string":share_string,
	}
	return render(request,"post_detail.html",context)
Ejemplo n.º 19
0
    def sendsms(self, phone, msgs):
        """
        Send SMS by calling the assigned URL with appropriate parametres

        Arguments:
            ``phone'' is the phone number the messages are to be dispatched to.
            ``msgs'' is a list of messages ordered with the most severe first.
            Each message is a tuple with ID, text and severity of the message.

        Returns five values:
            The formatted SMS.
            A list of IDs of sent messages.
            A list of IDs of ignored messages.
            A boolean which is true for success and false for failure.
            An integer which is the sending ID if available or 0 otherwise.
        """

        # Format SMS
        (sms, sent, ignored) = self.formatsms(msgs)
        sms = quote_plus(sms)

        # Format HTTP GET request
        get_data = {'phone': phone, 'sms': sms}
        url = self.url % get_data

        # Send SMS
        try:
            urlopen(url)
            result = True
        except HTTPError as ex:
            self.logger.error('HTTP error: <%s>: %s (%s).' %
                              (ex.url, ex.msg, ex.code))
            result = False

        smsid = 0
        self.logger.debug(
            'HttpGetDispatcher response: %s, %s, %s, %s, %s',
            sms,
            sent,
            ignored,
            result,
            smsid,
        )
        return (sms, sent, ignored, result, smsid)
Ejemplo n.º 20
0
    def _find_repository_id_v4(self, plan, owner, repo_name):
        """Find the ID of a repository matching the given name and owner.

        If the repository could not be found, an appropriate error will be
        raised.

        Args:
            plan (unicode):
                The plan name.

            owner (unicode):
                The name of the owning group or user.

            repo_name (unicode):
                The name of the repository.

        Returns:
            int:
            The ID of the repository.

        Raises:
            reviewboard.scmtools.errors.AuthorizationError:
                There was an issue with the authorization credentials.

            reviewboard.scmtools.errors.RepositoryError:
                The repository could be found or accessed.

            urllib2.HTTPError:
                There was an error communicating with the server.
        """
        project = '%s/%s' % (owner, repo_name)

        try:
            data, headers = self._api_get(self.account.hosting_url,
                                          'projects/%s' % quote_plus(project))
            return data['id']
        except HTTPError as e:
            if e.code == 404:
                raise RepositoryError(
                    ugettext('A repository with this name was not found, or '
                             'your user may not own it.'))

            raise
Ejemplo n.º 21
0
    def _find_repository_id_v4(self, plan, owner, repo_name):
        """Find the ID of a repository matching the given name and owner.

        If the repository could not be found, an appropriate error will be
        raised.

        Args:
            plan (unicode):
                The plan name.

            owner (unicode):
                The name of the owning group or user.

            repo_name (unicode):
                The name of the repository.

        Returns:
            int:
            The ID of the repository.

        Raises:
            reviewboard.scmtools.errors.AuthorizationError:
                There was an issue with the authorization credentials.

            reviewboard.scmtools.errors.RepositoryError:
                The repository could be found or accessed.

            urllib2.HTTPError:
                There was an error communicating with the server.
        """
        project = '%s/%s' % (owner, repo_name)

        try:
            data, headers = self._api_get(self.account.hosting_url,
                                          'projects/%s' % quote_plus(project))
            return data['id']
        except HTTPError as e:
            if e.code == 404:
                raise RepositoryError(
                    ugettext('A repository with this name was not found, or '
                             'your user may not own it.'))

            raise
Ejemplo n.º 22
0
    def _build_project_api_url(self, repository, *rest_parts):
        """Return an API URL for the Gerrit projects API.

        Args:
            repository (reviewboard.scmtools.models.Repository):
                The repository configured to use Gerrit.

            *rest_parts (tuple):
                The rest of the URL parts.

        Returns:
            unicode:
            The full URL.
        """
        parts = [
            'a',
            'projects',
            quote_plus(repository.extra_data['gerrit_project_name']),
        ] + list(rest_parts)

        return '%s/' % urljoin(repository.extra_data['gerrit_url'],
                               '/'.join(parts))
Ejemplo n.º 23
0
    def process_request(self, request):
        """when askbot is in the closed mode
        it will let through only authenticated users.
        All others will be redirected to the login url.
        """
        if (askbot_settings.ASKBOT_CLOSED_FORUM_MODE and request.user.is_anonymous()):
            resolver_match = ResolverMatch(resolve(request.path))

            internal_ips = getattr(settings, 'ASKBOT_INTERNAL_IPS', None)
            if internal_ips and request.META.get('REMOTE_ADDR') in internal_ips:
                return None

            if is_view_allowed(resolver_match.func):
                return

            if is_view_protected(resolver_match.func):
                # request.user.message_set.create(_('Please log in to use %s') % askbot_settings.APP_SHORT_NAME)
                django_messages.info(request, _('Please log in to use %s') % askbot_settings.APP_SHORT_NAME)
                redirect_url = '%s?next=%s' % (
                    settings.LOGIN_URL,
                    quote_plus(request.get_full_path())
                )
                return HttpResponseRedirect(redirect_url)
        return None
Ejemplo n.º 24
0
 def get_quoted_path(self, path):
     """Return quoted path to be used in proxied request"""
     return quote_plus(path.encode('utf8'), QUOTE_SAFE)
Ejemplo n.º 25
0
    SESSION_COOKIE_DOMAIN = os.environ['SESSION_COOKIE_DOMAIN']
    SESSION_COOKIE_NAME = 'kobonaut'

SESSION_SERIALIZER = 'django.contrib.sessions.serializers.JSONSerializer'

# for debugging
# print "KOBOFORM_URL=%s" % KOBOFORM_URL
# print "SECRET_KEY=%s" % SECRET_KEY
# print "CSRF_COOKIE_DOMAIN=%s " % CSRF_COOKIE_DOMAIN

# MongoDB - moved here from common.py
if MONGO_DATABASE.get('USER') and MONGO_DATABASE.get('PASSWORD'):
    MONGO_CONNECTION_URL = "mongodb://{user}:{password}@{host}:{port}/{db_name}".\
        format(
            user=MONGO_DATABASE['USER'],
            password=quote_plus(MONGO_DATABASE['PASSWORD']),
            host=MONGO_DATABASE['HOST'],
            port=MONGO_DATABASE['PORT'],
            db_name=MONGO_DATABASE['NAME']
        )
else:
    MONGO_CONNECTION_URL = "mongodb://%(HOST)s:%(PORT)s/%(NAME)s" % MONGO_DATABASE

# PyMongo 3 does acknowledged writes by default
# https://emptysqua.re/blog/pymongos-new-default-safe-writes/
MONGO_CONNECTION = MongoClient(
    MONGO_CONNECTION_URL, j=True, tz_aware=True)

MONGO_DB = MONGO_CONNECTION[MONGO_DATABASE['NAME']]

# BEGIN external service integration codes
Ejemplo n.º 26
0
from __future__ import unicode_literals
Ejemplo n.º 27
0
 def url(self, name):
     return urlparse.urljoin(self.base_url,
                             os.path.join(urlparse.quote_plus(self.db.name),
                             urlparse.quote_plus(name),
                             'content'))
Ejemplo n.º 28
0
 def get_quoted_path(self, path):
     """Return quoted path to be used in proxied request"""
     return quote_plus(path.encode('utf8'), QUOTE_SAFE)
Ejemplo n.º 29
0
    def check_repository(self, gerrit_url=None, gerrit_project_name=None,
                         *args, **kwargs):
        """Check that the repository is configured correctly.

        This method ensures that the user has access to an existing repository
        on the Gerrit server and that the ``gerrit-reviewboard`` plugin is
        installed and of a compatible version.

        Args:
            gerrit_url (unicode):
                The URL to the Gerrit server.

            gerrit_project_name (unicode):
                The repository's configured project name on Gerrit.

            *args (tuple):
                Ignored positional arguments.

            **kwargs (dict):
                Ignored keyword arguments.

        Raises:
            reviewboard.hostingsvcs.errors.RepositoryError:
                One of the following:

                * The repository does not exist.
                * The user does not have access to the repository.
                * The ``gerrit-reviewboard`` plugin is not installed.
                * The ``gerrit-reviewboard`` plugin that is installed is an
                  incompatible version.
        """
        url = urljoin(gerrit_url,
                      'a/projects/%s' % quote_plus(gerrit_project_name))

        try:
            self.client.api_get(url, json=False)
        except HostingServiceAPIError as e:
            if e.http_code == 404:
                raise RepositoryError(
                    ugettext('The project "%s" does not exist or you do not '
                             'have access to it.')
                    % gerrit_project_name)

            raise

        url = urljoin(gerrit_url, 'a/plugins/')

        try:
            rsp = self.client.api_get(url)
        except HostingServiceError as e:
            logger.exception(
                'Could not retrieve the list of Gerrit plugins from %s: %s',
                url, e)
            raise RepositoryError(
                ugettext('Could not retrieve the list of Gerrit plugins '
                         'from %(url).')
                % {
                    'url': url,
                })

        if 'gerrit-reviewboard' not in rsp:
            raise RepositoryError(
                ugettext('The "gerrit-reviewboard" plugin is not installed '
                         'on the server. See %(plugin_url)s for installation '
                         'instructions.')
                % {
                    'plugin_url': _PLUGIN_URL,
                })
        else:
            version_str = rsp['gerrit-reviewboard']['version']

            try:
                version = self._parse_plugin_version(version_str)
            except Exception as e:
                logger.exception(
                    'Could not parse gerrit-reviewboard plugin version "%s" '
                    'from %s: %s',
                    version, url, e)
                raise RepositoryError(
                    ugettext('Could not parse gerrit-reviewboard version: '
                             '"%(version)s" from URL %(url)s: %(error)s')
                    % {
                        'version': version,
                        'error': e,
                        'url': url,
                    })

            if version < self.REQUIRED_PLUGIN_VERSION:
                raise RepositoryError(
                    ugettext('The "gerrit-reviewboard" plugin on the server '
                             'is an incompatible version: found %(found)s but '
                             'version %(required)s or higher is required.')
                    % {
                        'found': version_str,
                        'required': self.REQUIRED_PLUGIN_VERSION_STR,
                    }
                )
Ejemplo n.º 30
0
def urlify(value):
	return quote_plus(value)
Ejemplo n.º 31
0
    def check_repository(self, gerrit_url=None, gerrit_project_name=None,
                         *args, **kwargs):
        """Check that the repository is configured correctly.

        This method ensures that the user has access to an existing repository
        on the Gerrit server and that the ``gerrit-reviewboard`` plugin is
        installed and of a compatible version.

        Args:
            gerrit_url (unicode):
                The URL to the Gerrit server.

            gerrit_project_name (unicode):
                The repository's configured project name on Gerrit.

            *args (tuple):
                Ignored positional arguments.

            **kwargs (dict):
                Ignored keyword arguments.

        Raises:
            reviewboard.hostingsvcs.errors.RepositoryError:
                One of the following:

                * The repository does not exist.
                * The user does not have access to the repository.
                * The ``gerrit-reviewboard`` plugin is not installed.
                * The ``gerrit-reviewboard`` plugin that is installed is an
                  incompatible version.
        """
        url = urljoin(gerrit_url,
                      'a/projects/%s' % quote_plus(gerrit_project_name))

        try:
            self.client.api_get(url, json=False)
        except HostingServiceAPIError as e:
            if e.http_code == 404:
                raise RepositoryError(
                    ugettext('The project "%s" does not exist or you do not '
                             'have access to it.')
                    % gerrit_project_name)

            raise

        url = urljoin(gerrit_url, 'a/plugins/')

        try:
            rsp = self.client.api_get(url)
        except HostingServiceError as e:
            logger.exception(
                'Could not retrieve the list of Gerrit plugins from %s: %s',
                url, e)
            raise RepositoryError(
                ugettext('Could not retrieve the list of Gerrit plugins '
                         'from %(url).')
                % {
                    'url': url,
                })

        if 'gerrit-reviewboard' not in rsp:
            raise RepositoryError(
                ugettext('The "gerrit-reviewboard" plugin is not installed '
                         'on the server. See %(plugin_url)s for installation '
                         'instructions.')
                % {
                    'plugin_url': _PLUGIN_URL,
                })
        else:
            version_str = rsp['gerrit-reviewboard']['version']

            try:
                version = self._parse_plugin_version(version_str)
            except Exception as e:
                logger.exception(
                    'Could not parse gerrit-reviewboard plugin version "%s" '
                    'from %s: %s',
                    version, url, e)
                raise RepositoryError(
                    ugettext('Could not parse gerrit-reviewboard version: '
                             '"%(version)s" from URL %(url)s: %(error)s')
                    % {
                        'version': version,
                        'error': e,
                        'url': url,
                    })

            if version < self.REQUIRED_PLUGIN_VERSION:
                raise RepositoryError(
                    ugettext('The "gerrit-reviewboard" plugin on the server '
                             'is an incompatible version: found %(found)s but '
                             'version %(required)s or higher is required.')
                    % {
                        'found': version_str,
                        'required': self.REQUIRED_PLUGIN_VERSION_STR,
                    }
                )
Ejemplo n.º 32
0
 def get_absolute_url(self):
     return reverse('view',
                    kwargs={
                        'name': quote_plus(self.name),
                        'id': self.id
                    })