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:
|
2022-01-24 04:07:52 +00:00
|
|
|
data["label"] = self.label or False
|
2019-04-06 12:26:42 +00:00
|
|
|
if self.labelHint:
|
2022-01-24 04:07:52 +00:00
|
|
|
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:
|
2022-01-24 04:07:52 +00:00
|
|
|
data["name"] = name
|
2019-04-06 12:26:42 +00:00
|
|
|
elif self.name:
|
2022-01-24 04:07:52 +00:00
|
|
|
data["name"] = self.name
|
2019-04-06 12:26:42 +00:00
|
|
|
else:
|
|
|
|
raise MissingNameException(cls=self.__class__)
|
|
|
|
return data
|
|
|
|
|
|
|
|
|
|
|
|
class SimpleText(BaseField):
|
2022-01-24 04:07:52 +00:00
|
|
|
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)
|
2022-01-24 04:07:52 +00:00
|
|
|
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)
|
2022-01-24 04:07:52 +00:00
|
|
|
data["inputOptions"] = {}
|
|
|
|
data["width"] = self.width
|
2019-04-06 12:26:42 +00:00
|
|
|
return data
|
|
|
|
|
|
|
|
|
|
|
|
class Image(BaseField):
|
2022-01-24 04:07:52 +00:00
|
|
|
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)
|
2022-01-24 04:07:52 +00:00
|
|
|
data["contentType"] = self.contentType
|
|
|
|
data["content"] = self.imageUrl
|
2019-04-06 12:26:42 +00:00
|
|
|
return data
|
|
|
|
|
|
|
|
|
|
|
|
class WebLink(BaseField):
|
2022-01-24 04:07:52 +00:00
|
|
|
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)
|
2022-01-24 04:07:52 +00:00
|
|
|
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)
|
2022-01-24 04:07:52 +00:00
|
|
|
data["inputOptions"] = {}
|
|
|
|
data["width"] = self.width
|
2019-04-06 12:26:42 +00:00
|
|
|
|
2022-01-24 04:07:52 +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)
|
2022-01-24 04:07:52 +00:00
|
|
|
data["inputType"] = "text"
|
2019-04-06 12:26:42 +00:00
|
|
|
|
2022-01-24 04:07:52 +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)
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
data["inputOptions"]["width"] = str(self.width)
|
2019-04-06 12:26:42 +00:00
|
|
|
|
|
|
|
if self.placeHolder:
|
2022-01-24 04:07:52 +00:00
|
|
|
data["inputOptions"]["placeHolder"] = self.placeHolder
|
2019-04-06 12:26:42 +00:00
|
|
|
return data
|
|
|
|
|
|
|
|
|
|
|
|
class Textarea(NameBaseField):
|
2022-01-24 04:07:52 +00:00
|
|
|
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)
|
2022-01-24 04:07:52 +00:00
|
|
|
data["inputType"] = "textarea"
|
|
|
|
data["inputOptions"] = {}
|
2019-04-06 12:26:42 +00:00
|
|
|
if self.rows:
|
2022-01-24 04:07:52 +00:00
|
|
|
data["inputOptions"]["rows"] = str(self.rows)
|
2019-04-06 12:26:42 +00:00
|
|
|
if self.placeHolder:
|
2022-01-24 04:07:52 +00:00
|
|
|
data["inputOptions"]["placeHolder"] = self.placeHolder
|
2019-04-06 12:26:42 +00:00
|
|
|
if self.width:
|
2022-01-24 04:07:52 +00:00
|
|
|
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)
|
2022-01-24 04:07:52 +00:00
|
|
|
data["inputType"] = "checkbox"
|
|
|
|
data["inputOptions"] = {"label": self.text}
|
2019-04-06 12:26:42 +00:00
|
|
|
return data
|
|
|
|
|
|
|
|
|
|
|
|
class Select(NameBaseField):
|
2022-01-24 04:07:52 +00:00
|
|
|
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)
|
2022-01-24 04:07:52 +00:00
|
|
|
data["inputType"] = self.type
|
2019-04-06 12:26:42 +00:00
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
data["inputOptions"] = []
|
2019-04-06 12:26:42 +00:00
|
|
|
for value, caption in self.get_choices():
|
2022-01-24 04:07:52 +00:00
|
|
|
data["inputOptions"].append({"value": value, "caption": caption})
|
2019-04-06 12:26:42 +00:00
|
|
|
|
|
|
|
return data
|
|
|
|
|
|
|
|
|
|
|
|
class Radio(Select):
|
2022-01-24 04:07:52 +00:00
|
|
|
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)
|
2022-01-24 04:07:52 +00:00
|
|
|
data["inputType"] = "imageUpload"
|
2019-04-06 12:26:42 +00:00
|
|
|
return data
|