def db_init_bookmark(): """install the initial bookmark in a new install""" require('hosts', provided_by=[sample]) require('ini', provided_by=[sample]) parse_ini(env["ini_file"]) from datetime import datetime import transaction from bookie.models import initialize_sql from sqlalchemy import create_engine engine = create_engine(env.ini.get('app:bookie', 'sqlalchemy.url')) initialize_sql(engine) from bookie.models import DBSession from bookie.models import Bmark bmark_us = Bmark(u'http://bmark.us', u'admin', desc=u"Bookie Website", ext= u"Bookie Documentation Home", tags = u"bookmarks") bmark_us.stored = datetime.now() bmark_us.updated = datetime.now() DBSession.add(bmark_us) DBSession.flush() transaction.commit()
def new_user(username, email): """Add new user function, pass username, email :param username: string of new user :param email: string of new email """ require('hosts', provided_by=[sample]) require('ini', provided_by=[sample]) parse_ini(env["ini_file"]) import transaction from bookie.models import initialize_sql initialize_sql(dict(env.ini.items('app:main'))) from bookie.models import DBSession from bookie.models.auth import get_random_word, User sess = DBSession() u = User() u.username = unicode(username) passwd = get_random_word(8) u.password = passwd u.email = unicode(email) u.activated = True u.is_admin = False u.api_key = User.gen_api_key() print dict(u) print passwd sess.add(u) sess.flush() transaction.commit()
def send_code(code, receiver): _user = parse_ini("blog.ini", "email", "username") _pwd = parse_ini("blog.ini", "email", "password") _to = receiver msg = MIMEText(code) msg["Subject"] = "Verification code" msg["From"] = '*****@*****.**' msg["To"] = _to # try: s = smtplib.SMTP_SSL("smtp.qq.com", 465) print(_user, _pwd) s.login(_user, _pwd) s.sendmail(_user, _to, msg.as_string()) s.quit() LOG.info("Success!")
def reset_password(username, password): """Reset a user's password""" require('hosts', provided_by=[sample]) require('ini', provided_by=[sample]) parse_ini(env["ini_file"]) import transaction from bookie.models import initialize_sql initialize_sql(dict(env.ini.items('app:main'))) from bookie.models import DBSession from bookie.models.auth import UserMgr sess = DBSession() u = UserMgr.get(username=username) u.password = password sess.flush() transaction.commit()
def db_init(): """ Initiate the versioning of the db. :Requires: prerun a environment setting function such as sample/prod :: $ fab prod db_setup """ require("hosts", provided_by=[sample]) require("ini", provided_by=[sample]) # load up the ini for this environment parse_ini(env["ini_file"]) local("migrate version_control --url={0} --repository={1}".format( env.ini.get('app:bookie', 'sqlalchemy.url'), 'migrations'))
def db_driver(): """Determine which driver we need and make sure it's installed""" require('hosts', provided_by=[sample]) require('ini', provided_by=[sample]) drivers = { # sqlite is already there in a python install 'sqlite': False, 'mysql': 'MySQL-python==1.2.3', 'postgresql': 'psycopg2==2.4 ', } parse_ini(env["ini_file"]) selected_db = env.ini.get('app:bookie', 'sqlalchemy.url') for key, package in drivers.iteritems(): if key in selected_db and package is not False: # perform the pip install of the package local("{0} install {1}".format(env.pip_path, package))
def db_upgrade(): """Upgrade the system to the latest migration available :Requires: prerun a environment setting function such as sample/prod To upgrade migrations on the sample server :: $ fab sample db_upgrade """ require("hosts", provided_by=[sample]) require("ini", provided_by=[sample]) # load up the ini for this environment parse_ini(env["ini_file"]) local('migrate upgrade --url={0} --repository={1} '.format( env.ini.get('app:bookie', 'sqlalchemy.url'), 'migrations',))
def db_add(desc): """Call: fab db_add:'Some New Change' :param desc: the text string to identify the migrate file :: $ fab db_add:"Initial Table Setup" """ require("hosts", provided_by=[sample]) require("ini", provided_by=[sample]) # load up the ini for this environment parse_ini(env["ini_file"]) local('migrate script --url={0} --repository={1} "{2}"'.format( env.ini.get('app:bookie', 'sqlalchemy.url'), 'migrations', desc,))
def db_test(): """Test a new migration against the database: :WARNING: could fubar db so only use on test db :Requires: prerun a environment setting function such as sample/prod To test on the sample server :: $ fab sample db_test """ require("hosts", provided_by=[sample]) require("ini", provided_by=[sample]) # load up the ini for this environment parse_ini(env["ini_file"]) local('migrate test --url={0} --repository={1} '.format( env.ini.get('app:bookie', 'sqlalchemy.url'), 'migrations',))
def db_downgrade(db_version): """Downgrade the database system to the specified migration :param db_version: the specific migration integer to downgrade t :Requires: prerun a environment setting function such as sample/prod To upgrade migrations on the sample server :: $ fab sample db_downgrade:12 """ require('hosts', provided_by=[sample]) require('ini', provided_by=[sample]) # load up the ini for this environment parse_ini(env["ini_file"]) local('migrate downgrade --url={0} --repository={1} {2} '.format( env.ini.get('app:bookie', 'sqlalchemy.url'), 'migrations', db_version,))