def get_solutions(self, obj): expand_param = self.context.get('expand_param', None) request, expand, _, relations, expedite = gather_request_data( self.context, expedite_param=self.context.get('expedite_param', None), expand_param=expand_param) if expedite == "true": return [] solutions = [] if expand == "true" and relations != "hyperlink": query = 'MATCH (a:Question {object_uuid: "%s"})' \ '-[:POSSIBLE_ANSWER]->(solutions:Solution) ' \ 'WHERE solutions.to_be_deleted = false ' \ 'RETURN solutions' % obj.object_uuid res, _ = db.cypher_query(query) solutions = SolutionSerializerNeo( [Solution.inflate(row[0]) for row in res], many=True, context={ "request": request, "expand_param": expand_param }).data else: if relations == "hyperlink": solutions = [ reverse('solution-detail', kwargs={'object_uuid': solution_uuid}, request=request) for solution_uuid in obj.get_solution_ids() ] else: return solutions return solutions
def migrate_solutions(self): query = 'MATCH (a:Solution)<-[:POSSIBLE_ANSWER]-(b:Question) ' \ 'SET a.parent_id=b.object_uuid RETURN a' res, _ = db.cypher_query(query) for solution in [Solution.inflate(row[0]) for row in res]: spawn_task(task_func=create_solution_summary_task, task_param={ 'object_uuid': solution.object_uuid }) self.stdout.write("completed solution migration\n", ending='')
def migrate_to_new_editor(self): skip = 0 while True: query = 'MATCH (m:Mission) RETURN m SKIP %s LIMIT 25' % skip skip += 24 res, _ = db.cypher_query(query) if not res.one: break for mission in [Mission.inflate(row[0]) for row in res]: rendered = render_content( markdown.markdown(mission.epic.replace( '>', '>')).replace('<a', '<a target="_blank"')) mission.epic = rendered mission.temp_epic = rendered mission.save() skip = 0 while True: query = 'MATCH (m:Question) RETURN m SKIP %s LIMIT 25' % skip skip += 24 res, _ = db.cypher_query(query) if not res.one: break for question in [Question.inflate(row[0]) for row in res]: rendered = render_content( markdown.markdown(question.content.replace( '>', '>')).replace('<a', '<a target="_blank"')) question.content = rendered question.save() skip = 0 while True: query = 'MATCH (m:Solution) RETURN m SKIP %s LIMIT 25' % skip skip += 24 res, _ = db.cypher_query(query) if not res.one: break for solution in [Solution.inflate(row[0]) for row in res]: rendered = render_content( markdown.markdown(solution.content.replace( '>', '>')).replace('<a', '<a target="_blank"')) solution.content = rendered solution.save() skip = 0 while True: query = 'MATCH (m:Update) RETURN m SKIP %s LIMIT 25' % skip skip += 24 res, _ = db.cypher_query(query) if not res.one: break for update in [Update.inflate(row[0]) for row in res]: rendered = render_content( markdown.markdown(update.content.replace( '>', '>')).replace('<a', '<a target="_blank"')) update.content = rendered update.save() cache.set("migrated_to_new_editor", True)
def get_public_content(api, username, request): then = (datetime.now(pytz.utc) - timedelta(days=120)).strftime("%s") query = \ '// Retrieve all the current users questions\n' \ 'MATCH (a:Pleb {username: "******"})<-[:OWNED_BY]-' \ '(questions:Question) ' \ 'WHERE questions.to_be_deleted = False AND questions.created > %s' \ ' AND questions.is_closed = False ' \ 'RETURN questions, NULL AS solutions, ' \ 'questions.created AS created, NULL AS s_question UNION ' \ '// Retrieve all the current users solutions\n' \ 'MATCH (a:Pleb {username: "******"})<-' \ '[:OWNED_BY]-(solutions:Solution)<-' \ '[:POSSIBLE_ANSWER]-(s_question:Question) ' \ 'WHERE s_question.to_be_deleted = False ' \ 'AND solutions.created > %s' \ ' AND solutions.is_closed = False ' \ 'AND s_question.is_closed = False ' \ 'AND solutions.to_be_deleted = False ' \ 'RETURN solutions, NULL AS questions, ' \ 'solutions.created AS created, s_question AS s_question' \ % (username, then, username, then) news = [] res, _ = db.cypher_query(query) # Profiled with ~50 objects and it was still performing under 1 ms. # By the time sorting in python becomes an issue the above mentioned # ticket should be resolved. res = sorted(res, key=attrgetter('created'), reverse=True)[:5] page = api.paginate_queryset(res) for row in page: news_article = None if row.questions is not None: row.questions.pull() news_article = QuestionSerializerNeo(Question.inflate( row.questions), context={ 'request': request }).data elif row.solutions is not None: row.s_question.pull() row.solutions.pull() question_data = QuestionSerializerNeo( Question.inflate(row.s_question)).data news_article = SolutionSerializerNeo(Solution.inflate( row.solutions), context={ 'request': request }).data news_article['question'] = question_data news.append(news_article) return api.get_paginated_response(news)
def list(self, request, *args, **kwargs): council_list = [] html = request.query_params.get('html', 'false').lower() queryset = self.get_queryset() page = self.paginate_queryset(queryset) for row in page: if row[0] is not None: row[0].pull() # fix for None objects being returned # from the query due to multiple column returns council_object = None if row.questions is not None: council_object = QuestionSerializerNeo(Question.inflate( row.questions), context={ 'request': request }).data elif row.solutions is not None: council_object = SolutionSerializerNeo(Solution.inflate( row.solutions), context={ 'request': request }).data elif row.comments is not None: council_object = CommentSerializer(Comment.inflate( row.comments), context={ 'request': request }).data elif row.posts is not None: council_object = PostSerializerNeo(Post.inflate(row.posts), context={ 'request': request }).data if html == 'true': council_object['last_edited_on'] = parser.parse( council_object['last_edited_on']) council_object['request'] = request council_object = { "html": render_to_string("council_votable.html", council_object), "id": council_object["id"], "type": council_object["type"] } council_list.append(council_object) return self.get_paginated_response(council_list)
def newsfeed(self, request): """ The newsfeed endpoint expects to be called on the me endpoint and assumes that the request object provided will contain the user the newsfeed is being provided to. It is not included as a list_route on the me endpoint due to the me endpoint not being a viewset. If we transition to that structure it could easily be moved to a list_route there. Query if we want to grab tags: MATCH (a:Pleb {username: "******"})- [OWNS_QUESTION]->(questions:Question)-[:TAGGED_AS]->(tags:Tag) WHERE questions.to_be_deleted = False AND questions.created > %s RETURN questions, tags.name AS tags, NULL as solutions, NULL as posts UNION MATCH (a)-[manyFriends:FRIENDS_WITH*2 {active: True}]-> ()-[OWNS_QUESTION]->(questions:Question)-[:TAGGED_AS]-> (tags:Tag) WHERE questions.to_be_deleted = False AND questions.created > %s RETURN questions, tags.name AS tags, NULL as posts, NULL as solutions UNION :param request: """ # This query retrieves all of the current user's posts, solutions, # and questions as well as their direct friends posts, solutions, # and questions. It then looks for all of their friends friends # solutions and questions, combines all of the content and # returns the result. The query filters out content scheduled for # deletion and only looks for content created more recently than the # time provided. The reasoning for not including friends of friends # posts is to try and improve privacy. Friends of friends have not # actually been accepted potentially as friends by the user and # therefore should not have access to information posted on the user's # wall which in this case would be their posts. # We currently do not sort this query in neo because we are waiting # for post processing on unions as a whole to be added as a feature. # See Github issue #2725 for updates # https://github.com/neo4j/neo4j/issues/2725 then = (datetime.now(pytz.utc) - timedelta(days=120)).strftime("%s") query = \ '// Retrieve all the current users questions\n' \ 'MATCH (a:Pleb {username: "******"})<-[:OWNED_BY]-' \ '(questions:Question) ' \ 'WHERE questions.to_be_deleted = False AND questions.created > %s' \ ' AND questions.is_closed = False ' \ 'RETURN questions, NULL AS solutions, NULL AS posts, ' \ 'questions.created AS created, NULL AS s_question, ' \ 'NULL AS mission, NULL AS updates, NULL AS q_mission, ' \ 'NULL AS news UNION ' \ '' \ '// Retrieve all the news articles the user may be \n' \ '// interested in\n' \ 'MATCH (a:Pleb {username: "******"})-[:INTERESTED_IN]->' \ '(tag:Tag)<-[:TAGGED_AS]-(news:NewsArticle) ' \ 'WHERE news.published > %s AND news.is_closed = False ' \ ' RETURN DISTINCT news, NULL AS solutions, NULL AS posts, ' \ 'news.published AS created, NULL AS s_question, ' \ 'NULL AS mission, NULL AS updates, NULL AS q_mission, ' \ 'NULL AS questions UNION ' \ '' \ '// Retrieve all the current users solutions\n' \ 'MATCH (a:Pleb {username: "******"})<-' \ '[:OWNED_BY]-(solutions:Solution)<-' \ '[:POSSIBLE_ANSWER]-(s_question:Question) ' \ 'WHERE s_question.to_be_deleted = False ' \ 'AND solutions.created > %s' \ ' AND solutions.is_closed = False ' \ 'AND s_question.is_closed = False ' \ 'RETURN solutions, NULL AS questions, NULL AS posts, ' \ 'solutions.created AS created, s_question AS s_question,' \ 'NULL AS mission, NULL AS updates, NULL AS q_mission, ' \ 'NULL AS news UNION ' \ '' \ '// Retrieve all the current users posts\n' \ 'MATCH (a:Pleb {username: "******"})<-[:OWNED_BY]-(posts:Post) ' \ 'WHERE posts.to_be_deleted = False AND posts.created > %s ' \ 'AND posts.is_closed = False ' \ 'RETURN posts, NULL as questions, NULL as solutions, ' \ 'posts.created AS created, NULL AS s_question,' \ 'NULL AS mission, NULL AS updates, NULL AS q_mission, ' \ 'NULL AS news UNION ' \ '' \ '// Retrieve all the posts on the current users wall that are \n' \ '// not owned by the current user \n' \ 'MATCH (a:Pleb {username: "******"})-[:OWNS_WALL]->(w:Wall)' \ '-[:HAS_POST]->(posts:Post) ' \ 'WHERE NOT (posts)-[:OWNED_BY]->(a) AND ' \ 'posts.to_be_deleted = False AND posts.created > %s ' \ 'AND posts.is_closed = False ' \ 'RETURN posts, NULL as questions, NULL as solutions, ' \ 'posts.created AS created, NULL AS s_question,' \ 'NULL AS mission, NULL AS updates, NULL AS q_mission, ' \ 'NULL AS news UNION ' \ '' \ '// Retrieve the missions affecting the given user\n' \ 'MATCH (a:Pleb {username: "******"})-[:LIVES_AT]->(:Address)-' \ '[:ENCOMPASSED_BY*..]->' \ '(:Location)<-[:WITHIN]-(mission:Mission {active: true})' \ '<-[:EMBARKS_ON]-(quest:Quest {active: true}) ' \ 'WHERE NOT((mission)-[:FOCUSED_ON]->(:Position {verified:false}))' \ ' AND mission.created > %s ' \ 'RETURN mission, NULL AS solutions, NULL AS posts, ' \ 'NULL AS questions, mission.created AS created, ' \ 'NULL AS s_question, NULL AS updates, NULL AS q_mission, ' \ 'NULL AS news UNION ' \ '' \ '// Retrieve the mission updates affecting ' \ '// the given user\n' \ 'MATCH (a:Pleb {username: "******"})-[:LIVES_AT]->(:Address)-' \ '[:ENCOMPASSED_BY*..]->' \ '(:Location)<-[:WITHIN]-(q_mission:Mission {active: true})' \ '<-[:EMBARKS_ON]-(quest:Quest {active: true}) WITH q_mission ' \ 'MATCH (q_mission)<-[:ABOUT]-(updates:Update) ' \ 'WHERE NOT((q_mission)-[:FOCUSED_ON]' \ '->(:Position {verified:false}))' \ ' AND updates.created > %s AND updates.is_closed = False ' \ 'RETURN updates, NULL AS solutions, NULL AS posts, ' \ 'NULL AS questions, updates.created AS created, ' \ 'NULL AS s_question, NULL as mission, q_mission, ' \ 'NULL AS news UNION ' \ '' \ '// Retrieve all the posts owned by users that the current user ' \ '// is following \n' \ 'MATCH (a:Pleb {username: "******"})-[r:FOLLOWING {active: True}]->' \ '(:Pleb)<-[:OWNED_BY]-(posts:Post) ' \ 'WHERE posts.to_be_deleted = False AND ' \ 'posts.created > %s AND posts.is_closed = False ' \ 'RETURN NULL AS solutions, posts AS posts, ' \ 'NULL AS questions, posts.created AS created, ' \ 'NULL AS s_question, NULL AS mission, NULL AS updates, ' \ 'NULL AS q_mission, ' \ 'NULL AS news UNION ' \ '' \ '// Retrieve all the users questions that the current user is ' \ '// following \n' \ 'MATCH (a:Pleb {username: "******"})-[r:FOLLOWING {active: True}]->' \ '(:Pleb)<-[:OWNED_BY]-(questions:Question) ' \ 'WHERE questions.to_be_deleted = False AND ' \ 'questions.created > %s AND questions.is_closed = False ' \ 'RETURN NULL AS solutions, NULL AS posts, ' \ 'questions AS questions, questions.created AS created, ' \ 'NULL AS s_question, NULL AS mission, NULL AS updates, ' \ 'NULL AS q_mission, ' \ 'NULL AS news UNION ' \ '' \ '// Retrieve all the users solutions that the current user is ' \ '// following \n' \ 'MATCH (a:Pleb {username: "******"})-[r:FOLLOWING {active: True}]->' \ '(:Pleb)<-[:OWNED_BY]-(solutions:Solution)<-' \ '[:POSSIBLE_ANSWER]-(s_question:Question) ' \ 'WHERE s_question.to_be_deleted = False AND ' \ 'solutions.created > %s AND solutions.is_closed = False ' \ 'RETURN solutions, NULL AS posts, ' \ 'NULL AS questions, solutions.created AS created, ' \ 's_question as s_question, NULL AS mission, NULL AS updates, ' \ 'NULL AS q_mission, ' \ 'NULL AS news' \ % ( request.user.username, then, request.user.username, then, request.user.username, then, request.user.username, then, request.user.username, then, request.user.username, then, request.user.username, then, request.user.username, then, request.user.username, then, request.user.username, then) news = [] res, _ = db.cypher_query(query) # Profiled with ~50 objects and it was still performing under 1 ms. # By the time sorting in python becomes an issue the above mentioned # ticket should be resolved. res = sorted(res, key=attrgetter('created'), reverse=True) page = self.paginate_queryset(res) for row in page: news_article = None if row.questions is not None: row.questions.pull() news_article = QuestionSerializerNeo(Question.inflate( row.questions), context={ 'request': request }).data elif row.solutions is not None: row.s_question.pull() row.solutions.pull() question_data = QuestionSerializerNeo( Question.inflate(row.s_question)).data news_article = SolutionSerializerNeo(Solution.inflate( row.solutions), context={ 'request': request }).data news_article['question'] = question_data elif row.posts is not None: row.posts.pull() news_article = PostSerializerNeo(Post.inflate(row.posts), context={ 'request': request }).data elif row.mission is not None: row.mission.pull() news_article = MissionSerializer(Mission.inflate(row.mission), context={ 'request': request }).data news_article['reputation'] = Pleb.get( username=news_article['owner_username']).reputation elif row.updates is not None: row.updates.pull() row.q_mission.pull() news_article = UpdateSerializer(Update.inflate(row.updates), context={ 'request': request }).data news_article['mission'] = MissionSerializer( Mission.inflate(row.q_mission), context={ 'request': request }).data elif row.news is not None: row.news.pull() news_article = NewsArticleSerializer(NewsArticle.inflate( row.news), context={ 'request': request }).data news.append(news_article) return self.get_paginated_response(news)