diff --git a/api/models/call_log_event.py b/api/models/call_log_event.py index 14ed7c2..6ab866c 100644 --- a/api/models/call_log_event.py +++ b/api/models/call_log_event.py @@ -1,18 +1,8 @@ from api.mixins import TimestampMixin from api import db -from flask_sqlalchemy.query import Query as BaseQuery - - -class CallLogEventQuery(BaseQuery): - def call_sid_exist(self, form_data): - return ( - self.filter(CallLogEvent.call_sid == form_data["CallSid"]).first() - is not None - ) class CallLogEvent(TimestampMixin, db.Model): - query_class = CallLogEventQuery __tablename__ = "call_log_event" id = db.Column(db.Integer, primary_key=True) call_sid = db.Column(db.String(255)) diff --git a/api/models/registration.py b/api/models/registration.py index 90ea7c0..5a756bd 100644 --- a/api/models/registration.py +++ b/api/models/registration.py @@ -10,7 +10,6 @@ class Registration(TimestampMixin, db.Model): user_id = db.Column(db.Integer, db.ForeignKey("user.id")) partner_id = db.Column(db.Integer, db.ForeignKey("partner.id")) program_id = db.Column(db.Integer, db.ForeignKey("program.id")) - language_id = db.Column(db.Integer) district = db.Column(db.String(100), nullable=True) state = db.Column(db.String(100), nullable=True) parent_type = db.Column(db.String(100), nullable=True) diff --git a/api/models/system_phone.py b/api/models/system_phone.py index 65ece13..2efc934 100644 --- a/api/models/system_phone.py +++ b/api/models/system_phone.py @@ -1,20 +1,8 @@ from api.mixins import TimestampMixin from api import db -from flask_sqlalchemy.query import Query as BaseQuery - - -class SystemPhoneQuery(BaseQuery): - def system_phone_exists(self, url_decoded_system_phone): - return ( - self.filter( - SystemPhone.phone.contains(url_decoded_system_phone[-10:]) - ).first() - is not None - ) class SystemPhone(TimestampMixin, db.Model): - query_class = SystemPhoneQuery __tablename__ = "system_phone" id = db.Column(db.Integer, primary_key=True) phone = db.Column(db.String(50)) diff --git a/api/models/user.py b/api/models/user.py index df98c21..835d99b 100644 --- a/api/models/user.py +++ b/api/models/user.py @@ -1,6 +1,6 @@ from api.mixins import TimestampMixin from api import db -from flask_sqlalchemy.query import Query as BaseQuery +from flask_sqlalchemy import BaseQuery class UserQuery(BaseQuery): diff --git a/api/models/user_program.py b/api/models/user_program.py index ca83077..2632d31 100644 --- a/api/models/user_program.py +++ b/api/models/user_program.py @@ -1,7 +1,7 @@ from api.mixins import TimestampMixin from api import db from api import app, models, helpers -from flask_sqlalchemy.query import Query as BaseQuery +from flask_sqlalchemy import BaseQuery from api.helpers.common_helper import current_ist_time diff --git a/api/services/call_log_event_service.py b/api/services/call_log_event_service.py index 8fefc75..bac1cd4 100644 --- a/api/services/call_log_event_service.py +++ b/api/services/call_log_event_service.py @@ -1,5 +1,6 @@ # This file is treated as service layer -from api import helpers, models +from flask import request +from api import helpers, models, db from datetime import datetime from utils.loggingutils import logger import traceback diff --git a/api/services/handle_event_service.py b/api/services/handle_event_service.py index 65dc091..e394bfa 100644 --- a/api/services/handle_event_service.py +++ b/api/services/handle_event_service.py @@ -1,5 +1,6 @@ import requests -from api import models, app +from flask import request +from api import models, db, app from . import user_service as user from . import registration_service as registration from . import call_log_event_service as call_log_event @@ -9,16 +10,18 @@ class HandleEventService: def handle_event_service(self, form_data): - call_sid_exist = models.CallLogEvent.query.call_sid_exist(form_data) + call_sid_exist = db.session.query( + db.exists().where(models.CallLogEvent.call_sid == form_data["CallSid"]) + ).scalar() if call_sid_exist: return url_decoded_system_phone = requests.utils.unquote(form_data["To"]) - system_phone_exists = models.SystemPhone.query.system_phone_exists( - url_decoded_system_phone - ) + system_phone_exists = db.session.query( + db.exists().where(models.SystemPhone.phone == url_decoded_system_phone) + ).scalar() if not system_phone_exists: return diff --git a/config.py b/config.py index ec9dfa7..d07abdb 100644 --- a/config.py +++ b/config.py @@ -34,6 +34,7 @@ % POSTGRES ) +SQLALCHEMY_TRACK_MODIFICATIONS = True WTF_CSRF_ENABLED = True SECRET_KEY = os.environ.get("SECRET_KEY") diff --git a/main.py b/main.py index fffc1ad..844ed88 100644 --- a/main.py +++ b/main.py @@ -1,52 +1,52 @@ -from api import app, services, helpers -from flask import jsonify +# from pprint import pprint +from api import models, db, services +from flask import jsonify, request import json from utils.loggingutils import logger def callback(request): - with app.app_context(): - json_data = None - form_data = None + json_data = None + form_data = None - if request.method == "POST": - content_type = request.headers.get("Content-Type") + if request.method == "POST": + content_type = request.headers.get("Content-Type") - if content_type == "application/json": - json_data = request.get_json() - else: - form_data = request.form + if content_type == "application/json": + json_data = request.get_json() + else: + form_data = request.form - transaction_log_service = services.TransactionLogService() + transaction_log_service = services.TransactionLogService() - try: - if json_data and json_data.get("type", None) == "retry_failed_log": - retry_failed_webhook(transaction_log_service) - return "Success" + try: + if json_data and json_data.get("type", None) == "retry_failed_log": + retry_failed_webhook(transaction_log_service) + return "Success" - ivr_transaction_log = ( - transaction_log_service.create_new_ivr_transaction_log(form_data) - ) - except Exception as e: - logger.error( - f"Issues with transaction logs creation for form data {form_data}. Error message: {e}" - ) + ivr_transaction_log = ( + transaction_log_service.create_new_ivr_transaction_log(form_data) + ) + except Exception as e: + logger.error( + f"Issues with transaction logs creation for form data {form_data}. Error message: {e}" + ) - processed = process_form_data(form_data) + processed = process_form_data(form_data) - if not processed: - return jsonify(message="Something went wrong!"), 400 + if not processed: + return jsonify(message="Something went wrong!"), 400 - transaction_log_service.mark_ivr_transaction_log_as_processed( - ivr_transaction_log - ) - else: - return ( - jsonify(message="Currently, the system do not accept a GET request"), - 405, - ) + transaction_log_service.mark_ivr_transaction_log_as_processed( + ivr_transaction_log + ) + else: + return ( + jsonify(message="Currently, the system do not accept a GET request"), + 405, + ) - return "Success" + return "Success" def retry_failed_webhook(transaction_log_service): @@ -57,7 +57,8 @@ def retry_failed_webhook(transaction_log_service): payload["log_created_on"] = log.created_on log.processed = process_form_data(payload) log.attempts += 1 - helpers.save(log) + db.session.add(log) + db.session.commit() def process_form_data(form_data): diff --git a/requirements-dev.txt b/requirements-dev.txt index 2219eaf..89e6b9f 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -1,4 +1,4 @@ # Includes dev dependencies on top of requirements.txt -r requirements.txt -functions-framework==3.5.0 +functions-framework==2.1.2 pre-commit==2.11.0 diff --git a/requirements.txt b/requirements.txt index 163ad58..46e0a2e 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,19 +1,18 @@ certifi==2020.12.5 chardet==4.0.0 -click==8.1.3 -Flask==3.0.0 +click==7.1.2 +Flask==1.1.2 +requests==2.25.1 python-dotenv==0.15.0 -Flask-SQLAlchemy==3.1.1 -functions-framework==3.5.0 +Flask-SQLAlchemy==2.1 idna==2.10 -itsdangerous==2.1.2 -Jinja2==3.1.2 -MarkupSafe==2.1.1 -psycopg2==2.9.9 +itsdangerous==1.1.0 +Jinja2==2.11.3 +MarkupSafe==1.1.1 +psycopg2==2.8.6 python-dotenv==0.15.0 -requests==2.31.0 -SQLAlchemy==2.0.23 +requests==2.25.1 +SQLAlchemy==1.3.23 urllib3==1.26.3 -Werkzeug==3.0.0 -watchdog==3.0.0 +Werkzeug==1.0.1 google-cloud-logging==3.5.0