openwebrx-clone/owrx/form/input/gfx.py

68 lines
1.9 KiB
Python
Raw Normal View History

2021-02-09 16:54:02 +00:00
from abc import ABCMeta, abstractmethod
2021-04-29 13:17:21 +00:00
from owrx.form.input import Input
2021-02-09 17:00:56 +00:00
from datetime import datetime
2021-02-08 19:30:12 +00:00
2021-02-09 16:54:02 +00:00
class ImageInput(Input, metaclass=ABCMeta):
def render_input(self, value, errors):
# TODO display errors
return """
2021-04-29 16:18:18 +00:00
<div class="imageupload" data-max-size="{maxsize}">
<input type="hidden" id="{id}" name="{id}">
<div class="image-container">
2021-02-09 16:54:02 +00:00
<img class="{classes}" src="{url}" alt="{label}"/>
</div>
<button type="button" class="btn btn-primary upload">Upload new image...</button>
<button type="button" class="btn btn-secondary restore">Restore original image</button>
</div>
""".format(
2021-04-29 16:18:18 +00:00
id=self.id,
label=self.label,
url=self.cachebuster(self.getUrl()),
classes=" ".join(self.getImgClasses()),
maxsize=self.getMaxSize(),
2021-02-09 17:00:56 +00:00
)
def cachebuster(self, url: str):
return "{url}{separator}cb={cachebuster}".format(
url=url,
cachebuster=datetime.now().timestamp(),
separator="&" if "?" in url else "?",
)
2021-02-09 16:54:02 +00:00
@abstractmethod
def getUrl(self) -> str:
pass
2021-02-08 19:30:12 +00:00
2021-02-09 16:54:02 +00:00
@abstractmethod
def getImgClasses(self) -> list:
pass
2021-04-29 16:18:18 +00:00
@abstractmethod
def getMaxSize(self) -> int:
pass
2021-02-09 16:54:02 +00:00
class AvatarInput(ImageInput):
def getUrl(self) -> str:
2021-02-13 15:44:14 +00:00
return "../static/gfx/openwebrx-avatar.png"
2021-02-09 16:54:02 +00:00
def getImgClasses(self) -> list:
return ["webrx-rx-avatar"]
2021-04-29 16:18:18 +00:00
def getMaxSize(self) -> int:
# 256 kB
return 250 * 1024
2021-02-09 16:54:02 +00:00
class TopPhotoInput(ImageInput):
def getUrl(self) -> str:
2021-02-13 15:44:14 +00:00
return "../static/gfx/openwebrx-top-photo.jpg"
2021-02-09 16:54:02 +00:00
def getImgClasses(self) -> list:
return ["webrx-top-photo"]
2021-04-29 16:18:18 +00:00
def getMaxSize(self) -> int:
# 2 MB
return 2 * 1024 * 1024