Пример #1
0
def account(extra_attrs: dict = None) -> Account:
    attrs = {
        'user_id': fake.word(),
        'email': fake.email(),
        'name': fake.sentence()
    }
    if extra_attrs is not None:
        attrs = {**attrs, **extra_attrs}
    _id = Account(attrs).save()
    return Account.get_one(_id)
Пример #2
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)
Пример #3
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
Пример #4
0
def send_firebase_message(message_id, purchase_id):
    # ☢️☢️☢️☢️☢️
    # Sends a firebase message, to the inferred recipient
    message = ChatMessage.get_one(message_id)

    sender_id = message['sender_user_id']
    purchase = Purchase.get_one(purchase_id)
    seller = purchase.seller()
    buyer = Account.get_one(purchase['user_id'])
    if sender_id == seller.get_id():
        sender = seller
        recipient = buyer
    else:
        sender = buyer
        recipient = seller
    FirebaseMessage(message_data={
        'title': sender['name'],
        'message': message['text'],
        'purchase_id': purchase_id,
        "type": "chat"
    },
                    to=recipient).send()
Пример #5
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
Пример #6
0
 def purchaser(self) -> Optional[Account]:
     return Account.get_one(self['user_id'])
Пример #7
0
def test_register_sale():
    _account = account({'score': random.randint(0, 100)})
    previous_score = _account.score()
    _account.register('sale', 'article_name')
    assert Account.get_one(_account.get_id()).score() == previous_score + 10