Skip to content

Models: SafeUser/SafePrintJob, PrintJobLog redesign, job_id→id, datetime timestamps, model-based request.state.user#2

Merged
afonsoingles merged 6 commits into
new-email-handlerfrom
copilot/integrate-db-models-schemas
Mar 16, 2026
Merged

Models: SafeUser/SafePrintJob, PrintJobLog redesign, job_id→id, datetime timestamps, model-based request.state.user#2
afonsoingles merged 6 commits into
new-email-handlerfrom
copilot/integrate-db-models-schemas

Conversation

Copilot AI commented Mar 15, 2026

Copy link
Copy Markdown
Contributor

Replaces loose float timestamps and plain-dict user/job representations with properly typed Pydantic v2 models throughout the stack.

Models

models/user.py

  • SafeUser — base model, all fields except password; holds the backward-compat _coerce_permissions validator
  • User(SafeUser) — adds Optional[str] password

models/print_job.py

  • PrintJobLog redesigned: id, timestamp, actor ("system" or user id), type (e.g. "job_created"), description (optional)
  • PrintJob.job_idPrintJob.id
  • SafePrintJob — excludes cups_job_id and file (R2 path); available via PrintJob.to_safe()
  • All float timestamp fields (created_at, updated_at, timestamp) → datetime

Backward-compat model_validator(mode="before") on every renamed/redesigned model transparently coerces old MongoDB documents on read — no migration required. Pydantic v2 natively coerces stored Unix epoch floats to UTC-aware datetime.

Tools & decorator

  • get_user_by_id / get_user_by_email return SafeUser / User instances instead of dicts
  • request.state.user is now a model instance — attribute access everywhere:
# before
request.state.user["id"], user["printer"]["credits"]

# after
request.state.user.id, user.printer.credits
  • tools/printer.py_create_log emits the new PrintJobLog schema; user-facing methods return SafePrintJob dicts via to_safe()
  • tools/users.py.timestamp() calls removed; datetime.now(utc) passed directly to models

📱 Kick off Copilot coding agent tasks wherever you are with GitHub Mobile, available on iOS and Android.

Co-authored-by: afonsoingles <110609949+afonsoingles@users.noreply.github.com>
Copilot AI changed the title [WIP] Add models and schemas for database integration feat: Pydantic v2 models/schemas for all DB entities Mar 15, 2026
Copilot AI requested a review from afonsoingles March 15, 2026 20:36
Co-authored-by: afonsoingles <110609949+afonsoingles@users.noreply.github.com>
Copilot AI changed the title feat: Pydantic v2 models/schemas for all DB entities feat: structured UserPermissions model with backward compatibility migration Mar 15, 2026
…model-based request.state.user

Co-authored-by: afonsoingles <110609949+afonsoingles@users.noreply.github.com>
Copilot AI changed the title feat: structured UserPermissions model with backward compatibility migration Structured models: SafeUser/SafePrintJob, PrintJobLog redesign, job_id→id, model-based request.state.user Mar 15, 2026
Co-authored-by: afonsoingles <110609949+afonsoingles@users.noreply.github.com>
Copilot AI changed the title Structured models: SafeUser/SafePrintJob, PrintJobLog redesign, job_id→id, model-based request.state.user Models: SafeUser/SafePrintJob, PrintJobLog redesign, job_id→id, datetime timestamps, model-based request.state.user Mar 15, 2026
Comment thread models/print_job.py Outdated
id: str
timestamp: float
actor: str # "system" or a user id
type: str # e.g. "job_created", "job_accepted", "job_rejected"

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
type: str # e.g. "job_created", "job_accepted", "job_rejected"
type: str

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed in c23fea5.

Comment thread tools/users.py Outdated
self.db.mongo.users.insert_one(user)

self.db.redis.set(f"users.user:{id}", json.dumps(user), ex=10800)
now_ts = datetime.datetime.now(datetime.timezone.utc)

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
now_ts = datetime.datetime.now(datetime.timezone.utc)
now_ts = datetime.datetime.now(datetime.timezone.utc).timestamp()

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reverted in c23fea5. Pydantic v2 still coerces the float to a UTC-aware datetime when building the model.

@afonsoingles afonsoingles left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot check suggestions!

…) in create_user

Co-authored-by: afonsoingles <110609949+afonsoingles@users.noreply.github.com>

Copilot AI commented Mar 16, 2026

Copy link
Copy Markdown
Contributor Author

@copilot check suggestions!

Both suggestions were applied in c23fea5 and are confirmed in the current state of the branch:

  • models/print_job.pytype: str (no trailing comment)
  • tools/users.pynow_ts = datetime.datetime.now(datetime.timezone.utc).timestamp()

Copilot AI requested a review from afonsoingles March 16, 2026 01:12
@afonsoingles afonsoingles marked this pull request as ready for review March 16, 2026 01:12
@afonsoingles afonsoingles merged commit 788199b into new-email-handler Mar 16, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants