'Creates a <resource> element from a record
@param parent: the parent element in the document tree
@param table: the database table
@param record: the record
@param alias: the resource alias (for disambiguation of components)
@param fields: list of field names to include
@param url: URL of the record
@param lazy: lazy representation map
@param postprocess: post-process hook (xml_post_render)'

def resource(self, parent, table, record, alias=None, fields=[], url=None, lazy=None, postprocess=None):

    SubElement = etree.SubElement
    UID = self.UID
    MCI = self.MCI
    DELETED = self.DELETED
    TAG = self.TAG
    RESOURCE = TAG['resource']
    DATA = TAG['data']
    ATTRIBUTE = self.ATTRIBUTE
    NAME = ATTRIBUTE['name']
    ALIAS = ATTRIBUTE['alias']
    FIELD = ATTRIBUTE['field']
    VALUE = ATTRIBUTE['value']
    FILEURL = ATTRIBUTE['url']
    tablename = table._tablename
    deleted = False
    download_url = (current.response.s3.download_url or '')
    auth_group = current.auth.settings.table_group_name
    if (parent is not None):
        elem = SubElement(parent, RESOURCE)
    else:
        elem = etree.Element(RESOURCE)
    attrib = elem.attrib
    attrib[NAME] = tablename
    if alias:
        attrib[ALIAS] = alias
    if ((UID in table.fields) and (UID in record)):
        uid = record[UID]
        uid = str(table[UID].formatter(uid)).decode('utf-8')
        if (tablename != auth_group):
            attrib[UID] = self.export_uid(uid)
        else:
            attrib[UID] = uid
    if ((DELETED in record) and record[DELETED]):
        deleted = True
        attrib[DELETED] = 'True'
        fields = [self.MTIME]
    FIELDS_TO_ATTRIBUTES = self.FIELDS_TO_ATTRIBUTES
    _repr = self.represent
    to_json = json.dumps
    for f in fields:
        if (f == DELETED):
            continue
        v = record.get(f, None)
        if (f == MCI):
            if (v is None):
                v = 0
            attrib[MCI] = str((int(v) + 1))
            continue
        if ((v is None) or (not hasattr(table, f))):
            continue
        dbfield = ogetattr(table, f)
        fieldtype = str(dbfield.type)
        formatter = dbfield.formatter
        represent = dbfield.represent
        value = None
        if (fieldtype in ('datetime', 'date', 'time')):
            value = s3_encode_iso_datetime(v).decode('utf-8')
        elif (fieldtype[:7] == 'decimal'):
            value = str(formatter(v)).decode('utf-8')
        is_lazy = False
        if (fieldtype not in ('upload', 'password', 'blob')):
            if ((represent is not None) and (fieldtype != 'id') and (f not in ('created_on', 'modified_on'))):
                if ((lazy is not None) and hasattr(represent, 'bulk')):
                    is_lazy = True
                    text = S3RepresentLazy(v, represent)
                else:
                    text = _repr(table, f, v)
            elif (value is not None):
                text = value
            else:
                text = s3_unicode(formatter(v))
        else:
            text = None
        if (f in FIELDS_TO_ATTRIBUTES):
            if (text is not None):
                if is_lazy:
                    lazy.append((text, None, attrib, f))
                else:
                    attrib[f] = s3_unicode(text)
        elif (fieldtype == 'upload'):
            if v:
                fileurl = None
                if dbfield.custom_retrieve_file_properties:
                    prop = dbfield.custom_retrieve_file_properties(v)
                else:
                    prop = dbfield.retrieve_file_properties(v)
                filename = prop['filename']
                folder = prop['path']
                if folder:
                    path = os.path.relpath(folder, current.request.folder).split(os.sep)
                    if ((path[0] == 'static') and (len(path) > 1)):
                        path.append(filename)
                        fileurl = URL(c=path[0], f=path[1], args=path[2:])
                if (fileurl is None):
                    fileurl = ('%s/%s' % (download_url, v))
                data = SubElement(elem, DATA)
                attr = data.attrib
                attr[FIELD] = f
                attr[FILEURL] = fileurl
                attr[ATTRIBUTE.filename] = s3_unicode(filename)
        elif (fieldtype == 'password'):
            data = SubElement(elem, DATA)
            data.attrib[FIELD] = f
            data.text = v
        elif (fieldtype == 'blob'):
            continue
        else:
            data = SubElement(elem, DATA)
            attr = data.attrib
            attr[FIELD] = f
            if (represent or (fieldtype not in ('string', 'text'))):
                if (value is None):
                    value = to_json(v).decode('utf-8')
                attr[VALUE] = value
            if is_lazy:
                lazy.append((text, data, None, None))
            else:
                data.text = text
    if (url and (not deleted)):
        attrib[FILEURL] = url
    if postprocess:
        postprocess(elem, record)
    return elem
