Skip to content

Commit

Permalink
Merge pull request #10 from lnbits/codequality
Browse files Browse the repository at this point in the history
feat: code quality
  • Loading branch information
dni authored Aug 30, 2024
2 parents bbe3948 + c131594 commit faece84
Show file tree
Hide file tree
Showing 19 changed files with 2,837 additions and 86 deletions.
10 changes: 10 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
name: lint
on:
push:
branches:
- main
pull_request:

jobs:
lint:
uses: lnbits/lnbits/.github/workflows/lint.yml@dev
15 changes: 7 additions & 8 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
on:
push:
tags:
- "v[0-9]+.[0-9]+.[0-9]+"
- 'v[0-9]+.[0-9]+.[0-9]+'

jobs:

release:
runs-on: ubuntu-latest
steps:
Expand Down Expand Up @@ -34,12 +33,12 @@ jobs:
- name: Create pull request in extensions repo
env:
GH_TOKEN: ${{ secrets.EXT_GITHUB }}
repo_name: "${{ github.event.repository.name }}"
tag: "${{ github.ref_name }}"
branch: "update-${{ github.event.repository.name }}-${{ github.ref_name }}"
title: "[UPDATE] ${{ github.event.repository.name }} to ${{ github.ref_name }}"
body: "https://github.com/lnbits/${{ github.event.repository.name }}/releases/${{ github.ref_name }}"
archive: "https://github.com/lnbits/${{ github.event.repository.name }}/archive/refs/tags/${{ github.ref_name }}.zip"
repo_name: '${{ github.event.repository.name }}'
tag: '${{ github.ref_name }}'
branch: 'update-${{ github.event.repository.name }}-${{ github.ref_name }}'
title: '[UPDATE] ${{ github.event.repository.name }} to ${{ github.ref_name }}'
body: 'https://github.com/lnbits/${{ github.event.repository.name }}/releases/${{ github.ref_name }}'
archive: 'https://github.com/lnbits/${{ github.event.repository.name }}/archive/refs/tags/${{ github.ref_name }}.zip'
run: |
cd lnbits-extensions
git checkout -b $branch
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
__pycache__
node_modules
.mypy_cache
.venv
12 changes: 12 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"semi": false,
"arrowParens": "avoid",
"insertPragma": false,
"printWidth": 80,
"proseWrap": "preserve",
"singleQuote": true,
"trailingComma": "none",
"useTabs": false,
"bracketSameLine": false,
"bracketSpacing": false
}
47 changes: 47 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
all: format check

format: prettier black ruff

check: mypy pyright checkblack checkruff checkprettier

prettier:
poetry run ./node_modules/.bin/prettier --write .
pyright:
poetry run ./node_modules/.bin/pyright

mypy:
poetry run mypy .

black:
poetry run black .

ruff:
poetry run ruff check . --fix

checkruff:
poetry run ruff check .

checkprettier:
poetry run ./node_modules/.bin/prettier --check .

checkblack:
poetry run black --check .

checkeditorconfig:
editorconfig-checker

test:
PYTHONUNBUFFERED=1 \
DEBUG=true \
poetry run pytest
install-pre-commit-hook:
@echo "Installing pre-commit hook to git"
@echo "Uninstall the hook with poetry run pre-commit uninstall"
poetry run pre-commit install

pre-commit:
poetry run pre-commit run --all-files


checkbundle:
@echo "skipping checkbundle"
17 changes: 6 additions & 11 deletions __init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
from fastapi import APIRouter

from lnbits.db import Database
from lnbits.helpers import template_renderer

db = Database("ext_lncalendar")
from .crud import db
from .views import lncalendar_generic_router
from .views_api import lncalendar_api_router

lncalendar_ext: APIRouter = APIRouter(prefix="/lncalendar", tags=["LNCalendar"])
lncalendar_ext.include_router(lncalendar_generic_router)
lncalendar_ext.include_router(lncalendar_api_router)

lncalendar_static_files = [
{
Expand All @@ -14,10 +15,4 @@
}
]


def lncalendar_renderer():
return template_renderer(["lncalendar/templates"])


from .views import * # noqa: F401,F403
from .views_api import * # noqa: F401,F403
__all__ = ["db", "lncalendar_ext", "lncalendar_static_files"]
34 changes: 20 additions & 14 deletions crud.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
from typing import List, Optional, Union
from datetime import datetime, timedelta
from typing import Optional, Union

from lnbits.db import Database
from lnbits.helpers import urlsafe_short_hash

