Skip to content

Commit

Permalink
Merge branch 'develop' into feature/43/create_email_templates
Browse files Browse the repository at this point in the history
  • Loading branch information
adrian committed Jun 26, 2023
2 parents 4b84dc8 + 3b965e8 commit 0b091fc
Show file tree
Hide file tree
Showing 26 changed files with 933 additions and 154 deletions.
File renamed without changes.
37 changes: 20 additions & 17 deletions .envtest
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
COGNITO_CLIENT_ID=********\*\*********
AWS_ACCESS_KEY=********\*\*********
AWS_CLIENT_SECRET=********\*\*********
AWS_REGION=********\*\*********
COGNITO_POOL_ID=********\*\*********
EMAIL_SENDER_ADDRESS=********\*\*********
DATABASE_URL=********\*\*********
SECOP_APP_TOKEN=********\*\*********
COLOMBIA_SECOP_APP_TOKEN=********\*\*********
FRONTEND_URL=********\*\*********
HASH_KEY=********\*\*********
FRONT_PUBLIC_IMAGES_ES = ********\*\*********
FRONT_PUBLIC_IMAGES_EN = ********\*\*********
TEMPORAL_BUCKET = ********\*\*********
FACEBOOK_LINK = ********\*\*********
TWITTER_LINK = ********\*\*********
LINK_LINK = ********\*\*********
AWS_ACCESS_KEY = XXXXXX
AWS_CLIENT_SECRET = XXXXXX
AWS_REGION = XXXXXX
COGNITO_POOL_ID = XXXXXX
COGNITO_CLIENT_ID = XXXXXX
COGNITO_CLIENT_SECRE = XXXXXX
EMAIL_SENDER_ADDRESS = XXXXXX
FRONTEND_URL = XXXXXX
SENTRY_DNS = XXXXXX
DATABASE_URL = XXXXXX

COLOMBIA_SECOP_APP_TOKEN = XXXXXX
SECOP_PAGINATION_LIMIT = XXXXXX
HASH_KEY = XXXXXX
TEST_MAIL_RECEIVER = XXXXXX


IMAGES_LANG_SUBPATH = XXXXXX
FACEBOOK_LINK = XXXXXX
TWITTER_LINK = XXXXXX
LINK_LINK = XXXXXX
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
with:
python-version: '3.10'
python-version: '3.11'
cache: pip
cache-dependency-path: '**/requirements*.txt'
- run: pip install -r requirements_dev.txt
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
with:
python-version: '3.10'
python-version: '3.11'
cache: pip
cache-dependency-path: '**/requirements*.txt'
- id: changed-files
Expand All @@ -24,4 +24,4 @@ jobs:
- shell: bash
run: curl -s -S --retry 3 $BASEDIR/tests/script.sh | bash -
- run: pip install -r requirements_dev.txt
- run: pytest /tmp/test_requirements.py
- run: pytest /tmp/test_requirements.py
8 changes: 6 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,13 @@ RUN \
apk --purge del .build-deps

#
COPY ./app /code/app

COPY ./.env /code/.env
COPY ./migrations /code/migrations
COPY ./alembic.ini /code/alembic.ini
COPY ./migrations /code/migrations
COPY ./app /code/app

# Run Alembic migrations
RUN alembic upgrade head
#
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"]
51 changes: 30 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,25 +51,26 @@ uvicorn app.main:app --reload

you can use .envtest as an example, it has the following keys:

COGNITO_CLIENT_ID -> your client id inside cognito
COGNITO_CLIENT_SECRET -> your client secret from cognito client app
AWS_ACCESS_KEY -> AWS key from the account that owns the users pool
AWS_CLIENT_SECRET -> AWS secret from the account that owns the users pool
AWS_REGION -> conigo and SES pool region
COGNITO_POOL_ID -> cognito pool id
EMAIL_SENDER_ADDRESS -> authorized sender in cognito
FRONTEND_URL -> frontend url, use http://127.0.0.1:3000/ for dev
SENTRY_DNS -> the DNS for sentry
COLOMBIA_SECOP_APP_TOKEN -> Token from fetching SECOP data
SECOP_PAGINATION_LIMIT -> Page size for the fected SECOP data
HASH_KEY -> hash key for hashing personal data before submitting it to Database
APPLICATION_EXPIRATION_DAYS -> Days to expire link after application creation
FRONT_PUBLIC_IMAGES_ES -> Directory on the frontend server containing the Spanish versions of the text in the images.
FRONT_PUBLIC_IMAGES_EN -> Directory on the frontend server containing the English versions of the text in the images.
TEMPORAL_BUCKET -> Temporal S3 bucket used in develop to serve images
FACEBOOK_LINK -> Link to OCP Facebook account
TWITTER_LINK -> Link to OCP Twitter account
LINK_LINK -> Link to (pending to ask)
- COGNITO_CLIENT_ID -> your client id inside cognito
- COGNITO_CLIENT_SECRET -> your client secret from cognito client app
- AWS_ACCESS_KEY -> AWS key from the account that owns the users pool
- AWS_CLIENT_SECRET -> AWS secret from the account that owns the users pool
- AWS_REGION -> cognito and SES pool region
- COGNITO_POOL_ID -> cognito pool id
- EMAIL_SENDER_ADDRESS -> authorized sender in cognito
- FRONTEND_URL -> frontend url, use http://localhost:3000/ for dev
- SENTRY_DNS -> the DNS for sentry
- COLOMBIA_SECOP_APP_TOKEN -> token to set header to fetch SECOP data
- SECOP_PAGINATION_LIMIT -> page size to fetch SECOP data
- SECOP_DEFAULT_DAYS_FROM_ULTIMA_ACTUALIZACION -> days used to compare field ultima_actualizacion the first time a fetching to SECOP data is made (or no awards in database)
- HASH_KEY -> key for hashing identifiers for privacy concerns
- APPLICATION_EXPIRATION_DAYS -> days to expire link after application creation
- IMAGES_BASE_URL -> url where the images are served
- IMAGES_LANG_SUBPATH -> static sub-path to IMAGES_BASE_URL containing the localized versions of the text in the images buttons.
- FACEBOOK_LINK -> link to OCP Facebook account
- TWITTER_LINK -> link to OCP Twitter account
- LINK_LINK -> link to (Pending to define)
- TEST_MAIL_RECEIVER -> email used to send invitations when fetching new awards (Will be removed)

