federation/federation/entities/base.py

102 wiersze
3.1 KiB
Python
Czysty Zwykły widok Historia

# -*- coding: utf-8 -*-
from datetime import datetime
from dirty_validators.basic import Email
2015-07-02 21:39:17 +00:00
class BaseEntity(object):
_required = []
2015-07-02 21:39:17 +00:00
def __init__(self, *args, **kwargs):
for key, value in kwargs.items():
if hasattr(self, key):
setattr(self, key, value)
2015-07-02 21:39:17 +00:00
def validate(self):
"""Do validation.
2015-07-02 21:39:17 +00:00
1) Loop through attributes and call their `validate_<attr>` methods, if any.
2) Check `_required` contents and make sure all attrs in there have a value.
"""
attributes = []
for attr in dir(self):
if not attr.startswith("_"):
attr_type = type(getattr(self, attr))
if attr_type != "method":
if getattr(self, "validate_{attr}".format(attr=attr), None):
getattr(self, "validate_{attr}".format(attr=attr))()
attributes.append(attr)
required_fulfilled = set(self._required).issubset(set(attributes))
if not required_fulfilled:
raise ValueError(
"Not all required attributes fulfilled. Required: {required}".format(required=self._required)
)
2015-07-02 21:39:17 +00:00
2015-07-02 22:13:42 +00:00
class GUIDMixin(BaseEntity):
guid = ""
2015-07-02 22:13:42 +00:00
def __init__(self, *args, **kwargs):
super(GUIDMixin, self).__init__(*args, **kwargs)
self._required += ["guid"]
2015-07-02 22:13:42 +00:00
def validate_guid(self):
if len(self.guid) < 16:
raise ValueError("GUID must be at least 16 characters")
class HandleMixin(BaseEntity):
handle = ""
def __init__(self, *args, **kwargs):
super(HandleMixin, self).__init__(*args, **kwargs)
self._required += ["handle"]
def validate_handle(self):
validator = Email()
if not validator.is_valid(self.handle):
raise ValueError("Handle is not valid")
2015-07-02 22:13:42 +00:00
2015-07-02 21:39:17 +00:00
class PublicMixin(BaseEntity):
public = False
2015-07-02 21:39:17 +00:00
class CreatedAtMixin(BaseEntity):
created_at = datetime.now()
def __init__(self, *args, **kwargs):
super(CreatedAtMixin, self).__init__(*args, **kwargs)
self._required += ["created_at"]
class Post(GUIDMixin, HandleMixin, PublicMixin, CreatedAtMixin, BaseEntity):
2015-07-02 21:39:17 +00:00
"""Reflects a post, status message, etc, which will be composed from the message or to the message."""
raw_content = ""
provider_display_name = ""
location = ""
photos = []
def __init__(self, *args, **kwargs):
super(Post, self).__init__(*args, **kwargs)
self._required += ["raw_content"]
2015-07-02 22:13:42 +00:00
@property
def tags(self):
"""Returns a `set` of unique tags contained in `raw_content`."""
return set({word.strip("#") for word in self.raw_content.split() if word.startswith("#")})
2015-07-02 22:13:42 +00:00
class Image(GUIDMixin, HandleMixin, PublicMixin, CreatedAtMixin, BaseEntity):
2015-07-02 22:13:42 +00:00
"""Reflects a single image, possibly linked to another object."""
remote_path = ""
remote_name = ""
text = ""
linked_type = ""
linked_guid = ""
height = 0
width = 0
def __init__(self, *args, **kwargs):
super(Image, self).__init__(*args, **kwargs)
self._required += ["remote_path", "remote_name"]