Last active
December 11, 2015 22:18
-
-
Save appcove/4668572 to your computer and use it in GitHub Desktop.
AppStruct Examples
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
FlagType | |
An extended `int` type which implements a very convenient mechansim for | |
defining flags and operating on them. | |
One of the primary uses is the convenient syntax: | |
flags = +front -back +side -top | |
Calling the unary operator (__pos__, __neg__) by prefixing one of the | |
instances of this class with (+ or -) will result in a new default | |
instance being created, and then adding the instance to that default | |
or subtracting the instance from that default. | |
Considering this definition: | |
class color(FlagType): | |
red = 1 | |
blue = 2 | |
green = 4 | |
_default_ = red | |
Then: | |
flags = color.blue + color.green | |
Is very different from: | |
flags = +color.blue +color.green | |
Because the first example simply performs a bitwise OR between blue and green | |
where the second example first creates a default value, and then performs | |
a bitwise OR with first blue, and then green. This is the perferred syntax. | |
---- | |
Usage: | |
class color(FlagType): | |
red = 0b0001 | |
blue = 0b0010 | |
purple = 0b0011 | |
_default_ = red | |
color() | |
:: <color (0b1) {'blue': False, 'purple': False, 'red': True}> | |
flags = +color.blue | |
flags | |
:: # Note the default of red came through | |
:: # Note that purple is also true because blue and red are set | |
:: <color (0b11) {'blue': True, 'purple': True, 'red': True}> | |
flags.blue | |
:: True | |
flags.red | |
:: True | |
flags -= color.blue | |
flags.blue | |
:: False | |
flags.purple | |
:: False | |
flags.red | |
:: True | |
flags | |
:: <color (0b1) {'blue': False, 'purple': False, 'red': True}> | |
flags[color.red] | |
:: True |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# vim:encoding=utf-8:ts=2:sw=2:expandtab | |
from AppCove import * | |
import AppCove.User | |
from AppStruct.Security import RandomHex | |
import time | |
############################################################################### | |
@Expose | |
def Request(self): | |
self.Auth_Required = False | |
yield | |
Username = IN_Str(self.Post.Username) | |
Password = IN_Str(self.Post.Password) | |
URI = self.Get.URI or '/' | |
Remember = ('Remember' in self.Post) | |
ErrorMessage = self.Get.EM | |
InfoMessage = self.Get.IM | |
if Username or Password: | |
try: | |
User_MNID = AppCove.User.AuthenticateByUsernameAndPassword(Username, Password) | |
self.SessionSetInteger('Auth.User_MNID', User_MNID) | |
self.SessionSetInteger('Auth.LoginTime', int(time.time())) | |
yield self.RedirectResponse(URI) | |
except AuthenticationError as e: | |
self.UI.Error.Add(e) | |
else: | |
if ErrorMessage: | |
self.UI.Error.Add(ErrorMessage) | |
if InfoMessage: | |
self.UI.Info.Add(InfoMessage) | |
#---------------------------------------------------------------------------- | |
self.UI.Title = 'Login' | |
#---------------------------------------------------------------------------- | |
W = self.UI.Script | |
W(''' | |
$(function() { | |
$('#Username').focus(); | |
}); | |
''') | |
#---------------------------------------------------------------------------- | |
W = self.UI.Body | |
W(''' | |
<form name="Login" method="post" action=''' + QA(App.URL_HTTPS + self.Env.URI) + '''> | |
<h2>Login to your account</h2> | |
<div class="vfield"> | |
<label for="Username">Username:</label><br /> | |
<input type="text" name="Username" id="Username" size="30" value=''' + QA(Username) + ''' /> | |
</div> | |
<div class="vfield"> | |
<label for="Password">Password:</label><br /> | |
<input type="password" name="Password" size="18" /> | |
</div> | |
<div class="vfield"> | |
<input type="checkbox" name="Remember" ''' + ('checked="checked"' if Remember else '') + ''' /> | |
<label for="Remember">Keep me logged in</label> | |
</div> | |
<div class="vfield"> | |
<input type="submit" value="Login" /> | |
</div> | |
<div class="vfield"> | |
<a href="/resetpassword">Forgot your password?</a> | |
</div> | |
</form> | |
''') | |
#---------------------------------------------------------------------------- | |
yield self.UI | |
############################################################################### | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# vim:encoding=utf-8:ts=2:sw=2:expandtab | |
from AppCove import * | |
from AppStruct.Base.V1 import * | |
############################################################################### | |
class Location(metaclass=MetaRecord): | |
#============================================================================ | |
class Location_MNID(Integer): | |
Flags = +UpdateRead | |
InsertValue = SQL('''nextval("Main"."Seq"::regclass)''') | |
#============================================================================ | |
class CreateDate(Datetime): | |
Flags = +UpdateRead | |
InsertValue = SQL('NOW()') | |
#============================================================================ | |
class Name(String): | |
Flags = +Read +Write +InsertRequired | |
MaxLength = 20 | |
def Validate(self): | |
if self.value == 'Jason': | |
self.adderror('You cannot use the name Jason') | |
#============================================================================ | |
class Note(String): | |
Flags = +Read +Write -Required | |
InsertValue = '' | |
#============================================================================ | |
class Note(String): | |
Flags = +Read +Write -Required -InsertRead | |
#=========================================================================== | |
_= PrimaryKey(Location_MNID, Location_MNID) | |
_= TableName('Network', 'Location') | |
_= GetDB(lambda: App.DB) | |
############################################################################### | |
x = Location(129128) | |
x.Name | |
>>> "Joe Smith" | |
x.Name = "Sam Smith" | |
x.Save() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment