| 1 |
#if $identity != 'none' |
|---|
| 2 |
from datetime import datetime |
|---|
| 3 |
#end if |
|---|
| 4 |
#if $sqlobject == 'True' |
|---|
| 5 |
import pkg_resources |
|---|
| 6 |
pkg_resources.require("$sqlobjectversion") |
|---|
| 7 |
#elif $sqlalchemy == 'True' |
|---|
| 8 |
import pkg_resources |
|---|
| 9 |
pkg_resources.require("$sqlalchemyversion") |
|---|
| 10 |
#if $elixir == 'True' |
|---|
| 11 |
pkg_resources.require("$elixirversion") |
|---|
| 12 |
#end if |
|---|
| 13 |
#end if |
|---|
| 14 |
#if $sqlobject == 'True' |
|---|
| 15 |
from turbogears.database import PackageHub |
|---|
| 16 |
# import some basic SQLObject classes for declaring the data model |
|---|
| 17 |
# (see http://www.sqlobject.org/SQLObject.html#declaring-the-class) |
|---|
| 18 |
from sqlobject import SQLObject, SQLObjectNotFound, RelatedJoin |
|---|
| 19 |
# import some datatypes for table columns from SQLObject |
|---|
| 20 |
# (see http://www.sqlobject.org/SQLObject.html#column-types for more) |
|---|
| 21 |
from sqlobject import StringCol, UnicodeCol, IntCol, DateTimeCol |
|---|
| 22 |
#end if |
|---|
| 23 |
#if $sqlalchemy == 'True' |
|---|
| 24 |
#if $elixir == 'True' |
|---|
| 25 |
# import the basic Elixir classes and functions for declaring the data model |
|---|
| 26 |
# (see http://elixir.ematia.de/trac/wiki/TutorialDivingIn) |
|---|
| 27 |
from elixir import Entity, Field, OneToMany, ManyToOne, ManyToMany |
|---|
| 28 |
from elixir import options_defaults, using_options, setup_all |
|---|
| 29 |
# import some datatypes for table columns from Elixir |
|---|
| 30 |
# (see http://www.sqlalchemy.org/docs/04/types.html for more) |
|---|
| 31 |
from elixir import String, Unicode, Integer, DateTime |
|---|
| 32 |
#else |
|---|
| 33 |
from turbogears.database import metadata, mapper |
|---|
| 34 |
# import some basic SQLAlchemy classes for declaring the data model |
|---|
| 35 |
# (see http://www.sqlalchemy.org/docs/04/ormtutorial.html) |
|---|
| 36 |
from sqlalchemy import Table, Column, ForeignKey |
|---|
| 37 |
from sqlalchemy.orm import relation |
|---|
| 38 |
# import some datatypes for table columns from SQLAlchemy |
|---|
| 39 |
# (see http://www.sqlalchemy.org/docs/04/types.html for more) |
|---|
| 40 |
from sqlalchemy import String, Unicode, Integer, DateTime |
|---|
| 41 |
#end if |
|---|
| 42 |
#end if |
|---|
| 43 |
#if $identity != 'none' |
|---|
| 44 |
from turbogears import identity |
|---|
| 45 |
#end if |
|---|
| 46 |
|
|---|
| 47 |
#if $sqlobject == 'True' |
|---|
| 48 |
__connection__ = hub = PackageHub('${package}') |
|---|
| 49 |
|
|---|
| 50 |
|
|---|
| 51 |
# your data model |
|---|
| 52 |
|
|---|
| 53 |
|
|---|
| 54 |
# class YourDataClass(SQLObject): |
|---|
| 55 |
# pass |
|---|
| 56 |
#end if |
|---|
| 57 |
#if $sqlalchemy == 'True' |
|---|
| 58 |
#if $elixir == 'True' |
|---|
| 59 |
options_defaults['autosetup'] = False |
|---|
| 60 |
|
|---|
| 61 |
|
|---|
| 62 |
# your data model |
|---|
| 63 |
|
|---|
| 64 |
# class YourDataClass(Entity): |
|---|
| 65 |
# pass |
|---|
| 66 |
#else |
|---|
| 67 |
|
|---|
| 68 |
# your data tables |
|---|
| 69 |
|
|---|
| 70 |
# your_table = Table('yourtable', metadata, |
|---|
| 71 |
# Column('my_id', Integer, primary_key=True) |
|---|
| 72 |
# ) |
|---|
| 73 |
|
|---|
| 74 |
|
|---|
| 75 |
# your model classes |
|---|
| 76 |
|
|---|
| 77 |
|
|---|
| 78 |
# class YourDataClass(object): |
|---|
| 79 |
# pass |
|---|
| 80 |
|
|---|
| 81 |
|
|---|
| 82 |
# set up mappers between your data tables and classes |
|---|
| 83 |
|
|---|
| 84 |
# mapper(YourDataClass, your_table) |
|---|
| 85 |
#end if |
|---|
| 86 |
#end if |
|---|
| 87 |
|
|---|
| 88 |
|
|---|
| 89 |
#if $identity == 'sqlobject' |
|---|
| 90 |
# the identity model |
|---|
| 91 |
|
|---|
| 92 |
|
|---|
| 93 |
class Visit(SQLObject): |
|---|
| 94 |
""" |
|---|
| 95 |
A visit to your site |
|---|
| 96 |
""" |
|---|
| 97 |
class sqlmeta: |
|---|
| 98 |
table = 'visit' |
|---|
| 99 |
|
|---|
| 100 |
visit_key = StringCol(length=40, alternateID=True, |
|---|
| 101 |
alternateMethodName='by_visit_key') |
|---|
| 102 |
created = DateTimeCol(default=datetime.now) |
|---|
| 103 |
expiry = DateTimeCol() |
|---|
| 104 |
|
|---|
| 105 |
def lookup_visit(cls, visit_key): |
|---|
| 106 |
try: |
|---|
| 107 |
return cls.by_visit_key(visit_key) |
|---|
| 108 |
except SQLObjectNotFound: |
|---|
| 109 |
return None |
|---|
| 110 |
lookup_visit = classmethod(lookup_visit) |
|---|
| 111 |
|
|---|
| 112 |
|
|---|
| 113 |
class VisitIdentity(SQLObject): |
|---|
| 114 |
""" |
|---|
| 115 |
A Visit that is link to a User object |
|---|
| 116 |
""" |
|---|
| 117 |
visit_key = StringCol(length=40, alternateID=True, |
|---|
| 118 |
alternateMethodName='by_visit_key') |
|---|
| 119 |
user_id = IntCol() |
|---|
| 120 |
|
|---|
| 121 |
|
|---|
| 122 |
class Group(SQLObject): |
|---|
| 123 |
""" |
|---|
| 124 |
An ultra-simple group definition. |
|---|
| 125 |
""" |
|---|
| 126 |
# names like "Group", "Order" and "User" are reserved words in SQL |
|---|
| 127 |
# so we set the name to something safe for SQL |
|---|
| 128 |
class sqlmeta: |
|---|
| 129 |
table = 'tg_group' |
|---|
| 130 |
|
|---|
| 131 |
group_name = UnicodeCol(length=16, alternateID=True, |
|---|
| 132 |
alternateMethodName='by_group_name') |
|---|
| 133 |
display_name = UnicodeCol(length=255) |
|---|
| 134 |
created = DateTimeCol(default=datetime.now) |
|---|
| 135 |
|
|---|
| 136 |
# collection of all users belonging to this group |
|---|
| 137 |
users = RelatedJoin('User', intermediateTable='user_group', |
|---|
| 138 |
joinColumn='group_id', otherColumn='user_id') |
|---|
| 139 |
|
|---|
| 140 |
# collection of all permissions for this group |
|---|
| 141 |
permissions = RelatedJoin('Permission', joinColumn='group_id', |
|---|
| 142 |
intermediateTable='group_permission', |
|---|
| 143 |
otherColumn='permission_id') |
|---|
| 144 |
|
|---|
| 145 |
|
|---|
| 146 |
class User(SQLObject): |
|---|
| 147 |
""" |
|---|
| 148 |
Reasonably basic User definition. |
|---|
| 149 |
Probably would want additional attributes. |
|---|
| 150 |
""" |
|---|
| 151 |
# names like "Group", "Order" and "User" are reserved words in SQL |
|---|
| 152 |
# so we set the name to something safe for SQL |
|---|
| 153 |
class sqlmeta: |
|---|
| 154 |
table = 'tg_user' |
|---|
| 155 |
|
|---|
| 156 |
user_name = UnicodeCol(length=16, alternateID=True, |
|---|
| 157 |
alternateMethodName='by_user_name') |
|---|
| 158 |
email_address = UnicodeCol(length=255, alternateID=True, |
|---|
| 159 |
alternateMethodName='by_email_address') |
|---|
| 160 |
display_name = UnicodeCol(length=255) |
|---|
| 161 |
password = UnicodeCol(length=40) |
|---|
| 162 |
created = DateTimeCol(default=datetime.now) |
|---|
| 163 |
|
|---|
| 164 |
# groups this user belongs to |
|---|
| 165 |
groups = RelatedJoin('Group', intermediateTable='user_group', |
|---|
| 166 |
joinColumn='user_id', otherColumn='group_id') |
|---|
| 167 |
|
|---|
| 168 |
def _get_permissions(self): |
|---|
| 169 |
perms = set() |
|---|
| 170 |
for g in self.groups: |
|---|
| 171 |
perms |= set(g.permissions) |
|---|
| 172 |
return perms |
|---|
| 173 |
|
|---|
| 174 |
def _set_password(self, cleartext_password): |
|---|
| 175 |
"""Runs cleartext_password through the hash algorithm before saving.""" |
|---|
| 176 |
password_hash = identity.encrypt_password(cleartext_password) |
|---|
| 177 |
self._SO_set_password(password_hash) |
|---|
| 178 |
|
|---|
| 179 |
def set_password_raw(self, password): |
|---|
| 180 |
"""Saves the password as-is to the database.""" |
|---|
| 181 |
self._SO_set_password(password) |
|---|
| 182 |
|
|---|
| 183 |
|
|---|
| 184 |
class Permission(SQLObject): |
|---|
| 185 |
""" |
|---|
| 186 |
A relationship that determines what each Group can do |
|---|
| 187 |
""" |
|---|
| 188 |
permission_name = UnicodeCol(length=16, alternateID=True, |
|---|
| 189 |
alternateMethodName='by_permission_name') |
|---|
| 190 |
description = UnicodeCol(length=255) |
|---|
| 191 |
|
|---|
| 192 |
groups = RelatedJoin('Group', |
|---|
| 193 |
intermediateTable='group_permission', |
|---|
| 194 |
joinColumn='permission_id', |
|---|
| 195 |
otherColumn='group_id') |
|---|
| 196 |
#end if |
|---|
| 197 |
#if $identity == 'sqlalchemy' |
|---|
| 198 |
#if $elixir == 'True' |
|---|
| 199 |
# the identity model |
|---|
| 200 |
|
|---|
| 201 |
|
|---|
| 202 |
class Visit(Entity): |
|---|
| 203 |
""" |
|---|
| 204 |
A visit to your site |
|---|
| 205 |
""" |
|---|
| 206 |
using_options(tablename='visit') |
|---|
| 207 |
|
|---|
| 208 |
visit_key = Field(String(40), primary_key=True) |
|---|
| 209 |
created = Field(DateTime, nullable=False, default=datetime.now) |
|---|
| 210 |
expiry = Field(DateTime) |
|---|
| 211 |
|
|---|
| 212 |
def lookup_visit(cls, visit_key): |
|---|
| 213 |
return Visit.get(visit_key) |
|---|
| 214 |
lookup_visit = classmethod(lookup_visit) |
|---|
| 215 |
|
|---|
| 216 |
|
|---|
| 217 |
class VisitIdentity(Entity): |
|---|
| 218 |
""" |
|---|
| 219 |
A Visit that is link to a User object |
|---|
| 220 |
""" |
|---|
| 221 |
using_options(tablename='visit_identity') |
|---|
| 222 |
|
|---|
| 223 |
visit_key = Field(String(40), primary_key=True) |
|---|
| 224 |
user = ManyToOne('User', colname='user_id', use_alter=True) |
|---|
| 225 |
|
|---|
| 226 |
|
|---|
| 227 |
class Group(Entity): |
|---|
| 228 |
""" |
|---|
| 229 |
An ultra-simple group definition. |
|---|
| 230 |
""" |
|---|
| 231 |
using_options(tablename='tg_group') |
|---|
| 232 |
|
|---|
| 233 |
group_id = Field(Integer, primary_key=True) |
|---|
| 234 |
group_name = Field(Unicode(16), unique=True) |
|---|
| 235 |
display_name = Field(Unicode(255)) |
|---|
| 236 |
created = Field(DateTime, default=datetime.now) |
|---|
| 237 |
users = ManyToMany('User', tablename='user_group') |
|---|
| 238 |
permissions = ManyToMany('Permission', tablename='group_permission') |
|---|
| 239 |
|
|---|
| 240 |
|
|---|
| 241 |
class User(Entity): |
|---|
| 242 |
""" |
|---|
| 243 |
Reasonably basic User definition. |
|---|
| 244 |
Probably would want additional attributes. |
|---|
| 245 |
""" |
|---|
| 246 |
using_options(tablename='tg_user') |
|---|
| 247 |
|
|---|
| 248 |
user_id = Field(Integer, primary_key=True) |
|---|
| 249 |
user_name = Field(Unicode(16), unique=True) |
|---|
| 250 |
email_address = Field(Unicode(255), unique=True) |
|---|
| 251 |
display_name = Field(Unicode(255)) |
|---|
| 252 |
password = Field(Unicode(40)) |
|---|
| 253 |
created = Field(DateTime, default=datetime.now) |
|---|
| 254 |
groups = ManyToMany('Group', tablename='user_group') |
|---|
| 255 |
|
|---|
| 256 |
def permissions(self): |
|---|
| 257 |
p = set() |
|---|
| 258 |
for g in self.groups: |
|---|
| 259 |
p |= set(g.permissions) |
|---|
| 260 |
return p |
|---|
| 261 |
permissions = property(permissions) |
|---|
| 262 |
|
|---|
| 263 |
|
|---|
| 264 |
class Permission(Entity): |
|---|
| 265 |
""" |
|---|
| 266 |
A relationship that determines what each Group can do |
|---|
| 267 |
""" |
|---|
| 268 |
using_options(tablename='permission') |
|---|
| 269 |
|
|---|
| 270 |
permission_id = Field(Integer, primary_key=True) |
|---|
| 271 |
permission_name = Field(Unicode(16), unique=True) |
|---|
| 272 |
description = Field(Unicode(255)) |
|---|
| 273 |
groups = ManyToMany('Group', tablename='group_permission') |
|---|
| 274 |
|
|---|
| 275 |
|
|---|
| 276 |
# Set up all Elixir entities declared above |
|---|
| 277 |
|
|---|
| 278 |
setup_all() |
|---|
| 279 |
#else |
|---|
| 280 |
# the identity schema |
|---|
| 281 |
|
|---|
| 282 |
visits_table = Table('visit', metadata, |
|---|
| 283 |
Column('visit_key', String(40), primary_key=True), |
|---|
| 284 |
Column('created', DateTime, nullable=False, default=datetime.now), |
|---|
| 285 |
Column('expiry', DateTime) |
|---|
| 286 |
) |
|---|
| 287 |
|
|---|
| 288 |
visit_identity_table = Table('visit_identity', metadata, |
|---|
| 289 |
Column('visit_key', String(40), primary_key=True), |
|---|
| 290 |
Column('user_id', Integer, ForeignKey('tg_user.user_id'), index=True) |
|---|
| 291 |
) |
|---|
| 292 |
|
|---|
| 293 |
groups_table = Table('tg_group', metadata, |
|---|
| 294 |
Column('group_id', Integer, primary_key=True), |
|---|
| 295 |
Column('group_name', Unicode(16), unique=True), |
|---|
| 296 |
Column('display_name', Unicode(255)), |
|---|
| 297 |
Column('created', DateTime, default=datetime.now) |
|---|
| 298 |
) |
|---|
| 299 |
|
|---|
| 300 |
users_table = Table('tg_user', metadata, |
|---|
| 301 |
Column('user_id', Integer, primary_key=True), |
|---|
| 302 |
Column('user_name', Unicode(16), unique=True), |
|---|
| 303 |
Column('email_address', Unicode(255), unique=True), |
|---|
| 304 |
Column('display_name', Unicode(255)), |
|---|
| 305 |
Column('password', Unicode(40)), |
|---|
| 306 |
Column('created', DateTime, default=datetime.now) |
|---|
| 307 |
) |
|---|
| 308 |
|
|---|
| 309 |
permissions_table = Table('permission', metadata, |
|---|
| 310 |
Column('permission_id', Integer, primary_key=True), |
|---|
| 311 |
Column('permission_name', Unicode(16), unique=True), |
|---|
| 312 |
Column('description', Unicode(255)) |
|---|
| 313 |
) |
|---|
| 314 |
|
|---|
| 315 |
user_group_table = Table('user_group', metadata, |
|---|
| 316 |
Column('user_id', Integer, ForeignKey('tg_user.user_id', |
|---|
| 317 |
onupdate='CASCADE', ondelete='CASCADE'), primary_key=True), |
|---|
| 318 |
Column('group_id', Integer, ForeignKey('tg_group.group_id', |
|---|
| 319 |
onupdate='CASCADE', ondelete='CASCADE'), primary_key=True) |
|---|
| 320 |
) |
|---|
| 321 |
|
|---|
| 322 |
group_permission_table = Table('group_permission', metadata, |
|---|
| 323 |
Column('group_id', Integer, ForeignKey('tg_group.group_id', |
|---|
| 324 |
onupdate='CASCADE', ondelete='CASCADE'), primary_key=True), |
|---|
| 325 |
Column('permission_id', Integer, ForeignKey('permission.permission_id', |
|---|
| 326 |
onupdate='CASCADE', ondelete='CASCADE'), primary_key=True) |
|---|
| 327 |
) |
|---|
| 328 |
|
|---|
| 329 |
|
|---|
| 330 |
# the identity model |
|---|
| 331 |
|
|---|
| 332 |
|
|---|
| 333 |
class Visit(object): |
|---|
| 334 |
""" |
|---|
| 335 |
A visit to your site |
|---|
| 336 |
""" |
|---|
| 337 |
def lookup_visit(cls, visit_key): |
|---|
| 338 |
return cls.query.get(visit_key) |
|---|
| 339 |
lookup_visit = classmethod(lookup_visit) |
|---|
| 340 |
|
|---|
| 341 |
|
|---|
| 342 |
class VisitIdentity(object): |
|---|
| 343 |
""" |
|---|
| 344 |
A Visit that is link to a User object |
|---|
| 345 |
""" |
|---|
| 346 |
pass |
|---|
| 347 |
|
|---|
| 348 |
|
|---|
| 349 |
class Group(object): |
|---|
| 350 |
""" |
|---|
| 351 |
An ultra-simple group definition. |
|---|
| 352 |
""" |
|---|
| 353 |
pass |
|---|
| 354 |
|
|---|
| 355 |
|
|---|
| 356 |
class User(object): |
|---|
| 357 |
""" |
|---|
| 358 |
Reasonably basic User definition. |
|---|
| 359 |
Probably would want additional attributes. |
|---|
| 360 |
""" |
|---|
| 361 |
|
|---|
| 362 |
def permissions(self): |
|---|
| 363 |
p = set() |
|---|
| 364 |
for g in self.groups: |
|---|
| 365 |
p |= set(g.permissions) |
|---|
| 366 |
return p |
|---|
| 367 |
permissions = property(permissions) |
|---|
| 368 |
|
|---|
| 369 |
def by_email_address(cls, email): |
|---|
| 370 |
""" |
|---|
| 371 |
A class method that can be used to search users |
|---|
| 372 |
based on their email addresses since it is unique. |
|---|
| 373 |
""" |
|---|
| 374 |
return cls.query.filter_by(email_address=email).first() |
|---|
| 375 |
by_email_address = classmethod(by_email_address) |
|---|
| 376 |
|
|---|
| 377 |
def by_user_name(cls, username): |
|---|
| 378 |
""" |
|---|
| 379 |
A class method that permits to search users |
|---|
| 380 |
based on their user_name attribute. |
|---|
| 381 |
""" |
|---|
| 382 |
return cls.query.filter_by(user_name=username).first() |
|---|
| 383 |
by_user_name = classmethod(by_user_name) |
|---|
| 384 |
|
|---|
| 385 |
def _set_password(self, password): |
|---|
| 386 |
""" |
|---|
| 387 |
encrypts password on the fly using the encryption |
|---|
| 388 |
algo defined in the configuration |
|---|
| 389 |
""" |
|---|
| 390 |
self._password = identity.encrypt_password(password) |
|---|
| 391 |
|
|---|
| 392 |
def _get_password(self): |
|---|
| 393 |
""" |
|---|
| 394 |
returns password |
|---|
| 395 |
""" |
|---|
| 396 |
return self._password |
|---|
| 397 |
|
|---|
| 398 |
password = property(_get_password, _set_password) |
|---|
| 399 |
|
|---|
| 400 |
|
|---|
| 401 |
class Permission(object): |
|---|
| 402 |
""" |
|---|
| 403 |
A relationship that determines what each Group can do |
|---|
| 404 |
""" |
|---|
| 405 |
pass |
|---|
| 406 |
|
|---|
| 407 |
|
|---|
| 408 |
# set up mappers between identity tables and classes |
|---|
| 409 |
|
|---|
| 410 |
mapper(Visit, visits_table) |
|---|
| 411 |
|
|---|
| 412 |
mapper(VisitIdentity, visit_identity_table, |
|---|
| 413 |
properties=dict(users=relation(User, backref='visit_identity'))) |
|---|
| 414 |
|
|---|
| 415 |
mapper(User, users_table, |
|---|
| 416 |
properties=dict(_password=users_table.c.password)) |
|---|
| 417 |
|
|---|
| 418 |
mapper(Group, groups_table, |
|---|
| 419 |
properties=dict(users=relation(User, |
|---|
| 420 |
secondary=user_group_table, backref='groups'))) |
|---|
| 421 |
|
|---|
| 422 |
mapper(Permission, permissions_table, |
|---|
| 423 |
properties=dict(groups=relation(Group, |
|---|
| 424 |
secondary=group_permission_table, backref='permissions'))) |
|---|
| 425 |
#end if |
|---|
| 426 |
#end if |
|---|