bazarr/libs/python_anticaptcha/fields.py

200 lines
5.3 KiB
Python
Raw Normal View History

2019-04-06 12:26:42 +00:00
import six
from python_anticaptcha.exceptions import InvalidWidthException, MissingNameException
class BaseField(object):
label = None
labelHint = None
def serialize(self, name=None):
data = {}
if self.label:
data["label"] = self.label or False
2019-04-06 12:26:42 +00:00
if self.labelHint:
data["labelHint"] = self.labelHint or False
2019-04-06 12:26:42 +00:00
return data
class NameBaseField(BaseField):
name = None
def serialize(self, name=None):
data = super(NameBaseField, self).serialize(name)
if name:
data["name"] = name
2019-04-06 12:26:42 +00:00
elif self.name:
data["name"] = self.name
2019-04-06 12:26:42 +00:00
else:
raise MissingNameException(cls=self.__class__)
return data
class SimpleText(BaseField):
contentType = "text"
2019-04-06 12:26:42 +00:00
def __init__(self, content, label=None, labelHint=None, width=None):
self.label = label
self.labelHint = labelHint
self.content = content
self.width = width
def serialize(self, name=None):
data = super(SimpleText, self).serialize(name)
data["contentType"] = self.contentType
data["content"] = self.content
2019-04-06 12:26:42 +00:00
if self.width:
if self.width not in [100, 50, 33, 25]:
raise InvalidWidthException(self.width)
data["inputOptions"] = {}
data["width"] = self.width
2019-04-06 12:26:42 +00:00
return data
class Image(BaseField):
contentType = "image"
2019-04-06 12:26:42 +00:00
def __init__(self, imageUrl, label=None, labelHint=None):
self.label = label
self.labelHint = labelHint
self.imageUrl = imageUrl
def serialize(self, name=None):
data = super(Image, self).serialize(name)
data["contentType"] = self.contentType
data["content"] = self.imageUrl
2019-04-06 12:26:42 +00:00
return data
class WebLink(BaseField):
contentType = "link"
2019-04-06 12:26:42 +00:00
def __init__(self, linkText, linkUrl, label=None, labelHint=None, width=None):
self.label = label
self.labelHint = labelHint
self.linkText = linkText
self.linkUrl = linkUrl
self.width = width
def serialize(self, name=None):
data = super(WebLink, self).serialize(name)
data["contentType"] = self.contentType
2019-04-06 12:26:42 +00:00
if self.width:
if self.width not in [100, 50, 33, 25]:
raise InvalidWidthException(self.width)
data["inputOptions"] = {}
data["width"] = self.width
2019-04-06 12:26:42 +00:00
data.update({"content": {"url": self.linkUrl, "text": self.linkText}})
2019-04-06 12:26:42 +00:00
return data
class TextInput(NameBaseField):
def __init__(self, placeHolder=None, label=None, labelHint=None, width=None):
self.label = label
self.labelHint = labelHint
self.placeHolder = placeHolder
self.width = width
def serialize(self, name=None):
data = super(TextInput, self).serialize(name)
data["inputType"] = "text"
2019-04-06 12:26:42 +00:00
data["inputOptions"] = {}
2019-04-06 12:26:42 +00:00
if self.width:
if self.width not in [100, 50, 33, 25]:
raise InvalidWidthException(self.width)
data["inputOptions"]["width"] = str(self.width)
2019-04-06 12:26:42 +00:00
if self.placeHolder:
data["inputOptions"]["placeHolder"] = self.placeHolder
2019-04-06 12:26:42 +00:00
return data
class Textarea(NameBaseField):
def __init__(
self, placeHolder=None, rows=None, label=None, width=None, labelHint=None
):
2019-04-06 12:26:42 +00:00
self.label = label
self.labelHint = labelHint
self.placeHolder = placeHolder
self.rows = rows
self.width = width
def serialize(self, name=None):
data = super(Textarea, self).serialize(name)
data["inputType"] = "textarea"
data["inputOptions"] = {}
2019-04-06 12:26:42 +00:00
if self.rows:
data["inputOptions"]["rows"] = str(self.rows)
2019-04-06 12:26:42 +00:00
if self.placeHolder:
data["inputOptions"]["placeHolder"] = self.placeHolder
2019-04-06 12:26:42 +00:00
if self.width:
data["inputOptions"]["width"] = str(self.width)
2019-04-06 12:26:42 +00:00
return data
class Checkbox(NameBaseField):
def __init__(self, text, label=None, labelHint=None):
self.label = label
self.labelHint = labelHint
self.text = text
def serialize(self, name=None):
data = super(Checkbox, self).serialize(name)
data["inputType"] = "checkbox"
data["inputOptions"] = {"label": self.text}
2019-04-06 12:26:42 +00:00
return data
class Select(NameBaseField):
type = "select"
2019-04-06 12:26:42 +00:00
def __init__(self, label=None, choices=None, labelHint=None):
self.label = label
self.labelHint = labelHint
self.choices = choices or ()
def get_choices(self):
for choice in self.choices:
if isinstance(choice, six.text_type):
yield choice, choice
else:
yield choice
def serialize(self, name=None):
data = super(Select, self).serialize(name)
data["inputType"] = self.type
2019-04-06 12:26:42 +00:00
data["inputOptions"] = []
2019-04-06 12:26:42 +00:00
for value, caption in self.get_choices():
data["inputOptions"].append({"value": value, "caption": caption})
2019-04-06 12:26:42 +00:00
return data
class Radio(Select):
type = "radio"
2019-04-06 12:26:42 +00:00
class ImageUpload(NameBaseField):
def __init__(self, label=None, labelHint=None):
self.label = label
self.labelHint = labelHint
def serialize(self, name=None):
data = super(ImageUpload, self).serialize(name)
data["inputType"] = "imageUpload"
2019-04-06 12:26:42 +00:00
return data