You should configure the pre-commit for the repo one time

Expand Down Expand Up @@ -131,9 +132,11 @@ You need to have a postgresql service running. You can either install postgres f

Once you have the service you need to create a database

then you can construct the env variable like this
then you can set the env variable like this

```
DATABASE_URL=postgresql://{username}:{password}@{host_adress:port}/{db_name}
```

in order to apply migrations in tables use

Expand Down Expand Up @@ -173,5 +176,11 @@ python -m app.commands --help
The command to fetch new awards is

```
python -m app.commands fetcher-awards
python -m app.commands fetch-awards
```

It will send invitations to the email configure in the env variable _TEST_MAIL_RECEIVER_. Alternative could receive a custom email destination with **--email-invitation** argument

```
python -m app.commands fetch-awards --email-invitation test@example.com
```
18 changes: 14 additions & 4 deletions app/background_processes/fetcher.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import logging
from contextlib import contextmanager

from app.core.settings import app_settings
from app.core.user_dependencies import sesClient
from app.db.session import get_db
from app.schema.core import Borrower
Expand All @@ -11,7 +12,7 @@
from .borrower_utils import get_or_create_borrower


def fetch_new_awards_from_date(last_updated_award_date: str):
def fetch_new_awards_from_date(last_updated_award_date: str, email_invitation: str):
index = 0
contracts_response = awards_utils.get_new_contracts(index, last_updated_award_date)
contracts_response_json = contracts_response.json()
Expand Down Expand Up @@ -40,10 +41,19 @@ def fetch_new_awards_from_date(last_updated_award_date: str):

message = insert_message(application, session)

# change in PROD
logging.info(f"Parameter email_invitation: {email_invitation}")
if not email_invitation or email_invitation == "":
email_invitation = app_settings.test_mail_receiver

logging.info(
f"NON PROD - Email to: {borrower.email} sent to {email_invitation}"
)

