def oauth_authorized(): remote_app = current_app.auth['app'] # ripped off from Flask-OAuth # since `remote_app` should be defined in app start time if 'oauth_verifier' in request.args: data = remote_app.handle_oauth1_response() elif 'code' in request.args: data = remote_app.handle_oauth2_response() else: data = remote_app.handle_unknown_response() remote_app.free_request_token() # --- if data is None: return 'Access denied: reason=%s error=%s' % ( request.args['error_reason'], request.args['error_description'] ) db = NodeIndex(current_app.config) session['oauth_token'] = (data['access_token'], '') user = current_app.auth['authenticator'].get_user_data() identity = current_app.auth['provider'] + ':' + str(user['id']) user_id = hashlib.sha1(identity).hexdigest() user_db = User.get(db.session, user_id) session['user_id'] = user_id session['user'] = user if not user_db: user_db = User(user_id=user_id, identity=identity, secret_key=os.urandom(10).encode('hex')) db.add(user_db) db.query(User).filter_by(user_id=user_id).update({ 'name': user['name'], 'picture': user['picture'] }) db.session.commit() broadcast('login', user_db.dict(private=False)) next_page = request.args.get('next') if next_page: return redirect(next_page) else: return redirect(url_for('index'))
class DistributedStorage(Storage): _songClass = RemoteSong def __init__(self, config): self.db = NodeIndex(config) self._config = config self._logger = logging.getLogger('radiocrepe.dist_storage') self.node_registry = NodeRegistry(self) def initialize(self): HubSideBase.metadata.create_all(self.db.engine) self.node_registry.detach_all() def mark_available(self, node_id): """ Mark all songs stored in this node as available """ self.db.query(self._songClass).filter_by(node_id=node_id).update({ self._songClass.available: True }) self.db.commit() def mark_unavailable(self, node_id): """ Mark all songs in this node as unavailable """ self.db.query(self._songClass).filter_by(node_id=node_id).update({ self._songClass.available: False }) self.db.commit() def file(self, uid): r = self._node_registry.get(uid) if r: return urlopen('http://%s/song/%s' % (r.server, r.uid)) else: return None def update_node(self, node_id, data): for song in data: self._index(node_id=node_id, **song) @property def stats(self): return { "nodes": self.node_registry.stats }
def __init__(self, config): self.db = NodeIndex(config) self._config = config self._logger = logging.getLogger('radiocrepe.dist_storage') self.node_registry = NodeRegistry(self)