Skip to content
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

feat: code quality #11

Merged
merged 2 commits into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Offline Shop - <small>[LNbits](https://github.com/lnbits/lnbits) extension</small>

<small>For more about LNBits extension check [this tutorial](https://github.com/lnbits/lnbits/wiki/LNbits-Extensions)</small>

## Create QR codes for each product and display them on your store for receiving payments Offline
Expand Down
18 changes: 6 additions & 12 deletions __init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
from fastapi import APIRouter

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

db = Database("ext_offlineshop")
from .crud import db
from .views import offlineshop_generic_router
from .views_api import offlineshop_api_router

offlineshop_static_files = [
{
Expand All @@ -13,12 +12,7 @@
]

offlineshop_ext: APIRouter = APIRouter(prefix="/offlineshop", tags=["Offlineshop"])
offlineshop_ext.include_router(offlineshop_generic_router)
offlineshop_ext.include_router(offlineshop_api_router)


def offlineshop_renderer():
return template_renderer(["offlineshop/templates"])


from .lnurl import * # noqa: F401,F403
from .views import * # noqa: F401,F403
from .views_api import * # noqa: F401,F403
__all__ = ["offlineshop_ext", "offlineshop_static_files", "db"]
18 changes: 10 additions & 8 deletions crud.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
from typing import List, Optional

from lnbits.db import SQLITE
from lnbits.db import SQLITE, Database

from . import db
from .models import Item, Shop
from .wordlists import animals

db = Database("ext_offlineshop")


async def create_shop(*, wallet_id: str) -> int:
returning = "" if db.type == SQLITE else "RETURNING ID"
method = db.execute if db.type == SQLITE else db.fetchone

result = await (method)(
result = await method(
f"""
INSERT INTO offlineshop.shops (wallet, wordlist, method)
VALUES (?, ?, 'wordlist')
Expand All @@ -25,8 +26,8 @@ async def create_shop(*, wallet_id: str) -> int:
return result[0] # type: ignore


async def get_shop(id: int) -> Optional[Shop]:
row = await db.fetchone("SELECT * FROM offlineshop.shops WHERE id = ?", (id,))
async def get_shop(shop_id: int) -> Optional[Shop]:
row = await db.fetchone("SELECT * FROM offlineshop.shops WHERE id = ?", (shop_id,))
return Shop(**row) if row else None


Expand Down Expand Up @@ -62,7 +63,8 @@ async def add_item(
) -> int:
result = await db.execute(
"""
INSERT INTO offlineshop.items (shop, name, description, image, price, unit, fiat_base_multiplier)
INSERT INTO offlineshop.items
(shop, name, description, image, price, unit, fiat_base_multiplier)
VALUES (?, ?, ?, ?, ?, ?, ?)
""",
(shop, name, description, image, price, unit, fiat_base_multiplier),
Expand Down Expand Up @@ -96,9 +98,9 @@ async def update_item(
return item_id


async def get_item(id: int) -> Optional[Item]:
async def get_item(item_id: int) -> Optional[Item]:
row = await db.fetchone(
"SELECT * FROM offlineshop.items WHERE id = ? LIMIT 1", (id,)
"SELECT * FROM offlineshop.items WHERE id = ? LIMIT 1", (item_id,)
)
return Item.from_row(row) if row else None

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 @@ LNbits Offline Shop allows for merchants to receive Bitcoin payments while offli

Merchant will create items and associate a QR code (a LNURLp) with a price. He can then print the QR codes and display them on their shop. When a customer chooses an item, scans the QR code, gets the description and price. After payment, the customer gets a confirmation code that the merchant can validate to be sure the payment was successful.

Customers must use an LNURL pay capable wallet.
Customers must use an LNURL pay capable wallet.
14 changes: 7 additions & 7 deletions manifest.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"repos": [
{
"id": "offlineshop",
"organisation": "lnbits",
"repository": "offlineshop"
}
]
"repos": [
{
"id": "offlineshop",
"organisation": "lnbits",
"repository": "offlineshop"
}
]
}
4 changes: 3 additions & 1 deletion migrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,7 @@ async def m002_fiat_base_multiplier(db):
remember to multiply by 100 when we use it to convert to Dollars.
"""
await db.execute(
"ALTER TABLE offlineshop.items ADD COLUMN fiat_base_multiplier INTEGER DEFAULT 1;"
"""
ALTER TABLE offlineshop.items ADD COLUMN fiat_base_multiplier INTEGER DEFAULT 1
"""
)
21 changes: 3 additions & 18 deletions models.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from typing import Dict, List, Optional

from lnurl import encode as lnurl_encode
from lnurl.models import ClearnetUrl, Max144Str, UrlAction
from lnurl.types import LnurlPayMetadata
from pydantic import BaseModel
from starlette.requests import Request
Expand Down Expand Up @@ -104,7 +103,9 @@ def from_row(cls, row: Row) -> "Item":
return cls(**data)

def lnurl(self, req: Request) -> str:
return lnurl_encode(str(req.url_for("offlineshop.lnurl_response", item_id=self.id)))
return lnurl_encode(
str(req.url_for("offlineshop.lnurl_response", item_id=self.id))
)

def values(self, req: Request):
values = self.dict()
Expand All @@ -120,19 +121,3 @@ async def lnurlpay_metadata(self) -> LnurlPayMetadata:
metadata.append(self.image.split(":")[1].split(","))

return LnurlPayMetadata(json.dumps(metadata))

def success_action(
self, shop: Shop, payment_hash: str, req: Request
) -> Optional[UrlAction]:
if not shop.wordlist:
return None

return UrlAction(
url=ClearnetUrl(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why remove ClearnetUrl?

str(req.url_for("offlineshop.confirmation_code", p=payment_hash)),
scheme="https",
),
description=Max144Str(
"Open to get the confirmation code for your purchase."
),
)
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": "offlineshop",
"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