--
This commit is contained in:
Executable
+27
@@ -0,0 +1,27 @@
|
||||
#!/bin/sh
|
||||
# Daily database backup, run by the `backup` service in docker-compose.yml.
|
||||
#
|
||||
# Takes a compressed pg_dump of the whole database once a day (and once straight away on
|
||||
# start), keeps the last BACKUP_KEEP_DAYS days, and writes them to ./backups on the host so
|
||||
# they can be copied off the server. Restore one with:
|
||||
# gunzip -c backups/pharma-YYYYMMDD-HHMMSS.sql.gz | docker compose exec -T db psql -U pharma -d pharma
|
||||
set -eu
|
||||
|
||||
KEEP_DAYS="${BACKUP_KEEP_DAYS:-14}"
|
||||
INTERVAL="${BACKUP_INTERVAL_SECONDS:-86400}"
|
||||
mkdir -p /backups
|
||||
|
||||
while true; do
|
||||
stamp=$(date -u +%Y%m%d-%H%M%S)
|
||||
target="/backups/${PGDATABASE}-${stamp}.sql.gz"
|
||||
# Written under a temporary name, so a half-written file is never taken for a backup.
|
||||
if pg_dump --clean --if-exists --no-owner | gzip > "${target}.part"; then
|
||||
mv "${target}.part" "${target}"
|
||||
echo "backup: wrote ${target} ($(du -h "${target}" | cut -f1))"
|
||||
else
|
||||
rm -f "${target}.part"
|
||||
echo "backup: pg_dump FAILED at ${stamp}" >&2
|
||||
fi
|
||||
find /backups -name "${PGDATABASE}-*.sql.gz" -mtime "+${KEEP_DAYS}" -delete
|
||||
sleep "${INTERVAL}"
|
||||
done
|
||||
@@ -0,0 +1,59 @@
|
||||
"""Create (or rotate the password of) the protected super admin.
|
||||
|
||||
python -m scripts.create_superadmin --email owner@example.com --password 'S3cret-...'
|
||||
|
||||
The super admin has the ADMIN role, so it can do everything an administrator can, but no
|
||||
one can edit, deactivate, re-role or delete it afterwards (API and DB trigger both refuse).
|
||||
Re-running with the same email only resets its password and clears any lockout.
|
||||
"""
|
||||
|
||||
import argparse
|
||||
import asyncio
|
||||
import sys
|
||||
|
||||
from sqlalchemy import select
|
||||
|
||||
from app.auth.security import hash_password
|
||||
from app.common.enums import Role
|
||||
from app.database.session import AsyncSessionLocal
|
||||
from app.organization.models import Company
|
||||
from app.users.models import User
|
||||
|
||||
|
||||
async def run(email: str, password: str, name: str, code: str) -> None:
|
||||
async with AsyncSessionLocal() as db:
|
||||
existing = (
|
||||
await db.execute(select(User).where(User.is_superadmin.is_(True)))
|
||||
).scalar_one_or_none()
|
||||
if existing is not None:
|
||||
if existing.email != email.lower():
|
||||
sys.exit(f"A super admin already exists ({existing.email}); only one is allowed.")
|
||||
existing.password_hash = hash_password(password)
|
||||
existing.failed_login_attempts = 0
|
||||
await db.commit()
|
||||
print(f"Super admin {existing.email}: password reset.")
|
||||
return
|
||||
|
||||
company = (await db.execute(select(Company).order_by(Company.created_at))).scalars().first()
|
||||
if company is None:
|
||||
sys.exit("No company exists yet — seed or create one first.")
|
||||
db.add(
|
||||
User(
|
||||
company_id=company.id, email=email.lower(), password_hash=hash_password(password),
|
||||
full_name=name, employee_code=code, role=Role.ADMIN, is_superadmin=True,
|
||||
)
|
||||
)
|
||||
await db.commit()
|
||||
print(f"Super admin created: {email.lower()}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
p = argparse.ArgumentParser()
|
||||
p.add_argument("--email", required=True)
|
||||
p.add_argument("--password", required=True)
|
||||
p.add_argument("--name", default="Super Admin")
|
||||
p.add_argument("--code", default="SUPERADMIN")
|
||||
a = p.parse_args()
|
||||
if len(a.password) < 12:
|
||||
sys.exit("Use a password of at least 12 characters.")
|
||||
asyncio.run(run(a.email, a.password, a.name, a.code))
|
||||
@@ -29,7 +29,7 @@ CSV_TABLES = {
|
||||
"Sales_and_Stock_Statement.csv": ("sales_and_stock_statement", ["name", "headquarter", "report_date", "month", "serial_no", "product_name", "opening", "receipt", "sale", "closing", "value_in_pts"]),
|
||||
"TOUR_PLAN.csv": ("tour_plan", ["name", "headquarter", "serial_no", "report_date", "from_location", "to_location", "pob", "stockiest_visit"]),
|
||||
"STANDARD_TOUR_PLAN.csv": ("standard_tour_plan", ["name", "headquarter", "serial_no", "place"]),
|
||||
"sales_projection.csv": ("sales_projection", ["name", "headquarter", "report_date", "month", "serial_no", "product_name", "pts", "secondary_qty", "value", "closing", "secondary_value", "aug_sales", "value_2"]),
|
||||
"sales_projection.csv": ("sales_projection", ["name", "headquarter", "report_date", "month", "serial_no", "product_name", "pts", "closing_qty", "closing_value", "projection_qty", "projection_value"]),
|
||||
}
|
||||
SERIAL_COLUMNS = {"serial_no", "doctor_sr_no", "chemist_sr_no"}
|
||||
|
||||
@@ -57,7 +57,7 @@ async def apply_sales_projection_account(db) -> None:
|
||||
headquarter = headquarters.name
|
||||
FROM "user" AS account
|
||||
LEFT JOIN headquarters ON headquarters.id = account.headquarters_id
|
||||
WHERE account.role = 'MR' '''
|
||||
WHERE account.id = projection.owner_user_id '''
|
||||
)
|
||||
)
|
||||
|
||||
@@ -76,13 +76,12 @@ async def import_csv_data(only_empty: bool = False) -> None:
|
||||
continue
|
||||
else:
|
||||
await db.execute(text(f'DELETE FROM "{table_name}"'))
|
||||
source_columns = columns[4:] if table_name == "sales_projection" else columns
|
||||
# The Sales Projection CSV is the product list: only Sl. No., product and PTS come
|
||||
# from it; every figure is filled in later, per month, on the form.
|
||||
source_columns = ["serial_no", "product_name", "pts"] if table_name == "sales_projection" else columns
|
||||
rows = read_rows(path, source_columns)
|
||||
if table_name == "sales_projection":
|
||||
rows = [
|
||||
(row_number, {"name": "", "headquarter": "", "report_date": "", "month": "", **data})
|
||||
for row_number, data in rows
|
||||
]
|
||||
rows = [(row_number, {**{c: "" for c in columns}, **data}) for row_number, data in rows]
|
||||
if rows:
|
||||
column_names = ", ".join(f'"{column}"' for column in columns)
|
||||
parameters = ", ".join(f":{column}" for column in columns)
|
||||
|
||||
Reference in New Issue
Block a user