mirror of
https://github.com/GSA/notifications-api.git
synced 2026-01-30 14:31:57 -05:00
merge from main
This commit is contained in:
160
app/commands.py
160
app/commands.py
@@ -1,6 +1,7 @@
|
||||
import csv
|
||||
import functools
|
||||
import itertools
|
||||
import secrets
|
||||
import uuid
|
||||
from datetime import datetime, timedelta
|
||||
from os import getenv
|
||||
@@ -8,6 +9,7 @@ from os import getenv
|
||||
import click
|
||||
import flask
|
||||
from click_datetime import Datetime as click_dt
|
||||
from faker import Faker
|
||||
from flask import current_app, json
|
||||
from notifications_python_client.authentication import create_jwt_token
|
||||
from notifications_utils.recipients import RecipientCSV
|
||||
@@ -62,6 +64,17 @@ from app.models import (
|
||||
User,
|
||||
)
|
||||
from app.utils import get_midnight_in_utc
|
||||
from tests.app.db import (
|
||||
create_job,
|
||||
create_notification,
|
||||
create_organization,
|
||||
create_service,
|
||||
create_template,
|
||||
create_user,
|
||||
)
|
||||
|
||||
# used in add-test-* commands
|
||||
fake = Faker(["en_US"])
|
||||
|
||||
|
||||
@click.group(name="command", help="Additional commands")
|
||||
@@ -832,3 +845,150 @@ def purge_csv_bucket():
|
||||
print("ABOUT TO RUN PURGE CSV BUCKET")
|
||||
s3.purge_bucket(bucket_name, access_key, secret, region)
|
||||
print("RAN PURGE CSV BUCKET")
|
||||
|
||||
|
||||
"""
|
||||
Commands to load test data into the database for
|
||||
Orgs, Services, Users, Jobs, Notifications
|
||||
|
||||
faker is used to generate some random fields. All
|
||||
database commands were used from tests/app/db.py
|
||||
where possible to enable better maintainability.
|
||||
"""
|
||||
|
||||
|
||||
# generate n number of test orgs into the dev DB
|
||||
@notify_command(name="add-test-organizations-to-db")
|
||||
@click.option("-g", "--generate", required=True, prompt=True, default=1)
|
||||
def add_test_organizations_to_db(generate):
|
||||
if getenv("NOTIFY_ENVIRONMENT", "") not in ["development", "test"]:
|
||||
current_app.logger.error("Can only be run in development")
|
||||
return
|
||||
|
||||
def generate_gov_agency():
|
||||
agency_names = [
|
||||
"Bureau",
|
||||
"Department",
|
||||
"Administration",
|
||||
"Authority",
|
||||
"Commission",
|
||||
"Division",
|
||||
"Office",
|
||||
"Institute",
|
||||
"Agency",
|
||||
"Council",
|
||||
"Board",
|
||||
"Committee",
|
||||
"Corporation",
|
||||
"Service",
|
||||
"Center",
|
||||
"Registry",
|
||||
"Foundation",
|
||||
"Task Force",
|
||||
"Unit",
|
||||
]
|
||||
|
||||
government_sectors = [
|
||||
"Healthcare",
|
||||
"Education",
|
||||
"Transportation",
|
||||
"Defense",
|
||||
"Law Enforcement",
|
||||
"Environmental Protection",
|
||||
"Housing and Urban Development",
|
||||
"Finance and Economy",
|
||||
"Social Services",
|
||||
"Energy",
|
||||
"Agriculture",
|
||||
"Labor and Employment",
|
||||
"Foreign Affairs",
|
||||
"Trade and Commerce",
|
||||
"Science and Technology",
|
||||
]
|
||||
|
||||
agency = secrets.choice(agency_names)
|
||||
speciality = secrets.choice(government_sectors)
|
||||
|
||||
return f"{fake.word().capitalize()} {speciality} {agency}"
|
||||
|
||||
for num in range(1, int(generate) + 1):
|
||||
org = create_organization(
|
||||
name=generate_gov_agency(),
|
||||
organization_type=secrets.choice(["federal", "state", "other"]),
|
||||
)
|
||||
print(f"{num} {org.name} created")
|
||||
|
||||
|
||||
# generate n number of test services into the dev DB
|
||||
@notify_command(name="add-test-services-to-db")
|
||||
@click.option("-g", "--generate", required=True, prompt=True, default=1)
|
||||
def add_test_services_to_db(generate):
|
||||
if getenv("NOTIFY_ENVIRONMENT", "") not in ["development", "test"]:
|
||||
current_app.logger.error("Can only be run in development")
|
||||
return
|
||||
|
||||
for num in range(1, int(generate) + 1):
|
||||
service_name = f"{fake.company()} sample service"
|
||||
service = create_service(service_name=service_name)
|
||||
print(f"{num} {service.name} created")
|
||||
|
||||
|
||||
# generate n number of test jobs into the dev DB
|
||||
@notify_command(name="add-test-jobs-to-db")
|
||||
@click.option("-g", "--generate", required=True, prompt=True, default=1)
|
||||
def add_test_jobs_to_db(generate):
|
||||
if getenv("NOTIFY_ENVIRONMENT", "") not in ["development", "test"]:
|
||||
current_app.logger.error("Can only be run in development")
|
||||
return
|
||||
|
||||
for num in range(1, int(generate) + 1):
|
||||
service = create_service(check_if_service_exists=True)
|
||||
template = create_template(service=service)
|
||||
job = create_job(template)
|
||||
print(f"{num} {job.id} created")
|
||||
|
||||
|
||||
# generate n number of notifications into the dev DB
|
||||
@notify_command(name="add-test-notifications-to-db")
|
||||
@click.option("-g", "--generate", required=True, prompt=True, default=1)
|
||||
def add_test_notifications_to_db(generate):
|
||||
if getenv("NOTIFY_ENVIRONMENT", "") not in ["development", "test"]:
|
||||
current_app.logger.error("Can only be run in development")
|
||||
return
|
||||
|
||||
for num in range(1, int(generate) + 1):
|
||||
service = create_service(check_if_service_exists=True)
|
||||
template = create_template(service=service)
|
||||
job = create_job(template=template)
|
||||
notification = create_notification(
|
||||
template=template,
|
||||
job=job,
|
||||
)
|
||||
print(f"{num} {notification.id} created")
|
||||
|
||||
|
||||
# generate n number of test users into the dev DB
|
||||
@notify_command(name="add-test-users-to-db")
|
||||
@click.option("-g", "--generate", required=True, prompt=True, default="1")
|
||||
@click.option("-s", "--state", default="active")
|
||||
@click.option("-d", "--admin", default=False, type=bool)
|
||||
def add_test_users_to_db(generate, state, admin):
|
||||
if getenv("NOTIFY_ENVIRONMENT", "") not in ["development", "test"]:
|
||||
current_app.logger.error("Can only be run in development")
|
||||
return
|
||||
|
||||
for num in range(1, int(generate) + 1):
|
||||
|
||||
def fake_email(name):
|
||||
first_name, last_name = name.split(maxsplit=1)
|
||||
username = f"{first_name.lower()}.{last_name.lower()}"
|
||||
return f"{username}@test.gsa.gov"
|
||||
|
||||
name = fake.name()
|
||||
user = create_user(
|
||||
name=name,
|
||||
email=fake_email(name),
|
||||
state=state,
|
||||
platform_admin=admin,
|
||||
)
|
||||
print(f"{num} {user.email_address} created")
|
||||
|
||||
@@ -159,6 +159,7 @@ def create_job(service_id):
|
||||
raise InvalidRequest("Create job is not allowed: service is inactive ", 403)
|
||||
|
||||
data = request.get_json()
|
||||
original_file_name = data.get("original_file_name")
|
||||
data.update({"service": service_id})
|
||||
try:
|
||||
data.update(**get_job_metadata_from_s3(service_id, data["id"]))
|
||||
@@ -181,6 +182,9 @@ def create_job(service_id):
|
||||
data.update({"template_version": template.version})
|
||||
|
||||
job = job_schema.load(data)
|
||||
# See admin #1148, for whatever reason schema loading doesn't load this
|
||||
if original_file_name is not None:
|
||||
job.original_file_name = original_file_name
|
||||
|
||||
if job.scheduled_for:
|
||||
job.job_status = JobStatus.SCHEDULED
|
||||
|
||||
@@ -55,7 +55,7 @@ def invite_user_to_org(organization_id):
|
||||
service=template.service,
|
||||
personalisation={
|
||||
"user_name": (
|
||||
"The GOV.UK Notify team"
|
||||
"The Notify.gov team"
|
||||
if invited_org_user.invited_by.platform_admin
|
||||
else invited_org_user.invited_by.name
|
||||
),
|
||||
|
||||
Reference in New Issue
Block a user