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
def shipment_cost(article: Article, lat: float, lon: float, payment_method: str): account = Account.current() now = datetime.now() return request('POST', 'shipment-cost', json={ 'antiquity': account.antiquity(), 'email': account['email'], 'userScore': account.score(), 'paymentMethod': payment_method, 'distance': haversine((lat, lon), (article['latitude'], article['longitude'])), 'latitude': lat, 'longitude': lon, 'tripDate': now.strftime('%Y/%m/%d'), 'tripTime': now.strftime('%H:%M') })
def get_purchases(): return jsonify({ 'data': add_payment_and_shipment_status( Purchase.get_for_user(Account.current())), 'ok': True }), 200
def current_account(): account = Account.current() return jsonify({ "ok": True, "data": { **account.to_json(), **{ 'events': [event.to_json() for event in account.events()] } } }), 200
def create(room_id): purchase_id = request.get_json(silent=True).get('purchase_id') account = Account.current() response, status_code = \ utils.create(ChatMessage, additional_fields={'room': room_id, 'name': account['name'], 'sender_user_id': account.get_id()}) if status_code == 200 and purchase_id is not None: send_firebase_message(response.json['_id'], purchase_id) return response, status_code
def update_current_account(): body = request.get_json(silent=True) if not body: return response("Invalid or empty request body", ok=False), 400 account = Account.current() account['email'] = body.get('email') account['name'] = body.get('name') account['profile_picture_url'] = body.get('profile_picture_url') account['instance_id'] = body.get('instance_id') account.save() return response(message="Successfully updated current account!", ok=True)
def buy(): body = request.json error_response = check_required_fields(body) if error_response: return error_response article, error_response = get_article(body) if error_response: return error_response account = Account.current() account_id = account.get_id() if user_id() == article['user']: return response("You can't purchase your own article", ok=False), 400 article['available_units'] -= body.get('units') if article['available_units'] < 0: return response("not enough units", ok=False), 400 article.save() resp, status_code = create(Purchase, additional_fields={ 'user_id': account_id, 'requested_shipment': bool(body.get('shipment_address')) }, after_save=after_purchase( body.get('price'), body.get('payment_method'), body.get('shipment_address'))) if status_code == 200: msg = f'Vendiste {body.get("units")} de ' \ f'{article["name"]} a {account["name"]})' FirebaseMessage( { 'title': "Artículo vendido", "message": msg, "type": "product_sold", }, to=Purchase.get_one(resp.json['_id']).seller()).send() return resp, status_code
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