def get_or_create(longurl, author=None): try: return Session.query(Link).filter_by(long_url=longurl).one() except NoResultFound: l = Link() l.long_url = longurl l.userkey = author Session.add(l) if asbool(config.get('test_shortener')): # add and commit to get the id, then shorten Session.commit() Session.flush() l.shorten() Session.commit() return l
def _get_or_create_account(self, domain, userid, username): keys = [k for k in session.get('account_keys', '').split(',') if k] # Find or create an account try: acct = Session.query(Account).filter(and_(Account.domain==domain, Account.userid==userid)).one() except NoResultFound: acct = Account() acct.domain = domain acct.userid = userid acct.username = username Session.add(acct) if acct.key not in keys: keys.append(acct.key) session['account_keys'] = ','.join(keys) session[acct.key] = acct.to_dict() session.save() return acct