Skip to content

Commit

Permalink
refactor: crud to not use the increment
Browse files Browse the repository at this point in the history
- code quality
- saves db fetches
- not using a crud parameter which is not persisted
  • Loading branch information
dni committed Aug 8, 2024
1 parent 4e6d61f commit 0d5123e
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 53 deletions.
22 changes: 5 additions & 17 deletions crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,35 +56,23 @@ async def create_withdraw_link(
data.custom_url,
),
)
link = await get_withdraw_link(link_id, 0)
link = await get_withdraw_link(link_id)
assert link, "Newly created link couldn't be retrieved"
return link


async def get_withdraw_link(link_id: str, num=0) -> Optional[WithdrawLink]:
async def get_withdraw_link(link_id: str) -> Optional[WithdrawLink]:
row = await db.fetchone(
"SELECT * FROM withdraw.withdraw_link WHERE id = ?", (link_id,)
)
if not row:
return None

link = dict(**row)
link["number"] = num

return WithdrawLink.parse_obj(link)
return WithdrawLink(**row) if row else None


async def get_withdraw_link_by_hash(unique_hash: str, num=0) -> Optional[WithdrawLink]:
async def get_withdraw_link_by_hash(unique_hash: str) -> Optional[WithdrawLink]:
row = await db.fetchone(
"SELECT * FROM withdraw.withdraw_link WHERE unique_hash = ?", (unique_hash,)
)
if not row:
return None

link = dict(**row)
link["number"] = num

return WithdrawLink.parse_obj(link)
return WithdrawLink(**row) if row else None


async def get_withdraw_links(
Expand Down
5 changes: 2 additions & 3 deletions models.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ class WithdrawLink(BaseModel):
open_time: int = Query(0)
used: int = Query(0)
usescsv: str = Query(None)
number: int = Query(0)
webhook_url: str = Query(None)
webhook_headers: str = Query(None)
webhook_body: str = Query(None)
Expand All @@ -43,10 +42,10 @@ class WithdrawLink(BaseModel):
def is_spent(self) -> bool:
return self.used >= self.uses

def lnurl(self, req: Request) -> Lnurl:
def lnurl(self, req: Request, num: int = 0) -> Lnurl:
if self.is_unique:
usescssv = self.usescsv.split(",")
tohash = self.id + self.unique_hash + usescssv[self.number]
tohash = self.id + self.unique_hash + usescssv[num]
multihash = shortuuid.uuid(name=tohash)
url = str(
req.url_for(
Expand Down
38 changes: 7 additions & 31 deletions views.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ async def index(request: Request, user: User = Depends(check_user_exists)):

@withdraw_ext_generic.get("/{link_id}", response_class=HTMLResponse)
async def display(request: Request, link_id):
link = await get_withdraw_link(link_id, 0)
link = await get_withdraw_link(link_id)

if not link:
raise HTTPException(
Expand All @@ -45,7 +45,7 @@ async def display(request: Request, link_id):

@withdraw_ext_generic.get("/img/{link_id}", response_class=StreamingResponse)
async def img(request: Request, link_id):
link = await get_withdraw_link(link_id, 0)
link = await get_withdraw_link(link_id)
if not link:
raise HTTPException(
status_code=HTTPStatus.NOT_FOUND, detail="Withdraw link does not exist."
Expand Down Expand Up @@ -76,26 +76,14 @@ async def print_qr(request: Request, link_id):
raise HTTPException(
status_code=HTTPStatus.NOT_FOUND, detail="Withdraw link does not exist."
)
# response.status_code = HTTPStatus.NOT_FOUND
# return "Withdraw link does not exist."

if link.uses == 0:

return withdraw_renderer().TemplateResponse(
"withdraw/print_qr.html",
{"request": request, "link": link.dict(), "unique": False},
)
links = []
count = 0

for _ in link.usescsv.split(","):
linkk = await get_withdraw_link(link_id, count)
if not linkk:
raise HTTPException(
status_code=HTTPStatus.NOT_FOUND, detail="Withdraw link does not exist."
)
links.append(str(linkk.lnurl(request)))
count = count + 1

links = [link.lnurl(request, num=i) for i in range(len(link.usescsv.split(",")))]
page_link = list(chunks(links, 2))
linked = list(chunks(page_link, 5))

Expand Down Expand Up @@ -123,26 +111,14 @@ async def csv(request: Request, link_id):
raise HTTPException(
status_code=HTTPStatus.NOT_FOUND, detail="Withdraw link does not exist."
)
# response.status_code = HTTPStatus.NOT_FOUND
# return "Withdraw link does not exist."

if link.uses == 0:

if not link.is_unique:
return withdraw_renderer().TemplateResponse(
"withdraw/csv.html",
{"request": request, "link": link.dict(), "unique": False},
)
links = []
count = 0

for _ in link.usescsv.split(","):
linkk = await get_withdraw_link(link_id, count)
if not linkk:
raise HTTPException(
status_code=HTTPStatus.NOT_FOUND, detail="Withdraw link does not exist."
)
links.append(str(linkk.lnurl(request)))
count = count + 1

links = [link.lnurl(request, num=i) for i in range(len(link.usescsv.split(",")))]
page_link = list(chunks(links, 2))
linked = list(chunks(page_link, 5))

Expand Down
4 changes: 2 additions & 2 deletions views_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ async def api_links(
async def api_link_retrieve(
link_id: str, request: Request, wallet: WalletTypeInfo = Depends(get_key_type)
):
link = await get_withdraw_link(link_id, 0)
link = await get_withdraw_link(link_id)

if not link:
raise HTTPException(
Expand Down Expand Up @@ -110,7 +110,7 @@ async def api_link_create_or_update(
) from exc

if link_id:
link = await get_withdraw_link(link_id, 0)
link = await get_withdraw_link(link_id)
if not link:
raise HTTPException(
detail="Withdraw link does not exist.", status_code=HTTPStatus.NOT_FOUND
Expand Down

0 comments on commit 0d5123e

Please sign in to comment.