-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #876 from ChildMindInstitute/M2-3741
M2-3741: creator_id save and transfer ownership history
- Loading branch information
Showing
9 changed files
with
206 additions
and
15 deletions.
There are no files selected for viewing
This file contains 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 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 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,7 @@ | ||
from enum import Enum | ||
|
||
|
||
class TransferOwnershipStatus(str, Enum): | ||
PENDING = "pending" | ||
APPROVED = "approved" | ||
DECLINED = "declined" |
This file contains 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 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 |
---|---|---|
@@ -1,14 +1,18 @@ | ||
from sqlalchemy import Column, ForeignKey, String | ||
from sqlalchemy import Column, ForeignKey, String, Unicode | ||
from sqlalchemy.dialects.postgresql import UUID | ||
from sqlalchemy_utils import StringEncryptedType | ||
|
||
from apps.shared.encryption import get_key | ||
from apps.transfer_ownership.constants import TransferOwnershipStatus | ||
from infrastructure.database import Base | ||
|
||
|
||
class TransferSchema(Base): | ||
__tablename__ = "transfer_ownership" | ||
|
||
email = Column(String()) | ||
email = Column(StringEncryptedType(Unicode, get_key)) | ||
applet_id = Column( | ||
ForeignKey("applets.id", ondelete="RESTRICT"), nullable=False | ||
) | ||
key = Column(UUID(as_uuid=True)) | ||
status = Column(String(), server_default=TransferOwnershipStatus.PENDING) |
This file contains 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 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 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
146 changes: 146 additions & 0 deletions
146
...re/database/migrations/versions/2023_11_28_11_51-creator_id_to_applet_transfer_status_.py
This file contains 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,146 @@ | ||
"""Creator id to applet, transfer status, email encryption | ||
Revision ID: 75c9ca1f506b | ||
Revises: 93087521e7ee | ||
Create Date: 2023-11-28 11:51:29.381770 | ||
""" | ||
import uuid | ||
|
||
import sqlalchemy as sa | ||
from alembic import op | ||
from sqlalchemy.dialects import postgresql | ||
from sqlalchemy_utils.types.encrypted.encrypted_type import StringEncryptedType | ||
|
||
from apps.shared.encryption import get_key | ||
|
||
# revision identifiers, used by Alembic. | ||
revision = "75c9ca1f506b" | ||
down_revision = "93087521e7ee" | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def upgrade() -> None: | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.add_column( | ||
"applets", | ||
sa.Column("creator_id", postgresql.UUID(as_uuid=True), nullable=True), | ||
) | ||
op.create_foreign_key( | ||
op.f("fk_applets_creator_id_users"), | ||
"applets", | ||
"users", | ||
["creator_id"], | ||
["id"], | ||
ondelete="RESTRICT", | ||
) | ||
conn = op.get_bind() | ||
result = conn.execute( | ||
sa.text( | ||
f""" | ||
SELECT DISTINCT a.id, uaa.user_id, a.extra_fields->>'creator' FROM applets a | ||
JOIN user_applet_accesses uaa on a.id = uaa.applet_id | ||
WHERE a.is_deleted is false and uaa.role='owner' | ||
and uaa.is_deleted is false; | ||
""" | ||
) | ||
) | ||
for row in result: | ||
pk, owner_id, creator_id = row | ||
if creator_id: | ||
creator_id = uuid.UUID(str(creator_id) + "00000000") | ||
conn.execute( | ||
sa.text( | ||
f""" | ||
UPDATE applets | ||
SET creator_id = :creator_id | ||
WHERE id = :pk | ||
""" | ||
), | ||
{"creator_id": creator_id, "pk": pk}, | ||
) | ||
else: | ||
if owner_id: | ||
conn.execute( | ||
sa.text( | ||
f""" | ||
UPDATE applets | ||
SET creator_id = :owner_id | ||
WHERE id = :pk | ||
""" | ||
), | ||
{"owner_id": owner_id, "pk": pk}, | ||
) | ||
|
||
op.add_column( | ||
"transfer_ownership", | ||
sa.Column( | ||
"status", sa.String(), server_default="pending", nullable=True | ||
), | ||
) | ||
|
||
# encrypt email in transfer_ownership table | ||
result_emails = conn.execute( | ||
sa.text( | ||
"SELECT id, email FROM transfer_ownership WHERE email IS NOT NULL" | ||
) | ||
) | ||
op.alter_column( | ||
"transfer_ownership", | ||
"email", | ||
type_=StringEncryptedType(sa.Unicode, get_key), | ||
default=None, | ||
) | ||
for row in result_emails: | ||
pk, email = row | ||
encrypted_field = StringEncryptedType( | ||
sa.Unicode, get_key | ||
).process_bind_param(email, dialect=conn.dialect) | ||
conn.execute( | ||
sa.text( | ||
f""" | ||
UPDATE transfer_ownership | ||
SET email = :encrypted_field | ||
WHERE id = :pk | ||
""" | ||
), | ||
{"encrypted_field": encrypted_field, "pk": pk}, | ||
) | ||
# ### end Alembic commands ### | ||
|
||
|
||
def downgrade() -> None: | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.drop_column("transfer_ownership", "status") | ||
op.drop_constraint( | ||
op.f("fk_applets_creator_id_users"), "applets", type_="foreignkey" | ||
) | ||
op.drop_column("applets", "creator_id") | ||
|
||
# decrypt email in transfer_ownership table | ||
conn = op.get_bind() | ||
result_emails = conn.execute( | ||
sa.text( | ||
"SELECT id, email FROM transfer_ownership WHERE email IS NOT NULL" | ||
) | ||
) | ||
op.alter_column( | ||
"transfer_ownership", "email", type_=sa.String(), default=None | ||
) | ||
for row in result_emails: | ||
pk, email = row | ||
decrypted_field = StringEncryptedType( | ||
sa.Unicode, get_key | ||
).process_result_value(email, dialect=conn.dialect) | ||
conn.execute( | ||
sa.text( | ||
f""" | ||
UPDATE transfer_ownership | ||
SET email = :decrypted_field | ||
WHERE id = :pk | ||
""" | ||
), | ||
{"decrypted_field": decrypted_field, "pk": pk}, | ||
) | ||
# ### end Alembic commands ### |