kopia lustrzana https://gitlab.com/jaywink/federation
More modelling of entities
rodzic
4b4661423e
commit
7935417275
|
@ -1,4 +1,5 @@
|
|||
from federation.entities.fields import TextField, GUIDField, HandleField, BooleanField, DateTimeField
|
||||
from federation.entities.fields import TextField, GUIDField, HandleField, BooleanField, DateTimeField, ListField, \
|
||||
IntegerField
|
||||
|
||||
|
||||
class GUIDMixin(object):
|
||||
|
@ -9,13 +10,32 @@ class HandleMixin(object):
|
|||
handle = HandleField(required=True)
|
||||
|
||||
|
||||
class PublicMixin(object):
|
||||
public = BooleanField()
|
||||
|
||||
|
||||
class CreatedAtMixin(object):
|
||||
created_at = DateTimeField(required=True)
|
||||
|
||||
|
||||
class BaseEntity(object):
|
||||
pass
|
||||
|
||||
|
||||
class PostEntity(GUIDMixin, HandleMixin, BaseEntity):
|
||||
class PostEntity(GUIDMixin, HandleMixin, PublicMixin, CreatedAtMixin, BaseEntity):
|
||||
"""Reflects a post, status message, etc, which will be composed from the message or to the message."""
|
||||
raw_content = TextField(required=True)
|
||||
public = BooleanField()
|
||||
created_at = DateTimeField(required=True)
|
||||
provider_display_name = TextField() # For example, client info
|
||||
location = TextField() # Free text of a location
|
||||
photos = ListField(type_of=ImageEntity)
|
||||
|
||||
|
||||
class ImageEntity(GUIDMixin, HandleMixin, PublicMixin, CreatedAtMixin, BaseEntity):
|
||||
"""Reflects a single image, possibly linked to another object."""
|
||||
remote_path = TextField(required=True)
|
||||
remote_name = TextField(required=True)
|
||||
text = TextField()
|
||||
linked_type = TextField(default="post")
|
||||
linked_guid = GUIDField()
|
||||
height = IntegerField()
|
||||
width = IntegerField()
|
||||
|
|
|
@ -4,10 +4,9 @@ from dirty_validators.basic import Email
|
|||
|
||||
|
||||
class BaseField(object):
|
||||
value = None
|
||||
|
||||
def __init__(self, required=False):
|
||||
def __init__(self, required=False, default=None):
|
||||
self.required = required
|
||||
self.value = default
|
||||
|
||||
def __setattr__(self, key, value):
|
||||
if key == "value":
|
||||
|
@ -18,10 +17,8 @@ class BaseField(object):
|
|||
|
||||
class TextField(BaseField):
|
||||
"""A base field with value."""
|
||||
value = ""
|
||||
|
||||
def __init__(self, required=False, min_length=None):
|
||||
super(TextField, self).__init__(required)
|
||||
def __init__(self, min_length=None, *args, **kwargs):
|
||||
super(TextField, self).__init__(*args, **kwargs)
|
||||
self.min_length = min_length
|
||||
|
||||
def __setattr__(self, key, value):
|
||||
|
@ -57,6 +54,15 @@ class BooleanField(BaseField):
|
|||
return super(BooleanField, self).__setattr__(key, value)
|
||||
|
||||
|
||||
class IntegerField(BaseField):
|
||||
"""An integer field."""
|
||||
def __setattr__(self, key, value):
|
||||
if key == "value":
|
||||
if not isinstance(value, int) and value is not None:
|
||||
raise ValueError("Value must be an int")
|
||||
return super(IntegerField, self).__setattr__(key, value)
|
||||
|
||||
|
||||
class DateTimeField(BaseField):
|
||||
"""A field with datetime."""
|
||||
def __setattr__(self, key, value):
|
||||
|
@ -64,3 +70,21 @@ class DateTimeField(BaseField):
|
|||
if not isinstance(value, datetime):
|
||||
raise ValueError("Value must be a datetime object")
|
||||
return super(DateTimeField, self).__setattr__(key, value)
|
||||
|
||||
|
||||
class ListField(BaseField):
|
||||
"""A field where value is a list of something."""
|
||||
def __init__(self, type_of=None, *args, **kwargs):
|
||||
self.type_of = type_of
|
||||
super(ListField, self).__init__(*args, **kwargs)
|
||||
|
||||
def __setattr__(self, key, value):
|
||||
if key == "value" and value is not None:
|
||||
if not isinstance(value, list):
|
||||
raise ValueError("Value must be a list object")
|
||||
return super(ListField, self).__setattr__(key, value)
|
||||
|
||||
def append(self, obj):
|
||||
if self.type_of and not isinstance(obj, self.type_of):
|
||||
raise ValueError("Value must be a %s object" % self.type_of.__name__)
|
||||
return self.value.append(obj)
|
||||
|
|
Ładowanie…
Reference in New Issue