from . import db
from .models import (
Appointment,
CreateAppointment,
CreateSchedule,
CreateUnavailableTime,
Schedule,
UnavailableTime,
CreateUnavailableTime,
CreateAppointment,
Appointment,
)

db = Database("ext_lncalendar")


## Schedule CRUD
async def create_schedule(wallet_id: str, data: CreateSchedule) -> Schedule:
schedule_id = urlsafe_short_hash()
await db.execute(
"""
INSERT INTO lncalendar.schedule (id, wallet, name, start_day, end_day, start_time, end_time, amount)
INSERT INTO lncalendar.schedule
(id, wallet, name, start_day, end_day, start_time, end_time, amount)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
""",
(
Expand All @@ -41,7 +44,8 @@ async def create_schedule(wallet_id: str, data: CreateSchedule) -> Schedule:
async def update_schedule(schedule_id: str, data: CreateSchedule) -> Schedule:
await db.execute(
"""
UPDATE lncalendar.schedule SET name = ?, start_day = ?, end_day = ?, start_time = ?, end_time = ?, amount = ?
UPDATE lncalendar.schedule SET name = ?, start_day = ?, end_day = ?,
start_time = ?, end_time = ?, amount = ?
WHERE id = ?
""",
(
Expand All @@ -66,7 +70,7 @@ async def get_schedule(schedule_id: str) -> Optional[Schedule]:
return Schedule(**row) if row else None


async def get_schedules(wallet_ids: Union[str, List[str]]) -> List[Schedule]:
async def get_schedules(wallet_ids: Union[str, list[str]]) -> list[Schedule]:
if isinstance(wallet_ids, str):
wallet_ids = [wallet_ids]

Expand All @@ -89,7 +93,8 @@ async def create_appointment(
appointment_id = payment_hash
await db.execute(
"""
INSERT INTO lncalendar.appointment (id, name, email, info, start_time, end_time, schedule)
INSERT INTO lncalendar.appointment
(id, name, email, info, start_time, end_time, schedule)
VALUES (?, ?, ?, ?, ?, ?, ?)
""",
(
Expand All @@ -114,7 +119,7 @@ async def get_appointment(appointment_id: str) -> Optional[Appointment]:
return Appointment(**row) if row else None


async def get_appointments(schedule_id: str) -> List[Appointment]:
async def get_appointments(schedule_id: str) -> list[Appointment]:
print(schedule_id)
rows = await db.fetchall(
"SELECT * FROM lncalendar.appointment WHERE schedule = ?", (schedule_id,)
Expand All @@ -123,8 +128,8 @@ async def get_appointments(schedule_id: str) -> List[Appointment]:


async def get_appointments_wallets(
wallet_ids: Union[str, List[str]]
) -> List[Appointment]:
wallet_ids: Union[str, list[str]]
) -> list[Appointment]:
if isinstance(wallet_ids, str):
wallet_ids = [wallet_ids]

Expand Down Expand Up @@ -156,7 +161,8 @@ async def purge_appointments(schedule_id: str) -> None:
time_diff = datetime.now() - timedelta(hours=24)
await db.execute(
f"""
DELETE FROM lncalendar.appointment WHERE schedule = ? AND paid = false AND time < {db.timestamp_placeholder}
DELETE FROM lncalendar.appointment
WHERE schedule = ? AND paid = false AND time < {db.timestamp_placeholder}
""",
(
schedule_id,
Expand Down Expand Up @@ -187,7 +193,7 @@ async def get_unavailable_time(unavailable_time_id: str) -> Optional[Unavailable
return UnavailableTime(**row) if row else None


async def get_unavailable_times(schedule_id: str) -> List[UnavailableTime]:
async def get_unavailable_times(schedule_id: str) -> list[UnavailableTime]:
rows = await db.fetchall(
"SELECT * FROM lncalendar.unavailable WHERE schedule = ?", (schedule_id,)
)
Expand Down
2 changes: 1 addition & 1 deletion description.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ It's like Calendly for Bitcoin, but better!

Based on an idea by Mike Jarmuz.

(Direct Calendly intergration coming soon)
(Direct Calendly intergration coming soon)
2 changes: 1 addition & 1 deletion models.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class Schedule(BaseModel):

@property
def availabe_days(self):
return [i for i in range(self.start_day, self.end_day + 1)]
return list(range(self.start_day, self.end_day + 1))


class UnavailableTime(BaseModel):
Expand Down
59 changes: 59 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "lncalendar",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"prettier": "^3.2.5",
"pyright": "^1.1.358"
}
}
Loading

0 comments on commit faece84

Please sign in to comment.