def define_tables(self): """Defines the auth_user table""" db = self.db Field = db.Field if not "auth_user" in db.tables: ne = IS_NOT_EMPTY() if self.password_complexity: requires = [IS_STRONG(**self.password_complexity), CRYPT()] else: requires = [CRYPT()] auth_fields = [ Field( "email", requires=(IS_EMAIL(), IS_NOT_IN_DB(db, "auth_user.email")), unique=True, ), Field( "password", "password", requires=requires, readable=False, writable=False, ), Field("first_name", requires=ne), Field("last_name", requires=ne), Field("profile_pic", default="https://merriam-webster.com/assets/mw/images/article/art-wap-landing-mp-lg/[email protected]"), Field("sso_id", readable=False, writable=False), Field("action_token", readable=False, writable=False), Field( "last_password_change", "datetime", default=None, readable=False, writable=False, ), ] if self.use_username: auth_fields.insert( 0, Field( "username", requires=[ne, IS_NOT_IN_DB(db, "auth_user.username")], unique=True, ), ) if self.use_phone_number: auth_fields.insert( 2, Field( "phone_number", requires=[ ne, IS_MATCH(r"^[+]?(\(\d+\)|\d+)(\(\d+\)|\d+|[ -])+$"), ], ), ) if self.block_previous_password_num is not None: auth_fields.append( Field("past_passwords_hash", "list:string", writable=False, readable=False)) db.define_table("auth_user", *auth_fields, *self.extra_auth_user_fields)
def define_tables(self): """Defines the auth_user table""" db = self.db Field = db.Field if not "auth_user" in db.tables: ne = IS_NOT_EMPTY() auth_fields = [ Field( "email", requires=(IS_EMAIL(), IS_NOT_IN_DB(db, "auth_user.email")), unique=True, ), Field( "password", "password", requires=CRYPT(), readable=False, writable=False, ), Field("first_name", requires=ne), Field("last_name", requires=ne), Field("sso_id", readable=False, writable=False), Field("action_token", readable=False, writable=False), ] if self.use_username: auth_fields.insert( 0, Field( "username", requires=[ne, IS_NOT_IN_DB(db, "auth_user.username")], unique=True, ), ) db.define_table("auth_user", *auth_fields, *self.extra_auth_user_fields)
def define_tables(self): """Defines the auth_user table""" db = self.db Field = db.Field if not "auth_user" in db.tables: ne = IS_NOT_EMPTY() if self.password_complexity: requires = [IS_STRONG(**self.password_complexity), CRYPT()] else: requires= [CRYPT()] auth_fields = [ Field( "email", requires=(IS_EMAIL(), IS_NOT_IN_DB(db, "auth_user.email")), unique=True, ), Field( "password", "password", requires=requires, readable=False, writable=False, ), Field("first_name", requires=ne), Field("last_name", requires=ne), Field("sso_id", readable=False, writable=False), Field("action_token", readable=False, writable=False), Field( "last_password_change", "datetime", default=None, readable=False, writable=False, ), ] if self.use_username: auth_fields.insert( 0, Field( "username", requires=[ne, IS_NOT_IN_DB(db, "auth_user.username")], unique=True, ), ) if self.use_phone_number: auth_fields.insert( 2, Field( "phone_number", requires=[ ne, IS_MATCH(r"^[+]?(\(\d+\)|\d+)(\(\d+\)|\d+|[ -])+$"), ], ), ) if self.block_previous_password_num is not None: auth_fields.append( Field("past_passwords_hash", "list:string", writable=False, readable=False)) db.define_table("auth_user", *auth_fields, *self.extra_auth_user_fields)
def define_tables(self): db = self.db Field = db.Field if not 'auth_user' in db.tables: ne = IS_NOT_EMPTY() db.define_table( 'auth_user', Field('username', requires=[ne, IS_NOT_IN_DB(db, 'auth_user.username')], unique=True), Field('email', requires=(IS_EMAIL(), IS_NOT_IN_DB(db, 'auth_user.email')), unique=True), Field('password','password', requires=CRYPT(), readable=False, writable=False), Field('first_name', requires=ne), Field('last_name', requires=ne), Field('sso_id', readable=False, writable=False), Field('action_token', readable=False, writable=False), *self.extra_auth_user_fields)
def setUp(self): db = DAL("sqlite:memory") db.define_table("color", Field("name", requires=IS_NOT_IN_DB(db, "color.name"))) db.color.insert(name="red") db.color.insert(name="green") db.color.insert(name="blue") db.define_table("thing", Field("name"), Field("color", "reference color")) db.thing.insert(name="Chair", color=1) db.thing.insert(name="Chair", color=2) db.thing.insert(name="Table", color=1) db.thing.insert(name="Table", color=3) db.thing.insert(name="Lamp", color=2) db.define_table( "rel", Field("a", "reference thing"), Field("desc"), Field("b", "reference thing"), ) db.rel.insert(a=1, b=2, desc="is like") db.rel.insert(a=3, b=4, desc="is like") db.rel.insert(a=1, b=3, desc="is under") db.rel.insert(a=2, b=4, desc="is under") db.rel.insert(a=5, b=4, desc="is above") api = RestAPI(db, ALLOW_ALL_POLICY) self.db = db self.api = api
def setUp(self): db = DAL('sqlite:memory') db.define_table('color', Field('name', requires=IS_NOT_IN_DB(db, 'color.name'))) db.color.insert(name='red') db.color.insert(name='green') db.color.insert(name='blue') db.define_table('thing', Field('name'), Field('color', 'reference color')) db.thing.insert(name='Chair', color=1) db.thing.insert(name='Chair', color=2) db.thing.insert(name='Table', color=1) db.thing.insert(name='Table', color=3) db.thing.insert(name='Lamp', color=2) db.define_table('rel', Field('a', 'reference thing'), Field('desc'), Field('b', 'reference thing')) db.rel.insert(a=1, b=2, desc='is like') db.rel.insert(a=3, b=4, desc='is like') db.rel.insert(a=1, b=3, desc='is under') db.rel.insert(a=2, b=4, desc='is under') db.rel.insert(a=5, b=4, desc='is above') api = DBAPI(db, ALLOW_ALL_POLICY) self.db = db self.api = api
def __init__(self, projectConfig, DALDatabase): self.DALDatabase = DALDatabase self.DALDatabase.define_table( 'auth_cas', Field('email', 'string', notnull=True, unique=True), Field('password_hash', 'string', notnull=True, requires=IS_NOT_EMPTY())) self.DALDatabase.auth_cas.email.requires = [ IS_EMAIL(), IS_NOT_IN_DB(self.DALDatabase, self.DALDatabase.auth_cas.email, error_message="Email already in database.") ] self.DALDatabase.define_table( 'cas_activity', Field('auth_cas', 'reference auth_cas', requires=IS_IN_DB(self.DALDatabase, self.DALDatabase.auth_cas)), Field('request', 'text'), Field('activity', 'string'), Field('date_activity', 'datetime', default=datetime.now())) self.DALDatabase.define_table( 'apps_authorization', Field('app_name', 'string'), Field('auth_cas', 'reference auth_cas', requires=IS_IN_DB(self.DALDatabase, self.DALDatabase.auth_cas)), Field('origin', 'string'), Field('authorization', 'text', requires=IS_NOT_EMPTY())) self.DALDatabase.apps_authorization.app_name.requires = [ IS_NOT_EMPTY(), IS_NOT_IN_DB(self.DALDatabase, self.DALDatabase.apps_authorization.app_name, error_message="Email already in database.") ]
import os from py4web import DAL, Field from pydal.validators import IS_NOT_EMPTY, IS_NOT_IN_DB # define database and tables db = DAL('sqlite://storage.db', folder=os.path.join(os.path.dirname(__file__), 'databases')) # simple table example db.define_table('person', Field('name', requires=IS_NOT_IN_DB(db, 'person.name')), Field('job', requires=IS_NOT_EMPTY())) # simple reference example db.define_table('superhero', Field('name'), Field('real_identity', 'reference person')) db.define_table('superpower', Field('description')) # many to many example db.define_table('tag', Field('superhero', 'reference superhero'), Field('superpower', 'reference superpower'), Field('strength', 'integer')) if not db(db.person).count(): db.person.insert(name='Clark Kent', job='Journalist') db.person.insert(name='Peter Park', job='Photographer') db.person.insert(name='Bruce Wayne', job='CEO') db.superhero.insert(name='Superman', real_identity=1) db.superhero.insert(name='Spiderman', real_identity=2) db.superhero.insert(name='Batman', real_identity=3) db.superpower.insert(description='Flight')
def __init__(self, projectConfig, DALDatabase, i18nTranslator=None, logger_api=None): self.DALDatabase = DALDatabase default_language = projectConfig["PROJECT"].get( "default_language", "en-US") self.logger_api = logger_api self.DALDatabase.define_table( 'auth_user', Field('first_name', 'string', notnull=True, requires=IS_NOT_EMPTY(), phanterpwa={ 'group': 'group1', '_class': 'p-col w1p100 w4p70 e-float_right' }), Field('last_name', 'string', notnull=True, requires=IS_NOT_EMPTY(), phanterpwa={ 'group': 'group1', '_class': 'p-col w1p100 w4p70 e-float_right' }), Field('email', 'string', notnull=True, unique=True, phanterpwa={ 'group': 'group1', '_class': 'p-col w1p100 w4p70 e-float_right' }), Field('email_activated', 'boolean', default=False), Field('fone_number', 'string', phanterpwa={'out_of_form': True}), Field('fone_number_activated', 'boolean', default=False), Field('password_hash', 'string', notnull=True, requires=IS_NOT_EMPTY(), phanterpwa={'out_of_form': True}), Field('login_attempts', 'integer', default=0), Field( 'datetime_next_attempt_to_login', 'datetime', requires=IS_EMPTY_OR(IS_DATETIME()), phanterpwa={ 'validators': ['IS_EMPTY_OR', 'IS_DATETIME:yyyy-MM-dd HH:mm:ss'], # '_class': 'p-col w1p100 w4p40', # 'section': 'Identidade' }), Field('temporary_password', 'text', phanterpwa={'out_of_form': True}), # it's used in the debug Field('temporary_password_hash', 'text', phanterpwa={'out_of_form': True}), # datetime_next_attempt_to_login Field('temporary_password_expire', 'datetime', requires=IS_EMPTY_OR(IS_DATETIME()), phanterpwa={ 'validators': ['IS_EMPTY_OR', 'IS_DATETIME:yyyy-MM-dd HH:mm:ss'] }), Field('timeout_to_resend_temporary_password_mail', 'datetime', requires=IS_EMPTY_OR(IS_DATETIME()), phanterpwa={ 'validators': ['IS_EMPTY_OR', 'IS_DATETIME:yyyy-MM-dd HH:mm:ss'] }), Field('activation_code_by_mobile', 'string', default=0), Field('activation_code', 'string', default=0), Field('activation_attempts', 'integer', default=0), Field('timeout_to_resend_activation_email', 'datetime', requires=IS_EMPTY_OR(IS_DATETIME()), phanterpwa={ 'validators': ['IS_EMPTY_OR', 'IS_DATETIME:yyyy-MM-dd HH:mm:ss'] }), # wait_time_to_try_activate_again Field('datetime_next_attempt_to_activate', 'datetime', requires=IS_EMPTY_OR(IS_DATETIME()), phanterpwa={ 'validators': ['IS_EMPTY_OR', 'IS_DATETIME:yyyy-MM-dd HH:mm:ss'] }), Field('permit_mult_login', 'boolean', default=True), Field('activated', 'boolean', default=False, notnull=True), Field('websocket_opened', 'boolean', default=False, notnull=True, phanterpwa={'out_of_form': True}), Field('locale', 'string', default=default_language), Field('two_factor_login', 'boolean', default=False)) def delete_upload_folder(s): upload_folder = os.path.join(projectConfig["PROJECT"]["path"], "backapps", "api", "uploads") target = os.path.join(upload_folder, "user_{0}".format(s.select().first().id)) if os.path.exists(target) and os.path.isdir(target): try: shutil.rmtree(target) except Exception: if self.logger_api: self.logger_api.error( "Problem on delete folder: \"{0}\"".format(target), exc_info=True) else: if self.logger_api: self.logger_api.warning( "Ther folder \"{0}\" not exists".format(target)) self.DALDatabase.auth_user._before_delete.append( lambda s: delete_upload_folder(s)) self.DALDatabase.auth_user.email.requires = [ IS_EMAIL(), IS_NOT_IN_DB(self.DALDatabase, self.DALDatabase.auth_user.email, error_message="Email already in database.") ] self.DALDatabase.auth_user.fone_number.requires = [ IS_EMPTY_OR( IS_NOT_IN_DB( self.DALDatabase, self.DALDatabase.auth_user.fone_number, error_message="Mobile number already in database.")) ] self.DALDatabase.define_table('auth_group', Field('role', 'string'), Field('grade', 'integer', default=0), Field('description', 'text')) self.DALDatabase.define_table( 'auth_membership', Field('auth_user', 'reference auth_user', requires=IS_IN_DB(self.DALDatabase, self.DALDatabase.auth_user)), Field('auth_group', 'reference auth_group', requires=IS_IN_DB(self.DALDatabase, self.DALDatabase.auth_group))) self.DALDatabase.define_table( 'auth_activity', Field('auth_user', 'reference auth_user', requires=IS_IN_DB(self.DALDatabase, self.DALDatabase.auth_user)), Field('request', 'text'), Field('activity', 'string'), Field('date_activity', 'datetime', default=datetime.now())) self.DALDatabase.define_table( 'email_user_list', Field('auth_user', 'reference auth_user', requires=IS_IN_DB(self.DALDatabase, self.DALDatabase.auth_user)), Field('email', 'string', notnull=True), Field('datetime_changed', 'datetime', default=datetime.now())) self.DALDatabase.define_table( 'social_auth', Field('social_name', 'string'), Field('request_state', 'text'), Field('client_token', 'text'), Field('datetime_created', 'datetime', default=datetime.now()), Field('origin', 'text'), Field('used', 'boolean', default=False), Field('user_credentials', 'text')) self.DALDatabase.define_table( 'two_factor_login', Field('auth_user', 'reference auth_user', requires=IS_IN_DB(self.DALDatabase, self.DALDatabase.auth_user)), Field('two_factor_url', 'text'), Field('two_factor_code'), Field('datetime_changed', 'datetime', default=datetime.now())) self.DALDatabase.email_user_list.email.requires = [IS_EMAIL()] if self.DALDatabase(self.DALDatabase.auth_group).isempty(): self.DALDatabase.auth_group.insert( role="root", grade=100, description="Administrator of application (Developer)") self.DALDatabase.auth_group.insert( role="administrator", grade=10, description="Super user of site") self.DALDatabase.auth_group.insert(role="user", grade=1, description="Default user") self.DALDatabase.commit() if self.DALDatabase(self.DALDatabase.auth_membership).isempty(): if self.DALDatabase.auth_user[1]: id_role = self.DALDatabase(self.DALDatabase.auth_group.role == 'root').select().first() if id_role: self.DALDatabase.auth_membership.insert( auth_user=1, auth_group=id_role.id) self.DALDatabase.commit()