'Apply method.
@param r: the S3Request
@param attr: controller options for this request'

@staticmethod
def apply_method(r, **attr):

    authorised = current.auth.s3_has_permission('create', 'cap_alert')
    if (not authorised):
        r.unauthorised()
    if (r.representation == 'html'):
        T = current.T
        response = current.response
        title = T('Import    from    Feed    URL')
        fields = [Field('url', label=T('URL'), requires=[IS_NOT_EMPTY(), IS_URL()], comment=DIV(_class='tooltip', _title=('%s|%s' % (T('URL    of    the    alert'), T('The    URL    from    which    the    alert    should    be    imported!'))))), Field('user', label=T('Username'), comment=DIV(_class='tooltip', _title=('%s|%s' % (T('Username'), T('Optional    username    for    HTTP    Basic    Authentication.'))))), Field('password', label=T('Password'), widget=S3PasswordWidget(), comment=DIV(_class='tooltip', _title=('%s|%s' % (T('Password'), T('Optional    password    for    HTTP    Basic    Authentication.'))))), Field('ignore_errors', 'boolean', label=T('Ignore    Errors?'), represent=s3_yes_no_represent, comment=DIV(_class='tooltip', _title=('%s|%s' % (T('Ignore    Errors'), T('skip    invalid    record    silently?')))))]
        (labels, required) = s3_mark_required(fields)
        response.s3.has_required = required
        response.form_label_separator = ''
        form = SQLFORM.factory(formstyle=current.deployment_settings.get_ui_formstyle(), submit_button=T('Import'), labels=labels, separator='', table_name='import_alert', _id='importform', *fields)
        response.view = 'create.html'
        output = dict(title=title, form=form)
        if form.accepts(r.post_vars, current.session, formname='import_form'):
            form_vars_get = form.vars.get
            url = form_vars_get('url', None)
            username = form_vars_get('user', None)
            password = form_vars_get('password', None)
            request = urllib2.Request(url)
            if (username and password):
                import base64
                base64string = base64.encodestring(('%s:%s' % (username, password)))
                request.add_header('Authorization', ('Basic    %s' % base64string))
            try:
                file = urllib2.urlopen(request).read()
            except urllib2.URLError:
                response.error = str(sys.exc_info()[1])
                return output
            except urllib2.HTTPError:
                response.error = str(sys.exc_info()[1])
                return output
            File = StringIO(file)
            stylesheet = os.path.join(r.folder, 'static', 'formats', 'cap', 'import.xsl')
            resource = current.s3db.resource('cap_alert')
            try:
                resource.import_xml(File, stylesheet=stylesheet, ignore_errors=form_vars_get('ignore_errors', False))
            except:
                response.error = str(sys.exc_info()[1])
                return output
            else:
                if (not resource.error):
                    if len(resource.import_created):
                        alert_id = resource.import_created[0]
                    elif len(resource.import_updated):
                        alert_id = resource.import_updated[0]
                    response.confirmation = T('Alert    successfully    imported.')
                    redirect(URL(c='cap', f='alert', args=[alert_id]))
                else:
                    response.information = resource.error
                    return output
        return output
    else:
        raise HTTP(405, current.ERROR.BAD_METHOD)
