All pastes #490751 Raw Edit

model.py arr

public python v1 · immutable
·published 2007-05-16 06:51 UTC
rendered paste body
from datetime import datetimefrom turbogears.database import PackageHubfrom sqlobject import *from turbogears import identity hub = PackageHub("autoscott")__connection__ = hub# class YourDataClass(SQLObject):#     passclass Customer(SQLObject):          # customer sign up info  (customer = business??)    Users = MultipleJoin('User')    # customer entity may have many users (lets see if this works)    Name = StringCol()              # corp name    ContactEmail = StringCol()      # official POC email (as opposed to user's own email)    Address = StringCol()           # one big address block (split this later?)    Phone = StringCol()             # corp phone (can add user phones to User if needed)    Fax = StringCol()               # corp fax       Jobs = MultipleJoin('Job')    TCs = RelatedJoin('TC')        # accepted T&Cs, if any    Created = DateTimeCol()class TC(SQLObject):                # there might be multiple terms&conditions entities    Name = StringCol()    Body = StringCol()              # it's txt    Bin = BLOBCol()    Customers = RelatedJoin('Customer')    Created = DateTimeCol()class LogEntry(SQLObject):    Stamp = DateTimeCol()    Txt = StringCol()    # determine bindings to jobs, customers, resources as we go...class Job(SQLObject):    Customer = ForeignKey('Customer')    Status = IntCol()                # one of these will be used.. not sure which yet.    StatusTxt = StringCol()    #Requested = DateTimeCol()       # maybe simplify by integrating with LogEntryclass Rack(SQLObject):    Name = StringCol()    Shelves = MultipleJoin('Shelf')    class Shelf(SQLObject):    Name = StringCol()    Blades = MultipleJoin('Blade')    Rack = ForeignKey('Rack')class Blade(SQLObject):    Name = StringCol()    # current customer...? etc    Shelf = ForeignKey('Shelf')                # identity models.class Visit(SQLObject):    class sqlmeta:        table = "visit"    visit_key = StringCol(length=40, alternateID=True,                          alternateMethodName="by_visit_key")    created = DateTimeCol(default=datetime.now)    expiry = DateTimeCol()    def lookup_visit(cls, visit_key):        try:            return cls.by_visit_key(visit_key)        except SQLObjectNotFound:            return None    lookup_visit = classmethod(lookup_visit)class VisitIdentity(SQLObject):    visit_key = StringCol(length=40, alternateID=True,                          alternateMethodName="by_visit_key")    user_id = IntCol()class Group(SQLObject):    """    An ultra-simple group definition.    """    # names like "Group", "Order" and "User" are reserved words in SQL    # so we set the name to something safe for SQL    class sqlmeta:        table = "tg_group"    group_name = UnicodeCol(length=16, alternateID=True,                            alternateMethodName="by_group_name")    display_name = UnicodeCol(length=255)    created = DateTimeCol(default=datetime.now)    # collection of all users belonging to this group    users = RelatedJoin("User", intermediateTable="user_group",                        joinColumn="group_id", otherColumn="user_id")    # collection of all permissions for this group    permissions = RelatedJoin("Permission", joinColumn="group_id",                               intermediateTable="group_permission",                              otherColumn="permission_id")class User(SQLObject):    """    Reasonably basic User definition. Probably would want additional attributes.    """    # names like "Group", "Order" and "User" are reserved words in SQL    # so we set the name to something safe for SQL    class sqlmeta:        table = "tg_user"    user_name = UnicodeCol(length=16, alternateID=True,                           alternateMethodName="by_user_name")    email_address = UnicodeCol(length=255, alternateID=True,                               alternateMethodName="by_email_address")    display_name = UnicodeCol(length=255)    password = UnicodeCol(length=40)    created = DateTimeCol(default=datetime.now)    #bb: customer this user belongs to    parent_cust = ForeignKey("Customer")    # groups this user belongs to    groups = RelatedJoin("Group", intermediateTable="user_group",                         joinColumn="user_id", otherColumn="group_id")    def _get_permissions(self):        perms = set()        for g in self.groups:            perms = perms | set(g.permissions)        return perms    def _set_password(self, cleartext_password):        "Runs cleartext_password through the hash algorithm before saving."        hash = identity.encrypt_password(cleartext_password)        self._SO_set_password(hash)    def set_password_raw(self, password):        "Saves the password as-is to the database."        self._SO_set_password(password)class Permission(SQLObject):    permission_name = UnicodeCol(length=16, alternateID=True,                                 alternateMethodName="by_permission_name")    description = UnicodeCol(length=255)    groups = RelatedJoin("Group",                        intermediateTable="group_permission",                         joinColumn="permission_id",                          otherColumn="group_id")