Skip to content

Commit

Permalink
eliminate dependency on rstr; fix merge issues
Browse files Browse the repository at this point in the history
  • Loading branch information
pjbedard committed Sep 12, 2024
1 parent c20218f commit 0cbfd2d
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 12 deletions.
10 changes: 6 additions & 4 deletions backend/managers/SharesManager.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
from uuid import uuid4
import random
import string
from threading import Lock
from sqlalchemy import select, insert, update, delete, func
from backend.models import Share
from backend.db import db_session_context
from backend.schemas import ShareCreateSchema, ShareSchema
from typing import List, Tuple, Optional, Dict, Any
import rstr

SHARE_ID_REGEX = '[a-z]{3}-[a-z]{3}-[a-z]{3}'
def generate_share_id():
# SHARE_ID_REGEX = '[a-z]{3}-[a-z]{3}-[a-z]{3}'
return '-'.join(''.join(random.choices(string.ascii_lowercase, k=3)) for _ in range(3))

class SharesManager:
_instance = None
Expand All @@ -31,7 +33,7 @@ async def create_share(self, resource_id, user_id, expiration_dt, is_revoked=Fal
async with db_session_context() as session:
new_key = None
while not new_key:
new_key = rstr.xeger(SHARE_ID_REGEX)
new_key = generate_share_id()
result = await session.execute(select(Share).filter(Share.id == new_key))
share = result.scalar_one_or_none()
if share: # new_key already in use
Expand Down
11 changes: 6 additions & 5 deletions backend/models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from datetime import datetime
from sqlmodel import Field
from backend.db import SQLModelBase

Expand Down Expand Up @@ -31,8 +32,8 @@ class Persona(SQLModelBase, table=True):
face_id: str | None = Field(default=None)

class Share(SQLModelBase, table=True):
id = str = Field(primary_key=True) # the short URL tag, eg abc-def-ghi
resource_id = str = Field(foreign_key="resource.id") # the bot ID
user_id = str | None = Field(default=None) # the user granted access (optional)
expiration_dt = DateTime | None = Field(default=None) # the link expiration date/time (optional)
is_revoked = Boolean = Field()
id: str = Field(primary_key=True) # the short URL tag, eg abc-def-ghi
resource_id: str = Field(foreign_key="resource.id") # the bot ID
user_id: str | None = Field(default=None) # the user granted access (optional)
expiration_dt: datetime | None = Field(default=None) # the link expiration date/time (optional)
is_revoked: bool = Field()
1 change: 0 additions & 1 deletion backend/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,3 @@ aioftp
aiofiles
structlog
greenlet
rstr
3 changes: 1 addition & 2 deletions migrations/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@
# for 'autogenerate' support
# from myapp import mymodel
# target_metadata = mymodel.Base.metadata
from backend.models import Base, Config, Resource, User, Asset, Persona, Share
target_metadata = Base.metadata
target_metadata = SQLModel.metadata

# other values from the config, defined by the needs of env.py,
# can be acquired:
Expand Down

0 comments on commit 0cbfd2d

Please sign in to comment.