messageId = email_utility.send_invitation_email(
sesClient,
application.uuid,
borrower.email,
email_invitation, # change to borrower.email in prod
borrower.legal_name,
award.buyer_name,
award.title,
Expand All @@ -62,9 +72,9 @@ def fetch_new_awards_from_date(last_updated_award_date: str):
contracts_response_json = contracts_response.json()


def fetch_new_awards():
def fetch_new_awards(email_invitation: str = None):
last_updated_award_date = awards_utils.get_last_updated_award_date()
fetch_new_awards_from_date(last_updated_award_date)
fetch_new_awards_from_date(last_updated_award_date, email_invitation)


def fetch_previous_awards(borrower: Borrower):
Expand Down
8 changes: 4 additions & 4 deletions app/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@


@app.command()
def fetcher_awards():
fetcher.fetch_new_awards()
def fetch_awards(email_invitation: str = None):
fetcher.fetch_new_awards(email_invitation)


@app.command()
def fetch_new_awards_from_date(date: str):
fetcher.fetch_new_awards_from_date(date)
def fetch_new_awards_from_date(date: str, email_invitation: str = None):
fetcher.fetch_new_awards_from_date(date, email_invitation)


if __name__ == "__main__":
Expand Down
32 changes: 17 additions & 15 deletions app/core/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,26 @@

VERSION: str = "0.1.1"

config_env = {
**dotenv_values(".env"), # load local file development variables
**os.environ, # override loaded values with system environment variables
}

def merge_dicts_with_condition(main_dict, override_dict):
temp_dict = {**override_dict}
filtered_dict = {key: value for key, value in temp_dict.items() if value != ""}
merged_dict = {**main_dict, **filtered_dict}
return merged_dict


# load local file development variables
# override loaded values with system environment variables if not empty ''
config_env = merge_dicts_with_condition(dotenv_values(".env"), os.environ)


class Settings(BaseSettings):
app_name: str = "Credere API"
db_url: str = config_env.get("DB_URL", "sqlite:///./test_db.db")
version: str = config_env.get("VERSION", VERSION)
aws_region: str = config_env.get("AWS_REGION", "us-west-2")
cognito_pool_id: str = config_env.get("COGNITO_POOL_ID", None)
aws_access_key: str = config_env.get("AWS_ACCESS_KEY", None)
aws_client_secret: str = config_env.get("AWS_CLIENT_SECRETAWS_CLIENT_SECRET", None)
aws_client_secret: str = config_env.get("AWS_CLIENT_SECRET", None)
email_sender_address: str = config_env.get(
"EMAIL_SENDER_ADDRESS", "credere@noreply.open-contracting.org"
)
Expand All @@ -27,26 +33,22 @@ class Settings(BaseSettings):
database_url: str = config_env.get("DATABASE_URL", None)
frontend_url: str = config_env.get("FRONTEND_URL", "http://localhost:3000")
sentry_dsn: str = config_env.get("SENTRY_DNS", None)
temporal_bucket: str = config_env.get(
"TEMPORAL_BUCKET", "https://adrian-personal.s3.sa-east-1.amazonaws.com"
)
front_public_images_es: str = config_env.get(
"FRONT_PUBLIC_IMAGES_ES", "/public/images/es"
)
front_public_images_en: str = config_env.get(
"FRONT_PUBLIC_IMAGES_EN", "/public/images/es"
images_base_url: str = config_env.get(
"IMAGES_BASE_URL", "http://ocp22.open-contracting.org/images"
)
images_lang_subpath: str = config_env.get("IMAGES_LANG_SUBPATH", "")
facebook_link: str = config_env.get("FACEBOOK_LINK", "www.facebook.com")
twitter_link: str = config_env.get("TWITTER_LINK", "www.twitter.com")
link_link: str = config_env.get("LINK_LINK", "http://localhost:3000")
test_mail_receiver: str = config_env.get("TEST_MAIL_RECEIVER", "aomm24@gmail.com")
test_mail_receiver: str = config_env.get("TEST_MAIL_RECEIVER", None)
colombia_secop_app_token: str = config_env.get("COLOMBIA_SECOP_APP_TOKEN", None)
hash_key: str = config_env.get("HASH_KEY", None)
application_expiration_days: int = config_env.get("APPLICATION_EXPIRATION_DAYS", 7)
secop_pagination_limit: int = config_env.get("SECOP_PAGINATION_LIMIT", 5)
secop_default_days_from_ultima_actualizacion: int = config_env.get(
"SECOP_DEFAULT_DAYS_FROM_ULTIMA_ACTUALIZACION", 365
)
ocp_email_group: str = config_env.get("OCP_EMAIL_GROUP", "test@mail.com")

class Config:
env_file = ".env"
Expand Down
11 changes: 11 additions & 0 deletions app/core/user_dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,17 @@ def reset_password(self, username):

return responseSetPassword

def send_notifications_of_new_applications(
self,
ocp_email_group,
lender_name,
lender_email_group,
):
email_utility.send_notification_new_app_to_fi(self.ses, lender_email_group)
email_utility.send_notification_new_app_to_ocp(
self.ses, ocp_email_group, lender_name
)


cognito = boto3.client(
"cognito-idp",
Expand Down
5 changes: 5 additions & 0 deletions app/email_templates/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
NEW_USER_TEMPLATE_NAME = "credere-NewAccountCreated"
RESET_PASSWORD_TEMPLATE_NAME = "credere-ResetPassword"
ACCESS_TO_CREDIT_SCHEME_FOR_MSMES_TEMPLATE_NAME = "credere-AccessToCreditSchemeForMSMEs"
NEW_APPLICATION_SUBMISSION_FI_TEMPLATE_NAME = "credere-newApplicationSubmissionFIuser"
NEW_APPLICATION_SUBMISSION_OCP_TEMPLATE_NAME = "credere-newApplicationSubmissionOCPUser"
8 changes: 6 additions & 2 deletions app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from fastapi.middleware.cors import CORSMiddleware

from .core.settings import app_settings
from .routers import applications, awards, borrowers, users
from .routers import applications, awards, borrowers, lenders, users

if app_settings.sentry_dsn:
sentry_sdk.init(
Expand All @@ -17,7 +17,10 @@
app = FastAPI()

# Configure CORS settings
origins = [app_settings.frontend_url] # Add more allowed origins as needed
origins = [
"http://localhost:3000",
app_settings.frontend_url,
] # Add more allowed origins as needed

app.add_middleware(
CORSMiddleware,
Expand All @@ -31,6 +34,7 @@
app.include_router(awards.router)
app.include_router(borrowers.router)
app.include_router(applications.router)
app.include_router(lenders.router)

logging.basicConfig(
level=logging.INFO,
Expand Down

0 comments on commit 0b091fc

Please sign in to comment.