Esempio n. 1
0
def finish_auth():
    if 'error' in request.args:
        return 'You must grant access for this to work, silly'
    
    if 'code' in request.args:
        # auth success, retrieve flow then build and stash credentials.
        flow = memcache.get('flow:%s' % users.get_current_user().user_id())
        credentials = flow.step2_exchange(request.args['code'])
        UserCredential.set_credentials_for_user(users.get_current_user(), 
                                                credentials)
        
    return redirect(url_for('home'))
Esempio n. 2
0
def process_email(emailid):
    #get email
    email = UnprocessedEmail.get_by_id(emailid)
    email_key = email.to_address.split('@')[0]
    # find mapping
    mapping = AlbumEmailMapping.all().filter('email_address =', email_key) \
                               .get()
    if not mapping:
        logging.info('mapping not found: %s' % email_key)
        return

    # get credential
    credential = UserCredential.get_by_user(mapping.owner)
    if not credential:
        logging.info('No credential found for user %s, cannot upload.' %
                     mapping.owner)
        return

    # gotta grab url. check links on album data
    url = None
    for link in mapping.album['link']:
        if link['rel'] == 'http://schemas.google.com/g/2005#feed':
            url = link['href'].split('?alt')[0]

    if not url:
        logging.info('couldn\'t find feed url')
        return

    logging.info('here\s my uri!: %s' % url)

    h = httplib2.Http()
    h = credential.authorize(h)
    for key in email.attachment_keys:
        blobinfo = blobstore.BlobInfo.get(key)
        value = blobstore.BlobReader(blobinfo).read()
        response = h.request(url,
                             method="POST",
                             headers={
                                 'Content-Type': blobinfo.content_type,
                                 'Content-Length': str(blobinfo.size)
                             },
                             body=value)

        logging.info('posted some shit, see the response:')
        logging.info(response)

    UserCredential.set_credentials_for_user(mapping.owner, credential)

    # clean up
    blobinfo.delete()
    email.delete()
Esempio n. 3
0
def process_email(emailid):
    #get email   
    email = UnprocessedEmail.get_by_id(emailid)
    email_key = email.to_address.split('@')[0]
    # find mapping
    mapping = AlbumEmailMapping.all().filter('email_address =', email_key) \
                               .get()
    if not mapping:
        logging.info('mapping not found: %s' % email_key)
        return

    # get credential
    credential = UserCredential.get_by_user(mapping.owner)
    if not credential:
        logging.info('No credential found for user %s, cannot upload.' 
                     % mapping.owner)
        return

    # gotta grab url. check links on album data
    url = None
    for link in mapping.album['link']:
      if link['rel'] == 'http://schemas.google.com/g/2005#feed':
        url = link['href'].split('?alt')[0]
    
    if not url:
        logging.info('couldn\'t find feed url')
        return

    logging.info('here\s my uri!: %s' % url)

    h = httplib2.Http()
    h = credential.authorize(h)
    for key in email.attachment_keys:
        blobinfo = blobstore.BlobInfo.get(key)
        value = blobstore.BlobReader(blobinfo).read()
        response = h.request(url, method="POST", 
                       headers={
                           'Content-Type': blobinfo.content_type, 
                           'Content-Length': str(blobinfo.size)}, 
                       body=value)
                   
        logging.info('posted some shit, see the response:')
        logging.info(response)
    
    UserCredential.set_credentials_for_user(mapping.owner, credential)

    # clean up
    blobinfo.delete()
    email.delete()
Esempio n. 4
0
def home():
    user = users.get_current_user()
    credentials = None
    mappings = None
    if user:
        credentials = UserCredential.get_by_user(user)
        mappings = AlbumEmailMapping.all().filter('owner =', user)
        
    return render_template('home.html', user=user, credentials=credentials, 
                           mappings=mappings)
Esempio n. 5
0
def start_auth():
    if 'new' in request.args:
        # delete existing credential stores.
        memcache.delete()
    if 'new' not in request.args:
        # check for pre-existing auth
        credentials = UserCredential.get_by_user(users.get_current_user())
        if credentials:
            return redirect(url_for('list_albums'))
        
    # generate a flow to authenticate the user.
    flow = flow_from_clientsecrets('client_secrets.json', 
                                   scope=REQUEST_SCOPE, 
                                   redirect_uri=RETURN_URL)
    auth_url = str(flow.step1_get_authorize_url())
    logging.info('auth url: %s' % auth_url)
    
    # stash flow
    memcache.set('flow:%s' % users.get_current_user().user_id(), flow)
    return redirect(auth_url)