'Generate an ImportJob from the submitted upload form'

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

    response = current.response
    s3 = response.s3
    db = current.db
    table = self.upload_table
    output = None
    if self.ajax:
        sfilename = ofilename = r.post_vars['file'].filename
        upload_id = table.insert(controller=self.controller, function=self.function, filename=ofilename, file=sfilename, user_id=current.session.auth.user.id)
    else:
        title = self.uploadTitle
        form = self._upload_form(r, **attr)
        r = self.request
        r.read_body()
        sfilename = form.vars.file
        try:
            ofilename = r.post_vars['file'].filename
        except:
            form.errors.file = self.messages.no_file
        if form.errors:
            response.flash = ''
            output = self._create_upload_dataTable()
            output.update(form=form, title=title)
        elif ((not sfilename) or (ofilename not in r.files) or (r.files[ofilename] is None)):
            response.flash = ''
            response.error = self.messages.file_not_found
            output = self._create_upload_dataTable()
            output.update(form=form, title=title)
        else:
            query = (table.file == sfilename)
            db(query).update(controller=self.controller, function=self.function, filename=ofilename, user_id=current.session.auth.user.id)
            row = db(query).select(table.id, limitby=(0, 1)).first()
            upload_id = row.id
    if (not output):
        output = dict()
        db.commit()
        extension = ofilename.rsplit('.', 1).pop()
        if (extension not in ('csv', 'xls', 'xlsx', 'xlsm')):
            if self.ajax:
                return {'Error': self.messages.invalid_file_format}
            response.flash = None
            response.error = self.messages.invalid_file_format
            return self.upload(r, **attr)
        if self.ajax:
            upload_file = r.post_vars.file.file
        else:
            upload_file = r.files[ofilename]
        if (extension == 'xls'):
            if ('xls_parser' in s3):
                upload_file.seek(0)
                upload_file = s3.xls_parser(upload_file.read())
                extension = 'csv'
        if (upload_file is None):
            response.flash = None
            response.error = self.messages.file_not_found
            return self.upload(r, **attr)
        else:
            upload_file.seek(0)
        if ('single_pass' in r.vars):
            single_pass = r.vars['single_pass']
        else:
            single_pass = None
        self._generate_import_job(upload_id, upload_file, extension, commit_job=single_pass)
        if (upload_id is None):
            row = db(query).update(status=2)
            if (self.error != None):
                response.error = self.error
            if (self.warning != None):
                response.warning = self.warning
            response.flash = ''
            return self.upload(r, **attr)
        else:
            if single_pass:
                current.session.confirmation = self.messages.file_uploaded
                next_URL = URL(r=self.request, f=self.function, args=['import'], vars=current.request.get_vars)
                redirect(next_URL)
            s3.dataTable_vars = {'job': upload_id}
            return self.display_job(upload_id)
    return output
