Skip to content

Instantly share code, notes, and snippets.

@appcove
Created September 13, 2013 05:08
Show Gist options
  • Save appcove/6546950 to your computer and use it in GitHub Desktop.
Save appcove/6546950 to your computer and use it in GitHub Desktop.
Sample AppStruct.Base.V1 code
# vim:encoding=utf-8:ts=2:sw=2:expandtab
from Project import *
from AppStruct.Base.V1 import *
from AppStruct.Security import SHA1, RandomHex
###############################################################################
class Admin(metaclass=MetaRecord):
#============================================================================
@classmethod
def List(cls):
tmp = App.DB.ValueList('''
SELECT
"Admin_MNID"
FROM
"Main"."Admin"
ORDER BY
"Admin_MNID"
'''
)
return [cls(id) for id in tmp]
#============================================================================
class Admin_MNID(Integer):
Flags = +UpdateRead
class Admin_GSID(String):
Flags = +Read +Write +InsertRequired
MaxLength = 64
MinLength = 64
@property
def InsertValue(self):
return RandomHex()
class CreateDate(Datetime):
Flags = +UpdateRead
InsertDefault = SQL('NOW()')
class FirstName(String):
Flags = +Read +Write +InsertRequired
Label = 'First Name'
MaxLength = 35
class LastName(String):
Flags = +Read +Write +InsertRequired
MaxLength = 35
Label = 'Last Name'
class Email(String):
Flags = +Read +Write +InsertRequired
MaxLength = 100
Label = 'Primary Email'
class Phone(String):
Flags = +Read +Write +InsertRequired
MaxLength = 20
Label ='Phone'
class Login_Username(String):
Flags = +Read +Write +InsertRequired
AllowNone = False
MaxLength = 40
Label = 'User Name'
def Validate(self, record, fv):
if not String.Validate(self, record, fv):
return False
if App.DB.Bool('''
SELECT true
FROM "Admin"
WHERE "Login_Username" = $A
AND "Admin_MNID" != $B::int
''',
A = fv.value,
B = record.PrimaryKey[0] if record.PrimaryKey else 0,
):
fv.AddError("Username '{un}' already exists", un=fv.value)
return False
return True
class Login_Password(String):
Flags = +Read +Write +Virtual
MaxLength = 40
Label = 'Password '
def Validate(self, record, fv):
if not String.Validate(self, record, fv):
return False
App.Log(record.__dict__['_CurValueMap'])
record.Login_Password_Hash = SHA1(fv.value + str(record.Login_Password_Salt)) #todo, salt should not be Nullable
return True
class Login_Password_Hash(String):
Flags = +Read +Write +InsertRequired
AllowNone = False
MinLength = 40
MaxLength = 40
Label = 'Password Hash'
class Login_Password_Salt(String):
Flags = +Read +Write +InsertRequired
AllowNone = False
MinLength = 60
MaxLength = 60
@property
def InsertValue(self):
return RandomHex()[:60]
class Perm_Active(Boolean):
Flags = +Read +Write +InsertRequired
InsertDefault = True
class ProfilePic_Large_GSID(String):
Flags = +Read +Write
AllowNone = True
MaxLength = 40
MinLength = 40
class ProfilePic_Small_GSID(String):
Flags = +Read +Write
AllowNone = True
MaxLength = 40
MinLength = 40
PrimaryKeyFields = ['Admin_MNID']
#============================================================================
def SELECT(self, fields):
return App.DB.Row('''
SELECT
[Field]
FROM
"Main"."Admin"
WHERE True
AND "Admin_MNID" = $ID
''',
*fields,
ID=self.PrimaryKey[0]
)
def INSERT(self, data):
return App.DB.TRow('''
INSERT INTO
"Main"."Admin"
([Field])
VALUES
([Value])
RETURNING
"Admin_MNID"
''',
*data.items()
)
def UPDATE(self, data):
App.DB.Execute('''
UPDATE
"Main"."Admin"
SET
[Field=Value]
WHERE True
AND "Admin_MNID" = $ID
''',
*data.items(),
ID=self.PrimaryKey[0]
)
def DELETE(self):
App.DB.Execute('''
DELETE FROM
"Main"."Admin"
WHERE True
AND "Admin_MNID" = $ID
''',
ID=self.PrimaryKey[0]
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment