コード例 #1
0
 def test_get_solution_ids(self):
     query = 'MATCH (n:Solution) OPTIONAL MATCH (n)-[r]-() DELETE n, r'
     db.cypher_query(query)
     pleb = Pleb(email=str(uuid1()),
                 first_name="test",
                 last_name="test",
                 username=str(uuid1())).save()
     pleb2 = Pleb(email=str(uuid1()),
                  first_name="test",
                  last_name="test",
                  username=str(uuid1())).save()
     solution = Solution(content=uuid1(),
                         owner_username=self.pleb.username).save()
     solution2 = Solution(content=uuid1(),
                          owner_username=pleb.username).save()
     solution3 = Solution(content=uuid1(),
                          owner_username=pleb2.username).save()
     self.question.solutions.connect(solution)
     self.question.solutions.connect(solution2)
     self.question.solutions.connect(solution3)
     solutions = self.question.get_solution_ids()
     self.assertEqual(len(solutions), 3)
     self.assertIn(solution.object_uuid, solutions)
     self.assertIn(solution2.object_uuid, solutions)
     self.assertIn(solution3.object_uuid, solutions)
コード例 #2
0
 def test_three_solutions_different_authors(self):
     pleb = Pleb(email=str(uuid1()),
                 first_name="test",
                 last_name="test",
                 username=str(uuid1())).save()
     pleb2 = Pleb(email=str(uuid1()),
                  first_name="test",
                  last_name="test",
                  username=str(uuid1())).save()
     solution = Solution(content=uuid1(),
                         owner_username=self.pleb.username).save()
     solution2 = Solution(content=uuid1(),
                          owner_username=pleb.username).save()
     solution3 = Solution(content=uuid1(),
                          owner_username=pleb2.username).save()
     self.question.solutions.connect(solution)
     self.question.solutions.connect(solution2)
     self.question.solutions.connect(solution3)
     authors = self.question.get_conversation_authors()
     self.assertIn(self.pleb.first_name, authors)
     self.assertIn(self.pleb.last_name, authors)
     self.assertIn(pleb.first_name, authors)
     self.assertIn(pleb.last_name, authors)
     self.assertIn(pleb2.first_name, authors)
     self.assertIn(pleb2.last_name, authors)
     self.assertEqual(authors.count(','), 2)
コード例 #3
0
    def test_list_least_recent_ordering(self):
        query = "MATCH (n:SBContent) OPTIONAL MATCH " \
                "(n:SBContent)-[r]-() DELETE n,r"

        res, _ = db.cypher_query(query)
        self.client.force_authenticate(user=self.user)
        question = Question(title='test_title',
                            content='test_content',
                            owner_username=self.pleb2.username).save()
        question.owned_by.connect(self.pleb2)
        # Solution 1
        solution = Solution(content='test_content',
                            owner_username=self.pleb2.username,
                            parent_id=question.object_uuid).save()
        solution.owned_by.connect(self.pleb)
        question.solutions.connect(solution)
        # Solution 2
        solution2 = Solution(content='test_content22',
                             owner_username=self.pleb.username,
                             parent_id=question.object_uuid).save()
        solution2.owned_by.connect(self.pleb)
        question.solutions.connect(solution2)
        # Solution 3
        solution3 = Solution(content='test_content33',
                             owner_username=self.pleb.username,
                             parent_id=question.object_uuid).save()
        solution3.owned_by.connect(self.pleb)
        question.solutions.connect(solution3)
        # Solution 4
        solution4 = Solution(content='test_content44',
                             owner_username=self.pleb2.username,
                             parent_id=question.object_uuid).save()
        solution4.owned_by.connect(self.pleb2)
        question.solutions.connect(solution4)
        # Solution 5
        solution5 = Solution(content='test_content55',
                             owner_username=self.pleb.username,
                             parent_id=question.object_uuid).save()
        solution5.owned_by.connect(self.pleb)
        question.solutions.connect(solution5)
        url = reverse('question-solutions',
                      kwargs={"object_uuid": question.object_uuid}) \
            + "?limit=5&offset=0&expand=true&ordering=created"
        response = self.client.get(url, format='json')
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(len(response.data['results']), 5)
        self.assertEqual(response.data['results'][0]['id'],
                         solution.object_uuid)
        self.assertEqual(response.data['results'][1]['id'],
                         solution2.object_uuid)
        self.assertEqual(response.data['results'][2]['id'],
                         solution3.object_uuid)
        self.assertEqual(response.data['results'][3]['id'],
                         solution4.object_uuid)
        self.assertEqual(response.data['results'][4]['id'],
                         solution5.object_uuid)
