def get_credentials(account_id, db, secure=True): account = Accounts.findOne(db, account_id) if account: return account.credentials else: response.status = 404 return {}
def get_from_token(db): token = request.query.get('token') if token: account = Accounts.findOne(db, {'credentials.private_token': token}) if account: request.account = account return True
def get_valid_parrot(db, account_id, parrot_id): account = Accounts.findOne(db,account_id) if account: subscription = Subscriptions.findOne(db, {'parrot_id': ObjectId(parrot_id), 'account_id': ObjectId(account_id)}) if subscription: parrot = Parrots.findOne(db, parrot_id) if parrot: return parrot
def get_account(account_id, db, secure=True): account = Accounts.findOne(db, account_id) if account: account.update(request.json) return account.JSON() else: response.status = 404 return {}
def login(db): account_by_email = Accounts.findOne(db, { 'email': request.forms.get('email'), }) if account_by_email: account = Accounts.findOne(db, { 'email': request.forms.get('email'), 'password': sha1(account_by_email.salt+request.forms.get('password')).hexdigest() }) if account: account_session = AccountsSessions(db, {'account_id': account.id}) account_session.insert() response.set_cookie('sid', account_session.session_id, path='/', expires = account_session.expires) redirect('/logged') return raise UnauthorizeException()
def callback(account_id, db, secure=True): new_plan = Plans.findOne(db, {'name':request.json.get("name")}) if new_plan: #Verify payment method account = Accounts.findOne(db, account_id) new_account_plan = change_plan(db,new_plan,account) if new_account_plan: return {"id":str(new_account_plan.id)}
def create_account_and_login(app, db, account_data): response = app.post_json('/accounts', account_data ) app.post('/login', {'email': account_data['email'], 'password': account_data['password']} ) account = Accounts.findOne(db, response.json.get('id')) return account
def parrots_finish(db): if request.query.denied: redirect('/twitter_denied.html') session = Sessions.findOne(db, {'oauth_token': request.query.oauth_token}) if session: account = Accounts.findOne(db, session.account_id) if not account: response.status = 404 return {'error': 'Invalid token'} twitter = Twitter() try: access_tokens = twitter.get_access_tokens(request.query.oauth_verifier,{'oauth_token': session.oauth_token, 'oauth_token_secret': session.oauth_token_secret}) twitter.create_client(access_tokens.get('oauth_token'),access_tokens.get('oauth_token_secret')) headers, body = twitter.get('https://api.twitter.com/1/account/verify_credentials.json') except: redirect('/twitter_auth_problem.html') return if headers.status == 200: body = json.loads(body) parrot = Parrots.findOne(db, {'twitter_id': body.get('id')}) if not parrot: new_parrot = Parrots(db, { 'twitter_info': body, 'twitter_id': body.get('id'), 'oauth_token': access_tokens.get('oauth_token'), 'oauth_token_secret': access_tokens.get('oauth_token_secret') }) new_parrot.insert() parrot = new_parrot else: parrot.update({ 'oauth_token': access_tokens.get('oauth_token'), 'oauth_token_secret': access_tokens.get('oauth_token_secret') }) subscription = Subscriptions.findOne(db, {'account_id': account.id, 'parrot_id': parrot.id}) subscription_parameters = { 'parrot_id': parrot.id, 'account_id': account.id, 'active': True, 'external_id': session.external_id, 'twitter_screen_name': body.get("screen_name") } if not subscription: subscription = Subscriptions(db, subscription_parameters) subscription.insert() else: subscription.update(subscription_parameters) notification_id = _create_notification(db, account, parrot, subscription) if notification_id: redirect_url = generate_redirect_url(account.callback_url, session.external_id, subscription.id, notification_id) redirect(redirect_url) else: response.status = 404 return {'error': 'Expired token'}
def callback(db): params = {'startup':'','name':'','email':'','password':''} params.update(request.forms) account = Accounts.findOne(db, {'email': params.get("email","")}) status = False if not account: new_account = Accounts(db, params) new_account._data['password'] = sha1(new_account.salt+params.get('password')).hexdigest() new_account.insert() status = True params['status'] = status return template('signedup',params)
def send_notification(db, payment, notification_type): subscription = Subscriptions.findOne(db, {'account_id': payment.account_id, 'parrot_id': payment.parrot_id}) if subscription: account = Accounts.findOne(db, payment.account_id) if account: notification = Notifications(db, { 'account_id': payment.account_id, 'parrot_id': payment.parrot_id, 'type': notification_type, 'subscription_id': subscription.id, 'external_id': subscription.external_id, 'request_url': account.notification_url }) notification.insert()
def notify(db, notification_raw, notification_message): notification = Notifications.findOne(db, notification_message.get('notification_id')) account = Accounts.findOne(db, notification.account_id) if notification: log('cron3', 'Notifying remote customer', notification_message.get('subscription_id')) if not notification_message.get('type') in VALID_NOTIFICATIONS: log('cron3', 'ERROR: Unknown notification', notification_message.get('subscription_id')) # TODO: Check what to do in this case return if notification.request_url: query_data = { 'subscription_id': str(notification.subscription_id), 'account_id': str(notification.account_id), 'parrot_id': str(notification.parrot_id), 'type': notification_message.get('type'), 'external_id': str(notification.external_id), 'notification_id': str(notification.id), } log('cron3', 'Notification URL: %s' % notification.request_url, notification_message.get('subscription_id')) utf8_query_data = dict([(key,val.encode('utf-8')) for key, val in query_data.items() if isinstance(val, basestring)]) delete_message = False try: if account.notification_active: http_client = Http() headers, body = http_client.request(uri = notification.request_url, body = urlencode(utf8_query_data), method = 'POST') if int(headers.status) >= 200 and int(headers.status) < 300: notification.update({ 'response_status': headers.status, 'response_headers': headers, 'response_body': body, 'status': 'sent' }) log('cron3', 'Remote notification succeded', notification_message.get('subscription_id')) else: log('cron3', "Failed. Notification response not 2XX (received %s) from url %s" % ( headers.status, notification.request_url ), notification_message.get('subscription_id')) else: delete_message = True notification.update({ 'status': 'off' }) if delete_message: Queue.delete_message('notifications', notification_raw) except Exception, e: log('cron3', "Failed. Exception %specified" % e) else: log('cron3', 'ERROR: No remote url specified', notification_message.get('subscription_id'))
def parrots_start(db): account = Accounts.findOne(db,{'credentials.public_token': request.query.get("token")}) if account: twitter = Twitter() client = twitter.create_session() try: tokens = twitter.get_request_token(client) except: redirect('/twitter_down.html') return session = Sessions(db, {'account_id': account.id, 'oauth_token': tokens['oauth_token'], 'oauth_token_secret': tokens['oauth_token_secret'], 'external_id': request.query.external_id}) session.insert() redirect_url = twitter.redirect_url(tokens) redirect(redirect_url) else: print "Noooo ---" response.status = 404 return {}
def get_account(account_id,db, secure = True): if account_id == 'me': account_id = request.account.id account = Accounts.findOne(db, account_id) filter_parrots = {'account_id': ObjectId(account_id), 'active': True} account._data['stats']['parrots_total'] = db.subscriptions.find(filter_parrots).count() from_ = datetime.today() from_ = datetime(from_.year, from_.month, from_.day) to_ = from_ + relativedelta(days=+1) filter_parrots['created_at'] = {'$gte': from_, '$lt': to_} account._data['stats']['parrots_today'] = db.subscriptions.find(filter_parrots).count() account._data['stats']['payments_total'] = db.payments.find({'account_id': ObjectId(account_id), 'success': True}).count() filter_payments = {'account_id': ObjectId(account_id), 'success': True} filter_payments['created_at'] = {'$gte': from_, '$lt': to_} account._data['stats']['payments_today'] = db.payments.find(filter_payments).count() # 'payments_total': 0, # 'payments_today': 0 if account: return account.JSON() else: response.status = 404 return {}