'Field shortcut creation:
Takes field names `db_*` and creates property wrappers named
without the `db_` prefix. So db_key -> key
This wrapper happens on the class level, so there is no
overhead when creating objects.  If a class already has a
wrapper of the given name, the automatic creation is skipped.
Notes:
Remember to document this auto-wrapping in the class
header, this could seem very much like magic to the user
otherwise.'

def __new__(cls, name, bases, attrs):

    attrs['typename'] = cls.__name__
    attrs['path'] = ('%s.%s' % (attrs['__module__'], name))
    attrs['_is_deleted'] = False
    def create_wrapper(cls, fieldname, wrappername, editable=True, foreignkey=False):
        'Helper    method    to    create    property    wrappers    with    unique    names    (must    be    in    separate    call)'
        def _get(cls, fname):
            'Wrapper    for    getting    database    field'
            if _GA(cls, '_is_deleted'):
                raise ObjectDoesNotExist(('Cannot    access    %s:    Hosting    object    was    already    deleted.' % fname))
            return _GA(cls, fieldname)
        def _get_foreign(cls, fname):
            'Wrapper    for    returning    foreignkey    fields'
            if _GA(cls, '_is_deleted'):
                raise ObjectDoesNotExist(('Cannot    access    %s:    Hosting    object    was    already    deleted.' % fname))
            return _GA(cls, fieldname)
        def _set_nonedit(cls, fname, value):
            'Wrapper    for    blocking    editing    of    field'
            raise FieldError(('Field    %s    cannot    be    edited.' % fname))
        def _set(cls, fname, value):
            'Wrapper    for    setting    database    field'
            if _GA(cls, '_is_deleted'):
                raise ObjectDoesNotExist(('Cannot    set    %s    to    %s:    Hosting    object    was    already    deleted!' % (fname, value)))
            _SA(cls, fname, value)
            update_fields = ([fname] if (_GA(cls, '_get_pk_val')(_GA(cls, '_meta')) is not None) else None)
            _GA(cls, 'save')(update_fields=update_fields)
        def _set_foreign(cls, fname, value):
            'Setter    only    used    on    foreign    key    relations,    allows    setting    with    #dbref'
            if _GA(cls, '_is_deleted'):
                raise ObjectDoesNotExist(('Cannot    set    %s    to    %s:    Hosting    object    was    already    deleted!' % (fname, value)))
            if isinstance(value, (basestring, int)):
                value = to_str(value, force_string=True)
                if (value.isdigit() or value.startswith('#')):
                    dbid = dbref(value, reqhash=False)
                    if dbid:
                        model = _GA(cls, '_meta').get_field(fname).model
                        try:
                            value = model._default_manager.get(id=dbid)
                        except ObjectDoesNotExist:
                            pass
            _SA(cls, fname, value)
            update_fields = ([fname] if (_GA(cls, '_get_pk_val')(_GA(cls, '_meta')) is not None) else None)
            _GA(cls, 'save')(update_fields=update_fields)
        def _del_nonedit(cls, fname):
            'wrapper    for    not    allowing    deletion'
            raise FieldError(('Field    %s    cannot    be    edited.' % fname))
        def _del(cls, fname):
            'Wrapper    for    clearing    database    field    -    sets    it    to    None'
            _SA(cls, fname, None)
            update_fields = ([fname] if (_GA(cls, '_get_pk_val')(_GA(cls, '_meta')) is not None) else None)
            _GA(cls, 'save')(update_fields=update_fields)
        fget = (lambda cls: _get(cls, fieldname))
        if (not editable):
            fset = (lambda cls, val: _set_nonedit(cls, fieldname, val))
        elif foreignkey:
            fget = (lambda cls: _get_foreign(cls, fieldname))
            fset = (lambda cls, val: _set_foreign(cls, fieldname, val))
        else:
            fset = (lambda cls, val: _set(cls, fieldname, val))
        fdel = (lambda cls: (_del(cls, fieldname) if editable else _del_nonedit(cls, fieldname)))
        fget.__doc__ = ('A    wrapper    for    getting    database    field    `%s`.' % fieldname)
        fset.__doc__ = ('A    wrapper    for    setting    (and    saving)    database    field    `%s`.' % fieldname)
        fdel.__doc__ = ('A    wrapper    for    deleting    database    field    `%s`.' % fieldname)
        attrs[wrappername] = property(fget, fset, fdel)
    if (cls.__name__ in ('ServerConfig', 'TypeNick')):
        return
    for (fieldname, field) in ((fname, field) for (fname, field) in listitems(attrs) if (fname.startswith('db_') and (type(field).__name__ != 'ManyToManyField'))):
        foreignkey = (type(field).__name__ == 'ForeignKey')
        wrappername = ('dbid' if (fieldname == 'id') else fieldname.replace('db_', '', 1))
        if (wrappername not in attrs):
            create_wrapper(cls, fieldname, wrappername, editable=field.editable, foreignkey=foreignkey)
    return super(SharedMemoryModelBase, cls).__new__(cls, name, bases, attrs)
