-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from jamakase/feature/backend-api
update create_conversation.py
- Loading branch information
Showing
8 changed files
with
63 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,12 @@ | ||
import os | ||
|
||
database_url = os.environ.get('DATABASE_URL') | ||
celery_broker_url = os.environ.get('CELERY_BROKER_URL') | ||
celery_result_backend = os.environ.get('CELERY_RESULT_BACKEND') | ||
|
||
|
||
class Config: | ||
SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_URL', 'postgresql://user:password@db:5432/afana_propdoc') | ||
SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_URL', database_url) | ||
SQLALCHEMY_TRACK_MODIFICATIONS = False | ||
CELERY_BROKER_URL = os.environ.get('CELERY_BROKER_URL', 'redis://:password@redis:6379/0') | ||
CELERY_RESULT_BACKEND = os.environ.get('CELERY_RESULT_BACKEND', 'redis://:password@redis:6379/0') | ||
CELERY_BROKER_URL = os.environ.get('CELERY_BROKER_URL', celery_broker_url) | ||
CELERY_RESULT_BACKEND = os.environ.get('CELERY_RESULT_BACKEND', celery_result_backend) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 15 additions & 3 deletions
18
services/backend/routes/conversation/create_conversation.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,31 @@ | ||
import uuid | ||
from functools import wraps | ||
|
||
from flasgger import swag_from | ||
from flask import jsonify, Blueprint | ||
from flask import jsonify, Blueprint, g, session, request, make_response | ||
from services.conversation_service import ConversationService | ||
from schemas.conversation_schema import ConversationSchema, create_conversation_swagger | ||
|
||
from utils.user_required import user_required | ||
|
||
api = Blueprint('api', __name__) | ||
|
||
conversation_schema = ConversationSchema() | ||
|
||
|
||
@api.route('/conversation/create', methods=['POST']) | ||
@swag_from(create_conversation_swagger) | ||
@user_required | ||
def create_conversation(): | ||
""" | ||
Создает новую беседу. | ||
Возвращает ID созданной беседы. | ||
""" | ||
new_conversation = ConversationService.create_conversation() | ||
return jsonify({'conversation_id': new_conversation.id}), 201 | ||
user_id = request.cookies.get('user_id') | ||
|
||
new_conversation = ConversationService.create_conversation(user_id) | ||
|
||
resp = make_response(jsonify({'conversation_id': new_conversation.id}), 201) | ||
|
||
return resp | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import os | ||
import uuid | ||
from dotenv import load_dotenv | ||
from flask import make_response, request | ||
|
||
|
||
load_dotenv() | ||
access_control_allow_origin = os.environ.get('ACCESS_CONTROL_ALLOW_ORIGIN') | ||
|
||
def user_required(f): | ||
def decorated_function(*args, **kws): | ||
response = f(*args, **kws) | ||
response = make_response(response) | ||
|
||
if not request.cookies.get('user_id'): | ||
response.set_cookie('user_id', value=str(uuid.uuid4()), max_age=60 * 60 * 24 * 365 * 2, httponly = True) | ||
|
||
response.headers.add('Access-Control-Allow-Origin', access_control_allow_origin) | ||
response.headers.add('Access-Control-Allow-Credentials', 'true') | ||
|
||
return response | ||
|
||
return decorated_function |