コード例 #4
0
    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
コード例 #5
0
 def test_one_solution_by_same_author(self):
     solution = Solution(content=uuid1(),
                         owner_username=self.pleb.username).save()
     self.question.solutions.connect(solution)
     authors = self.question.get_conversation_authors()
     self.assertEqual("%s %s" % (self.pleb.first_name, self.pleb.last_name),
                      authors)
コード例 #6
0
 def setUp(self):
     self.unit_under_test_name = 'pleb'
     self.email = "*****@*****.**"
     self.email = "*****@*****.**"
     self.email2 = "*****@*****.**"
     self.pleb = create_user_util_test(self.email)
     self.user = User.objects.get(email=self.email)
     self.pleb2 = create_user_util_test(self.email2)
     self.user2 = User.objects.get(email=self.email2)
     self.title = str(uuid1())
     self.question = Question(content="Hey I'm a question",
                              title=self.title,
                              owner_username=self.pleb.username).save()
     self.solution = Solution(content="This is a test solution",
                              owner_username=self.pleb.username,
                              parent_id=self.question.object_uuid).save()
     self.solution.owned_by.connect(self.pleb)
     self.question.owned_by.connect(self.pleb)
     try:
         Tag.nodes.get(name='taxes')
     except DoesNotExist:
         Tag(name='taxes').save()
     try:
         Tag.nodes.get(name='fiscal')
     except DoesNotExist:
         Tag(name='fiscal').save()
     try:
         Tag.nodes.get(name='environment')
     except DoesNotExist:
         Tag(name='environment').save()
コード例 #7
0
 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='')
コード例 #8
0
 def get_mission(cls, object_uuid, request):
     from sb_solutions.neo_models import Solution
     from sb_questions.neo_models import Question
     comment = Comment.nodes.get(object_uuid=object_uuid)
     if comment.parent_type == "solution":
         return Solution.get_mission(comment.parent_id, request)
     elif comment.parent_type == "question":
         return Question.get_mission(comment.parent_id, request)
     else:
         return None
コード例 #9
0
 def setUp(self):
     self.unit_under_test_name = 'pleb'
     self.email = "*****@*****.**"
     self.pleb = create_user_util_test(self.email)
     self.user = User.objects.get(email=self.email)
     self.question = Question(content="Hey I'm a question",
                              title=str(uuid1()),
                              owner_username=self.pleb.username).save()
     self.solution = Solution(content="This is a test solution",
                              owner_username=self.pleb.username).save()
コード例 #10
0
 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(
                     '&gt;', '>')).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(
                     '&gt;', '>')).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(
                     '&gt;', '>')).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(
                     '&gt;', '>')).replace('<a', '<a target="_blank"'))
             update.content = rendered
             update.save()
     cache.set("migrated_to_new_editor", True)
コード例 #11
0
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)
コード例 #12
0
 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)
