'Create and process the upload form, including csv_extra_fields'

def _upload_form(self, r, **attr):

    EXTRA_FIELDS = 'csv_extra_fields'
    TEMPLATE = 'csv_template'
    REPLACE_OPTION = 'replace_option'
    response = current.response
    s3 = response.s3
    request = self.request
    table = self.upload_table
    formstyle = s3.crud.formstyle
    response.view = self._view(request, 'list_filter.html')
    if (REPLACE_OPTION in attr):
        replace_option = attr[REPLACE_OPTION]
        if (replace_option is not None):
            field = table.replace_option
            field.readable = field.writable = True
            field.label = replace_option
            field.comment = DIV(_class='tooltip', _title=('%s|%s' % (replace_option, current.T('Delete    all    data    of    this    type    which    the    user    has    permission    to    before    upload.    This    is    designed    for    workflows    where    the    data    is    maintained    in    an    offline    spreadsheet    and    uploaded    just    for    Reads.'))))
    fields = [f for f in table if (f.readable or (f.writable and (not f.compute)))]
    if (EXTRA_FIELDS in attr):
        extra_fields = attr[EXTRA_FIELDS]
        if (extra_fields is not None):
            fields.extend([f['field'] for f in extra_fields if ('field' in f)])
        self.csv_extra_fields = extra_fields
    (labels, required) = s3_mark_required(fields)
    if required:
        s3.has_required = True
    form = SQLFORM.factory(table_name=self.UPLOAD_TABLE_NAME, labels=labels, formstyle=formstyle, upload=os.path.join(request.folder, 'uploads', 'imports'), separator='', message=self.messages.file_uploaded, *fields)
    args = ['s3csv']
    template = attr.get(TEMPLATE, True)
    if (template is True):
        args.extend([self.controller, ('%s.csv' % self.function)])
    elif isinstance(template, basestring):
        args.extend([self.controller, ('%s.csv' % template)])
    elif isinstance(template, (tuple, list)):
        args.extend(template[:(-1)])
        args.append(('%s.csv' % template[(-1)]))
    else:
        template = None
    if (template is not None):
        url = URL(r=request, c='static', f='formats', args=args)
        try:
            open(('%s/../%s' % (r.folder, url)))
            form[0][0].insert(0, TR(TD(A(self.messages.download_template, _href=url)), _id='template__row'))
        except:
            pass
    if form.accepts(r.post_vars, current.session, formname='upload_form'):
        formvars = form.vars
        upload_id = table.insert(file=formvars.file)
        if self.csv_extra_fields:
            extra_data = self.csv_extra_data = Storage()
            for f in self.csv_extra_fields:
                label = f.get('label')
                if (not label):
                    continue
                field = f.get('field')
                value = f.get('value')
                if field:
                    if (field.name in formvars):
                        data = formvars[field.name]
                    else:
                        data = field.default
                    value = data
                    requires = field.requires
                    if (not isinstance(requires, (list, tuple))):
                        requires = [requires]
                    if requires:
                        requires = requires[0]
                        if isinstance(requires, IS_EMPTY_OR):
                            requires = requires.other
                        try:
                            options = requires.options()
                        except:
                            pass
                        else:
                            for (k, v) in options:
                                if (k == str(data)):
                                    value = v
                                    break
                            if hasattr(value, 'm'):
                                value = value.m
                elif (value is None):
                    continue
                extra_data[label] = value
    s3.no_formats = True
    return form
