'Generic method to create a record from the data passed in
@param tablename: The table where the record belongs
@param data: The data that is to be inserted
@param success: The expectation that this create will succeed
@param dbcallback: Used by getRows to return extra data from
the database before & after the create
This will return a dictionary of rows before and after the create'

def create(self, tablename, data, success=True, dbcallback=None, components={}):

    browser = self.browser
    result = {}
    id_data = []
    table = current.s3db[tablename]
    settings = current.deployment_settings
    date_format = str(settings.get_L10n_date_format())
    datetime_format = str(settings.get_L10n_datetime_format())
    try:
        elem = self.get_confirmation(0.2)
        elem.click()
        time.sleep(1)
    except:
        pass
    for details in data:
        el_id = ('%s_%s' % (tablename, details[0]))
        el_value = details[1]
        if (len(details) >= 4):
            time.sleep(details[3])
        if (len(details) >= 3):
            el_type = details[2]
            if (el_type == 'checkbox'):
                for value in el_value:
                    self.browser.find_element_by_xpath(("//label[contains(text(),'%s')]" % value)).click()
            elif (el_type == 'inv_widget'):
                raw_value = self.w_inv_item_select(el_value, tablename, details[0])
            elif (el_type == 'supply_widget'):
                raw_value = self.w_supply_select(el_value, tablename, details[0])
            elif (el_type == 'facility_widget'):
                raw_value = self.w_facility_select(el_value, tablename, details[0])
            elif (el_type == 'gis_location'):
                self.w_gis_location(el_value, details[0])
                raw_value = None
            else:
                el_id = ('%s_%s' % (el_type, details[0]))
                el = browser.find_element_by_id(el_id)
                el.send_keys(el_value)
                raw_value = None
        else:
            el = browser.find_elements_by_id(('dummy_' + el_id))
            if (len(el) != 0):
                el = el[0]
            else:
                el = browser.find_element_by_id(el_id)
            class_name = el.get_attribute('class')
            if ('generic-widget' in class_name):
                raw_value = False
                options_list = el.find_elements_by_tag_name('option')
                option = self.find_max_match(options_list, el_value)
                if (option is None):
                    raise NoSuchElementException(('%s    option    could    not    be    found    in    %s' % (el_value, el_id)))
                option.click()
                raw_value = option.get_attribute('value')
                try:
                    raw_value = int(raw_value)
                except:
                    pass
                if (el_value and (el_value != '-')):
                    self.assertTrue(raw_value, ('%s    option    cannot    be    found    in    %s' % (el_value, el_id)))
            elif ('ui-autocomplete-input' in class_name):
                raw_value = self.w_autocomplete(el_value, el_id, el_value)
            elif isinstance(table[details[0]].widget, S3DateWidget):
                el_value_date = datetime.datetime.strptime(el_value, '%Y-%m-%d')
                el_value = el_value_date.strftime(date_format)
                el.clear()
                el.send_keys(el_value)
                raw_value = el_value_date
            elif isinstance(table[details[0]].widget, S3DateTimeWidget):
                el_value_datetime = datetime.datetime.strptime(el_value, '%Y-%m-%d    %H:%M:%S')
                el_value = el_value_datetime.strftime(datetime_format)
                el.clear()
                el.send_keys(el_value)
                raw_value = None
            else:
                el.clear()
                el.send_keys(el_value)
                raw_value = el_value
        if raw_value:
            id_data.append([details[0], raw_value])
    result['before'] = self.getRows(table, id_data, dbcallback, components)
    submit_btn = browser.find_element_by_css_selector("input[type='submit']")
    submit_btn.click()
    confirm = True
    try:
        elem = self.get_confirmation(3)
        self.reporter(elem.text)
    except (NoSuchElementException, TimeoutException):
        confirm = False
    if (confirm != success):
        try:
            elem_error = browser.find_element_by_xpath("//div[@class='error']")
            if elem_error:
                msg = ('%s    %s' % (elem_error.get_attribute('id'), elem_error.text))
                self.reporter(msg)
        except NoSuchElementException:
            pass
    if success:
        self.assertTrue(confirm, ('Confirmation    of    record    creation    not    received.\nRecord    -    %s' % data))
    else:
        self.assertFalse(confirm, ('Unexpected    confirmation    of    record    creation    received.\nRecord    -    %s' % data))
    result['after'] = self.getRows(table, id_data, dbcallback, components)
    successMsg = ('Records    added    to    database:    %s' % id_data)
    failMsg = ('Records    not    added    to    database    %s' % id_data)
    if success:
        self.assertTrue(((len(result['after']) - len(result['before'])) == 1), failMsg)
        self.reporter(successMsg)
    else:
        self.assertTrue((len(result['after']) == len(result['before'])), successMsg)
        self.reporter(failMsg)
    return result