コード例 #13
0
 def setUp(self):
     cache.clear()
     self.email = "*****@*****.**"
     self.email2 = "*****@*****.**"
     self.pleb = create_user_util_test(self.email)
     self.pleb2 = create_user_util_test(self.email2)
     self.user = User.objects.get(email=self.email)
     self.user2 = User.objects.get(email=self.email2)
     self.title = str(uuid1())
     self.factory = RequestFactory()
     self.question = Question(content="Hey I'm a question",
                              title=self.title,
                              owner_username=self.pleb.username).save()
     self.solution = Solution(content="This is a test solution",
                              owner_username=self.pleb.username,
                              parent_id=self.question.object_uuid).save()
     self.solution.owned_by.connect(self.pleb)
     self.question.owned_by.connect(self.pleb)
     self.user = User.objects.get(email=self.email)
コード例 #14
0
    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)
コード例 #15
0
 def test_get_mission(self):
     solution = Solution(content='some test content').save()
     self.question.solutions.connect(solution)
     res = Solution.get_mission(solution.object_uuid)
     self.assertEqual(res['id'], self.mission.object_uuid)
コード例 #16
0
    def test_list_vote_count_ordering(self):
        query = "MATCH (n:SBContent) OPTIONAL MATCH " \
                "(n:SBContent)-[r]-() DELETE n,r"
        res, _ = db.cypher_query(query)
        self.client.force_authenticate(user=self.user)
        pleb3 = create_user_util_test("*****@*****.**")
        question = Question(title='test_title',
                            content='test_content',
                            owner_username=self.pleb2.user_weight).save()

        question.owned_by.connect(self.pleb2)
        # Solution 1 1 Upvote
        solution = Solution(content='test_content',
                            owner_username=self.pleb2.username,
                            parent_id=question.object_uuid).save()
        solution.owned_by.connect(self.pleb)
        question.solutions.connect(solution)
        create_vote_relationship(solution.object_uuid, self.pleb2.username,
                                 "true", "true")
        # Solution 2 1 Downvote
        solution2 = Solution(content='test_content22',
                             owner_username=self.pleb.username,
                             parent_id=question.object_uuid).save()
        solution2.owned_by.connect(self.pleb)
        question.solutions.connect(solution2)
        create_vote_relationship(solution2.object_uuid, self.pleb2.username,
                                 "true", "false")
        # Solution 3 No Votes
        solution3 = Solution(content='test_content33',
                             owner_username=self.pleb.username,
                             parent_id=question.object_uuid).save()
        solution3.owned_by.connect(self.pleb)
        question.solutions.connect(solution3)
        # Solution 4 2 Downvotes
        solution4 = Solution(content='test_content44',
                             owner_username=self.pleb2.username,
                             parent_id=question.object_uuid).save()
        solution4.owned_by.connect(self.pleb2)
        question.solutions.connect(solution4)
        create_vote_relationship(solution4.object_uuid, self.pleb2.username,
                                 "true", "false")

        create_vote_relationship(solution4.object_uuid, pleb3.username, "true",
                                 "false")
        # Solution 5 2 Upvotes
        solution5 = Solution(content='test_content55',
                             owner_username=self.pleb.username,
                             parent_id=question.object_uuid).save()
        solution5.owned_by.connect(self.pleb)
        question.solutions.connect(solution5)
        create_vote_relationship(solution5.object_uuid, self.pleb2.username,
                                 "true", "true")
        create_vote_relationship(solution5.object_uuid, pleb3.username, "true",
                                 "true")

        url = reverse('question-solutions',
                      kwargs={"object_uuid": question.object_uuid}) \
            + "?limit=5&offset=0&expand=true&ordering=vote_count"
        response = self.client.get(url, format='json')
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(len(response.data['results']), 5)
        self.assertEqual(response.data['results'][0]['id'],
                         solution5.object_uuid)
        self.assertEqual(response.data['results'][1]['id'],
                         solution.object_uuid)
        self.assertEqual(response.data['results'][2]['id'],
                         solution3.object_uuid)
        self.assertEqual(response.data['results'][3]['id'],
                         solution2.object_uuid)
        self.assertEqual(response.data['results'][4]['id'],
                         solution4.object_uuid)