def merge_in_db(self): self.log("merging block props in db") g.run("""MERGE (b:Block {id: {id}}) SET b += {props} """, id=self.id, props=self.backup_props)
def merge_in_db(self): self.log("merging channel props in db") g.run("""MERGE (c:Channel {id: {id}}) SET c += {props} """, id=self.id, props=self.backup_props)
def merge_in_db(self): self.log("merging user in db") g.run("""MERGE (c:User {id: {id}}) SET c += {props} """, id=self.id, props=self.backup_props)
def fetch_all_from_db(returns): return_raw = 'raw' in returns return_ids = 'ids' in returns return_dates = 'dates' in returns logging.warning(f"Fetching DB: all channels for their ids and dates") res_raw = g.run("""MATCH (c:Channel) RETURN c.id as id, c.updated_at as updated_at, c.added_to_at as added_to_at """).data() res_ids = [o['id'] for o in res_raw] if return_ids else None if return_dates: res_dates = { o['id']: { 'updated_at': datetime.strptime(o['updated_at'], '%Y-%m-%dT%H:%M:%S.%fZ'), 'added_to_at': datetime.strptime(o['added_to_at'], '%Y-%m-%dT%H:%M:%S.%fZ'), } for o in res_raw } else: res_dates = None if not return_raw: res_raw = None return res_raw, res_ids, res_dates
def jsonblock(block_id): res = g.run("""MATCH (b:Block {id: {block_id}}) RETURN b as data""", block_id=block_id).data() if (len(res) == 0): return jsonify('nope') else: return jsonify(res[0]['data'])
def __delete_block_channel_relationships(self): self.log("deleting all block-channel rels") res = g.run( """MATCH (:Block)-[r:CONNECTS_TO]->(c:Channel {id: {channel_id}}) DELETE r RETURN count(r) as cnt""", channel_id=self.id, ).data() self.log(f"done deleted {str(res[0]['cnt'])} rels")
def __delete_user_channel_relationships(self): self.log("deleting all user-channel rels") res = g.run( """MATCH (:User {id: {user_id}})-[r:OWNS]->(:Channel) DELETE r RETURN count(r) as cnt""", user_id=self.id, ).data() self.log(f"done deleted {str(res[0]['cnt'])} rels")
def __create_block_channel_relationships(self): self.log("creating all block-channel rels") res = g.run( """MATCH (b:Block), (c:Channel {id: {channel_id}}) WHERE b.id in {list} MERGE (b)-[r:CONNECTS_TO]->(c) RETURN count(r) as cnt""", channel_id=self.id, list=self._block_relationships_to_create, ).data() self.log(f"done created {str(res[0]['cnt'])} rels")
def __create_user_channel_relationships(self): self.log("creating all user-channel rels") res = g.run( """MATCH (u:User {id: {user_id}}), (c:Channel) WHERE c.id in {list} MERGE (u)-[r:OWNS]->(c) RETURN count(r) as cnt""", user_id=self.id, list=self._channel_relationships_to_create, ).data() self.log(f"done created {str(res[0]['cnt'])} rels")
def fetch_all_from_db(returns): return_raw = 'raw' in returns return_ids = 'ids' in returns return_dates = 'dates' in returns logging.warning(f"Fetching DB: all blocks for their ids") res_raw = g.run("""MATCH (c:Block) RETURN c.id as id """).data() res_ids = [o['id'] for o in res_raw] if return_ids else None res_dates = None if not return_raw: res_raw = None return res_raw, res_ids, res_dates
def route_all(page=1): per = request.args.get('per') or 500 try: limit = int(per) except ValueError: limit = 500 skip = (page - 1) * limit if request.args.get('channel'): blocks = g.run( """MATCH (b:Block), (b)-[:CONNECTS_TO]-(c:Channel {id: {channel_id}}) RETURN b as data, collect({id: c.id, title: c.title, slug: c.slug, user_slug: c.user_slug, status: c.status}) as channels ORDER BY b.connected_at DESC SKIP {skip} LIMIT {limit}""", channel_id=int(request.args.get('channel')), skip=skip, limit=limit, ).data() this_channel = g.run( """MATCH (c:Channel {id: {channel_id}}) RETURN c.title as title, c.slug as slug, c.user_slug as user_slug, c.length as length LIMIT 1""", channel_id=int(request.args.get('channel')), ).data()[0] elif request.args.get('nsfw'): blocks = g.run( """MATCH (b:Block), (b)-[:CONNECTS_TO]-(c:Channel) RETURN b as data, collect({id: c.id, title: c.title, slug: c.slug, user_slug: c.user_slug, status: c.status}) as channels ORDER BY b.connected_at DESC SKIP {skip} LIMIT {limit}""", skip=skip, limit=limit, ).data() this_channel = None else: blocks = g.run( """MATCH (b:Block), (b)-[:CONNECTS_TO]-(c:Channel) WHERE c.status <> 'private' RETURN b as data, collect({id: c.id, title: c.title, slug: c.slug, user_slug: c.user_slug, status: c.status}) as channels ORDER BY b.connected_at DESC SKIP {skip} LIMIT {limit}""", skip=skip, limit=limit, ).data() this_channel = None channels = g.run( """MATCH (c:Channel) RETURN c.id as id, c.title as title, c.status as status ORDER BY c.added_to_at DESC""", ).data() pagination_previous_arg = '?channel=' + request.args.get( 'channel') if request.args.get('channel') else '' pagination_previous = '/blocks/' + str( page - 1) + pagination_previous_arg if page != 1 else None pagination_next_arg = '?channel=' + request.args.get( 'channel') if request.args.get('channel') else '' pagination_next = '/blocks/' + str(page + 1) + pagination_next_arg if len( blocks) == 500 else None return render_template('blocks.html', blocks=blocks, channels=channels, this_channel=this_channel, pagination_previous=pagination_previous, pagination_next=pagination_next)
def clear_db(): g.run("MATCH (n) DETACH DELETE n")