def run_testsuite(db_name): import quick_orm.testsuite.fixtures as fixtures global db db_str = db_strs[db_name] db = Database(db_str) db.drop_tables() db.create_tables() db.load_data(fixtures) from quick_orm.testsuite.core import CoreTestCase test_suite = unittest.TestSuite() test_suite.addTest(unittest.makeSuite(CoreTestCase)) unittest.TextTestRunner(verbosity=1).run(test_suite)
class BlogEntry(Article): """Represents a note in evernote or a blog entry in everblog.""" snippet = Column(String(128), nullable=False) lang = Column(Enum('en', 'cn', name='language_code'), nullable=False, index=True, default='en') def synchronize(self): super(BlogEntry, self).synchronize() text_content = TextConverter.html_to_text(self.content) self.snippet = text_content[:128] if float(len(chinese_character_pattern.findall(text_content))) / len( text_content) > 0.1: self.lang = 'cn' class Page(Article): "Represents a page in the website, such as the about page" order = Column(Integer, nullable=False, default=1) @Database.many_to_many(Article) class Tag: """represents a tag in Evernote""" name = Column(String(64), nullable=False, index=True, unique=True) Database.register()
@Database.many_to_one(Question) class Answer(Post): pass @Database.many_to_one(Post) class Comment(Post): pass @Database.many_to_many(Post) class Tag: name = Column(String(70)) Database.register() if __name__ == '__main__': db = Database('sqlite://') db.create_tables() user1 = User(name='Tyler Long') user2 = User(name='Peter Lau') tag1 = Tag(name='quick_orm') tag2 = Tag(name='nice') question = Question(user=user1, title='What is quick_orm?', content='What is quick_orm?', tags=[
from quick_orm.core import Database from sqlalchemy import Column, String __metaclass__ = Database.DefaultMeta class User: name = Column(String(30)) @Database.one_to_one(User) class Contact: email = Column(String(70)) address = Column(String(70)) Database.register() if __name__ == '__main__': db = Database('sqlite://') db.create_tables() contact = Contact(email = '*****@*****.**', address = 'Shenzhen, China') user = User(name = 'Tyler Long', contact = contact) db.session.add_then_commit(user) user = db.session.query(User).get(1) print 'User:'******'Email:', user.contact.email print 'Address:', user.contact.address
class Group: name = Column(String(36), nullable = False, unique = True) @Database.many_to_one(User, backref_name = 'blog_entries') class BlogEntry: title = Column(String(64), nullable = False) content = Column(Text) class Topic: name = Column(String(64), nullable = False) @Database.many_to_one(User) class Post: content = Column(Text) @Database.many_to_many(Topic) class Question(Post): title = Column(String(64), nullable = False) class Answer(Post): pass Database.register()
from quick_orm.core import Database from sqlalchemy import Column, String __metaclass__ = Database.DefaultMeta class User: name = Column(String(30)) Database.register() if __name__ == '__main__': db = Database('sqlite://') # database urls: http://docs.sqlalchemy.org/en/latest/core/engines.html#database-urls db.create_tables() # create tables, you don't have to write any SQL. user = User(name = 'Hello World') db.session.add_then_commit(user) # commit user to database. user = db.session.query(User).get(1) print 'My name is', user.name print 'created_at', user.created_at # created_at and updated_at timestamps are added automatically. print 'updated_at', user.updated_at user.name = 'Tyler Long' db.session.commit() # commit changes to database. print 'My name is', user.name print 'created_at', user.created_at print 'updated_at', user.updated_at
def __init__(self): self._db = Database("sqlite:///../news.db")
from quick_orm.core import Database from sqlalchemy import Column, String __metaclass__ = Database.DefaultMeta class User: name = Column(String(30)) Database.register() if __name__ == '__main__': db1 = Database('sqlite://') db1.create_tables() db2 = Database('sqlite://') db2.create_tables() user1 = User(name='user in db1') user2 = User(name='user in db2') db1.session.add_then_commit(user1) db2.session.add_then_commit(user2) print 'I am', db1.session.query(User).get(1).name print 'I am', db2.session.query(User).get(1).name
app.config.update( DEBUG = True, SECRET_KEY = Encryption.generate_random_string(), CONNECTION_STRING = 'postgresql://*****:*****@localhost/everblog', IMAGE_CACHE = 'everblog/everblog.dbm', ADMIN_USERNAME = '******', ADMIN_PASSWORD = '******', BLOG_OWNER = 'Tyler Long', CONTACT_METHODS = [ ('email', 'mailto:[email protected]'), (u'微博', 'http://weibo.com/tylerlong'), ('github', 'https://github.com/tylerlong'), ('bitbucket', 'https://bitbucket.org/tylerlong'), ('stackoverflow', 'http://stackoverflow.com/users/862862/tyler-long'), ], DEFAULT_LANG = 'en', TIME_ZONE = +8, PAGE_SIZE = 8, #third party settings DISQUS_SHORTNAME = 'tylerlong', GOOGLE_ANALYTICS_TRACKING_ID = 'UA-28950403-1' ) db = Database(app.config['CONNECTION_STRING']) import everblog.blueprints
from quick_orm.core import Database from sqlalchemy import Column, String class DefaultModel: name = Column(String(70)) __metaclass__ = Database.MetaBuilder(DefaultModel) class User: pass class Group: pass Database.register() if __name__ == '__main__': db = Database('sqlite://') db.create_tables() user = User(name='tylerlong') db.session.add(user) group = Group(name='python') db.session.add_then_commit(group) print user.name print group.name
from quick_orm.core import Database from sqlalchemy import Column, String __metaclass__ = Database.DefaultMeta class User: name = Column(String(30)) Database.register() if __name__ == '__main__': db = Database( 'sqlite://' ) # database urls: http://docs.sqlalchemy.org/en/latest/core/engines.html#database-urls db.create_tables() # create tables, you don't have to write any SQL. user = User(name='Hello World') db.session.add_then_commit(user) # commit user to database. user = db.session.query(User).get(1) print 'My name is', user.name print 'created_at', user.created_at # created_at and updated_at timestamps are added automatically. print 'updated_at', user.updated_at user.name = 'Tyler Long' db.session.commit() # commit changes to database. print 'My name is', user.name print 'created_at', user.created_at print 'updated_at', user.updated_at