Example #1
0
def create_question(_id):
    try:
        article = Article.get_one(_id)
    except ValueError as e:
        return response(message=str(e), ok=False), 400

    if not article:
        return response(message=f"Article {_id} not found", ok=False), 400

    account = Account.current()
    resp, status_code = create(Question,
                               additional_fields={
                                   'created_at': datetime.utcnow(),
                                   'article_id': _id,
                                   'user_id': account.get_id()
                               })

    if status_code == 200:
        question = Question.get_one(resp.json['_id'])
        FirebaseMessage(
            {
                'title': f'Nueva pregunta de {account["name"]}',
                'message': f'{question["question"]}',
                'question': question.get_id(),
                'type': 'new_question'
            },
            to=article.account()).send()

    return resp, status_code
Example #2
0
def test_article_post_question(client, article):
    resp = client.post(f'/article/{article.get_id()}/question/',
                       data=json.dumps({'question': "my question"}),
                       content_type='application/json')

    assert resp.status_code == 200
    assert len(Question.get_many()) == 1
Example #3
0
 def get_by_owner(self):
     account = Account.get_one(self.owner)
     article_ids = [a.get_id()
                    for a in Article.get_many(user=account['user_id'])]
     questions = []
     for article_id in article_ids:
         questions.extend(
             Question.get_many(article_id=article_id)
         )
     return self.append_article_to_questions(questions)
Example #4
0
def patch_question():
    resp, status_code = patch(Question)
    if status_code == 200:
        question = Question.get_one(resp.json['data']['_id'])
        article = Article.get_one(question['article_id'])
        FirebaseMessage(
            {
                'title': f'Nueva respuesta sobre {article["name"]}',
                'message': f'{question["answer"]}',
                'article_id': question['article_id'],
                'type': 'new_answer'
            },
            to=Account.get_one(question['user_id'])).send()
    return resp, status_code
Example #5
0
def questions(_id):
    try:
        article = Article.get_one(_id)
    except ValueError as e:
        return response(message=str(e), ok=False), 400

    if not article:
        return response(message=f"Article {_id} not found", ok=False), 400

    return jsonify({
        'data': [q.to_json() for q in Question.get_many(article_id=_id)],
        'ok':
        True
    })
Example #6
0
def answer_question(article_id, question_id):
    try:
        article = Article.get_one(article_id)
    except ValueError as e:
        return response(message=str(e), ok=False), 400

    if not article:
        return response(message=f"Article {article_id} not found",
                        ok=False), 400

    try:
        question = Question.get_one(question_id)
    except ValueError as e:
        return response(message=str(e), ok=False), 400

    body = request.get_json(silent=True)
    if not body:
        return response("Invalid or empty request body", ok=False), 400

    answer = body.get('answer')
    if not answer:
        return response("No answer specified", ok=False), 400

    answered_at = datetime.utcnow()

    question.update(**{'answer': answer, 'answered_at': answered_at})
    account = Account.current()
    FirebaseMessage(
        {
            'title': f'Nueva respuesta de {account["name"]}',
            'message': f'{question["answer"]}',
            'article_id': article_id,
            'type': 'new_answer'
        },
        to=Account.get_one(question['user_id'])).send()

    return jsonify({"ok": True, "data": question.to_json()}), 200
Example #7
0
    def get_questions(self):
        if self.owner:
            return self.get_by_owner()

        queryset = Question.get_many(**self.args)
        return self.append_article_to_questions(queryset)
Example #8
0
def get_one(_id):
    try:
        return jsonify(Question.get_one(_id).to_json()), 200
    except ValueError:
        return response(message=f"Question {_id} not found", ok=False), 400