-
Notifications
You must be signed in to change notification settings - Fork 4
Добавление софт делитов в принтер #87
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
Open
gitfresnel
wants to merge
17
commits into
main
Choose a base branch
from
29-добавить-софт-делиты-в-принтер-1
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
The head ref may contain hidden characters: "29-\u0434\u043E\u0431\u0430\u0432\u0438\u0442\u044C-\u0441\u043E\u0444\u0442-\u0434\u0435\u043B\u0438\u0442\u044B-\u0432-\u043F\u0440\u0438\u043D\u0442\u0435\u0440-1"
Open
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
2115821
f
gitfresnel 2a6326b
f
gitfresnel afa9d5f
Merge branch 'main' into 29-добавить-софт-делиты-в-принтер-1
gitfresnel 5495e19
Delete migrations/versions/dddf1cc0c593_add_is_deleted_field_to_union…
gitfresnel dae0028
f
gitfresnel 4a3039f
Merge remote-tracking branch 'origin/29-добавить-софт-делиты-в-принте…
gitfresnel 64679c1
f
gitfresnel 6becd9e
f
gitfresnel 897ad47
f
gitfresnel 9fdb0e8
Delete migrations/versions/2b86076bf074_add_is_deleted_field_to_union…
gitfresnel 3cb4784
f
gitfresnel bbc7607
Merge remote-tracking branch 'origin/29-добавить-софт-делиты-в-принте…
gitfresnel 4a8981a
Delete migrations/versions/808f98fc21f5_add_is_deleted_field_to_union…
gitfresnel 77bca1a
f
gitfresnel b9ca32b
Merge remote-tracking branch 'origin/29-добавить-софт-делиты-в-принте…
gitfresnel f175205
f
gitfresnel 3357caf
Merge branch 'main' into 29-добавить-софт-делиты-в-принтер-1
gitfresnel 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
28 changes: 28 additions & 0 deletions
28
migrations/versions/c29b6ffbfed4_add_is_deleted_field_to_unionmember.py
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,28 @@ | ||
| """Add is_deleted field to UnionMember | ||
|
|
||
| Revision ID: c29b6ffbfed4 | ||
| Revises: a68c6bb2972c | ||
| Create Date: 2024-11-22 17:50:35.569723 | ||
|
|
||
| """ | ||
|
|
||
| import sqlalchemy as sa | ||
| from alembic import op | ||
|
|
||
|
|
||
| # revision identifiers, used by Alembic. | ||
| revision = 'c29b6ffbfed4' | ||
| down_revision = 'a68c6bb2972c' | ||
| branch_labels = None | ||
| depends_on = None | ||
|
|
||
|
|
||
| def upgrade(): | ||
| op.add_column( | ||
| 'union_member', sa.Column('is_deleted', sa.Boolean(), nullable=False, server_default=sa.false()) | ||
| ) | ||
|
|
||
|
|
||
| def downgrade(): | ||
| op.drop_column('union_member', 'is_deleted') | ||
| op.alter_column('file', 'source', existing_type=sa.VARCHAR(), nullable=True) |
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,75 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import re | ||
|
|
||
| from sqlalchemy import not_ | ||
| from sqlalchemy.exc import NoResultFound | ||
| from sqlalchemy.orm import Query, Session, as_declarative, declared_attr | ||
|
|
||
| from print_service.exceptions import ObjectNotFound | ||
|
|
||
|
|
||
| @as_declarative() | ||
| class Base: | ||
| """Base class for all database entities""" | ||
|
|
||
| @declared_attr | ||
| def __tablename__(cls) -> str: # pylint: disable=no-self-argument | ||
| """Generate database table name automatically. | ||
| Convert CamelCase class name to snake_case db table name. | ||
| """ | ||
| return re.sub(r"(?<!^)(?=[A-Z])", "_", cls.__name__).lower() | ||
|
|
||
| def __repr__(self): | ||
| attrs = [] | ||
| for c in self.__table__.columns: | ||
| attrs.append(f"{c.name}={getattr(self, c.name)}") | ||
| return "{}({})".format(c.__class__.__name__, ', '.join(attrs)) | ||
|
|
||
|
|
||
| class BaseDbModel(Base): | ||
| __abstract__ = True | ||
|
|
||
| @classmethod | ||
| def create(cls, *, session: Session, **kwargs) -> BaseDbModel: | ||
| obj = cls(**kwargs) | ||
| session.add(obj) | ||
| session.flush() | ||
| return obj | ||
|
|
||
| @classmethod | ||
| def query(cls, *, session: Session, with_deleted: bool = False) -> Query: | ||
| """Get all objects with soft deletes""" | ||
| objs = session.query(cls) | ||
| if not with_deleted and hasattr(cls, "is_deleted"): | ||
| objs = objs.filter(not_(cls.is_deleted)) | ||
| return objs | ||
|
|
||
| @classmethod | ||
| def get(cls, id: int, *, with_deleted=False, session: Session) -> BaseDbModel: | ||
| """Get object with soft deletes""" | ||
| objs = session.query(cls) | ||
| if not with_deleted and hasattr(cls, "is_deleted"): | ||
| objs = objs.filter(not_(cls.is_deleted)) | ||
| try: | ||
| return objs.filter(cls.id == id).one() | ||
| except NoResultFound: | ||
| raise ObjectNotFound(cls, id) | ||
|
|
||
| @classmethod | ||
| def update(cls, id: int, *, session: Session, **kwargs) -> BaseDbModel: | ||
| obj = cls.get(id, session=session) | ||
| for k, v in kwargs.items(): | ||
| setattr(obj, k, v) | ||
| session.flush() | ||
| return obj | ||
|
|
||
| @classmethod | ||
| def delete(cls, id: int, *, session: Session) -> None: | ||
| """Soft delete object if possible, else hard delete""" | ||
| obj = cls.get(id, session=session) | ||
| if hasattr(obj, "is_deleted"): | ||
| obj.is_deleted = True | ||
| else: | ||
| session.delete(obj) | ||
| session.flush() |
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.
Uh oh!
There was an error while loading. Please reload this page.