From 3afdbe8f305fb838ff23601a26e159b57a6e49f3 Mon Sep 17 00:00:00 2001 From: Lucas Dutra Date: Tue, 1 Oct 2019 21:00:47 -0300 Subject: [PATCH 01/63] pax-app/Wiki#103 Set database comunication with pax service --- database.py | 6 ++++++ manage.py | 2 ++ project/__init__.py | 4 ++++ requirements.txt | 11 ++++++++++- 4 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 database.py diff --git a/database.py b/database.py new file mode 100644 index 0000000..1ce2ce3 --- /dev/null +++ b/database.py @@ -0,0 +1,6 @@ +from flask_sqlalchemy import SQLAlchemy +from flask_migrate import Migrate + + +db = SQLAlchemy() +migrate = Migrate() diff --git a/manage.py b/manage.py index ce0fa6a..ddfc73e 100644 --- a/manage.py +++ b/manage.py @@ -1,5 +1,7 @@ from project import create_app from flask.cli import FlaskGroup +from database import db +from flask import current_app # Config coverage report app = create_app() diff --git a/project/__init__.py b/project/__init__.py index 9b58693..3b90c80 100644 --- a/project/__init__.py +++ b/project/__init__.py @@ -1,6 +1,7 @@ import os from flask import Flask, jsonify from project.api.views import pax_blueprint +from database import db, migrate # instantiate the app @@ -12,6 +13,9 @@ def create_app(script_info=None): app_settings = os.getenv('APP_SETTINGS') app.config.from_object(app_settings) + db.init_app(app) + migrate.init_app(app, db) + # register blueprints app.register_blueprint(pax_blueprint) diff --git a/requirements.txt b/requirements.txt index ae09db0..284b9e5 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1,10 @@ -Flask==1.0.2 \ No newline at end of file +pymysql +ConfigParser +Flask==1.0.2 +Flask-SQLAlchemy==2.3.2 +sqlalchemy_utils +passlib +Flask-Testing==0.6.2 +coverage==4.5.1 +gunicorn==19.8.1 +flask-migrate==2.2.0 \ No newline at end of file From ea2e6401cd9eee058f33499a655680720d02b13a Mon Sep 17 00:00:00 2001 From: Lucas Dutra Date: Tue, 1 Oct 2019 21:06:34 -0300 Subject: [PATCH 02/63] pax-app/Wiki#103 Update database URL into environment variables --- .dockerignore | 4 ++++ .env.example | 2 ++ Dockerfile | 2 ++ docker-compose.yml | 4 ++++ 4 files changed, 12 insertions(+) create mode 100644 .dockerignore create mode 100644 .env.example diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..9039563 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,4 @@ +env +.dockerignore +Dockerfile-dev +Dockerfile-prod \ No newline at end of file diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..fe7b0bf --- /dev/null +++ b/.env.example @@ -0,0 +1,2 @@ +DATABASE_URI= +APP_SECRET_KEY= \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index c642991..04c7887 100644 --- a/Dockerfile +++ b/Dockerfile @@ -10,6 +10,8 @@ RUN apk update && \ apk add --virtual build-deps gcc python-dev musl-dev # Dealing with requirements +RUN pip install --upgrade pip +RUN pip install Flask-SQLAlchemy COPY ./requirements.txt /app/requirements.txt RUN pip install -r requirements.txt diff --git a/docker-compose.yml b/docker-compose.yml index ba7a4aa..f3e7dac 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -9,3 +9,7 @@ services: - 5003:5000 environment: - FLASK_APP=project/__init__.py + - FLASK_ENV=development + - APP_SETTINGS=project.config.DevelopmentConfig + - DATABASE_URL=mysql+pymysql://f15jto2ojbvbb18m:ojjbjm6lswedzs91@bqmayq5x95g1sgr9.cbetxkdyhwsb.us-east-1.rds.amazonaws.com:3306/j3uyakutbdjh3tdy + - DATABASE_TEST_URL=mysql+pymysql://username:password@localhost:3306/pax From 8c8fc762ace8684c42129a7c6a4dc8c30e08bb1f Mon Sep 17 00:00:00 2001 From: Lucas Dutra Date: Tue, 1 Oct 2019 21:07:50 -0300 Subject: [PATCH 03/63] pax-app/Wiki#103 Update flask configs for each stage --- README.md | 11 ++++++++++- project/api/models.py | 7 +++++++ project/config.py | 16 ++++++++++++---- 3 files changed, 29 insertions(+), 5 deletions(-) create mode 100644 project/api/models.py diff --git a/README.md b/README.md index 697d7d7..4c534ab 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,11 @@ -# Pax_Pax +# Pax + Microsserviço responsável pelos contratos cliente-prestador. + +## Colocando no ar + +Com o Docker e Docker-Compose instalados, basta apenas utilizar os comandos: + +```shell + sudo docker-compose up +``` diff --git a/project/api/models.py b/project/api/models.py new file mode 100644 index 0000000..8ab3630 --- /dev/null +++ b/project/api/models.py @@ -0,0 +1,7 @@ +from database import db +from project import create_app +from Flask import current_app + + +class Pax(db.Model): + __tablename__ = 'PAX' diff --git a/project/config.py b/project/config.py index 12fc951..ddd6eeb 100644 --- a/project/config.py +++ b/project/config.py @@ -1,19 +1,27 @@ -# services/users/project/config.py +import os + + class BaseConfig: """Base configuration""" TESTING = False + SQLALCHEMY_TRACK_MODIFICATIONS = False class DevelopmentConfig(BaseConfig): """Development configuration""" - pass + SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_URL') class TestingConfig(BaseConfig): """Testing configuration""" TESTING = True + SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_TEST_URL') class ProductionConfig(BaseConfig): - """Production configuration""" - pass + DEBUG = False + SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_URL') + + +class HomologationConfig(BaseConfig): + SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_URL') From 8f81c31a8da1be0e4fd14a8b38b08b9616ff41c0 Mon Sep 17 00:00:00 2001 From: Lucas Dutra Date: Sat, 12 Oct 2019 12:54:42 -0300 Subject: [PATCH 04/63] pax-app/Wiki#165 Update database string connection Co-authored-by: Matheus Pimenta --- docker-compose.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docker-compose.yml b/docker-compose.yml index f3e7dac..f6bfa08 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -11,5 +11,5 @@ services: - FLASK_APP=project/__init__.py - FLASK_ENV=development - APP_SETTINGS=project.config.DevelopmentConfig - - DATABASE_URL=mysql+pymysql://f15jto2ojbvbb18m:ojjbjm6lswedzs91@bqmayq5x95g1sgr9.cbetxkdyhwsb.us-east-1.rds.amazonaws.com:3306/j3uyakutbdjh3tdy + - DATABASE_URL=mysql+pymysql://paxadmin:LpvuM1QnfcxOOVeNQQvl@pax-database.cd0j8avv0tgq.sa-east-1.rds.amazonaws.com:3306/paxdb - DATABASE_TEST_URL=mysql+pymysql://username:password@localhost:3306/pax From d401e7ed74caaccb9ab49539104a11f8b5ec8688 Mon Sep 17 00:00:00 2001 From: Lucas Dutra Ferreira do Nascimento Date: Tue, 22 Oct 2019 23:26:10 -0300 Subject: [PATCH 05/63] Update README.md --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 4c534ab..f4bae39 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,6 @@ -# Pax +# Pax +[![Maintainability](https://api.codeclimate.com/v1/badges/337ba69b4fe369201c75/maintainability)](https://codeclimate.com/github/pax-app/Pax/maintainability) + Microsserviço responsável pelos contratos cliente-prestador. From 285608bca529a5184d9467381bc5f2c51d61cc6e Mon Sep 17 00:00:00 2001 From: Lucas Dutra Date: Thu, 24 Oct 2019 16:19:22 -0300 Subject: [PATCH 06/63] pax-app/Wiki#174 Update Dockerfile python version --- Dockerfile | 2 +- database.py | 6 ------ database_singleton.py | 23 +++++++++++++++++++++++ 3 files changed, 24 insertions(+), 7 deletions(-) delete mode 100644 database.py create mode 100644 database_singleton.py diff --git a/Dockerfile b/Dockerfile index 04c7887..58657b0 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,5 +1,5 @@ # base image -FROM python:3.6.5-alpine +FROM python:3.8.0-alpine # set working directory diff --git a/database.py b/database.py deleted file mode 100644 index 1ce2ce3..0000000 --- a/database.py +++ /dev/null @@ -1,6 +0,0 @@ -from flask_sqlalchemy import SQLAlchemy -from flask_migrate import Migrate - - -db = SQLAlchemy() -migrate = Migrate() diff --git a/database_singleton.py b/database_singleton.py new file mode 100644 index 0000000..d0a9413 --- /dev/null +++ b/database_singleton.py @@ -0,0 +1,23 @@ +from __future__ import annotations +from flask_sqlalchemy import SQLAlchemy +from flask_migrate import Migrate +from typing import Optional + + +class SingletonMeta(type): + _instance: Optional[Singleton] = None + + def __call__(self) -> Singleton: + if self._instance is None: + self._instance = super().__call__() + return self._instance + + +class Singleton(metaclass=SingletonMeta): + def database_connection(self): + db = SQLAlchemy() + return db + + def migration(self): + migrate = Migrate() + return migrate From 2d6f139c99b842fa452f099599b3c1e68681ce71 Mon Sep 17 00:00:00 2001 From: Lucas Dutra Date: Thu, 24 Oct 2019 16:37:48 -0300 Subject: [PATCH 07/63] pax-app/Wiki#174 Change database instatiation to singleton class --- project/__init__.py | 5 ++++- project/api/models.py | 2 ++ project/api/views.py | 2 ++ 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/project/__init__.py b/project/__init__.py index 3b90c80..38225f5 100644 --- a/project/__init__.py +++ b/project/__init__.py @@ -1,7 +1,7 @@ import os from flask import Flask, jsonify from project.api.views import pax_blueprint -from database import db, migrate +from database_singleton import Singleton # instantiate the app @@ -13,6 +13,9 @@ def create_app(script_info=None): app_settings = os.getenv('APP_SETTINGS') app.config.from_object(app_settings) + db = Singleton().database_connection() + migrate = Singleton().migration() + db.init_app(app) migrate.init_app(app, db) diff --git a/project/api/models.py b/project/api/models.py index 8ab3630..6825270 100644 --- a/project/api/models.py +++ b/project/api/models.py @@ -1,7 +1,9 @@ from database import db from project import create_app from Flask import current_app +from database_singleton import Singleton +db = Singleton().database_connection() class Pax(db.Model): __tablename__ = 'PAX' diff --git a/project/api/views.py b/project/api/views.py index 93916f3..1f4b215 100644 --- a/project/api/views.py +++ b/project/api/views.py @@ -1,6 +1,8 @@ from flask import request, jsonify, Blueprint +from database_singleton import Singleton pax_blueprint = Blueprint('pax', __name__) +db = Singleton().database_connection() @pax_blueprint.route('/pax/ping', methods=['GET']) From bb966bf7055a9bf3ce74cfbe026acd10f47c2825 Mon Sep 17 00:00:00 2001 From: Lucas Dutra Date: Tue, 29 Oct 2019 01:01:46 -0300 Subject: [PATCH 08/63] pax-app/Wiki#189 Create PAX models and its methods --- manage.py | 1 - project/api/models.py | 29 +++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/manage.py b/manage.py index ddfc73e..fe63b0d 100644 --- a/manage.py +++ b/manage.py @@ -1,6 +1,5 @@ from project import create_app from flask.cli import FlaskGroup -from database import db from flask import current_app # Config coverage report diff --git a/project/api/models.py b/project/api/models.py index 6825270..f245eaa 100644 --- a/project/api/models.py +++ b/project/api/models.py @@ -5,5 +5,34 @@ db = Singleton().database_connection() + class Pax(db.Model): __tablename__ = 'PAX' + + pax_id = db.Column(db.Integer, primary_key=True, autoincrement=True) + data = db.Column(db.Date, nullable=False) + description = db.Column(db.String(500), nullable=False) + name = db.Column(db.String(255), nullable=False) + price = db.Column(db.Float, nullable=False) + status = db.Column(db.Enum('F', 'P', 'C', 'I'), nullable=False) + user_id = db.Column(db.Integer, nullable=False) + provider_id = db.Column(db.Integer, nullable=False) + chat_id = db.Column(db.Integer, nullable=False) + address_id = db.Column(db.Integer, nullable=False) + + def __init__(self, data, description, name, price, status): + self.data = data + self.description = description + self.name = name + self.price = price + self.status = name + + def to_json(self): + return { + 'id': self.pax_id, + 'data': self.data, + 'description': self.description, + 'name': self.name, + 'price': self.price, + 'status': self.status, + } From 6d86e6a895ab8a602daa007a84d28531c4449555 Mon Sep 17 00:00:00 2001 From: Lucas Dutra Date: Tue, 29 Oct 2019 23:38:58 -0300 Subject: [PATCH 09/63] pax-app/Wiki#189 Update model constructor and to_json method --- project/api/models.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/project/api/models.py b/project/api/models.py index f245eaa..495aa5c 100644 --- a/project/api/models.py +++ b/project/api/models.py @@ -20,12 +20,16 @@ class Pax(db.Model): chat_id = db.Column(db.Integer, nullable=False) address_id = db.Column(db.Integer, nullable=False) - def __init__(self, data, description, name, price, status): + def __init__(self, data, description, name, price, status, user_id, provider_id, chat_id, address_id): self.data = data self.description = description self.name = name self.price = price self.status = name + self.user_id = user_id + self.provider_id = provider_id + self.chat_id = chat_id + self.address_id = address_id def to_json(self): return { @@ -35,4 +39,8 @@ def to_json(self): 'name': self.name, 'price': self.price, 'status': self.status, + 'user_id': self.user_id, + 'provider_id': self.provider_id, + 'chat_id': self.chat_id, + 'address_id': self.address_id } From 7612eab940fc6dfedc408678da1a2f4eb8acb0d9 Mon Sep 17 00:00:00 2001 From: Lucas Dutra Date: Tue, 29 Oct 2019 23:42:10 -0300 Subject: [PATCH 10/63] pax-app/Wiki#189 Create support methods --- project/api/utils/creation_utils.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 project/api/utils/creation_utils.py diff --git a/project/api/utils/creation_utils.py b/project/api/utils/creation_utils.py new file mode 100644 index 0000000..fb0c982 --- /dev/null +++ b/project/api/utils/creation_utils.py @@ -0,0 +1,27 @@ +import requests +from database_singleton import Singleton +from project.api.models import ProviderModel, UserModel, WorksModel +from flask import request, jsonify + +db = Singleton().database_connection() + + +class Utils: + def createFailMessage(self, message): + response_object = { + 'status': 'fail', + 'message': '{}'.format(message) + } + return response_object + + def createSuccessMessage(self, message): + response_object = { + 'status': 'success', + 'message': '{}'.format(message) + } + return response_object + + def commit_to_database(self, model): + db.session.add(model) + db.session.flush() + db.session.commit() From 1066f76c4d53ec9e76c4f55a364763459bb13c66 Mon Sep 17 00:00:00 2001 From: Lucas Dutra Date: Tue, 29 Oct 2019 23:42:58 -0300 Subject: [PATCH 11/63] pax-app/Wiki#189 Implement facade pattern through blueprint --- project/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/project/__init__.py b/project/__init__.py index 38225f5..05db26a 100644 --- a/project/__init__.py +++ b/project/__init__.py @@ -20,6 +20,6 @@ def create_app(script_info=None): migrate.init_app(app, db) # register blueprints - app.register_blueprint(pax_blueprint) + app.register_blueprint(pax_blueprint, url_prefix='/pax') return app From 8529a911d7f7864f5302cf1330173f51d1144dfd Mon Sep 17 00:00:00 2001 From: Lucas Dutra Date: Tue, 29 Oct 2019 23:51:09 -0300 Subject: [PATCH 12/63] pax-app/Wiki#189 Fix broken imports --- project/api/models.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/project/api/models.py b/project/api/models.py index 495aa5c..9c10d48 100644 --- a/project/api/models.py +++ b/project/api/models.py @@ -1,6 +1,3 @@ -from database import db -from project import create_app -from Flask import current_app from database_singleton import Singleton db = Singleton().database_connection() From 42b8bb77cacde7fc5139baf8fbe555706fb7c3d6 Mon Sep 17 00:00:00 2001 From: Lucas Dutra Date: Tue, 29 Oct 2019 23:51:44 -0300 Subject: [PATCH 13/63] pax-app/Wiki#189 Create POST pax api method --- project/api/utils/creation_utils.py | 2 -- project/api/views.py | 38 ++++++++++++++++++++++++----- 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/project/api/utils/creation_utils.py b/project/api/utils/creation_utils.py index fb0c982..3de9a69 100644 --- a/project/api/utils/creation_utils.py +++ b/project/api/utils/creation_utils.py @@ -1,6 +1,4 @@ -import requests from database_singleton import Singleton -from project.api.models import ProviderModel, UserModel, WorksModel from flask import request, jsonify db = Singleton().database_connection() diff --git a/project/api/views.py b/project/api/views.py index 1f4b215..1e2eea3 100644 --- a/project/api/views.py +++ b/project/api/views.py @@ -1,13 +1,39 @@ from flask import request, jsonify, Blueprint from database_singleton import Singleton +from project.api.utils.creation_utils import Utils +from project.api.models import Pax +from sqlalchemy import exc pax_blueprint = Blueprint('pax', __name__) db = Singleton().database_connection() +utils = Utils() -@pax_blueprint.route('/pax/ping', methods=['GET']) -def ping_pong(): - return jsonify({ - 'status': 'success', - 'message': 'pong!' - }) +@pax_blueprint.route('/receipt', methods=['POST']) +def add_receipt(): + post_data = request.get_json() + + if not post_data: + return jsonify(utils.createFailMessage('Wrong JSON')), 400 + + pax = post_data.get('pax') + + data = pax.get('data') + description = pax.get('description') + name = pax.get('name') + price = pax.get('price') + status = pax.get('status') + user_id = pax.get('user_id') + provider_id = pax.get('provider_id') + chat_id = pax.get('chat_id') + address_id = pax.get('address_id') + + try: + pax = Pax(data, description, name, + price, status, user_id, provider_id, chat_id, address_id) + utils.commit_to_database(pax) + return jsonify(utils.createSuccessMessage('Pax was created!')), 201 + + except exc.IntegrityError: + db.session.rollback() + return jsonify(utils.createFailMessage('Wrong JSON')), 400 From 27e439dad10fefecf426c3d6227b3105b9c39dbb Mon Sep 17 00:00:00 2001 From: Lucas Dutra Date: Tue, 29 Oct 2019 23:52:31 -0300 Subject: [PATCH 14/63] pax-app/Wiki#189 Add requests to imports --- project/api/views.py | 4 ++-- requirements.txt | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/project/api/views.py b/project/api/views.py index 1e2eea3..9734dc7 100644 --- a/project/api/views.py +++ b/project/api/views.py @@ -9,8 +9,8 @@ utils = Utils() -@pax_blueprint.route('/receipt', methods=['POST']) -def add_receipt(): +@pax_blueprint.route('/create_pax', methods=['POST']) +def add_pax(): post_data = request.get_json() if not post_data: diff --git a/requirements.txt b/requirements.txt index 284b9e5..3b75529 100644 --- a/requirements.txt +++ b/requirements.txt @@ -7,4 +7,5 @@ passlib Flask-Testing==0.6.2 coverage==4.5.1 gunicorn==19.8.1 -flask-migrate==2.2.0 \ No newline at end of file +flask-migrate==2.2.0 +requests \ No newline at end of file From ce360a532d08cb9ee0f1d8045e9a226cfc5a5798 Mon Sep 17 00:00:00 2001 From: Lucas Dutra Date: Wed, 30 Oct 2019 00:27:11 -0300 Subject: [PATCH 15/63] pax-app/Wiki#189 Create filter status query method --- project/api/utils/creation_utils.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/project/api/utils/creation_utils.py b/project/api/utils/creation_utils.py index 3de9a69..5addab1 100644 --- a/project/api/utils/creation_utils.py +++ b/project/api/utils/creation_utils.py @@ -1,5 +1,6 @@ from database_singleton import Singleton from flask import request, jsonify +from project.api.models import Pax db = Singleton().database_connection() @@ -23,3 +24,8 @@ def commit_to_database(self, model): db.session.add(model) db.session.flush() db.session.commit() + + def filter_by_status(self, value): + pax = Pax.query.filter_by( + status=value).all() + return pax From 01769c7cf3353ae2272683fb58b404d500e688bb Mon Sep 17 00:00:00 2001 From: Lucas Dutra Date: Wed, 30 Oct 2019 00:41:43 -0300 Subject: [PATCH 16/63] pax-app/Wiki#189 Update filtering method to different user kinds --- project/api/utils/creation_utils.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/project/api/utils/creation_utils.py b/project/api/utils/creation_utils.py index 5addab1..1fde3a1 100644 --- a/project/api/utils/creation_utils.py +++ b/project/api/utils/creation_utils.py @@ -25,7 +25,11 @@ def commit_to_database(self, model): db.session.flush() db.session.commit() - def filter_by_status(self, value): - pax = Pax.query.filter_by( - status=value).all() + def filter_by_status(self, value, user_type, id): + if user_type == 'provider': + pax = Pax.query.filter_by( + status=value, provider_id=int(id)).all() + elif user_type == 'user': + pax = Pax.query.filter_by( + status=value, user_id=int(id)).all() return pax From 9b24960fb3adc279df9c3014665c7e2dd3d5eda5 Mon Sep 17 00:00:00 2001 From: Lucas Dutra Date: Wed, 30 Oct 2019 00:42:59 -0300 Subject: [PATCH 17/63] pax-app/Wiki#189 Adapt filted by status to Strategy Pattern --- project/api/utils/status_strategy.py | 54 ++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 project/api/utils/status_strategy.py diff --git a/project/api/utils/status_strategy.py b/project/api/utils/status_strategy.py new file mode 100644 index 0000000..01e6670 --- /dev/null +++ b/project/api/utils/status_strategy.py @@ -0,0 +1,54 @@ +from __future__ import annotations +from abc import ABC, abstractmethod +from typing import List +from project.api.utils.creation_utils import Utils +from project.api.models import Pax + +utils = Utils() + + +class Context(): + def __init__(self, strategy: Strategy) -> None: + self._strategy = strategy + + @property + def strategy(self) -> Strategy: + return self._strategy + + @strategy.setter + def strategy(self, strategy: Strategy) -> None: + self._strategy = strategy + + def execute_filtering(self, id) -> list: + result = self._strategy.filter_status(id) + return result + + +class Strategy(ABC): + @abstractmethod + def filter_status(self, id): + pass + + +class InitiatedStrategy(Strategy): + def filter_status(self, user_type, id) -> list: + pax = utils.filter_by_status('I', self.user_type, self.id) + return pax + + +class CanceledStrategy(Strategy): + def filter_status(self, user_type, id) -> list: + pax = utils.filter_by_status('C', self.user_type, self.id) + return pax + + +class PendentStrategy(Strategy): + def filter_status(self, user_type, id) -> list: + pax = utils.filter_by_status('P', self.user_type, self.id) + return pax + + +class FinalizedStrategy(Strategy): + def filter_status(self, user_type, id) -> list: + pax = utils.filter_by_status('F', self.user_type, self.id) + return pax From dff96f1bd289403fac6966b7652bf5cc6bb8ab86 Mon Sep 17 00:00:00 2001 From: Lucas Dutra Date: Wed, 30 Oct 2019 01:02:31 -0300 Subject: [PATCH 18/63] pax-app/Wiki#189 Update attributes from parameters reference --- project/api/utils/status_strategy.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/project/api/utils/status_strategy.py b/project/api/utils/status_strategy.py index 01e6670..1181f57 100644 --- a/project/api/utils/status_strategy.py +++ b/project/api/utils/status_strategy.py @@ -19,36 +19,36 @@ def strategy(self) -> Strategy: def strategy(self, strategy: Strategy) -> None: self._strategy = strategy - def execute_filtering(self, id) -> list: - result = self._strategy.filter_status(id) + def execute_filtering(self, user_type, id) -> list: + result = self._strategy.filter_status(user_type, id) return result class Strategy(ABC): @abstractmethod - def filter_status(self, id): + def filter_status(self, user_type, id): pass class InitiatedStrategy(Strategy): def filter_status(self, user_type, id) -> list: - pax = utils.filter_by_status('I', self.user_type, self.id) + pax = utils.filter_by_status('I', user_type, id) return pax class CanceledStrategy(Strategy): def filter_status(self, user_type, id) -> list: - pax = utils.filter_by_status('C', self.user_type, self.id) + pax = utils.filter_by_status('C', user_type, id) return pax class PendentStrategy(Strategy): def filter_status(self, user_type, id) -> list: - pax = utils.filter_by_status('P', self.user_type, self.id) + pax = utils.filter_by_status('P', user_type, id) return pax class FinalizedStrategy(Strategy): def filter_status(self, user_type, id) -> list: - pax = utils.filter_by_status('F', self.user_type, self.id) + pax = utils.filter_by_status('F', user_type, id) return pax From 898b6d518f9ce9a67256a1f5326f8b110e489996 Mon Sep 17 00:00:00 2001 From: Lucas Dutra Date: Wed, 30 Oct 2019 01:03:03 -0300 Subject: [PATCH 19/63] pax-app/Wiki#189 Create getsuccess method --- project/api/utils/creation_utils.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/project/api/utils/creation_utils.py b/project/api/utils/creation_utils.py index 1fde3a1..317b4b1 100644 --- a/project/api/utils/creation_utils.py +++ b/project/api/utils/creation_utils.py @@ -20,6 +20,13 @@ def createSuccessMessage(self, message): } return response_object + def createSuccessGet(self, content): + response_object = { + 'status': 'success', + 'data': content + } + return response_object + def commit_to_database(self, model): db.session.add(model) db.session.flush() From cb72f1a9b47fd50e6a23899303a4df2fbf96167f Mon Sep 17 00:00:00 2001 From: Lucas Dutra Date: Wed, 30 Oct 2019 01:03:42 -0300 Subject: [PATCH 20/63] pax-app/Wiki#189 Create finalized pax GET method --- project/api/views.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/project/api/views.py b/project/api/views.py index 9734dc7..4b41a1c 100644 --- a/project/api/views.py +++ b/project/api/views.py @@ -3,6 +3,7 @@ from project.api.utils.creation_utils import Utils from project.api.models import Pax from sqlalchemy import exc +from project.api.utils.status_strategy import Context, InitiatedStrategy, FinalizedStrategy, CanceledStrategy, PendentStrategy pax_blueprint = Blueprint('pax', __name__) db = Singleton().database_connection() @@ -37,3 +38,16 @@ def add_pax(): except exc.IntegrityError: db.session.rollback() return jsonify(utils.createFailMessage('Wrong JSON')), 400 + + +@pax_blueprint.route('/finalized_pax//', methods=['GET']) +def get_finalized_pax(user_kind, id): + try: + context = Context(FinalizedStrategy()) + finalized_pax = context.execute_filtering(user_kind, int(id)) + if not finalized_pax: + return jsonify(utils.createFailMessage('Cannot find user')), 404 + except ValueError: + return jsonify(utils.createFailMessage('Cannot find user')), 404 + + return jsonify(utils.createSuccessGet(finalized_pax.to_json())), 200 From e7600a2b1b6bb0d18db5910f41e0eec7beb2baaa Mon Sep 17 00:00:00 2001 From: Lucas Dutra Date: Wed, 30 Oct 2019 01:05:36 -0300 Subject: [PATCH 21/63] pax-app/Wiki#189 Create initiated pax GET method --- project/api/views.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/project/api/views.py b/project/api/views.py index 4b41a1c..81d5f60 100644 --- a/project/api/views.py +++ b/project/api/views.py @@ -51,3 +51,16 @@ def get_finalized_pax(user_kind, id): return jsonify(utils.createFailMessage('Cannot find user')), 404 return jsonify(utils.createSuccessGet(finalized_pax.to_json())), 200 + + +@pax_blueprint.route('/initiated_pax//', methods=['GET']) +def get_initiated_pax(user_kind, id): + try: + context = Context(InitiatedStrategy()) + initiated_pax = context.execute_filtering(user_kind, int(id)) + if not initiated_pax: + return jsonify(utils.createFailMessage('Cannot find user')), 404 + except ValueError: + return jsonify(utils.createFailMessage('Cannot find user')), 404 + + return jsonify(utils.createSuccessGet(initiated_pax.to_json())), 200 From 6cf700ac76a6b4590b127a494c83809c35e04b50 Mon Sep 17 00:00:00 2001 From: Lucas Dutra Date: Wed, 30 Oct 2019 01:07:13 -0300 Subject: [PATCH 22/63] pax-app/Wiki#189 Create canceled pax GET method --- project/api/views.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/project/api/views.py b/project/api/views.py index 81d5f60..8008b51 100644 --- a/project/api/views.py +++ b/project/api/views.py @@ -64,3 +64,16 @@ def get_initiated_pax(user_kind, id): return jsonify(utils.createFailMessage('Cannot find user')), 404 return jsonify(utils.createSuccessGet(initiated_pax.to_json())), 200 + + +@pax_blueprint.route('/canceled_pax//', methods=['GET']) +def get_canceled_pax(user_kind, id): + try: + context = Context(CanceledStrategy()) + canceled_pax = context.execute_filtering(user_kind, int(id)) + if not canceled_pax: + return jsonify(utils.createFailMessage('Cannot find user')), 404 + except ValueError: + return jsonify(utils.createFailMessage('Cannot find user')), 404 + + return jsonify(utils.createSuccessGet(canceled_pax.to_json())), 200 From 1eb7b3a8b11103305c6668e52daec572bf22a3ae Mon Sep 17 00:00:00 2001 From: Lucas Dutra Date: Wed, 30 Oct 2019 01:08:03 -0300 Subject: [PATCH 23/63] pax-app/Wiki#189 Create pendent pax GET method --- project/api/views.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/project/api/views.py b/project/api/views.py index 8008b51..08efef0 100644 --- a/project/api/views.py +++ b/project/api/views.py @@ -77,3 +77,16 @@ def get_canceled_pax(user_kind, id): return jsonify(utils.createFailMessage('Cannot find user')), 404 return jsonify(utils.createSuccessGet(canceled_pax.to_json())), 200 + + +@pax_blueprint.route('/pendent_pax//', methods=['GET']) +def get_pendent_pax(user_kind, id): + try: + context = Context(PendentStrategy()) + pendent_pax = context.execute_filtering(user_kind, int(id)) + if not pendent_pax: + return jsonify(utils.createFailMessage('Cannot find user')), 404 + except ValueError: + return jsonify(utils.createFailMessage('Cannot find user')), 404 + + return jsonify(utils.createSuccessGet(pendent_pax.to_json())), 200 From abf5bbc4ab0ceb0d77113adbfd16e7c1d868bb99 Mon Sep 17 00:00:00 2001 From: Lucas Dutra Date: Wed, 30 Oct 2019 01:42:07 -0300 Subject: [PATCH 24/63] pax-app/Wiki#189 Update date field name --- project/api/models.py | 8 ++++---- project/api/views.py | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/project/api/models.py b/project/api/models.py index 9c10d48..cb4f0dc 100644 --- a/project/api/models.py +++ b/project/api/models.py @@ -7,7 +7,7 @@ class Pax(db.Model): __tablename__ = 'PAX' pax_id = db.Column(db.Integer, primary_key=True, autoincrement=True) - data = db.Column(db.Date, nullable=False) + date = db.Column(db.Date, nullable=False) description = db.Column(db.String(500), nullable=False) name = db.Column(db.String(255), nullable=False) price = db.Column(db.Float, nullable=False) @@ -17,8 +17,8 @@ class Pax(db.Model): chat_id = db.Column(db.Integer, nullable=False) address_id = db.Column(db.Integer, nullable=False) - def __init__(self, data, description, name, price, status, user_id, provider_id, chat_id, address_id): - self.data = data + def __init__(self, date, description, name, price, status, user_id, provider_id, chat_id, address_id): + self.date = date self.description = description self.name = name self.price = price @@ -31,7 +31,7 @@ def __init__(self, data, description, name, price, status, user_id, provider_id, def to_json(self): return { 'id': self.pax_id, - 'data': self.data, + 'date': self.date, 'description': self.description, 'name': self.name, 'price': self.price, diff --git a/project/api/views.py b/project/api/views.py index 08efef0..50f5ff0 100644 --- a/project/api/views.py +++ b/project/api/views.py @@ -19,7 +19,7 @@ def add_pax(): pax = post_data.get('pax') - data = pax.get('data') + date = pax.get('date') description = pax.get('description') name = pax.get('name') price = pax.get('price') @@ -30,7 +30,7 @@ def add_pax(): address_id = pax.get('address_id') try: - pax = Pax(data, description, name, + pax = Pax(date, description, name, price, status, user_id, provider_id, chat_id, address_id) utils.commit_to_database(pax) return jsonify(utils.createSuccessMessage('Pax was created!')), 201 From 1edd774b1f89db2c68a39ff317ade03e508a1cd7 Mon Sep 17 00:00:00 2001 From: Lucas Dutra Date: Wed, 30 Oct 2019 01:58:26 -0300 Subject: [PATCH 25/63] pax-app/Wiki#189 Update status field to static on Pax creation --- project/api/views.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/project/api/views.py b/project/api/views.py index 50f5ff0..4a71a5e 100644 --- a/project/api/views.py +++ b/project/api/views.py @@ -23,7 +23,6 @@ def add_pax(): description = pax.get('description') name = pax.get('name') price = pax.get('price') - status = pax.get('status') user_id = pax.get('user_id') provider_id = pax.get('provider_id') chat_id = pax.get('chat_id') @@ -31,7 +30,7 @@ def add_pax(): try: pax = Pax(date, description, name, - price, status, user_id, provider_id, chat_id, address_id) + price, 'P', user_id, provider_id, chat_id, address_id) utils.commit_to_database(pax) return jsonify(utils.createSuccessMessage('Pax was created!')), 201 @@ -50,7 +49,7 @@ def get_finalized_pax(user_kind, id): except ValueError: return jsonify(utils.createFailMessage('Cannot find user')), 404 - return jsonify(utils.createSuccessGet(finalized_pax.to_json())), 200 + return jsonify(utils.createSuccessGet(finalized_pax)), 200 @pax_blueprint.route('/initiated_pax//', methods=['GET']) @@ -63,7 +62,7 @@ def get_initiated_pax(user_kind, id): except ValueError: return jsonify(utils.createFailMessage('Cannot find user')), 404 - return jsonify(utils.createSuccessGet(initiated_pax.to_json())), 200 + return jsonify(utils.createSuccessGet(initiated_pax)), 200 @pax_blueprint.route('/canceled_pax//', methods=['GET']) @@ -76,7 +75,7 @@ def get_canceled_pax(user_kind, id): except ValueError: return jsonify(utils.createFailMessage('Cannot find user')), 404 - return jsonify(utils.createSuccessGet(canceled_pax.to_json())), 200 + return jsonify(utils.createSuccessGet(canceled_pax)), 200 @pax_blueprint.route('/pendent_pax//', methods=['GET']) @@ -89,4 +88,4 @@ def get_pendent_pax(user_kind, id): except ValueError: return jsonify(utils.createFailMessage('Cannot find user')), 404 - return jsonify(utils.createSuccessGet(pendent_pax.to_json())), 200 + return jsonify(utils.createSuccessGet(pendent_pax)), 200 From f26c9b50e95e1e5418b708495a0c20d31c5da199 Mon Sep 17 00:00:00 2001 From: Lucas Dutra Date: Wed, 30 Oct 2019 01:59:00 -0300 Subject: [PATCH 26/63] pax-app/Wiki#189 Implement for loop to change list of dicts into jsons --- project/api/utils/creation_utils.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/project/api/utils/creation_utils.py b/project/api/utils/creation_utils.py index 317b4b1..3fd1f5e 100644 --- a/project/api/utils/creation_utils.py +++ b/project/api/utils/creation_utils.py @@ -23,7 +23,9 @@ def createSuccessMessage(self, message): def createSuccessGet(self, content): response_object = { 'status': 'success', - 'data': content + 'data': { + 'pax': [pax.to_json() for pax in content] + } } return response_object From d5b1eafa46574554fe2c540b24b655235651eb1d Mon Sep 17 00:00:00 2001 From: Lucas Dutra Date: Thu, 31 Oct 2019 13:01:06 -0300 Subject: [PATCH 27/63] pax-app/Wiki#189 Remove .env.example unused file --- .env.example | 2 -- 1 file changed, 2 deletions(-) delete mode 100644 .env.example diff --git a/.env.example b/.env.example deleted file mode 100644 index fe7b0bf..0000000 --- a/.env.example +++ /dev/null @@ -1,2 +0,0 @@ -DATABASE_URI= -APP_SECRET_KEY= \ No newline at end of file From 456b62db434fb3fd78817d091987a9c42b1aab48 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rog=C3=A9rio=20J=C3=BAnior?= Date: Thu, 7 Nov 2019 00:01:04 -0200 Subject: [PATCH 28/63] pax-app/Wiki#190 Get pax data if it exists Co-authored-by: Youssef Muhamad --- project/api/views.py | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/project/api/views.py b/project/api/views.py index 4a71a5e..5218ae5 100644 --- a/project/api/views.py +++ b/project/api/views.py @@ -1,9 +1,9 @@ +from project.api.utils.status_strategy import Context, InitiatedStrategy, FinalizedStrategy, CanceledStrategy, PendentStrategy +from project.api.utils.creation_utils import Utils from flask import request, jsonify, Blueprint from database_singleton import Singleton -from project.api.utils.creation_utils import Utils from project.api.models import Pax from sqlalchemy import exc -from project.api.utils.status_strategy import Context, InitiatedStrategy, FinalizedStrategy, CanceledStrategy, PendentStrategy pax_blueprint = Blueprint('pax', __name__) db = Singleton().database_connection() @@ -39,6 +39,25 @@ def add_pax(): return jsonify(utils.createFailMessage('Wrong JSON')), 400 +@pax_blueprint.route('/consult_pax', methods=['GET']) +def consult_pax(): + chat_id = request.args.get('chat_id') + + pax = Pax.query.filter_by(chat_id=chat_id).all() + + if not pax: + return jsonify({'exists': 'false'}), 400 + + data = [row.to_json() for row in pax] + + response = { + 'exists': 'true', + 'pax': data[0] + } + + return jsonify(response), 400 + + @pax_blueprint.route('/finalized_pax//', methods=['GET']) def get_finalized_pax(user_kind, id): try: From 39c1a22f01edb8dfd7d53bf982b5f550f9f9754b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rog=C3=A9rio=20J=C3=BAnior?= Date: Thu, 7 Nov 2019 01:09:47 -0200 Subject: [PATCH 29/63] pax-app/Wiki#190 Change create pax endpoint to update pax in case it exists Co-authored-by: Youssef Muhamad --- project/api/utils/creation_utils.py | 8 +++++-- project/api/views.py | 33 ++++++++++++++++++++++------- 2 files changed, 31 insertions(+), 10 deletions(-) diff --git a/project/api/utils/creation_utils.py b/project/api/utils/creation_utils.py index 3fd1f5e..7385dc2 100644 --- a/project/api/utils/creation_utils.py +++ b/project/api/utils/creation_utils.py @@ -29,8 +29,12 @@ def createSuccessGet(self, content): } return response_object - def commit_to_database(self, model): - db.session.add(model) + def commit_to_database(self, type, model): + if (type == 'A'): + db.session.add(model) + elif (type == 'M'): + db.session.merge(model) + db.session.flush() db.session.commit() diff --git a/project/api/views.py b/project/api/views.py index 5218ae5..4ea9574 100644 --- a/project/api/views.py +++ b/project/api/views.py @@ -10,8 +10,8 @@ utils = Utils() -@pax_blueprint.route('/create_pax', methods=['POST']) -def add_pax(): +@pax_blueprint.route('/upCreate_pax', methods=['POST']) +def upCreate(): post_data = request.get_json() if not post_data: @@ -28,11 +28,28 @@ def add_pax(): chat_id = pax.get('chat_id') address_id = pax.get('address_id') + row = Pax.query.filter_by(chat_id=chat_id).first() + + if row is None: + try: + pax = Pax(date, description, name, + price, 'P', user_id, provider_id, chat_id, address_id) + utils.commit_to_database('A', pax) + return jsonify(utils.createSuccessMessage('Pax was created!')), 201 + + except exc.IntegrityError: + db.session.rollback() + return jsonify(utils.createFailMessage('Wrong JSON')), 400 + try: - pax = Pax(date, description, name, - price, 'P', user_id, provider_id, chat_id, address_id) - utils.commit_to_database(pax) - return jsonify(utils.createSuccessMessage('Pax was created!')), 201 + row.date = date + row.description = description + row.name = name + row.price = price + row.address_id = address_id + + utils.commit_to_database('M', row) + return jsonify(utils.createSuccessMessage('Pax was updated!')), 201 except exc.IntegrityError: db.session.rollback() @@ -46,7 +63,7 @@ def consult_pax(): pax = Pax.query.filter_by(chat_id=chat_id).all() if not pax: - return jsonify({'exists': 'false'}), 400 + return jsonify({'exists': 'false'}), 201 data = [row.to_json() for row in pax] @@ -55,7 +72,7 @@ def consult_pax(): 'pax': data[0] } - return jsonify(response), 400 + return jsonify(response), 201 @pax_blueprint.route('/finalized_pax//', methods=['GET']) From df53610bee1b6c219db89f20d974b0adf33b59cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rog=C3=A9rio=20J=C3=BAnior?= Date: Thu, 7 Nov 2019 01:47:54 -0200 Subject: [PATCH 30/63] pax-app/Wiki#190 Add PR template Co-authored-by: Youssef Muhamad --- .github/pull_request_template.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 .github/pull_request_template.md diff --git a/.github/pull_request_template.md b/.github/pull_request_template.md new file mode 100644 index 0000000..78c571f --- /dev/null +++ b/.github/pull_request_template.md @@ -0,0 +1,18 @@ +# Descrição +Breve descrição das atividades realizadas para conclusão da issue e outros pontos relevantes. + +# Issue Relacionada + + +resolve pax-app/Wiki# + +# Tipo de Mudanças + +- [ ] História de Usuário +- [ ] Historia Técnica +- [ ] Wiki + +# Responsáveis pela revisão + +@esiofreitas (PO) | @lucasdutraf (PO) +@(Membro do time de desenvolvimento que não esteja envolvido do PR) From 600b9e2740c001d6e405578d826473c9af31ccee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rog=C3=A9rio=20J=C3=BAnior?= Date: Thu, 7 Nov 2019 02:59:46 -0200 Subject: [PATCH 31/63] pax-app/Wiki#190 Change expected body shape on upCreate Pax Co-authored-by: Youssef Muhamad --- project/api/views.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/project/api/views.py b/project/api/views.py index 4ea9574..56ff077 100644 --- a/project/api/views.py +++ b/project/api/views.py @@ -12,13 +12,11 @@ @pax_blueprint.route('/upCreate_pax', methods=['POST']) def upCreate(): - post_data = request.get_json() + pax = request.get_json() - if not post_data: + if not pax: return jsonify(utils.createFailMessage('Wrong JSON')), 400 - pax = post_data.get('pax') - date = pax.get('date') description = pax.get('description') name = pax.get('name') From bf47a666022a861a99a04f9733a508b81dd63c5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rog=C3=A9rio=20J=C3=BAnior?= Date: Fri, 8 Nov 2019 00:29:11 -0200 Subject: [PATCH 32/63] pax-app/Wiki#190 Correct status parameter attribution Co-authored-by: Youssef Muhamad --- project/api/models.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/project/api/models.py b/project/api/models.py index cb4f0dc..43c1006 100644 --- a/project/api/models.py +++ b/project/api/models.py @@ -22,7 +22,7 @@ def __init__(self, date, description, name, price, status, user_id, provider_id, self.description = description self.name = name self.price = price - self.status = name + self.status = status self.user_id = user_id self.provider_id = provider_id self.chat_id = chat_id From 3ae4084564b4a4cb86e9b04816886b95825a1f59 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rog=C3=A9rio=20J=C3=BAnior?= Date: Fri, 8 Nov 2019 00:35:47 -0200 Subject: [PATCH 33/63] pax-app/Wiki#190 Set default status to NULL Co-authored-by: Youssef Muhamad --- project/api/views.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/project/api/views.py b/project/api/views.py index 56ff077..b5edfe1 100644 --- a/project/api/views.py +++ b/project/api/views.py @@ -31,7 +31,7 @@ def upCreate(): if row is None: try: pax = Pax(date, description, name, - price, 'P', user_id, provider_id, chat_id, address_id) + price, '', user_id, provider_id, chat_id, address_id) utils.commit_to_database('A', pax) return jsonify(utils.createSuccessMessage('Pax was created!')), 201 From b6bbed81e1d6a1a5c59df1aa831e81d7cb88a16e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rog=C3=A9rio=20J=C3=BAnior?= Date: Fri, 8 Nov 2019 10:49:56 -0200 Subject: [PATCH 34/63] pax-app/Wiki#190 Add chain of responsibility interface and abstract class Co-authored-by: Youssef Muhamad --- .../chain_of_responsibility/definitions.py | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 project/api/utils/chain_of_responsibility/definitions.py diff --git a/project/api/utils/chain_of_responsibility/definitions.py b/project/api/utils/chain_of_responsibility/definitions.py new file mode 100644 index 0000000..62af6ab --- /dev/null +++ b/project/api/utils/chain_of_responsibility/definitions.py @@ -0,0 +1,27 @@ +from __future__ import annotations +from abc import ABC, abstractmethod + + +class Handler(ABC): + + @abstractmethod + def set_next(self, handler: Handler) -> Handler: + pass + + def handle(self, request, row): + pass + + +class AbstractHandler(Handler): + + _next_handler: Handler = None + + def set_next(self, handler: Handler) -> Handler: + self._next_handler = handler + return handler + + def handle(self, request, row): + if self._next_handler: + return self._next_handler.handle(request, row) + + return None From d5d191f05440051e95d4373ad149ba96a5f1bfe5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rog=C3=A9rio=20J=C3=BAnior?= Date: Fri, 8 Nov 2019 10:58:51 -0200 Subject: [PATCH 35/63] pax-app/Wiki#190 Add Create concrete handler Co-authored-by: Youssef Muhamad --- .../utils/chain_of_responsibility/handlers.py | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 project/api/utils/chain_of_responsibility/handlers.py diff --git a/project/api/utils/chain_of_responsibility/handlers.py b/project/api/utils/chain_of_responsibility/handlers.py new file mode 100644 index 0000000..33bdaef --- /dev/null +++ b/project/api/utils/chain_of_responsibility/handlers.py @@ -0,0 +1,31 @@ +from project.api.utils.chain_of_responsibility.definitions import AbstractHandler +from project.api.utils.creation_utils import Utils +from database_singleton import Singleton +from project.api.models import Pax +from flask import jsonify + +db = Singleton().database_connection() +utils = Utils() + + +class CreateHandler(AbstractHandler): + def handle(self, request, row): + if not row: + pax = request.get_json() + + date = pax.get('date') + description = pax.get('description') + name = pax.get('name') + price = pax.get('price') + user_id = pax.get('user_id') + provider_id = pax.get('provider_id') + chat_id = pax.get('chat_id') + address_id = pax.get('address_id') + + pax = Pax(date, description, name, + price, '', user_id, provider_id, chat_id, address_id) + utils.commit_to_database('A', pax) + return jsonify(utils.createSuccessMessage('Pax was created!')), 201 + + else: + return super().handle(request, row) From 2ecfcaac7a1084a8471710251d5117a85b175c84 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rog=C3=A9rio=20J=C3=BAnior?= Date: Fri, 8 Nov 2019 10:59:20 -0200 Subject: [PATCH 36/63] pax-app/Wiki#190 Add Update concrete handler Co-authored-by: Youssef Muhamad --- .../utils/chain_of_responsibility/handlers.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/project/api/utils/chain_of_responsibility/handlers.py b/project/api/utils/chain_of_responsibility/handlers.py index 33bdaef..1be42f2 100644 --- a/project/api/utils/chain_of_responsibility/handlers.py +++ b/project/api/utils/chain_of_responsibility/handlers.py @@ -29,3 +29,21 @@ def handle(self, request, row): else: return super().handle(request, row) + + +class UpdateHandler(AbstractHandler): + def handle(self, request, row): + try: + pax = request.get_json() + + row.date = pax.get('date') + row.description = pax.get('description') + row.name = pax.get('name') + row.price = pax.get('price') + row.address_id = pax.get('address_id') + + utils.commit_to_database('M', row) + return jsonify(utils.createSuccessMessage('Pax was updated!')), 201 + + except: + return super().handle(request, row) From cde1c3d9a509bce3071a4e3afd1a59e32d2fd67f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rog=C3=A9rio=20J=C3=BAnior?= Date: Fri, 8 Nov 2019 11:00:25 -0200 Subject: [PATCH 37/63] pax-app/Wiki#190 Add Error concrete handler Co-authored-by: Youssef Muhamad --- project/api/utils/chain_of_responsibility/handlers.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/project/api/utils/chain_of_responsibility/handlers.py b/project/api/utils/chain_of_responsibility/handlers.py index 1be42f2..b6fbda7 100644 --- a/project/api/utils/chain_of_responsibility/handlers.py +++ b/project/api/utils/chain_of_responsibility/handlers.py @@ -47,3 +47,9 @@ def handle(self, request, row): except: return super().handle(request, row) + + +class ErrorHandler(AbstractHandler): + def handle(self, request, row): + db.session.rollback() + return jsonify(utils.createFailMessage('Wrong JSON')), 400 From b755e64e6ebdef1acd693a1a0b24bc5a144d9d97 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rog=C3=A9rio=20J=C3=BAnior?= Date: Fri, 8 Nov 2019 11:01:31 -0200 Subject: [PATCH 38/63] pax-app/Wiki#190 Add UpCreate class for better usage Co-authored-by: Youssef Muhamad --- project/api/utils/chain_of_responsibility/chain.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 project/api/utils/chain_of_responsibility/chain.py diff --git a/project/api/utils/chain_of_responsibility/chain.py b/project/api/utils/chain_of_responsibility/chain.py new file mode 100644 index 0000000..30fa1d4 --- /dev/null +++ b/project/api/utils/chain_of_responsibility/chain.py @@ -0,0 +1,13 @@ +from project.api.utils.chain_of_responsibility.handlers import CreateHandler, UpdateHandler, ErrorHandler + + +class UpCreate(): + _create = CreateHandler() + _update = UpdateHandler() + _error = ErrorHandler() + + def __init__(self): + self._create.set_next(self._update).set_next(self._error) + + def execute(self, request, row): + return self._create.handle(request, row) From 140ede9c2e9f7790d89c5e5823d17f42347ce76f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rog=C3=A9rio=20J=C3=BAnior?= Date: Fri, 8 Nov 2019 11:03:39 -0200 Subject: [PATCH 39/63] pax-app/Wiki#190 Add chain of responsibility pattern in route upCreate Co-authored-by: Youssef Muhamad --- project/api/views.py | 40 ++++------------------------------------ 1 file changed, 4 insertions(+), 36 deletions(-) diff --git a/project/api/views.py b/project/api/views.py index b5edfe1..2b1e662 100644 --- a/project/api/views.py +++ b/project/api/views.py @@ -1,4 +1,5 @@ from project.api.utils.status_strategy import Context, InitiatedStrategy, FinalizedStrategy, CanceledStrategy, PendentStrategy +from project.api.utils.chain_of_responsibility.chain import UpCreate from project.api.utils.creation_utils import Utils from flask import request, jsonify, Blueprint from database_singleton import Singleton @@ -10,48 +11,15 @@ utils = Utils() -@pax_blueprint.route('/upCreate_pax', methods=['POST']) +@pax_blueprint.route('/upCreate', methods=['POST']) def upCreate(): pax = request.get_json() - if not pax: - return jsonify(utils.createFailMessage('Wrong JSON')), 400 - - date = pax.get('date') - description = pax.get('description') - name = pax.get('name') - price = pax.get('price') - user_id = pax.get('user_id') - provider_id = pax.get('provider_id') chat_id = pax.get('chat_id') - address_id = pax.get('address_id') - row = Pax.query.filter_by(chat_id=chat_id).first() - if row is None: - try: - pax = Pax(date, description, name, - price, '', user_id, provider_id, chat_id, address_id) - utils.commit_to_database('A', pax) - return jsonify(utils.createSuccessMessage('Pax was created!')), 201 - - except exc.IntegrityError: - db.session.rollback() - return jsonify(utils.createFailMessage('Wrong JSON')), 400 - - try: - row.date = date - row.description = description - row.name = name - row.price = price - row.address_id = address_id - - utils.commit_to_database('M', row) - return jsonify(utils.createSuccessMessage('Pax was updated!')), 201 - - except exc.IntegrityError: - db.session.rollback() - return jsonify(utils.createFailMessage('Wrong JSON')), 400 + chain = UpCreate() + return chain.execute(request, row) @pax_blueprint.route('/consult_pax', methods=['GET']) From 11efc61d4b595d02d71dfd3f798cee6199280347 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rog=C3=A9rio=20J=C3=BAnior?= Date: Fri, 8 Nov 2019 11:27:59 -0200 Subject: [PATCH 40/63] pax-app/Wiki#190 Change id_chat query to match python standarts Co-authored-by: Youssef Muhamad --- project/api/views.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/project/api/views.py b/project/api/views.py index 2b1e662..45bd5da 100644 --- a/project/api/views.py +++ b/project/api/views.py @@ -22,10 +22,8 @@ def upCreate(): return chain.execute(request, row) -@pax_blueprint.route('/consult_pax', methods=['GET']) -def consult_pax(): - chat_id = request.args.get('chat_id') - +@pax_blueprint.route('/consult_pax/', methods=['GET']) +def consult_pax(chat_id): pax = Pax.query.filter_by(chat_id=chat_id).all() if not pax: From e1e270bfff0e7f67c2c472867bda1a61ddbaedd6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rog=C3=A9rio=20J=C3=BAnior?= Date: Fri, 8 Nov 2019 11:56:27 -0200 Subject: [PATCH 41/63] pax-app/Wiki#190 Add update state concrete handler Co-authored-by: Youssef Muhamad Co-authored-by: Lucas Dutra --- .../api/utils/chain_of_responsibility/handlers.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/project/api/utils/chain_of_responsibility/handlers.py b/project/api/utils/chain_of_responsibility/handlers.py index b6fbda7..ff5fb0b 100644 --- a/project/api/utils/chain_of_responsibility/handlers.py +++ b/project/api/utils/chain_of_responsibility/handlers.py @@ -49,6 +49,21 @@ def handle(self, request, row): return super().handle(request, row) +class UpdateStateHandler(AbstractHandler): + def handle(self, request, row): + try: + body = request.get_json() + + status = body.get('status') + + row.status = status + + utils.commit_to_database('M', row) + return jsonify(utils.createSuccessMessage('Pax state was updated!')), 201 + except: + return super().handle(request, row) + + class ErrorHandler(AbstractHandler): def handle(self, request, row): db.session.rollback() From 616294f93acc1f19acaafbc44c20c432258012d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rog=C3=A9rio=20J=C3=BAnior?= Date: Fri, 8 Nov 2019 11:57:15 -0200 Subject: [PATCH 42/63] pax-app/Wiki#190 Add update state class for better usage Co-authored-by: Youssef Muhamad Co-authored-by: Lucas Dutra --- project/api/utils/chain_of_responsibility/chain.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/project/api/utils/chain_of_responsibility/chain.py b/project/api/utils/chain_of_responsibility/chain.py index 30fa1d4..3f1efb9 100644 --- a/project/api/utils/chain_of_responsibility/chain.py +++ b/project/api/utils/chain_of_responsibility/chain.py @@ -1,4 +1,4 @@ -from project.api.utils.chain_of_responsibility.handlers import CreateHandler, UpdateHandler, ErrorHandler +from project.api.utils.chain_of_responsibility.handlers import CreateHandler, UpdateHandler, UpdateStateHandler, ErrorHandler class UpCreate(): @@ -11,3 +11,14 @@ def __init__(self): def execute(self, request, row): return self._create.handle(request, row) + + +class UpdateState(): + _update = UpdateStateHandler() + _error = ErrorHandler() + + def __init__(self): + self._update.set_next(self._error) + + def execute(self, request, row): + return self._update.handle(request, row) From d6a64419c8accee75142d6171f0f4469100d5635 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rog=C3=A9rio=20J=C3=BAnior?= Date: Fri, 8 Nov 2019 11:58:10 -0200 Subject: [PATCH 43/63] pax-app/Wiki#190 Add update state endpoint with chain of responsibility pattern Co-authored-by: Youssef Muhamad Co-authored-by: Lucas Dutra --- project/api/views.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/project/api/views.py b/project/api/views.py index 45bd5da..952dd13 100644 --- a/project/api/views.py +++ b/project/api/views.py @@ -1,5 +1,5 @@ from project.api.utils.status_strategy import Context, InitiatedStrategy, FinalizedStrategy, CanceledStrategy, PendentStrategy -from project.api.utils.chain_of_responsibility.chain import UpCreate +from project.api.utils.chain_of_responsibility.chain import UpCreate, UpdateState from project.api.utils.creation_utils import Utils from flask import request, jsonify, Blueprint from database_singleton import Singleton @@ -13,15 +13,26 @@ @pax_blueprint.route('/upCreate', methods=['POST']) def upCreate(): - pax = request.get_json() + body = request.get_json() - chat_id = pax.get('chat_id') + chat_id = body.get('chat_id') row = Pax.query.filter_by(chat_id=chat_id).first() chain = UpCreate() return chain.execute(request, row) +@pax_blueprint.route('/update_status', methods=['POST']) +def update_state(): + body = request.get_json() + + chat_id = body.get('chat_id') + row = Pax.query.filter_by(chat_id=chat_id).first() + + chain = UpdateState() + return chain.execute(request, row) + + @pax_blueprint.route('/consult_pax/', methods=['GET']) def consult_pax(chat_id): pax = Pax.query.filter_by(chat_id=chat_id).all() From 42f091193c6c298652a8eaf060e71e068a68a83c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rog=C3=A9rio=20J=C3=BAnior?= Date: Fri, 8 Nov 2019 14:30:36 -0200 Subject: [PATCH 44/63] pax-app/Wiki#190 Change udpate status method mode to PATCH Co-authored-by: Youssef Muhamad Co-authored-by: Lucas Dutra --- project/api/views.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/project/api/views.py b/project/api/views.py index 952dd13..b858550 100644 --- a/project/api/views.py +++ b/project/api/views.py @@ -22,7 +22,7 @@ def upCreate(): return chain.execute(request, row) -@pax_blueprint.route('/update_status', methods=['POST']) +@pax_blueprint.route('/update_status', methods=['PATCH']) def update_state(): body = request.get_json() From 726a8f69e130e1e06b54f8f78cfe8905ef64be76 Mon Sep 17 00:00:00 2001 From: Marcos Nery Date: Sat, 9 Nov 2019 03:44:11 -0300 Subject: [PATCH 45/63] pax-app/Wiki#188 Adding travis file for continuous deploy --- .travis.yml | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 .travis.yml diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..b55d5d0 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,23 @@ +sudo: required +language: python +python: 3.8 +services: + - docker + +before_install: + # install heroku CLI + - wget -qO- https://toolbelt.heroku.com/install.sh | sh + # login to docker registries on heroku + - heroku container:login + +script: + # building docker image + - heroku container:push web + +deploy: + provider: script + script: + # releasing image + heroku container:release web --app $HEROKU_APPNAME + on: + branch: devel \ No newline at end of file From c0f6bd010761ec9b9c1407332c9031d4a1dfc942 Mon Sep 17 00:00:00 2001 From: Marcos Nery Date: Sat, 9 Nov 2019 03:49:18 -0300 Subject: [PATCH 46/63] pax-app/Wiki#188 Updating Dockerfile for production deploy --- Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index 58657b0..7ff9252 100644 --- a/Dockerfile +++ b/Dockerfile @@ -21,4 +21,4 @@ COPY . /app # run server -CMD python manage.py run -h 0.0.0.0 \ No newline at end of file +CMD ["gunicorn","project:create_app()"] From d922df28373080481f6aa88204aa110c93751ccb Mon Sep 17 00:00:00 2001 From: Lucas Dutra Date: Sat, 9 Nov 2019 19:06:38 -0200 Subject: [PATCH 47/63] pax-app/Wiki#194 Update status nullable behaviour --- project/api/models.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/project/api/models.py b/project/api/models.py index 43c1006..a8abfe5 100644 --- a/project/api/models.py +++ b/project/api/models.py @@ -11,7 +11,7 @@ class Pax(db.Model): description = db.Column(db.String(500), nullable=False) name = db.Column(db.String(255), nullable=False) price = db.Column(db.Float, nullable=False) - status = db.Column(db.Enum('F', 'P', 'C', 'I'), nullable=False) + status = db.Column(db.Enum('F', 'P', 'C', 'I'), nullable=True) user_id = db.Column(db.Integer, nullable=False) provider_id = db.Column(db.Integer, nullable=False) chat_id = db.Column(db.Integer, nullable=False) From 320c0cfe9aa1d4f4df2b9df87bd606b49cd0b48b Mon Sep 17 00:00:00 2001 From: Marcos Nery Date: Sun, 10 Nov 2019 01:21:46 -0300 Subject: [PATCH 48/63] pax-app/Wiki#188 Updating travis file to select app based on travis environemnt variable --- .travis.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index b55d5d0..359f6ba 100644 --- a/.travis.yml +++ b/.travis.yml @@ -12,7 +12,7 @@ before_install: script: # building docker image - - heroku container:push web + - heroku container:push web --app $HEROKU_APPNAME deploy: provider: script @@ -20,4 +20,4 @@ deploy: # releasing image heroku container:release web --app $HEROKU_APPNAME on: - branch: devel \ No newline at end of file + branch: devel From b94702c85748a7fba9d29ece324f71a6b842a884 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rog=C3=A9rio=20J=C3=BAnior?= Date: Mon, 11 Nov 2019 11:36:04 -0200 Subject: [PATCH 49/63] pax-app/Wiki#190 Change route of update status to get params through url Co-authored-by: Youssef Muhamad --- project/api/views.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/project/api/views.py b/project/api/views.py index b858550..04f38e9 100644 --- a/project/api/views.py +++ b/project/api/views.py @@ -22,11 +22,8 @@ def upCreate(): return chain.execute(request, row) -@pax_blueprint.route('/update_status', methods=['PATCH']) -def update_state(): - body = request.get_json() - - chat_id = body.get('chat_id') +@pax_blueprint.route('/update_status/', methods=['PATCH']) +def update_state(chat_id): row = Pax.query.filter_by(chat_id=chat_id).first() chain = UpdateState() From 61a81d34b10ca6e0f75cb2fd44167458cdbd2b32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rog=C3=A9rio=20J=C3=BAnior?= Date: Mon, 11 Nov 2019 11:42:09 -0200 Subject: [PATCH 50/63] pax-app/Wiki#190 Change route of update status body Co-authored-by: Youssef Muhamad --- project/api/views.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/project/api/views.py b/project/api/views.py index 04f38e9..b858550 100644 --- a/project/api/views.py +++ b/project/api/views.py @@ -22,8 +22,11 @@ def upCreate(): return chain.execute(request, row) -@pax_blueprint.route('/update_status/', methods=['PATCH']) -def update_state(chat_id): +@pax_blueprint.route('/update_status', methods=['PATCH']) +def update_state(): + body = request.get_json() + + chat_id = body.get('chat_id') row = Pax.query.filter_by(chat_id=chat_id).first() chain = UpdateState() From 11c534244b0e84dcbe4fb0620a0912762359344b Mon Sep 17 00:00:00 2001 From: marcosnbj Date: Thu, 14 Nov 2019 02:31:19 -0300 Subject: [PATCH 51/63] pax-app/Wiki#211 Adding decorator to prevent page cacheing --- project/api/views.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/project/api/views.py b/project/api/views.py index b858550..04c4674 100644 --- a/project/api/views.py +++ b/project/api/views.py @@ -10,6 +10,16 @@ db = Singleton().database_connection() utils = Utils() +@pax_blueprint.after_request +def add_header(r): + """ + Adding headers to prevent page caching + """ + r.headers["Cache-Control"] = "no-cache, no-store, must-revalidate, public, max-age=0" + r.headers["Pragma"] = "no-cache" + r.headers["Expires"] = "0" + return r + @pax_blueprint.route('/upCreate', methods=['POST']) def upCreate(): From 08ca89d76b44bdad39978808ca80ed9cf8d5c186 Mon Sep 17 00:00:00 2001 From: Marcos Nery Date: Thu, 14 Nov 2019 02:45:03 -0300 Subject: [PATCH 52/63] =?UTF-8?q?Modificando=20compose=20file=20para=20n?= =?UTF-8?q?=C3=A3o=20utilizar=20gunicorn=20localmente?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docker-compose.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/docker-compose.yml b/docker-compose.yml index f6bfa08..6327e22 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -13,3 +13,4 @@ services: - APP_SETTINGS=project.config.DevelopmentConfig - DATABASE_URL=mysql+pymysql://paxadmin:LpvuM1QnfcxOOVeNQQvl@pax-database.cd0j8avv0tgq.sa-east-1.rds.amazonaws.com:3306/paxdb - DATABASE_TEST_URL=mysql+pymysql://username:password@localhost:3306/pax + command: python manage.py run -h 0.0.0.0 From 1c0670da371971a90f2f193d7fff6776c54b0d43 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rog=C3=A9rio=20J=C3=BAnior?= Date: Thu, 14 Nov 2019 10:57:20 -0200 Subject: [PATCH 53/63] Change expires header parameter to check if no cache works --- project/api/views.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/project/api/views.py b/project/api/views.py index 04c4674..8bd3e21 100644 --- a/project/api/views.py +++ b/project/api/views.py @@ -10,6 +10,7 @@ db = Singleton().database_connection() utils = Utils() + @pax_blueprint.after_request def add_header(r): """ @@ -17,7 +18,7 @@ def add_header(r): """ r.headers["Cache-Control"] = "no-cache, no-store, must-revalidate, public, max-age=0" r.headers["Pragma"] = "no-cache" - r.headers["Expires"] = "0" + r.headers["Expires"] = "-1" return r From 45841b2ad42c63b383e89bced1f9f3667ef85800 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rog=C3=A9rio=20J=C3=BAnior?= Date: Thu, 14 Nov 2019 11:02:40 -0200 Subject: [PATCH 54/63] Add a few more paremeters in cache-control to try to remove cache --- project/api/views.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/project/api/views.py b/project/api/views.py index 8bd3e21..515c34f 100644 --- a/project/api/views.py +++ b/project/api/views.py @@ -16,7 +16,7 @@ def add_header(r): """ Adding headers to prevent page caching """ - r.headers["Cache-Control"] = "no-cache, no-store, must-revalidate, public, max-age=0" + r.headers["Cache-Control"] = "no-cache, no-store, must-revalidate, public, max-age=0, post-check=0, pre-check=0" r.headers["Pragma"] = "no-cache" r.headers["Expires"] = "-1" return r From ec549037e7271759560c1188a2bd2cff81356724 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rog=C3=A9rio=20J=C3=BAnior?= Date: Sat, 16 Nov 2019 00:52:31 -0200 Subject: [PATCH 55/63] pax-app/Wiki#200 Change update status functions names Co-authored-by: Youssef Muhamad --- project/api/utils/chain_of_responsibility/chain.py | 6 +++--- project/api/utils/chain_of_responsibility/handlers.py | 2 +- project/api/views.py | 6 +++--- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/project/api/utils/chain_of_responsibility/chain.py b/project/api/utils/chain_of_responsibility/chain.py index 3f1efb9..5b507af 100644 --- a/project/api/utils/chain_of_responsibility/chain.py +++ b/project/api/utils/chain_of_responsibility/chain.py @@ -1,4 +1,4 @@ -from project.api.utils.chain_of_responsibility.handlers import CreateHandler, UpdateHandler, UpdateStateHandler, ErrorHandler +from project.api.utils.chain_of_responsibility.handlers import CreateHandler, UpdateHandler, UpdateStatusHandler, ErrorHandler class UpCreate(): @@ -13,8 +13,8 @@ def execute(self, request, row): return self._create.handle(request, row) -class UpdateState(): - _update = UpdateStateHandler() +class UpdateStatus(): + _update = UpdateStatusHandler() _error = ErrorHandler() def __init__(self): diff --git a/project/api/utils/chain_of_responsibility/handlers.py b/project/api/utils/chain_of_responsibility/handlers.py index ff5fb0b..bc43298 100644 --- a/project/api/utils/chain_of_responsibility/handlers.py +++ b/project/api/utils/chain_of_responsibility/handlers.py @@ -49,7 +49,7 @@ def handle(self, request, row): return super().handle(request, row) -class UpdateStateHandler(AbstractHandler): +class UpdateStatusHandler(AbstractHandler): def handle(self, request, row): try: body = request.get_json() diff --git a/project/api/views.py b/project/api/views.py index 515c34f..1e65c9e 100644 --- a/project/api/views.py +++ b/project/api/views.py @@ -1,5 +1,5 @@ from project.api.utils.status_strategy import Context, InitiatedStrategy, FinalizedStrategy, CanceledStrategy, PendentStrategy -from project.api.utils.chain_of_responsibility.chain import UpCreate, UpdateState +from project.api.utils.chain_of_responsibility.chain import UpCreate, UpdateStatus from project.api.utils.creation_utils import Utils from flask import request, jsonify, Blueprint from database_singleton import Singleton @@ -34,13 +34,13 @@ def upCreate(): @pax_blueprint.route('/update_status', methods=['PATCH']) -def update_state(): +def update_status(): body = request.get_json() chat_id = body.get('chat_id') row = Pax.query.filter_by(chat_id=chat_id).first() - chain = UpdateState() + chain = UpdateStatus() return chain.execute(request, row) From 7bf59203f6583d92a8b28a30a28650e1d494940e Mon Sep 17 00:00:00 2001 From: Lucas Dutra Date: Sat, 16 Nov 2019 00:54:32 -0200 Subject: [PATCH 56/63] pax-app/Wiki#200 Crete support functions for ordering reverse alphabetical order a list of dicts --- project/api/utils/creation_utils.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/project/api/utils/creation_utils.py b/project/api/utils/creation_utils.py index 7385dc2..c005e55 100644 --- a/project/api/utils/creation_utils.py +++ b/project/api/utils/creation_utils.py @@ -1,6 +1,7 @@ from database_singleton import Singleton from flask import request, jsonify from project.api.models import Pax +from operator import itemgetter db = Singleton().database_connection() @@ -46,3 +47,22 @@ def filter_by_status(self, value, user_type, id): pax = Pax.query.filter_by( status=value, user_id=int(id)).all() return pax + + def sqlalchemyobj_to_list(self, data): + final_list = [] + for item in data: + final_list.append(item.to_json()) + return final_list + + def ignore_empty_status(self, data) -> list: + query = self.sqlalchemyobj_to_list(data) + filtered_pax = [] + for pax in query: + if pax['status'] != '': + filtered_pax.append(pax) + return filtered_pax + + + def reverse_alphabetical_order(self, data: list) -> list: + reverse_alphabetical_pax = sorted(data, key=itemgetter('status'), reverse=True) + return reverse_alphabetical_pax From 6fb8be6234be34bf07033802bc66b544f65060fb Mon Sep 17 00:00:00 2001 From: Lucas Dutra Date: Sat, 16 Nov 2019 00:55:55 -0200 Subject: [PATCH 57/63] pax-app/Wiki#200 Create route for returning ordered user's pax --- project/api/views.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/project/api/views.py b/project/api/views.py index 515c34f..51a28e1 100644 --- a/project/api/views.py +++ b/project/api/views.py @@ -111,3 +111,15 @@ def get_pendent_pax(user_kind, id): return jsonify(utils.createFailMessage('Cannot find user')), 404 return jsonify(utils.createSuccessGet(pendent_pax)), 200 + +@pax_blueprint.route('/all_pax/', methods=['GET']) +def get_all_pax(user_id): + try: + pax = Pax.query.filter_by(user_id=int(user_id)).all() + pax = utils.ignore_empty_status(pax) + pax = utils.reverse_alphabetical_order(pax) + if not pax: + return jsonify(utils.createFailMessage('Cannot find user')), 404 + except ValueError: + return jsonify(utils.createFailMessage('Cannot find user')), 404 + return jsonify(pax), 200 \ No newline at end of file From 43f8751b96b130e369181b38c7a047e67333a152 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rog=C3=A9rio=20J=C3=BAnior?= Date: Sat, 16 Nov 2019 00:56:00 -0200 Subject: [PATCH 58/63] pax-app/Wiki#200 Add new field canceled_motive to model Co-authored-by: Youssef Muhamad --- project/api/models.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/project/api/models.py b/project/api/models.py index a8abfe5..e1f8b52 100644 --- a/project/api/models.py +++ b/project/api/models.py @@ -16,8 +16,9 @@ class Pax(db.Model): provider_id = db.Column(db.Integer, nullable=False) chat_id = db.Column(db.Integer, nullable=False) address_id = db.Column(db.Integer, nullable=False) + canceled_motive = db.Column(db.String(500), nullable=False) - def __init__(self, date, description, name, price, status, user_id, provider_id, chat_id, address_id): + def __init__(self, date, description, name, price, status, user_id, provider_id, chat_id, address_id, canceled_motive): self.date = date self.description = description self.name = name @@ -27,6 +28,7 @@ def __init__(self, date, description, name, price, status, user_id, provider_id, self.provider_id = provider_id self.chat_id = chat_id self.address_id = address_id + self.canceled_motive = canceled_motive def to_json(self): return { @@ -39,5 +41,6 @@ def to_json(self): 'user_id': self.user_id, 'provider_id': self.provider_id, 'chat_id': self.chat_id, - 'address_id': self.address_id + 'address_id': self.address_id, + 'canceled_motive': self.canceled_motive, } From efb37cf0416d3092df3ac669b85dc742f3d2b4d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rog=C3=A9rio=20J=C3=BAnior?= Date: Sat, 16 Nov 2019 02:40:53 -0200 Subject: [PATCH 59/63] pax-app/Wiki#200 Change the damn date format to ISO pattern Co-authored-by: Youssef Muhamad --- project/api/models.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/project/api/models.py b/project/api/models.py index e1f8b52..f9d5963 100644 --- a/project/api/models.py +++ b/project/api/models.py @@ -33,7 +33,7 @@ def __init__(self, date, description, name, price, status, user_id, provider_id, def to_json(self): return { 'id': self.pax_id, - 'date': self.date, + 'date': self.date.isoformat(), 'description': self.description, 'name': self.name, 'price': self.price, From 61e8a67e9592423749eb89538f7dbda1813d42b2 Mon Sep 17 00:00:00 2001 From: Lucas Dutra Date: Sat, 16 Nov 2019 13:15:55 -0200 Subject: [PATCH 60/63] pax-app/Wiki#200 update pax creation to fit new model requirements --- project/api/models.py | 2 +- project/api/utils/chain_of_responsibility/handlers.py | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/project/api/models.py b/project/api/models.py index f9d5963..f3ae03f 100644 --- a/project/api/models.py +++ b/project/api/models.py @@ -16,7 +16,7 @@ class Pax(db.Model): provider_id = db.Column(db.Integer, nullable=False) chat_id = db.Column(db.Integer, nullable=False) address_id = db.Column(db.Integer, nullable=False) - canceled_motive = db.Column(db.String(500), nullable=False) + canceled_motive = db.Column(db.String(500), nullable=True) def __init__(self, date, description, name, price, status, user_id, provider_id, chat_id, address_id, canceled_motive): self.date = date diff --git a/project/api/utils/chain_of_responsibility/handlers.py b/project/api/utils/chain_of_responsibility/handlers.py index bc43298..8b9d951 100644 --- a/project/api/utils/chain_of_responsibility/handlers.py +++ b/project/api/utils/chain_of_responsibility/handlers.py @@ -21,9 +21,10 @@ def handle(self, request, row): provider_id = pax.get('provider_id') chat_id = pax.get('chat_id') address_id = pax.get('address_id') + canceled_motive = pax.get('canceled_motive') pax = Pax(date, description, name, - price, '', user_id, provider_id, chat_id, address_id) + price, '', user_id, provider_id, chat_id, address_id, canceled_motive) utils.commit_to_database('A', pax) return jsonify(utils.createSuccessMessage('Pax was created!')), 201 From b3b01a862f112192747593b013b0523abeab1cd1 Mon Sep 17 00:00:00 2001 From: Lucas Dutra Date: Sat, 16 Nov 2019 14:23:48 -0200 Subject: [PATCH 61/63] pax-app/Wiki#200 Create update canceled_motive route --- project/api/views.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/project/api/views.py b/project/api/views.py index b4a7a01..2688dd2 100644 --- a/project/api/views.py +++ b/project/api/views.py @@ -122,4 +122,18 @@ def get_all_pax(user_id): return jsonify(utils.createFailMessage('Cannot find user')), 404 except ValueError: return jsonify(utils.createFailMessage('Cannot find user')), 404 - return jsonify(pax), 200 \ No newline at end of file + return jsonify(pax), 200 + +@pax_blueprint.route('/update_motive/', methods=['PATCH']) +def update_canceled_motive(pax_id): + post_data = request.get_json() + + pax_id = post_data.get('pax_id') + canceled_motive = post_data.get('canceled_motive') + + pax = Pax.query.filter_by(pax_id=int(pax_id)).first() + if not pax: + return jsonify(utils.createFailMessage('Unexistent Pax')), 404 + pax.canceled_motive = canceled_motive + db.session.commit() + return jsonify(utils.createSuccessGet('Updated canceled motive!')), 200 \ No newline at end of file From a34a9995ee7927e4bc8529f0420266eca33474ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rog=C3=A9rio=20J=C3=BAnior?= Date: Sat, 16 Nov 2019 16:23:04 -0200 Subject: [PATCH 62/63] pax-app/Wiki#190 Correct return strategy to canceled motive update Co-authored-by: Youssef Muhamad --- project/api/models.py | 2 +- project/api/views.py | 18 +++++++++++------- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/project/api/models.py b/project/api/models.py index f3ae03f..e918192 100644 --- a/project/api/models.py +++ b/project/api/models.py @@ -32,7 +32,7 @@ def __init__(self, date, description, name, price, status, user_id, provider_id, def to_json(self): return { - 'id': self.pax_id, + 'pax_id': self.pax_id, 'date': self.date.isoformat(), 'description': self.description, 'name': self.name, diff --git a/project/api/views.py b/project/api/views.py index 2688dd2..d269e02 100644 --- a/project/api/views.py +++ b/project/api/views.py @@ -112,6 +112,7 @@ def get_pendent_pax(user_kind, id): return jsonify(utils.createSuccessGet(pendent_pax)), 200 + @pax_blueprint.route('/all_pax/', methods=['GET']) def get_all_pax(user_id): try: @@ -124,16 +125,19 @@ def get_all_pax(user_id): return jsonify(utils.createFailMessage('Cannot find user')), 404 return jsonify(pax), 200 -@pax_blueprint.route('/update_motive/', methods=['PATCH']) -def update_canceled_motive(pax_id): + +@pax_blueprint.route('/update_motive', methods=['PATCH']) +def update_canceled_motive(): post_data = request.get_json() - pax_id = post_data.get('pax_id') + chat_id = post_data.get('chat_id') canceled_motive = post_data.get('canceled_motive') - pax = Pax.query.filter_by(pax_id=int(pax_id)).first() + pax = Pax.query.filter_by(chat_id=chat_id).first() if not pax: - return jsonify(utils.createFailMessage('Unexistent Pax')), 404 + return jsonify(utils.createFailMessage('Inexistent Pax')), 404 + pax.canceled_motive = canceled_motive - db.session.commit() - return jsonify(utils.createSuccessGet('Updated canceled motive!')), 200 \ No newline at end of file + utils.commit_to_database('M', pax) + + return jsonify(utils.createSuccessMessage('Updated canceled motive!')), 200 From da77938f545d48cc4b7f21bd47c9c681e440e34b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rog=C3=A9rio=20J=C3=BAnior?= Date: Sun, 17 Nov 2019 00:10:33 -0200 Subject: [PATCH 63/63] pax-app/Wiki#190 Modify update motive to change status to canceled as well, now cancel_pax Co-authored-by: Youssef Muhamad --- project/api/views.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/project/api/views.py b/project/api/views.py index d269e02..f0e3c2f 100644 --- a/project/api/views.py +++ b/project/api/views.py @@ -126,8 +126,8 @@ def get_all_pax(user_id): return jsonify(pax), 200 -@pax_blueprint.route('/update_motive', methods=['PATCH']) -def update_canceled_motive(): +@pax_blueprint.route('/cancel_pax', methods=['PATCH']) +def cancel_pax(): post_data = request.get_json() chat_id = post_data.get('chat_id') @@ -137,6 +137,7 @@ def update_canceled_motive(): if not pax: return jsonify(utils.createFailMessage('Inexistent Pax')), 404 + pax.status = 'C' pax.canceled_motive = canceled_motive utils.commit_to_database('M', pax)