本文整理汇总了Python中alchemist.models.base.DBSession.flush方法的典型用法代码示例。如果您正苦于以下问题:Python DBSession.flush方法的具体用法?Python DBSession.flush怎么用?Python DBSession.flush使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类alchemist.models.base.DBSession
的用法示例。
在下文中一共展示了DBSession.flush方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: main
# 需要导入模块: from alchemist.models.base import DBSession [as 别名]
# 或者: from alchemist.models.base.DBSession import flush [as 别名]
def main(argv=sys.argv):
if len(argv) < 2:
usage(argv)
config_uri = argv[1]
options = parse_vars(argv[2:])
setup_logging(config_uri)
settings = get_appsettings(config_uri, options=options)
engine = engine_from_config(settings, 'sqlalchemy.')
DBSession.configure(bind=engine)
Base.metadata.drop_all(engine)
Base.metadata.create_all(engine)
with transaction.manager:
user = User(username='admin', password='apass', email=settings.get('admin_mail'), activated=True,
groups='admin')
DBSession.add(user)
DBSession.flush()
for k, v in Settings.settings.iteritems():
if type(v) == dict:
DBSession.add(Settings(key=unicode(k), value=unicode(v['value'])))
else:
if isinstance(v, basestring) and v.startswith('app_config.'):
v = settings.get(v[len('app_config.'):])
DBSession.add(Settings(key=unicode(k), value=unicode(v)))
DBSession.flush()
示例2: handle_if_company
# 需要导入模块: from alchemist.models.base import DBSession [as 别名]
# 或者: from alchemist.models.base.DBSession import flush [as 别名]
def handle_if_company(k, v, required=True, request=False):
from alchemist.models.base import DBSession
from alchemist.models.company import BaseCompany
from alchemist.models.settings import Settings
if k in ['company', 'company_id']:
is_name = False
try:
company_id = int(v)
company = DBSession.query(BaseCompany).get(company_id)
except:
v = v.replace(new_company_text, '')
company = DBSession.query(BaseCompany).filter(BaseCompany.name == v).first()
if not company:
company = DBSession.query(BaseCompany).filter(BaseCompany.name.startswith(v.strip())).first()
is_name = True
assert not required or company, 'The company does not exist.'
if not company and is_name:
company = BaseCompany()
company.name = v
if request and request.referer and 'add/founder' in request.referer:
company.is_alchemist = True
company.alchemistclass = int(Settings.get('alchemistclass'))
company.companytype = 'startup'
DBSession.add(company)
DBSession.flush()
return company.id if k == 'company_id' else company
return v
示例3: __setattr__
# 需要导入模块: from alchemist.models.base import DBSession [as 别名]
# 或者: from alchemist.models.base.DBSession import flush [as 别名]
def __setattr__(self, name, value):
if name == 'user_id':
if not self._user:
self._user = DBSession.query(User).get(value)
types = self._user.type.split(',')
types.append(self.__tablename__)
self._user.type = ','.join(set(types))
return super(UserMixin, self).__setattr__(name, value)
if 'tags' in name:
name = 'tags'
if hasattr(User, name):
if not self._user and self.user_id:
self._user = DBSession.query(User).get(self.user_id)
if not self._user:
_user = User()
DBSession.add(_user)
DBSession.flush()
self.user_id = _user.id
else:
DBSession.add(self._user)
try:
return setattr(self._user, name, value)
except DetachedInstanceError:
DBSession.add(self._user)
return setattr(self._user, name, value)
else:
return super(UserMixin, self).__setattr__(name, value)
示例4: setUp
# 需要导入模块: from alchemist.models.base import DBSession [as 别名]
# 或者: from alchemist.models.base.DBSession import flush [as 别名]
def setUp(self):
from sqlalchemy import create_engine
engine = create_engine('sqlite://')
try:
# nosetest vs pycharm test runner, lets flex
settings = get_appsettings('../tests.ini')
except:
settings = get_appsettings('tests.ini')
self.config = testing.setUp(settings=settings)
DBSession.configure(bind=engine)
Base.metadata.create_all(engine)
with transaction.manager:
self.founder = User(username='founder', password='apass', email='[email protected]', activated=True,
active=True, firstname='founder1_firstname', lastname='founder1_lastname',
city='founder1_city', state='founder1_state', title='founder1_title')
self.company = BaseCompany(name='testcompany', founded=2015, description='company_desc',
startup_teamdescription='company_team_desc', website='company_website',
linkedin='company_linkedin', twitterhandle='company_twitter',
angelcoprofile='company_angellist')
self.admin = User(username='admin', password='apass', email=settings.get('admin_mail'), activated=False,
groups='admin')
DBSession.add(self.founder)
DBSession.add(self.company)
DBSession.add(self.admin)
for k, v in Settings.settings.iteritems():
if type(v) == dict:
DBSession.add(Settings(key=unicode(k), value=unicode(v['value'])))
else:
if isinstance(v, basestring) and v.startswith('app_config.'):
v = settings.get(v[len('app_config.'):])
DBSession.add(Settings(key=unicode(k), value=unicode(v)))
DBSession.flush()
self.user_id = self.founder.user_id
self.founders_created = []
示例5: add_empty_connection
# 需要导入模块: from alchemist.models.base import DBSession [as 别名]
# 或者: from alchemist.models.base.DBSession import flush [as 别名]
def add_empty_connection(request):
user = User(primary_type='base')
user.activated = True
user.login_enabled = False
DBSession.add(user)
DBSession.flush()
return HTTPFound('/base/%s' % user.id)
示例6: create_conn
# 需要导入模块: from alchemist.models.base import DBSession [as 别名]
# 或者: from alchemist.models.base.DBSession import flush [as 别名]
def create_conn(by_user_id, to_id):
try:
conn = Connection(by_user_id=by_user_id, user_id=to_id)
except:
logging.error('Please specify by_user_id and to_id fields', exc_info=1)
return
DBSession.add(conn)
DBSession.flush()
_, conn.code = gen_ver_code(conn.id, 14)
return conn
示例7: remove_connection
# 需要导入模块: from alchemist.models.base import DBSession [as 别名]
# 或者: from alchemist.models.base.DBSession import flush [as 别名]
def remove_connection(request):
auth_user = User.bid(request.authenticated_userid)
assert auth_user, 'No user'
conn_id = request.matchdict.get('conn_id')
conn = DBSession.query(Connection).filter(Connection.id == conn_id, Connection.by_user_id == auth_user.id).first()
assert conn, 'You are not authorized to perform this action'
DBSession.delete(conn)
DBSession.flush()
return ''
示例8: add_user_view
# 需要导入模块: from alchemist.models.base import DBSession [as 别名]
# 或者: from alchemist.models.base.DBSession import flush [as 别名]
def add_user_view(request):
utype = request.matchdict.get('type')
user = {'type': utype}
company_match = False
if request.method == 'POST':
try:
user = User()
for k, v in request.POST.iteritems():
if v == 'on':
v = True
v = {'true': True, 'false': False}.get(v, v)
v = Tag.handle_if_tags(k, v)
if k == 'company_id' or k == 'company':
try:
company_match = getattr(request.user, k) == int(v)
except:
company_match = request.user.company.name == v.strip() if request.user.company else False
v = v.replace(' - Add new company', '')
assert company_match or request.user.is_admin, \
'You just tried to add an user to a company You don\'t have access to. You can\'t do that!'
k = 'company'
v = handle_if_company(k, v, required=False, request=request)
v = handle_file_save('%s_adm_cr_%s' % (getattr(user, 'nicename', user.id),
request.authenticated_userid), v)
setattr(user, k, v)
plain_pass, _ = gen_ver_code(user.nicename)
user.password = plain_pass
user.activated = True
user.login_enabled = False
user.primary_type = utype
if not user.username:
user.username = user.email
_, user.ver_code = gen_ver_code('signup_%s' % user.id)
DBSession.add(user)
DBSession.flush()
admin_emails = Settings.get('admin_mail')
send_mail(admin_emails, 'roleadd', request, {'user': user,
'new_role': utype})
user.send_mail('invite', request, {'plain_pass': plain_pass,
'user_id': user.id,
'hash': user.ver_code})
request.session.flash('Successfully added a new %s' % (
'Team Member' if company_match else str(user.primary_type).capitalize()))
url = '/%s/%s' % (user.primary_type, user.id)
return HTTPFound(url)
except Exception, e:
request.session.flash('Error: %s' % e.message, 'error')
if request.referer:
return HTTPFound(request.referer)
raise
示例9: send_company
# 需要导入模块: from alchemist.models.base import DBSession [as 别名]
# 或者: from alchemist.models.base.DBSession import flush [as 别名]
def send_company(request):
cid = int(request.params.get('to'))
data = {'company_id': cid,
'message': request.params.get('message')}
conn = create_or_update_conn(request.user.id, None, None, **data)
DBSession.flush()
main_poc = find_poc(conn.company, request)
poc_emails = [main_poc[0].email]
if not request.params.get('poc_id'):
poc_emails = [u.email for u in conn.company.employees if u.point_of_contact and u.email]
conn.user = main_poc[0]
send_mail(poc_emails, 'company_connect_request', request, {'main_poc': main_poc[0],
'conn': conn,
'hash': conn.code,
'message': prep_message(conn.message)})
request.session.flash(render_template('/messaging/company/success_requestcompany.jinja2', {}, request), 'popup')
return HTTPFound(request.referer or '/alchemist_startups')
示例10: add_answer
# 需要导入模块: from alchemist.models.base import DBSession [as 别名]
# 或者: from alchemist.models.base.DBSession import flush [as 别名]
def add_answer(request):
editor_tags = ['ul', 'li', 'ol', 'strong', 'em', 'p', 'hr', 'span', 'del', 'a', 'br']
answer_text = bleach.clean(request.POST.get('answer', ''), editor_tags, {
"*": ["style"],
"img": ["src", "width", "height"],
}, styles=['text-align'])
qid = request.POST.get('qid', -1)
question = DBSession.query(Questions).filter(Questions.id == qid).first()
had_answers = len(question.answers)
answer = Answers(user=request.user, question=question, text=answer_text)
answer.created_at = datetime.now() # TODO put as default (also for question)
DBSession.add(answer)
DBSession.flush()
scheduler.add_job(send_emails_immediate_answer, next_run_time=now(scheduler.timezone).replace(seconds=+30).datetime,
kwargs={'answer_id': answer.id, 'attr_name': 'answer_notification',
'nice_type': 'New Answer', 'type': 'Answer'})
return HTTPFound('/exchange/%s/show%s' % (qid, '?first_answer=1' if had_answers == 0 else '?answered=1'))
示例11: add_question
# 需要导入模块: from alchemist.models.base import DBSession [as 别名]
# 或者: from alchemist.models.base.DBSession import flush [as 别名]
def add_question(request):
question = {}
if request.method == 'POST':
questiontext = bleach.clean(request.POST.get('questiontext'), strip=True)
str_tags = request.POST.get('hidTags')
question = Questions(text=questiontext, user=request.user)
question.tags = Tag.handle_if_tags('tags', str_tags)
question.created_at = datetime.now()
DBSession.add(question)
tracker = Trackers(tracker=request.user, question=question, is_bookmark=False)
DBSession.add(tracker)
DBSession.flush() # make sure there's no DB Errors before sending out the emails
scheduler.add_job(send_emails_immediate_question,
next_run_time=now(scheduler.timezone).replace(seconds=+30).datetime,
kwargs={'question_id': question.id, 'attr_name': 'question_notification',
'nice_type': 'New Question Asked', 'type': 'Question'})
return {'question': question, 'now': now(request.registry.settings.get('scheduler.timezone'))}
示例12: __getattribute__
# 需要导入模块: from alchemist.models.base import DBSession [as 别名]
# 或者: from alchemist.models.base.DBSession import flush [as 别名]
def __getattribute__(self, name):
if 'tags' in name:
name = 'tags'
try:
return super(UserMixin, self).__getattribute__(name)
except AttributeError:
if not self._user and self.user_id:
self._user = User.bid(self.user_id)
if not self._user:
self._user = User()
DBSession.add(self._user)
DBSession.flush()
self.user_id = self._user.id
if name == 'type':
return self._user.type.split(',')
return getattr(self._user, name)
except DetachedInstanceError:
DBSession.add(self._user)
return super(UserMixin, self).__getattribute__(name)
示例13: email_migrate
# 需要导入模块: from alchemist.models.base import DBSession [as 别名]
# 或者: from alchemist.models.base.DBSession import flush [as 别名]
def email_migrate(argv=sys.argv):
if len(argv) < 2:
usage(argv)
config_uri = argv[1]
options = parse_vars(argv[2:])
setup_logging(config_uri)
settings = get_appsettings(config_uri, options=options)
engine = engine_from_config(settings, 'sqlalchemy.')
DBSession.configure(bind=engine)
from petl import fromcsv
conn_data = fromcsv('import_connections.csv').dicts()
# conn_data = []
users_changed = []
users_added = 0
state_to_abbr = dict([(x[1], x[0]) for x in USPS_CHOICES])
print 'processing connections'
with transaction.manager:
for v in conn_data:
user = DBSession.query(User).filter(
or_(func.lower(User.email) == v['_email'].lower(),
func.lower(User.linkedin_email) == v['_email'].lower(),
and_(func.lower(User.firstname) == v['firstname'].lower(),
func.lower(User.lastname) == v['lastname'].lower()))
).first()
if user:
for attrn, val in v.iteritems():
if not val or not attrn or attrn in ('_email',):
continue
val = val.strip()
if attrn == 'company' and not user.company_id:
repl_val = val.lower().rstrip('.').replace('llc', '').replace('corporation', ''). \
replace('corp', '').replace('inc', '').rstrip(',').strip()
print 'searching:', repl_val, val
company = DBSession.query(BaseCompany).filter(
or_(BaseCompany.name == val,
func.lower(BaseCompany.name) == val.lower(),
func.regexp_replace(func.lower(func.btrim(BaseCompany.name)),
',\s(inc|corporation|corp|llc)\.?$', '') == repl_val,
func.regexp_replace(func.lower(func.btrim(BaseCompany.name)),
'\s(inc|corporation|corp|llc)\.?$', '') == repl_val
)).first()
if not company and val:
company = BaseCompany(name=val)
DBSession.add(company)
val = company
if attrn == 'area of expertise' and val:
attrn = 'tag'
tag = DBSession.query(Tag).filter(Tag.text == val).first()
if tag and len(filter(lambda t: t.text == val, user.tags)) == 0:
user.tags.append(tag)
if attrn == 'state':
val = state_to_abbr.get(val)
if val and hasattr(user, attrn) and not getattr(user, attrn):
print 'setting connection.%s.%s = %s' % (user.id, attrn, val)
setattr(user, attrn, val)
users_changed.append(user.id)
DBSession.flush()
users_changed = list(set(users_changed))
print '%s new users added, %s users changed: %s' % (users_added, len(users_changed), users_changed)
del conn_data
comp_data = fromcsv('companies_data.csv').dicts()
companys_changed = []
companys_added = 0
print 'starting to process companies'
with transaction.manager:
for v in comp_data:
if not v['name'].strip():
continue
company = DBSession.query(BaseCompany).filter(
or_(BaseCompany.name == v['name'].strip())
).first()
if company:
for attrn, val in v.iteritems():
if not val or not attrn or attrn in ('_email',):
continue
val = val.strip()
attrn = attrn.replace('company_', '')
if attrn == 'founded':
try:
val = int(val)
except:
continue
if val and hasattr(company, attrn) and not getattr(company, attrn):
print 'setting company.%s.%s = %s' % (company.id, attrn, val)
setattr(company, attrn, val)
companys_changed.append(company.id)
DBSession.flush()
companys_changed = list(set(companys_changed))
print '%s new companys added, %s companys changed: %s' % (
companys_added, len(companys_changed), companys_changed)
示例14: main
# 需要导入模块: from alchemist.models.base import DBSession [as 别名]
# 或者: from alchemist.models.base.DBSession import flush [as 别名]
def main(argv=sys.argv):
if len(argv) < 2:
usage(argv)
config_uri = argv[1]
options = parse_vars(argv[2:])
setup_logging(config_uri)
settings = get_appsettings(config_uri, options=options)
engine = engine_from_config(settings, 'sqlalchemy.')
DBSession.configure(bind=engine)
Base.metadata.drop_all(engine)
Base.metadata.create_all(engine)
with transaction.manager:
user = User(username='admin', password='apass', email=settings.get('admin_mail'), activated=True,
groups='admin')
DBSession.add(user)
DBSession.flush()
for k, v in Settings.settings.iteritems():
if type(v) == dict:
DBSession.add(Settings(key=unicode(k), value=unicode(v['value'])))
else:
if isinstance(v, basestring) and v.startswith('app_config.'):
v = settings.get(v[len('app_config.'):])
DBSession.add(Settings(key=unicode(k), value=unicode(v)))
DBSession.flush()
print 'Starting to import data from spreadsheets'
# if 1:
if 0:
from gdata.spreadsheet.service import SpreadsheetsService
founder_columns = {}
founders = []
to_founder_keys = {'Technical Co-Founder List?': 'technical',
'Email': 'email',
'Address': 'address',
'Alchemist Class #': 'alchemistclass',
'Public LinkedIn Profile': 'linkedin_profile',
'Areas of Expertise': 'tags',
'First Name': 'firstname',
'Last Name': 'lastname',
'Are you the CEO? (One and only one co-founder must be designated CEO)': 'ceo',
'Phone (Other)': '',
'Title': 'title',
'Are you an IoT company?': 'company.iot',
'Base Row': '',
'Company': 'company',
'Phone (Mobile)': 'phone',
}
to_company_keys = {
'name': 'name',
'description': 'description',
'twitterhandle': 'twitterhandle',
'email': 'email',
'website': 'website',
'tags': 'tags',
'braggingtext': 'braggingtext',
'totalraise': 'totalraise',
'haveraise': 'haveraise',
'logo': 'logo',
'alchemistclass': 'alchemistclass'
}
ss = SpreadsheetsService()
ss.email = 'email'
ss.password = 'pass'
ss.ProgrammaticLogin()
for shkey in []:
print 'Starting to import companies from %s' % shkey
linenr = 0
linestoskip = 7
headerline = 2
company_columns = {}
for t in ss.GetListFeed(shkey).entry:
linenr += 1
if headerline == linenr:
for k, v in t.custom.iteritems():
if (v.text or '').strip() in to_company_keys:
company_columns[k] = to_company_keys[v.text.strip()]
print 'Got company_columns %s' % company_columns
if linenr <= linestoskip:
print 'skipping line %s' % linenr
continue
company_values = {}
for k, v in t.custom.iteritems():
if k in company_columns:
val = (getattr(v, 'text', '') or '').strip()
if company_columns[k] in BaseCompany.boolean_fields:
val = val.lower() == 'yes'
company_values[company_columns[k]] = val
company_values['tags'] = handle_if_tags('tags', company_values.get('tags', '').strip())
if not (company_values and company_values.get('name', '').strip() and
company_values.get('description', '').strip()):
print 'skipping because no values %s ' % company_values
continue
if 'logo' in company_values:
logo_ = 'http%s' % company_values['logo'].split('http')[-1]
if 'dropbox' in logo_:
logo_ = logo_.split('?')[0] + '?raw=1'
elif 'google' in logo_:
#.........这里部分代码省略.........
示例15: initialize_weekly_focus
# 需要导入模块: from alchemist.models.base import DBSession [as 别名]
# 或者: from alchemist.models.base.DBSession import flush [as 别名]
def initialize_weekly_focus():
if Settings.get('use_weekly_focus'):
with transaction.manager:
DBSession.query(User).update({User.weekly_focus: None})
DBSession.flush()