Ok so I came up with a little bit of a different approach then you have, it
seems to be working for me. I did this pretty fast so it will require some
improvements.
First create a module:
test.py
#!/usr/bin/env python
# coding: utf8
from gluon import *
class TestMe(object):
def __init__(self, db):
self.db = db
self.T = current.T
self.auth = current.session.auth
self.tablename = "test_me"
self.define_tables()
def define_tables(self):
if self.tablename not in self.db.tables:
table = self.db.define_table(self.tablename,
Field('secret'),
Field('answer')
)
def save_form(self, form_data):
self.db[self.tablename].insert(secret=form_data['secret'],
answer=form_data['answer'])
def get_form(self):
return FORM(
FIELDSET(
LABEL(self.T('Secret Question'), _for="secret"),
INPUT(_type="text", _name="secret", _id="secret",
requires=IS_NOT_EMPTY(error_message=self.T('please enter a secret
question!'))),
BR(),
LABEL(self.T('Secret Answer'), _for="answer"),
INPUT(_type="text", _name="answer", _id="answer",
requires=IS_NOT_EMPTY(error_message=self.T('please enter a secret
answer!'))),
BR(),
INPUT(_type="submit", _value="Save Secret", _id="save")
)
)
Next create a controller to test:
testme.py
# coding: utf8
# try something like
def index():
from test import TestMe
secret = TestMe(db)
secret_form = secret.get_form()
if secret_form.accepts(request,session):
secret.save_form(secret_form.vars)
session.flash = T("Your secret was saved")
redirect("testme")
return dict(form=secret_form)
Finally create a view:
testme/index.py
{{extend 'layout.html'}}
<h1>This is an example of TestMe</h1>
{{=form}}
Now you can view the form, save, get errors etc... but if you go to
appadmin the database table wont show up.
To fix this edit appadmin.py:
from test import TestMe
testme = TestMe(db)
Now the database table is only ever created if it isn't in the db.tables
object, and you can use it per action, and in appadmin.
--
Regards,
Bruce
On Wed, Feb 1, 2012 at 10:12 AM, Bruce Wade <[email protected]> wrote:
> Actually just tested the same problem happens with all datamodels I
> created and try to save from using appadmin.
> appadmin.py:
> from myapp import MyApp
> app = MyApp()
> auth = app.auth
> #from datamodel.post import Post as PostModel
> from datamodel.user import UserAccount
> from datamodel.dist import Distributor
> from datamodel.lang import Language
> db = app.db([UserAccount, Distributor, Language])
>
> Traceback (most recent call last):
>
> File
> "/home/bruce/Development/bossteam_dev/projects/yaw_dev/gluon/restricted.py",
> line 204, in restricted
>
> exec ccode in environment
>
> File
> "/home/bruce/Development/bossteam_dev/projects/yaw_dev/applications/welcome/controllers/appadmin.py"
> <http://127.0.0.1:8000/admin/edit/welcome/controllers/appadmin.py>, line
> 444, in <module>
>
> File
> "/home/bruce/Development/bossteam_dev/projects/yaw_dev/gluon/globals.py",
> line 172, in <lambda>
>
> self._caller = lambda f: f()
>
> File
> "/home/bruce/Development/bossteam_dev/projects/yaw_dev/applications/welcome/controllers/appadmin.py"
> <http://127.0.0.1:8000/admin/edit/welcome/controllers/appadmin.py>, line
> 139, in insert
>
> if form.accepts(request.vars, session):
>
> File
> "/home/bruce/Development/bossteam_dev/projects/yaw_dev/gluon/sqlhtml.py",
> line 1267, in accepts
>
> self.vars.id = self.table.insert(**fields)
>
> File "/home/bruce/Development/bossteam_dev/projects/yaw_dev/gluon/dal.py",
> line 5597, in insert
>
> return self._db._adapter.insert(self,self._listify(fields))
>
> File "/home/bruce/Development/bossteam_dev/projects/yaw_dev/gluon/dal.py",
> line 914, in insert
>
> raise e
>
> OperationalError: no such table: user_account
>
>
> On Wed, Feb 1, 2012 at 10:08 AM, Bruce Wade <[email protected]> wrote:
>
>> Ok thanks that makes things clear.
>>
>> I am having another issue:
>>
>> from gluon.dal import Field
>> from basemodel import BaseModel
>> from gluon.validators import IS_NOT_EMPTY, IS_NOT_IN_DB
>> from gluon import current
>>
>> class Language(BaseModel):
>> tablename = "languages"
>>
>> def set_properties(self):
>> T = current.T
>>
>> self.fields = [
>> Field('code'),
>> Field('language')
>> ]
>>
>> self.validators = {
>> 'code': [IS_NOT_EMPTY(), IS_NOT_IN_DB(self.db,
>> 'Language.code')]
>> }
>>
>> self.lables = {
>> 'code': T('code'),
>> 'language': T('Language')
>> }
>>
>> When saving I get the error the table languages does not exist. For the
>> validator I have tried: 'languages.code', 'Language.code',
>> self.db.languages.code, self.db.Language.code, self.entity.code. Tried
>> deleting the database but still same issue.
>>
>> --
>> Regards,
>> Bruce
>>
>> On Wed, Feb 1, 2012 at 9:37 AM, Bruno Rocha <[email protected]>wrote:
>>
>>> You can set visibility and other options, but you cant use
>>> self.auth.settings.table_user_
>>> name because at this point self.auth does not exist and it is a circular
>>> reference given the fact that User class is the base for the creation of
>>> Auth object in that example.
>>>
>>> But, in most cases you will name it "auth_user" so no problem to set it
>>> in tablename attribute.
>>>
>>>
>>> On Wed, Feb 1, 2012 at 3:12 PM, Bruce Wade <[email protected]> wrote:
>>>
>>>> This is how I originally thought to do it. (Would have posted this last
>>>> night but code was at the office)
>>>>
>>>> class User(BaseAuth):
>>>>
>>>> def set_properties(self):
>>>> tablename = self.auth.settings.table_user_name
>>>>
>>>> T = current.T
>>>> # take a look in basemodel to see the options allowed here
>>>> # self.widgets, self.fields, self.validators etc....
>>>> # only extra fields are defined here
>>>>
>>>> self.fields = [
>>>> Field('username', length=128, default='0000000',
>>>> unique=True),
>>>> Field('account_id', self.db.user_account),
>>>> Field('email', length=128, default=''),
>>>> Field('password', 'password', length=512),
>>>> Field('security', 'password', length=512),
>>>> Field('registration_key', length=512),
>>>> Field('reset_password_key', length=512),
>>>> Field('reset_security_key', length=512),
>>>> Field('registration_id', length=512)
>>>> ]
>>>>
>>>> self.visibility = {
>>>> "password": (False, False),
>>>> "security": (False, False),
>>>> "registration_key": (False, False),
>>>> "reset_password_key": (False, False),
>>>> "reset_security_key": (False, False),
>>>> "registration_id": (False, False)
>>>> }
>>>>
>>>> self.validators = {
>>>> 'password': [IS_STRONG(), CRYPT()],
>>>> 'security': [IS_STRONG(), CRYPT()],
>>>> 'email':
>>>> [IS_EMAIL(error_message=self.auth.messages.invalid_email)]
>>>> }
>>>>
>>>> self.labels = {
>>>> "username": T("Username"),
>>>> "email": T("Email"),
>>>> "password": T("Your Password"),
>>>> "security": T("Security Password")
>>>> }
>>>>
>>>> self.auth.settings.table_user = self
>>>>
>>>> --
>>>> Regards,
>>>> Bruce
>>>>
>>>> On Tue, Jan 31, 2012 at 9:33 PM, Bruce Wade <[email protected]>wrote:
>>>>
>>>>> Thanks that will do it, I was close to that exact same approach when I
>>>>> was playing around today.
>>>>>
>>>>> --
>>>>> Regards,
>>>>> Bruce
>>>>>
>>>>>
>>>>> On Tue, Jan 31, 2012 at 7:54 PM, Bruno Rocha <[email protected]>wrote:
>>>>>
>>>>>> Hi Bruce,
>>>>>>
>>>>>> delete the database if the sample app created one, then replace
>>>>>> modules/datamodel/user.py with this https://gist.github.com/1714979
>>>>>>
>>>>>>
>>>>>> --
>>>>>>
>>>>>> Bruno Rocha
>>>>>> [http://rochacbruno.com.br]
>>>>>>
>>>>>>
>>>>>
>>>>>
>>>>> --
>>>>> --
>>>>> Regards,
>>>>> Bruce Wade
>>>>> http://ca.linkedin.com/in/brucelwade
>>>>> http://www.wadecybertech.com
>>>>> http://www.warplydesigned.com
>>>>> http://www.fitnessfriendsfinder.com
>>>>>
>>>>
>>>>
>>>>
>>>> --
>>>> --
>>>> Regards,
>>>> Bruce Wade
>>>> http://ca.linkedin.com/in/brucelwade
>>>> http://www.wadecybertech.com
>>>> http://www.warplydesigned.com
>>>> http://www.fitnessfriendsfinder.com
>>>>
>>>
>>>
>>>
>>> --
>>>
>>> Bruno Rocha
>>> [http://rochacbruno.com.br]
>>>
>>>
>>
>>
>> --
>> --
>> Regards,
>> Bruce Wade
>> http://ca.linkedin.com/in/brucelwade
>> http://www.wadecybertech.com
>> http://www.warplydesigned.com
>> http://www.fitnessfriendsfinder.com
>>
>
>
>
> --
> --
> Regards,
> Bruce Wade
> http://ca.linkedin.com/in/brucelwade
> http://www.wadecybertech.com
> http://www.warplydesigned.com
> http://www.fitnessfriendsfinder.com
>
--
--
Regards,
Bruce Wade
http://ca.linkedin.com/in/brucelwade
http://www.wadecybertech.com
http://www.warplydesigned.com
http://www.fitnessfriendsfinder.com