Beispiel #1
0
    def verify(self, *args, **kw):
        provider = request.params.get('provider')
        service = get_provider(provider)

        auth = service.responder()
        try:
            user = auth.verify()
            account = user['profile']['accounts'][0]
            if not user.get('oauth_token') and not user.get('oauth_token_secret'):
                raise Exception('Unable to get OAUTH access')
            acct = self._get_or_create_account(provider, account['userid'], account['username'])
            acct.profile = user['profile']
            acct.oauth_token = user.get('oauth_token', None)
            if 'oauth_token_secret' in user:
                acct.oauth_token_secret = user['oauth_token_secret']
            acct.updated = UTCDateTime.now()
            try:
                Session.commit()
            except UnicodeEncodeError, e:
                log.exception("***** UnicodeEncodeError! %r: %r: %r %r" % (acct.domain, acct.userid, acct.username,acct.json_attributes,))
                raise e
            # XXX argh, this is also done in get_or_create above, but we have to
            # ensure we have the updated data
            session[acct.key] = acct.to_dict()
            session.save()
Beispiel #2
0
 def get(self):
     keys = session.get('account_keys', '').split(',')
     stmt = Session.query(Account.id).filter(Account.key.in_(keys)).subquery()
     data = Session.query(History, Account.domain, Account.username, Account.id).filter(
         Account.id == History.account_id).\
         filter(History.account_id.in_(stmt)).\
         order_by(History.published.desc()).\
         all()
     res = []
     for h, d, u, i in data:
         r = h.to_dict()
         r['domain'] = d
         r['username'] = u
         r['userid'] = i
         res.append(r)
     return res
Beispiel #3
0
    def accounts(self):
        start = request.params.get('start',None)
        end = request.params.get('end',None)
        limit = int(request.params.get('days','0'))
        opts = request.params.get('opts','').split(',')
        groupby = []
        whereclause = []
        if limit and not start and not end:
            whereclause.append(Account.created >= UTCDateTime.now() - timedelta(days=limit))
        if start:
            whereclause.append(Account.created >= UTCDateTime.from_string(start))
        if end:
            whereclause.append(Account.created < UTCDateTime.from_string(end))
        if 'perday' in opts:
            if 'domain' in opts:
                s = select([func.date(Account.created), Account.domain, func.count(Account.id)])
                groupby.append(func.to_days(Account.created))
                groupby.append(Account.domain)
            else:
                s = select([func.date(Account.created), func.count(Account.id)])
                groupby.append(func.to_days(Account.created))
        else:
            s = select([func.count(Account.domain), Account.domain])
            groupby.append(Account.domain)

        if whereclause:
            s = s.where(*whereclause)
        if groupby:
            s = s.group_by(*groupby)
        return [list(a) for a in Session.execute(s).fetchall()]
Beispiel #4
0
 def history(self):
     start = request.params.get('start',None)
     end = request.params.get('end',None)
     limit = int(request.params.get('days','0'))
     opts = request.params.get('opts','').split(',')
     whereclause = []
     vars = {}
     groupby = []
     if limit and not start and not end:
         whereclause.append(History.published >= UTCDateTime.now() - timedelta(days=limit))
     if start:
         whereclause.append(History.published >= UTCDateTime.from_string(start))
     if end:
         whereclause.append(History.published < UTCDateTime.from_string(end))
     if 'perday' in opts:
         if 'domain' in opts:
             s = select([func.date_format(History.published, "%Y-%m-%d"), History.domain, func.count(History.domain)],)
             groupby.append(func.to_days(History.published))
             groupby.append(History.domain)
         else:
             s = select([func.date_format(History.published, "%Y-%m-%d"), func.count(History.id)])
             groupby.append(func.to_days(History.published))
     else:
         s = select([func.count(History.domain), History.domain])
         groupby.append(History.domain)
     
     if whereclause:
         s = s.where(and_(*whereclause))
     if groupby:
         s = s.group_by(*groupby)
     return [list(a) for a in Session.execute(s).fetchall()]
Beispiel #5
0
 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
Beispiel #6
0
 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
Beispiel #7
0
 def get(self, id):
     num_id = int(id, 16)
     #print "ID is ", num_id
     link = Session.query(Link).filter_by(id = num_id).first()
     #import sys
     #print >> sys.stderr, link.to_dict()
     #print "LONG_URL = ", link.long_url
     resp = get_redirect_response(link.long_url,
                                  additional_headers=[
                                     ('x-shortened-by', link.userkey),
                                     ('x-shortened-for', link.audience),
                                     ])
     raise resp.exception
Beispiel #8
0
    def verify(self, *args, **kw):
        provider = session.pop('oauth_provider')
        session.save()
        service = get_provider(provider)

        auth = service.responder()
        try:
            user = auth.verify()
            account = user['profile']['accounts'][0]
    
            acct = self._get_or_create_account(provider, account['userid'], account['username'])
            acct.profile = user['profile']
            acct.oauth_token = user.get('oauth_token', None)
            if 'oauth_token_secret' in user:
                acct.oauth_token_secret = user['oauth_token_secret']
            acct.updated = UTCDateTime.now()
            Session.commit()
            # XXX argh, this is also done in get_or_create above, but we have to
            # ensure we have the updated data
            session[acct.key] = acct.to_dict()
            session.save()
        except AccessException, e:
            self._redirectException(e)
Beispiel #9
0
 def signout(self):
     domain = request.params.get('domain')
     username = request.params.get('username')
     userid = request.params.get('userid')
     if domain and username or userid:
         try:
             keys = [k for k in session.get('account_keys', '').split(',') if k]
             for k in keys:
                 session.pop(k)
             _and = [Account.domain==domain]
             if username:
                 _and.append(Account.username==username)
             if userid:
                 _and.append(Account.userid==userid)
             accts = Session.query(Account).filter(Account.key.in_(keys)).filter(not_(and_(*_and))).all()
             session['account_keys'] = ','.join([a.key for a in accts])
             for a in accts:
                 session[a.key] = a.to_dict()
         except:
             session.clear()
     else:
         session.clear()
     session.save()
Beispiel #10
0
def exception_rollback(func, *args, **kwargs):
    try:
        return func(*args, **kwargs)
    except Exception, e:
        Session.rollback()
        raise
Beispiel #11
0
def init_model(engine):
    """Call me before using any of the tables or classes in the model"""
    Session.configure(bind=engine)
    Base.metadata.create_all(bind=Session.bind)