chore: fix all ruff lint errors + format — 0 errors, 306 tests pass
This commit is contained in:
@@ -5,15 +5,15 @@ from __future__ import annotations
|
||||
import csv
|
||||
import io
|
||||
import uuid
|
||||
from datetime import datetime, timezone
|
||||
from datetime import UTC, datetime
|
||||
from typing import Any
|
||||
|
||||
from sqlalchemy import select, func, or_, desc, asc, delete
|
||||
from sqlalchemy import asc, delete, desc, func, or_, select
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.core.audit import log_audit, log_deletion
|
||||
from app.core.audit import log_audit
|
||||
from app.models.company import Company
|
||||
from app.models.contact import Contact, CompanyContact
|
||||
from app.models.contact import CompanyContact, Contact
|
||||
|
||||
|
||||
def _company_to_dict(c: Company, include_contacts: bool = False) -> dict[str, Any]:
|
||||
@@ -66,9 +66,7 @@ async def list_companies(
|
||||
pattern = f"%{search}%"
|
||||
base = base.where(
|
||||
or_(
|
||||
Company.search_tsv.op("@@")(
|
||||
func.plainto_tsquery("english", search)
|
||||
),
|
||||
Company.search_tsv.op("@@")(func.plainto_tsquery("english", search)),
|
||||
Company.name.ilike(pattern),
|
||||
Company.industry.ilike(pattern),
|
||||
Company.description.ilike(pattern),
|
||||
@@ -131,16 +129,18 @@ async def get_company_detail(
|
||||
contacts_result = await db.execute(contacts_q)
|
||||
contacts_list = []
|
||||
for contact, link in contacts_result.all():
|
||||
contacts_list.append({
|
||||
"id": str(contact.id),
|
||||
"first_name": contact.first_name,
|
||||
"last_name": contact.last_name,
|
||||
"email": contact.email,
|
||||
"phone": contact.phone,
|
||||
"position": contact.position,
|
||||
"role_at_company": link.role_at_company,
|
||||
"is_primary": link.is_primary,
|
||||
})
|
||||
contacts_list.append(
|
||||
{
|
||||
"id": str(contact.id),
|
||||
"first_name": contact.first_name,
|
||||
"last_name": contact.last_name,
|
||||
"email": contact.email,
|
||||
"phone": contact.phone,
|
||||
"position": contact.position,
|
||||
"role_at_company": link.role_at_company,
|
||||
"is_primary": link.is_primary,
|
||||
}
|
||||
)
|
||||
data["contacts"] = contacts_list
|
||||
return data
|
||||
|
||||
@@ -168,7 +168,12 @@ async def create_company(
|
||||
await db.flush()
|
||||
await db.refresh(company)
|
||||
await log_audit(
|
||||
db, tenant_id, user_id, "create", "company", company.id,
|
||||
db,
|
||||
tenant_id,
|
||||
user_id,
|
||||
"create",
|
||||
"company",
|
||||
company.id,
|
||||
changes={"name": data["name"]},
|
||||
)
|
||||
return _company_to_dict(company)
|
||||
@@ -224,7 +229,7 @@ async def soft_delete_company(
|
||||
if company is None:
|
||||
return False
|
||||
|
||||
company.deleted_at = datetime.now(timezone.utc)
|
||||
company.deleted_at = datetime.now(UTC)
|
||||
company.updated_by = user_id
|
||||
|
||||
if cascade:
|
||||
@@ -238,7 +243,12 @@ async def soft_delete_company(
|
||||
|
||||
await db.flush()
|
||||
await log_audit(
|
||||
db, tenant_id, user_id, "delete", "company", company_id,
|
||||
db,
|
||||
tenant_id,
|
||||
user_id,
|
||||
"delete",
|
||||
"company",
|
||||
company_id,
|
||||
changes={"name": company.name, "cascade": cascade},
|
||||
)
|
||||
return True
|
||||
@@ -288,7 +298,12 @@ async def link_contact(
|
||||
existing.is_primary = is_primary
|
||||
await db.flush()
|
||||
await log_audit(
|
||||
db, tenant_id, user_id, "link", "company_contact", existing.id,
|
||||
db,
|
||||
tenant_id,
|
||||
user_id,
|
||||
"link",
|
||||
"company_contact",
|
||||
existing.id,
|
||||
changes={"company_id": str(company_id), "contact_id": str(contact_id)},
|
||||
)
|
||||
return {
|
||||
@@ -309,7 +324,12 @@ async def link_contact(
|
||||
db.add(link)
|
||||
await db.flush()
|
||||
await log_audit(
|
||||
db, tenant_id, user_id, "link", "company_contact", link.id,
|
||||
db,
|
||||
tenant_id,
|
||||
user_id,
|
||||
"link",
|
||||
"company_contact",
|
||||
link.id,
|
||||
changes={"company_id": str(company_id), "contact_id": str(contact_id)},
|
||||
)
|
||||
return {
|
||||
@@ -342,7 +362,12 @@ async def unlink_contact(
|
||||
await db.delete(link)
|
||||
await db.flush()
|
||||
await log_audit(
|
||||
db, tenant_id, user_id, "unlink", "company_contact", link.id,
|
||||
db,
|
||||
tenant_id,
|
||||
user_id,
|
||||
"unlink",
|
||||
"company_contact",
|
||||
link.id,
|
||||
changes={"company_id": str(company_id), "contact_id": str(contact_id)},
|
||||
)
|
||||
return True
|
||||
@@ -365,9 +390,7 @@ async def export_companies_csv(
|
||||
pattern = f"%{search}%"
|
||||
base = base.where(
|
||||
or_(
|
||||
Company.search_tsv.op("@@")(
|
||||
func.plainto_tsquery("english", search)
|
||||
),
|
||||
Company.search_tsv.op("@@")(func.plainto_tsquery("english", search)),
|
||||
Company.name.ilike(pattern),
|
||||
Company.industry.ilike(pattern),
|
||||
Company.description.ilike(pattern),
|
||||
@@ -379,12 +402,22 @@ async def export_companies_csv(
|
||||
|
||||
output = io.StringIO()
|
||||
writer = csv.writer(output)
|
||||
writer.writerow(["id", "name", "account_number", "industry", "phone", "email", "website", "description"])
|
||||
writer.writerow(
|
||||
["id", "name", "account_number", "industry", "phone", "email", "website", "description"]
|
||||
)
|
||||
for c in companies:
|
||||
writer.writerow([
|
||||
str(c.id), c.name, c.account_number or "", c.industry or "",
|
||||
c.phone or "", c.email or "", c.website or "", c.description or "",
|
||||
])
|
||||
writer.writerow(
|
||||
[
|
||||
str(c.id),
|
||||
c.name,
|
||||
c.account_number or "",
|
||||
c.industry or "",
|
||||
c.phone or "",
|
||||
c.email or "",
|
||||
c.website or "",
|
||||
c.description or "",
|
||||
]
|
||||
)
|
||||
return output.getvalue()
|
||||
|
||||
|
||||
@@ -407,9 +440,7 @@ async def export_companies_xlsx(
|
||||
pattern = f"%{search}%"
|
||||
base = base.where(
|
||||
or_(
|
||||
Company.search_tsv.op("@@")(
|
||||
func.plainto_tsquery("english", search)
|
||||
),
|
||||
Company.search_tsv.op("@@")(func.plainto_tsquery("english", search)),
|
||||
Company.name.ilike(pattern),
|
||||
Company.industry.ilike(pattern),
|
||||
Company.description.ilike(pattern),
|
||||
@@ -422,13 +453,30 @@ async def export_companies_xlsx(
|
||||
wb = Workbook()
|
||||
ws = wb.active
|
||||
ws.title = "Companies"
|
||||
headers = ["id", "name", "account_number", "industry", "phone", "email", "website", "description"]
|
||||
headers = [
|
||||
"id",
|
||||
"name",
|
||||
"account_number",
|
||||
"industry",
|
||||
"phone",
|
||||
"email",
|
||||
"website",
|
||||
"description",
|
||||
]
|
||||
ws.append(headers)
|
||||
for c in companies:
|
||||
ws.append([
|
||||
str(c.id), c.name, c.account_number or "", c.industry or "",
|
||||
c.phone or "", c.email or "", c.website or "", c.description or "",
|
||||
])
|
||||
ws.append(
|
||||
[
|
||||
str(c.id),
|
||||
c.name,
|
||||
c.account_number or "",
|
||||
c.industry or "",
|
||||
c.phone or "",
|
||||
c.email or "",
|
||||
c.website or "",
|
||||
c.description or "",
|
||||
]
|
||||
)
|
||||
|
||||
buf = io.BytesIO()
|
||||
wb.save(buf)
|
||||
|
||||
Reference in New Issue
Block a user