-
Notifications
You must be signed in to change notification settings - Fork 0
Models: SafeUser/SafePrintJob, PrintJobLog redesign, job_id→id, datetime timestamps, model-based request.state.user #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
afonsoingles
merged 6 commits into
new-email-handler
from
copilot/integrate-db-models-schemas
Mar 16, 2026
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b4af8bc
Initial plan
Copilot ff89eec
feat: add Pydantic models for DB entities and integrate into tools
Copilot 5e11c7f
feat: convert permissions to UserPermissions model with backward compat
Copilot a32de9c
feat: SafeUser/SafePrintJob models, PrintJobLog redesign, job_id→id, …
Copilot bbfd492
feat: use datetime instead of float for all timestamp fields
Copilot c23fea5
fix: remove type comment in PrintJobLog; revert now_ts to .timestamp(…
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| from models.user import User, SafeUser, UserPrinterSettings, UserPermissions | ||
| from models.print_job import PrintJob, SafePrintJob, PrintJobLog | ||
|
|
||
| __all__ = ["User", "SafeUser", "UserPrinterSettings", "UserPermissions", "PrintJob", "SafePrintJob", "PrintJobLog"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| from pydantic import BaseModel, Field, model_validator | ||
| from typing import Optional | ||
| from datetime import datetime | ||
| import hashlib | ||
|
|
||
|
|
||
| class PrintJobLog(BaseModel): | ||
| id: str | ||
| timestamp: datetime | ||
| actor: str # "system" or a user id | ||
| type: str # e.g. "job_created", "job_accepted", "job_rejected" | ||
| description: Optional[str] = None | ||
|
|
||
| @model_validator(mode="before") | ||
| @classmethod | ||
| def _coerce_old_format(cls, data): | ||
| # Backward compatibility: old logs had {timestamp, job_id, user_id, description}. | ||
| if isinstance(data, dict) and "job_id" in data and "id" not in data: | ||
| data = dict(data) | ||
| raw = f"{data.get('job_id', '')}{data.get('timestamp', '')}" | ||
| data["id"] = hashlib.md5(raw.encode()).hexdigest() | ||
| data["actor"] = data.pop("user_id", "system") | ||
| data["type"] = "legacy" | ||
| data.pop("job_id", None) | ||
| return data | ||
|
|
||
|
|
||
| class PrintJob(BaseModel): | ||
| id: str | ||
| user_id: str | ||
| cups_job_id: Optional[str] = None | ||
| filename: str | ||
| file: str | ||
| color: bool = True | ||
| copies: int = 1 | ||
| status: str | ||
| logs: list[PrintJobLog] = Field(default_factory=list) | ||
| created_at: datetime | ||
| updated_at: datetime | ||
|
|
||
| @model_validator(mode="before") | ||
| @classmethod | ||
| def _coerce_old_format(cls, data): | ||
| # Backward compatibility: old documents stored the job identifier as "job_id". | ||
| if isinstance(data, dict) and "job_id" in data and "id" not in data: | ||
| data = dict(data) | ||
| data["id"] = data.pop("job_id") | ||
| return data | ||
|
|
||
| def to_safe(self) -> "SafePrintJob": | ||
| return SafePrintJob.model_validate(self.model_dump()) | ||
|
|
||
|
|
||
| class SafePrintJob(BaseModel): | ||
| """PrintJob with internal/sensitive fields redacted (no cups_job_id, no file path).""" | ||
|
|
||
| id: str | ||
| user_id: str | ||
| filename: str | ||
| color: bool = True | ||
| copies: int = 1 | ||
| status: str | ||
| logs: list[PrintJobLog] = Field(default_factory=list) | ||
| created_at: datetime | ||
| updated_at: datetime | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| from pydantic import BaseModel, Field, model_validator | ||
| from typing import Optional | ||
| from datetime import datetime | ||
|
|
||
|
|
||
| class UserPrinterSettings(BaseModel): | ||
| credits: float = 0 | ||
| no_credits_action: str = "require_approval" | ||
|
|
||
|
|
||
| class UserPermissions(BaseModel): | ||
| manage_printer: bool = False | ||
| manage_users: bool = False | ||
|
|
||
|
|
||
| class SafeUser(BaseModel): | ||
| """User with personal/sensitive information redacted (no password).""" | ||
|
|
||
| id: str | ||
| name: str | ||
| email: str | ||
| auth_methods: list[str] = Field(default_factory=list) | ||
| region: str | ||
| language: str | ||
| superadmin: bool = False | ||
| admin: bool = False | ||
| printer: UserPrinterSettings = Field(default_factory=UserPrinterSettings) | ||
| permissions: UserPermissions = Field(default_factory=UserPermissions) | ||
| suspended: bool = False | ||
| created_at: datetime | ||
| updated_at: datetime | ||
|
|
||
| @model_validator(mode="before") | ||
| @classmethod | ||
| def _coerce_permissions(cls, data): | ||
| # Backward compatibility: existing users stored permissions as a list. | ||
| # Discard the list and use default UserPermissions values instead. | ||
| if isinstance(data, dict) and isinstance(data.get("permissions"), list): | ||
| data = dict(data) | ||
| data["permissions"] = UserPermissions() | ||
| return data | ||
|
|
||
|
|
||
| class User(SafeUser): | ||
| """Full user model including the hashed password.""" | ||
|
|
||
| password: Optional[str] = None |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed in c23fea5.