def test_on_the_fly(parent_model): with pytest.raises(TypeError): parent_model.self_base = Field() obj = parent_model() with pytest.raises(TypeError): obj.self_base = Field()
class Webhook(ViberModel): """Model for work with Viber webhook Params: https://developers.viber.com/docs/api/rest-bot-api/#resource-url Remove webhook: https://developers.viber.com/docs/api/rest-bot-api/#removing-your-webhook Response: https://developers.viber.com/docs/api/rest-bot-api/#set-webhook-response Event types: https://developers.viber.com/docs/api/rest-bot-api/#event-types-filtering """ url = Field(base=str) event_types = ListField(item_base=str) send_name = Field(base=bool) send_photo = Field(base=bool) def __init__(self, url: str, event_types: Optional[list] = None, send_name: Optional[bool] = None, send_photo: Optional[bool] = None, **kwargs): super().__init__(url=url, event_types=event_types, send_name=send_name, send_photo=send_photo, **kwargs)
class FileMessage(Message): """Represents a Viber file message object Params: https://developers.viber.com/docs/api/rest-bot-api/#file-message """ message_type = Field(default='file', alias='type') media = Field(base=str) size = Field(base=int) file_name = Field(base=str) def __init__( self, media: str, size: int, file_name: str, tracking_data: Optional[str] = None, keyboard: Optional[Keyboard] = None, **kwargs ): super().__init__( media=media, size=size, file_name=file_name, tracking_data=tracking_data, keyboard=keyboard, **kwargs )
class PictureMessage(Message): """Represents a Viber picture message object Params: https://developers.viber.com/docs/api/rest-bot-api/#picture-message """ message_type = Field(default='picture', alias='type') text = Field(base=str) media = Field(base=str) thumbnail = Field(base=str) def __init__(self, text: str, media: str, thumbnail: Optional[str] = None, tracking_data: Optional[str] = None, keyboard: Optional[Keyboard] = None, **kwargs): super().__init__(text=text, media=media, thumbnail=thumbnail, tracking_data=tracking_data, keyboard=keyboard, **kwargs)
class VideoMessage(Message): """Represents a Viber video message object Params: https://developers.viber.com/docs/api/rest-bot-api/#video-message """ message_type = Field(default='video', alias='type') media = Field(base=str) size = Field(base=int) duration = Field(base=int) thumbnail = Field(base=str) def __init__(self, media: str, size: int, duration: Optional[int] = None, thumbnail: Optional[str] = None, tracking_data: Optional[str] = None, keyboard: Optional[Keyboard] = None, **kwargs): super().__init__(media=media, size=size, duration=duration, thumbnail=thumbnail, tracking_data=tracking_data, keyboard=keyboard, **kwargs)
class RichMediaMessage(Message): """Represents a Viber rich media message object Params: https://developers.viber.com/docs/api/rest-bot-api/#rich-media-message--carousel-content-message """ @property def min_api_version(self) -> int: return 7 message_type = Field(default='rich_media', alias='type') rich_media = Field(base=RichMedia) alt_text = Field(base=str) def __init__(self, rich_media: Union[RichMedia, dict], alt_text: str = None, tracking_data: Optional[str] = None, keyboard: Optional[Keyboard] = None, **kwargs): super().__init__(rich_media=rich_media, alt_text=alt_text, tracking_data=tracking_data, keyboard=keyboard, **kwargs)
class MediaPlayer(ViberModel): """Represents a Viber media player object Params: https://developers.viber.com/docs/tools/keyboards/#buttons-parameters """ @property def min_api_version(self) -> int: return 6 title = Field(base=str, alias='Title') subtitle = Field(base=str, alias='Subtitle') thumbnail_url = Field(base=str, alias='ThumbnailURL') loop = Field(base=bool, alias='Loop') def __init__(self, title: str = None, subtitle: str = None, thumbnail_url: str = None, loop: bool = None, **kwargs): super().__init__(title=title, subtitle=subtitle, thumbnail_url=thumbnail_url, loop=loop, **kwargs)
class ParentModel(Model): self_base = Field(base=int, self_base=True) field = Field(base=int, alias='parent field', default=1) model = Field(base=nested_model) int_list = ListField(item_base=int) model_list = ListField(item_base=nested_model) simple_list = ListField() date_field = Field(base=datetime) datetime_field = DateTimeField() dateformat_field = DateTimeField(date_format='%H:%M:%S')
class Keyboard(ViberModel): """Represents a Viber keyboard object Keyboards: https://developers.viber.com/docs/tools/keyboards/ Keyboards examples: https://developers.viber.com/docs/tools/keyboard-examples/ """ @property def min_api_version(self) -> int: if self.favorites_metadata is not None: return 6 elif self.buttons_group_columns is not None or \ self.buttons_group_rows is not None or \ self.input_field_state is not None: return 4 elif self.custom_default_height is not None or self.height_scale is not None: return 3 else: return 1 kb_type = Field(default='keyboard', alias='Type') buttons = ListField(item_base=Button, default=[], alias='Buttons') bg_color = Field(base=str, alias='BgColor') default_height = Field(base=bool, alias='DefaultHeight') custom_default_height = Field(base=int, alias='CustomDefaultHeight') height_scale = Field(base=int, alias='HeightScale') buttons_group_columns = Field(base=int, alias='ButtonsGroupColumns') buttons_group_rows = Field(base=int, alias='ButtonsGroupRows') input_field_state = Field(base=str, alias='InputFieldState') favorites_metadata = Field(base=FavoritesMetadata, alias='FavoritesMetadata') def __init__( self, buttons: list = None, bg_color: str = None, default_height: bool = None, custom_default_height: int = None, height_scale: int = None, buttons_group_columns: int = None, buttons_group_rows: int = None, input_field_state: str = None, favorites_metadata: Union[FavoritesMetadata, dict, None] = None, **kwargs ): super().__init__( buttons=buttons, bg_color=bg_color, default_height=default_height, custom_default_height=custom_default_height, height_scale=height_scale, buttons_group_columns=buttons_group_columns, buttons_group_rows=buttons_group_rows, input_field_state=input_field_state, favorites_metadata=favorites_metadata, **kwargs )
class Message(ViberModel): """Represents a Viber message object with general message parameters Params: https://developers.viber.com/docs/api/rest-bot-api/#general-send-message-parameters """ tracking_data = Field() keyboard = Field(base=Keyboard) @property @abstractmethod def message_type(self): raise NotImplementedError
class Map(ViberModel): """Represents a Viber map object Params: https://developers.viber.com/docs/tools/keyboards/#buttons-parameters """ @property def min_api_version(self) -> int: return 6 latitude = Field(alias='Latitude') longitude = Field(alias='Longitude') def __init__(self, latitude: str = None, longitude: str = None, **kwargs): super().__init__(latitude=latitude, longitude=longitude, **kwargs)
class Sender(ViberModel): """Represents a Viber sender object Params: https://developers.viber.com/docs/api/rest-bot-api/#general-send-message-parameters """ name = Field(base=str) avatar = Field(base=str) def __init__(self, name: Optional[str], avatar: Optional[str] = None, **kwargs): super().__init__(name=name, avatar=avatar, **kwargs)
class InternalBrowser(ViberModel): """Represents a Viber internal browser object Params: https://developers.viber.com/docs/tools/keyboards/#buttons-parameters """ @property def min_api_version(self) -> int: return 3 if self.action_reply_data is None else 6 action_button = Field(base=str, alias='ActionButton') action_predefined_url = Field(base=str, alias='ActionPredefinedURL') title_type = Field(base=str, alias='TitleType') custom_title = Field(base=str, alias='CustomTitle') mode = Field(base=str, alias='Mode') footer_type = Field(base=str, alias='FooterType') action_reply_data = Field(base=str, alias='ActionReplyData') def __init__(self, action_button: str = None, action_predefined_url: str = None, title_type: str = None, custom_title: str = None, mode: str = None, footer_type: str = None, action_reply_data: str = None, **kwargs): super().__init__(action_button=action_button, action_predefined_url=action_predefined_url, title_type=title_type, custom_title=custom_title, mode=mode, footer_type=footer_type, action_reply_data=action_reply_data, **kwargs)
class StickerMessage(Message): """Represents a Viber sticker message object https://developers.viber.com/docs/api/rest-bot-api/#sticker-message """ message_type = Field(default='sticker', alias='type') sticker_id = Field(base=int) def __init__(self, sticker_id: int, tracking_data: Optional[str] = None, keyboard: Optional[Keyboard] = None, **kwargs): super().__init__(sticker_id=sticker_id, tracking_data=tracking_data, keyboard=keyboard, **kwargs)
class LocationMessage(Message): """Represents a Viber location message object Params: https://developers.viber.com/docs/api/rest-bot-api/#location-message """ message_type = Field(default='location', alias='type') location = Field(base=Location) def __init__(self, location: Location, tracking_data: Optional[str] = None, keyboard: Optional[Keyboard] = None, **kwargs): super().__init__(**kwargs) self.location = location self.tracking_data = tracking_data self.keyboard = keyboard
class UrlMessage(Message): """Represents a Viber url message object Params: https://developers.viber.com/docs/api/rest-bot-api/#url-message """ message_type = Field(default='url', alias='type') media = Field(base=str) def __init__(self, media: str, tracking_data: Optional[str] = None, keyboard: Optional[Keyboard] = None, **kwargs): super().__init__(media=media, tracking_data=tracking_data, keyboard=keyboard, **kwargs)
class TextMessage(Message): """Represents a Viber text message object Params: https://developers.viber.com/docs/api/rest-bot-api/#text-message """ message_type = Field(default='text', alias='type') text = Field(base=str) def __init__(self, text: str, tracking_data: Optional[str] = None, keyboard: Optional[Keyboard] = None, **kwargs): super().__init__(text=text, tracking_data=tracking_data, keyboard=keyboard, **kwargs)
class ContactMessage(Message): """Represents a Viber contact message object Params: https://developers.viber.com/docs/api/rest-bot-api/#contact-message """ message_type = Field(default='contact', alias='type') contact = Field(base=Contact) def __init__(self, contact: Union[Contact, dict], tracking_data: Optional[str] = None, keyboard: Optional[Keyboard] = None, **kwargs): super().__init__(contact=contact, tracking_data=tracking_data, keyboard=keyboard, **kwargs)
class Contact(ViberModel): """Represents a Viber contact object Params: https://developers.viber.com/docs/api/rest-bot-api/#contact-message """ name = Field(base=str) phone_number = Field(base=str) def __init__( self, name: str, phone_number: str, **kwargs ): super().__init__( name=name, phone_number=phone_number, **kwargs )
class Location(ViberModel): """Represents a Viber location object Params: https://developers.viber.com/docs/api/rest-bot-api/#location-message """ latitude = Field(base=str, alias='lat') longitude = Field(base=str, alias='lon') def __init__( self, latitude: str = None, longitude: str = None, **kwargs ): super().__init__( latitude=latitude, longitude=longitude, **kwargs )
class RichMedia(ViberModel): """Represents a Viber rich media object Params: https://developers.viber.com/docs/api/rest-bot-api/#rich-media-message--carousel-content-message Keyboards: https://developers.viber.com/docs/tools/keyboards/ Keyboards examples: https://developers.viber.com/docs/tools/keyboard-examples/ Favorites metadata: https://developers.viber.com/docs/tools/keyboards/#favoritesMetadata """ buttons = ListField(item_base=Button, default=[], alias='Buttons') bg_color = Field(base=str, alias='BgColor') media_type = Field(default='rich_media', alias='Type') buttons_group_columns = Field(base=int, alias='ButtonsGroupColumns') buttons_group_rows = Field(base=int, alias='ButtonsGroupRows') favorites_metadata = Field(base=FavoritesMetadata, alias='FavoritesMetadata') def __init__(self, buttons: list = None, bg_color: str = None, buttons_group_columns: int = None, buttons_group_rows: int = None, favorites_metadata: Union[FavoritesMetadata, dict, None] = None, **kwargs): super().__init__(buttons=buttons, bg_color=bg_color, buttons_group_columns=buttons_group_columns, buttons_group_rows=buttons_group_rows, favorites_metadata=favorites_metadata, **kwargs)
class Frame(ViberModel): """Represents a Viber frame object Params: https://developers.viber.com/docs/tools/keyboards/#buttons-parameters """ @property def min_api_version(self) -> int: return 6 border_width = Field(base=int, alias='BorderWidth') border_color = Field(base=str, alias='BorderColor') corner_radius = Field(base=int, alias='CornerRadius') def __init__(self, border_width: int = None, border_color: str = None, corner_radius: int = None, **kwargs): super().__init__(border_width=border_width, border_color=border_color, corner_radius=corner_radius, **kwargs)
class FavoritesMetadata(ViberModel): """ Represents a Viber favorites metadata object Params: https://developers.viber.com/docs/tools/keyboards/#favorites-metadata """ data_type = Field(base=str, alias='type') url = Field(base=str) title = Field(base=str) thumbnail = Field(base=str) domain = Field(base=str) width = Field(base=int) height = Field(base=int) alternative_url = Field(base=str, alias='alternativeUrl') alternative_text = Field(base=str, alias='alternativeText') def __init__(self, data_type: str = None, url: str = None, title: str = None, thumbnail: str = None, domain: str = None, width: int = None, height: int = None, alternative_url: str = None, alternative_text: str = None, **kwargs): super().__init__(data_type=data_type, url=url, title=title, thumbnail=thumbnail, domain=domain, width=width, height=height, alternative_url=alternative_url, alternative_text=alternative_text, **kwargs)
class Button(ViberModel): """Represents a Viber button object Params: https://developers.viber.com/docs/tools/keyboard-examples/ """ @property def min_api_version(self) -> int: if self.bg_media_scale_type is not None or \ self.image_scale_type is not None or \ self.text_should_fit is not None: return 6 elif self.text_paddings is not None: return 4 else: return 1 columns = Field(base=int, alias='Columns') rows = Field(base=int, alias='Rows') bg_color = Field(base=str, alias='BgColor') silent = Field(base=bool, alias='Silent') bg_media_type = Field(base=str, alias='BgMediaType') bg_media = Field(base=str, alias='BgMedia') bg_media_scale_type = Field(base=str, alias='BgMediaScaleType') image_scale_type = Field(base=str, alias='ImageScaleType') bg_loop = Field(base=bool, alias='BgLoop') action_type = Field(base=str, alias='ActionType') action_body = Field(base=str, alias='ActionBody') image = Field(base=str, alias='Image') text = Field(base=str, alias='Text') text_v_align = Field(base=str, alias='TextVAlign') text_h_align = Field(base=str, alias='TextHAlign') text_paddings = ListField(item_base=int, alias='TextPaddings') text_opacity = Field(base=int, alias='TextOpacity') text_size = Field(base=str, alias='TextSize') open_url_type = Field(base=str, alias='OpenURLType') open_url_media_type = Field(base=str, alias='OpenURLMediaType') text_bg_gradient_color = Field(base=str, alias='TextBgGradientColor') text_should_fit = Field(base=bool, alias='TextShouldFit') internal_browser = Field(base=InternalBrowser, alias='InternalBrowser') open_map = Field(base=Map, alias='Map') frame = Field(base=Frame, alias='Frame') media_player = Field(base=MediaPlayer, alias='MediaPlayer') def __init__(self, columns: int = None, rows: int = None, bg_color: str = None, silent: bool = None, bg_media_type: str = None, bg_media: str = None, bg_media_scale_type: str = None, image_scale_type: str = None, bg_loop: bool = None, action_type: str = None, action_body: str = None, image: str = None, text: str = None, text_v_align: str = None, text_h_align: str = None, text_paddings: list = None, text_opacity: int = None, text_size: str = None, open_url_type: str = None, open_url_media_type: str = None, text_bg_gradient_color: str = None, text_should_fit: bool = None, internal_browser: Union[InternalBrowser, dict, None] = None, open_map: Union[Map, dict, None] = None, frame: Union[Frame, dict, None] = None, media_player: Union[MediaPlayer, dict, None] = None, **kwargs): super().__init__(columns=columns, rows=rows, bg_color=bg_color, silent=silent, bg_media_type=bg_media_type, bg_media=bg_media, bg_media_scale_type=bg_media_scale_type, image_scale_type=image_scale_type, bg_loop=bg_loop, action_type=action_type, action_body=action_body, image=image, text=text, text_v_align=text_v_align, text_h_align=text_h_align, text_paddings=text_paddings, text_opacity=text_opacity, text_size=text_size, open_url_type=open_url_type, open_url_media_type=open_url_media_type, text_bg_gradient_color=text_bg_gradient_color, text_should_fit=text_should_fit, internal_browser=internal_browser, open_map=open_map, frame=frame, media_player=media_player, **kwargs)
class OrderModel(Model): user = Field(base=UserModel) paid = Field(base=bool, default=False) cart = ListField(item_base=CartItem, default=[], alias='items') order_date = DateTimeField()
class UserModel(Model): name = Field() surname = Field() phone = Field()
class CartItem(Item): quantity = Field(base=int) subtotal = Field()
class Item(Model): name = Field() item_id = Field(alias='id')
class TestModel(ViberModel): field = Field(default='field')
class NestedModel(Model): field = Field(default=1)