Beispiel #1
0
def handle_manual_add():
    url = request.params.get('url', '').strip()
    title = request.params.getunicode('title', '').strip()
    license = request.params.get('license') or None
    archive = request.params.get('archive') or None

    errors = {}
    if not url:
        # Translators, used as error message on failure to submit content
        errors['url'] = _('Please type in a valid URL')

    if not errors:
        try:
            content = Content.create(url=url,
                                     license=license,
                                     title=title,
                                     archive=archive)
            logging.info("Created content for '%s' (real url: '%s')", url,
                         content.url)
            response.flash(_('Content has been added'))
            redirect(i18n_path(PREFIX + '/'))
        except Content.InvalidURLError as err:
            logging.debug("URL error while parsing '%s': %s", url, err)
            # Translators, used as error message on failure submit suggestion
            errors['url'] = _('This URL is invalid')
        except Content.FetchError as err:
            logging.debug("Fetch error while parsing '%s': %s (%s)", url, err,
                          err.error)
            # Translators, used as error message on failure submit suggestion
            errors['url'] = _('The page at specified URL does not exist')
        except Content.NotAllowedError as err:
            logging.debug("Access error while parsing '%s': %s", url, err)
            # Translators, used as error message on failure submit suggestion
            errors['url'] = _('The page must be accessible to robots')
        except Content.ContentError as err:
            logging.debug("Content error while parsing '%s': %s (%s)", url,
                          err, err.error)
            # Translators, used as error message on failure submit suggestion
            errors['url'] = _('The content on the page could not be '
                              'understood, please provide and URL to a valid '
                              'web page')
        except Content.BotError as err:
            logging.exception("Error while fetching '%s':  %s", url, err)
            # Translators, used as error message on failure submit suggestion
            errors['url'] = _('There was an unknown error with the URL')

    return get_common_context(dict(vals=request.forms, errors=errors))
Beispiel #2
0
def bulk_create(rows, check=False):
    content = []
    logging.info('Enqueued bulk import with %s rows', len(rows))
    for url, title, license, archive, partner, timestamp, replaces, notes in rows:
        ts = datetime.strptime(timestamp, '%Y-%m-%d %H:%M:%S %Z')
        try:
            content.append(
                Content.create(
                    url=url,
                    title=title,
                    license=license or None,
                    archive=archive or None,
                    created=ts,
                    replaces=replaces or None,
                    notes=notes or None,
                    partner=partner or None,
                    is_partner=partner != '',
                    check_url=check and not url.startswith('outernet://'),
                    override_timestamp=ts,
                ))
        except Exception as err:
            logging.exception("Error while adding content with URL '%s'", url)
    logging.debug('Successfully importent following:\n\n%s',
                  '\n'.join([c.key.id() for c in content]))
Beispiel #3
0
def add_content_suggestion():
    """
    Handle a content suggestion request.
    """
    # TODO: Handle Unicode URLs
    url = Content.validate_url(request.forms.get('url', ''))
    license = request.forms.get('license') or None

    errors = {}

    if not url:
        # Translators, used as error message on failure submit suggestion
        errors['url'] = _('This URL is invalid')

    if license:
        license = license.strip().upper()
        if license not in Content.LICENSE_CHOICES:
            # Translators, used as error message on failure to submit
            # suggestion
            errors['license'] = _('Please select a license from provided '
                                  'choices')

    if not url:
        # Translators, used as error message on failure to submit suggestion
        errors['url'] = _('Please type in a valid URL')

    if not errors:
        try:
            content = Content.create(url=url, license=license)
            logging.info("Created content for '%s' (real url: '%s')", url,
                         content.url)
            response.flash(_('Your suggestion has been added'))
            redirect(i18n_path(content.path))
        except Content.InvalidURLError as err:
            logging.debug("URL error while parsing '%s': %s", url, err)
            # Translators, used as error message on failure submit suggestion
            errors['url'] = _('This URL is invalid')
        except Content.FetchError as err:
            logging.debug("Fetch error while parsing '%s': %s (%s)", url, err,
                          err.error)
            # Translators, used as error message on failure submit suggestion
            errors['url'] = _('The page at specified URL does not exist or '
                              'the domain cannot be reached.')
        except Content.NotAllowedError as err:
            logging.debug("Access error while parsing '%s': %s", url, err)
            # Translators, used as error message on failure submit suggestion
            errors['url'] = _('The page must be accessible to robots')
        except Content.ContentError as err:
            logging.debug("Content error while parsing '%s': %s (%s)", url,
                          err, err.error)
            # Translators, used as error message on failure submit suggestion
            errors['url'] = _('The content on the page could not be '
                              'understood, please provide and URL to a valid '
                              'web page')
        except Exception as err:
            logging.debug("Unknown error fetching '%s': %s", url, err)
            # Translators, used as error message on failure submit suggestion
            errors['url'] = _('There was an unknown error with the URL')

    return dict(vals=request.forms,
                errors=errors,
                Content=Content,
                content=get_content_list())
def add_content_suggestion():
    """
    Handle a content suggestion request.
    """
    # TODO: Handle Unicode URLs
    url = Content.validate_url(request.forms.get('url', ''))
    license = request.forms.get('license') or None

    errors = {}

    if not url:
        # Translators, used as error message on failure submit suggestion
        errors['url'] = _('This URL is invalid')

    if license:
        license = license.strip().upper()
        if license not in Content.LICENSE_CHOICES:
            # Translators, used as error message on failure to submit
            # suggestion
            errors['license'] = _('Please select a license from provided '
                                  'choices')

    if not url:
        # Translators, used as error message on failure to submit suggestion
        errors['url'] = _('Please type in a valid URL')

    if not errors:
        try:
            content = Content.create(url=url, license=license)
            logging.info("Created content for '%s' (real url: '%s')", url,
                         content.url)
            response.flash(_('Your suggestion has been added'))
            redirect(i18n_path(content.path))
        except Content.InvalidURLError as err:
            logging.debug("URL error while parsing '%s': %s", url, err)
            # Translators, used as error message on failure submit suggestion
            errors['url'] = _('This URL is invalid')
        except Content.FetchError as err:
            logging.debug("Fetch error while parsing '%s': %s (%s)",
                          url, err, err.error)
            # Translators, used as error message on failure submit suggestion
            errors['url'] = _('The page at specified URL does not exist or '
                              'the domain cannot be reached.')
        except Content.NotAllowedError as err:
            logging.debug("Access error while parsing '%s': %s", url, err)
            # Translators, used as error message on failure submit suggestion
            errors['url'] = _('The page must be accessible to robots')
        except Content.ContentError as err:
            logging.debug("Content error while parsing '%s': %s (%s)", url,
                          err, err.error)
            # Translators, used as error message on failure submit suggestion
            errors['url'] = _('The content on the page could not be '
                              'understood, please provide and URL to a valid '
                              'web page')
        except Exception as err:
            logging.debug("Unknown error fetching '%s': %s", url, err)
            # Translators, used as error message on failure submit suggestion
            errors['url'] = _('There was an unknown error with the URL')

    return dict(vals=request.forms, errors=errors, Content=Content,
                content=get_content_list())