-
Notifications
You must be signed in to change notification settings - Fork 2
/
views.py
121 lines (98 loc) · 3.67 KB
/
views.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
from http import HTTPStatus
from fastapi import Depends, HTTPException, Query, Request
from fastapi.templating import Jinja2Templates
from fastapi.responses import HTMLResponse, RedirectResponse, StreamingResponse, FileResponse, Response
from lnbits.core.crud import update_payment_status
from lnbits.core.models import User
from lnbits.core.views.api import api_payment
from lnbits.decorators import check_user_exists
from . import devicetimer_ext, devicetimer_renderer, devicetimer_static_files
from .crud import get_device, get_payment, get_payment_allowed
from .models import PaymentAllowed
from loguru import logger
import pyqrcode
from io import BytesIO
import os
import httpx
templates = Jinja2Templates(directory="templates")
@devicetimer_ext.get("/", response_class=HTMLResponse)
async def index(request: Request, user: User = Depends(check_user_exists)):
return devicetimer_renderer().TemplateResponse(
"devicetimer/index.html",
{"request": request, "user": user.dict()},
)
def proxy_allowed(url: str):
if not url:
raise
if 'localhost' in url.lower():
raise
if '127.0.0.1' in url:
raise
if url.startswith("https://"):
return
raise
def default_unavailable_image() -> FileResponse:
return FileResponse(
"lnbits/extensions/devicetimer/static/image/unavailable.png",
headers={
"Cache-Control": "no-cache, no-store, must-revalidate",
"Pragma": "no-cache",
"Expires": "0"
})
@devicetimer_ext.get("/device/{deviceid}/{switchid}/qrcode")
async def devicetimer_qrcode(request: Request, deviceid: str, switchid: str):
"""
return the landing page for a device where the customer
kan initiate the feeding or when it is a allowed, an LNURL payment
"""
device = await get_device(deviceid)
if not device:
raise HTTPException(
status_code=HTTPStatus.NOT_FOUND, detail="Device does not exist"
)
switch = None
for _switch in device.switches:
if _switch.id == switchid:
switch = _switch
if not switch:
raise HTTPException(
status_code=HTTPStatus.NOT_FOUND, detail="Switch does not exist"
)
result = await get_payment_allowed(device,switch)
logger.info(f"get_payment_allowed result = {result}")
if result == PaymentAllowed.CLOSED:
try:
proxy_allowed(device.closed_url)
async with httpx.AsyncClient() as client:
response = await client.get(device.closed_url)
return Response(response.content)
except:
logger.error("Failed to retrieve image")
return default_unavailable_image()
if result == PaymentAllowed.WAIT:
try:
proxy_allowed(device.wait_url)
async with httpx.AsyncClient() as client:
response = await client.get(device.wait_url)
return Response(response.content)
except:
logger.error("Failed to retrieve image")
return default_unavailable_image()
qr = pyqrcode.create(switch.lnurl)
stream = BytesIO()
qr.svg(stream, scale=3)
stream.seek(0)
async def _generator(stream: BytesIO):
yield stream.getvalue()
return StreamingResponse(
_generator(stream),
headers={
"Content-Type": "image/svg+xml",
"Cache-Control": "no-cache, no-store, must-revalidate",
"Pragma": "no-cache",
"Expires": "0",
},
)
raise HTTPException(
status_code=HTTPStatus.NOT_FOUND, detail="Unknown Feeder state"
)