2017-09-20 22:30:38 +10:00
|
|
|
# Eos - Verifiable elections
|
|
|
|
# Copyright © 2017 RunasSudo (Yingtong Li)
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Affero General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
try:
|
|
|
|
__pragma__ = __pragma__
|
|
|
|
is_python = False
|
|
|
|
except:
|
|
|
|
is_python = True
|
|
|
|
def __pragma__(*args):
|
|
|
|
pass
|
|
|
|
|
2017-09-25 17:18:02 +10:00
|
|
|
# Libraries
|
|
|
|
# =========
|
|
|
|
|
2017-12-07 15:32:39 +11:00
|
|
|
import eos.core.db
|
|
|
|
|
2017-09-20 22:30:38 +10:00
|
|
|
if is_python:
|
|
|
|
__pragma__('skip')
|
2017-11-27 19:40:01 +11:00
|
|
|
import eos.core.db.mongodb
|
2017-11-27 20:12:31 +11:00
|
|
|
import eos.core.db.postgresql
|
2017-11-27 19:40:01 +11:00
|
|
|
|
2017-09-25 17:18:02 +10:00
|
|
|
from bson.binary import UUIDLegacy
|
|
|
|
|
|
|
|
import base64
|
2017-11-24 20:26:18 +11:00
|
|
|
from datetime import datetime
|
2017-09-25 17:18:02 +10:00
|
|
|
import hashlib
|
2017-12-12 20:32:02 +11:00
|
|
|
import importlib
|
2017-09-25 17:18:02 +10:00
|
|
|
import json
|
2017-12-13 15:21:22 +11:00
|
|
|
import pytz
|
2017-09-25 17:18:02 +10:00
|
|
|
import uuid
|
2017-09-20 22:30:38 +10:00
|
|
|
__pragma__('noskip')
|
|
|
|
else:
|
2017-09-28 20:02:20 +10:00
|
|
|
# Load json.js
|
2017-09-25 17:18:02 +10:00
|
|
|
lib = __pragma__('js', '''
|
|
|
|
(function() {{
|
|
|
|
{}
|
|
|
|
var exports = {{}};
|
|
|
|
exports.stringify = stringify_main;
|
|
|
|
return exports;
|
2017-09-28 20:02:20 +10:00
|
|
|
}})()''', __include__('eos/core/objects/json.js'))
|
2017-09-25 17:18:02 +10:00
|
|
|
|
|
|
|
# Database
|
|
|
|
# ========
|
|
|
|
|
2017-11-25 22:37:20 +11:00
|
|
|
class DBInfo:
|
|
|
|
def __init__(self):
|
2017-12-07 15:32:39 +11:00
|
|
|
self.provider = eos.core.db.DummyProvider(None, None)
|
2017-11-25 22:37:20 +11:00
|
|
|
|
|
|
|
dbinfo = DBInfo()
|
|
|
|
|
2017-11-27 19:40:01 +11:00
|
|
|
def db_connect(db_name, db_uri='mongodb://localhost:27017/', db_type='mongodb'):
|
2017-12-07 18:32:01 +11:00
|
|
|
if is_python:
|
|
|
|
dbinfo.provider = eos.core.db.db_providers[db_type](db_name, db_uri)
|
|
|
|
dbinfo.provider.connect()
|
2017-09-25 17:18:02 +10:00
|
|
|
|
|
|
|
# Fields
|
|
|
|
# ======
|
|
|
|
|
|
|
|
class Field:
|
|
|
|
def __init__(self, *args, **kwargs):
|
2017-11-23 20:42:22 +11:00
|
|
|
self.default = kwargs['default'] if 'default' in kwargs else kwargs['py_default'] if 'py_default' in kwargs else None
|
|
|
|
self.is_protected = kwargs['is_protected'] if 'is_protected' in kwargs else False
|
|
|
|
self.is_hashed = kwargs['is_hashed'] if 'is_hashed' in kwargs else not self.is_protected
|
2017-12-15 20:21:03 +11:00
|
|
|
|
|
|
|
def object_get(self, obj):
|
|
|
|
return obj._field_values[self.real_name]
|
|
|
|
|
|
|
|
def object_set(self, obj, value):
|
|
|
|
obj._field_values[self.real_name] = value
|
|
|
|
|
|
|
|
if isinstance(value, EosObject):
|
|
|
|
value._instance = (obj, self.real_name)
|
|
|
|
if not value._inited:
|
|
|
|
value.post_init()
|
2017-12-15 20:51:57 +11:00
|
|
|
|
|
|
|
def object_init(self, obj, value):
|
|
|
|
self.object_set(obj, value)
|
2017-12-15 20:21:03 +11:00
|
|
|
|
|
|
|
class SerialiseOptions:
|
|
|
|
def __init__(self, for_hash=False, should_protect=False, combine_related=False):
|
|
|
|
self.for_hash = for_hash
|
|
|
|
self.should_protect = should_protect
|
|
|
|
self.combine_related = combine_related
|
|
|
|
|
|
|
|
SerialiseOptions.DEFAULT = SerialiseOptions()
|
2017-09-25 17:18:02 +10:00
|
|
|
|
|
|
|
class PrimitiveField(Field):
|
2017-12-15 20:21:03 +11:00
|
|
|
def serialise(self, value, options=SerialiseOptions.DEFAULT):
|
2017-09-25 17:18:02 +10:00
|
|
|
return value
|
|
|
|
|
|
|
|
def deserialise(self, value):
|
|
|
|
return value
|
|
|
|
|
|
|
|
DictField = PrimitiveField
|
|
|
|
IntField = PrimitiveField
|
|
|
|
StringField = PrimitiveField
|
2017-12-11 13:00:41 +11:00
|
|
|
BooleanField = PrimitiveField
|
2017-12-11 13:53:25 +11:00
|
|
|
BlobField = PrimitiveField
|
2017-09-25 17:18:02 +10:00
|
|
|
|
|
|
|
class EmbeddedObjectField(Field):
|
|
|
|
def __init__(self, object_type=None, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
self.object_type = object_type
|
|
|
|
|
2017-12-15 20:21:03 +11:00
|
|
|
def serialise(self, value, options=SerialiseOptions.DEFAULT):
|
|
|
|
return EosObject.serialise_and_wrap(value, self.object_type, options)
|
2017-09-25 17:18:02 +10:00
|
|
|
|
|
|
|
def deserialise(self, value):
|
|
|
|
return EosObject.deserialise_and_unwrap(value, self.object_type)
|
|
|
|
|
|
|
|
class ListField(Field):
|
|
|
|
def __init__(self, element_field=None, *args, **kwargs):
|
|
|
|
super().__init__(default=EosList, *args, **kwargs)
|
|
|
|
self.element_field = element_field
|
|
|
|
|
2017-12-15 20:21:03 +11:00
|
|
|
def serialise(self, value, options=SerialiseOptions.DEFAULT):
|
|
|
|
return [self.element_field.serialise(x, options) for x in (value.impl if isinstance(value, EosList) else value)]
|
2017-09-25 17:18:02 +10:00
|
|
|
|
|
|
|
def deserialise(self, value):
|
2017-11-24 19:37:48 +11:00
|
|
|
return EosList([self.element_field.deserialise(x) for x in value])
|
2017-09-25 17:18:02 +10:00
|
|
|
|
|
|
|
class EmbeddedObjectListField(Field):
|
|
|
|
def __init__(self, object_type=None, *args, **kwargs):
|
|
|
|
super().__init__(default=EosList, *args, **kwargs)
|
|
|
|
self.object_type = object_type
|
|
|
|
|
2017-12-15 20:21:03 +11:00
|
|
|
def serialise(self, value, options=SerialiseOptions.DEFAULT):
|
2017-11-24 19:37:48 +11:00
|
|
|
# TNYI: Doesn't know how to deal with iterators like EosList
|
2017-11-29 18:29:42 +11:00
|
|
|
if value is None:
|
|
|
|
return None
|
2017-12-15 20:21:03 +11:00
|
|
|
return [EosObject.serialise_and_wrap(x, self.object_type, options) for x in (value.impl if isinstance(value, EosList) else value)]
|
2017-09-25 17:18:02 +10:00
|
|
|
|
|
|
|
def deserialise(self, value):
|
2017-11-29 18:29:42 +11:00
|
|
|
if value is None:
|
|
|
|
return None
|
2017-11-24 18:09:43 +11:00
|
|
|
return EosList([EosObject.deserialise_and_unwrap(x, self.object_type) for x in value])
|
2017-09-25 17:18:02 +10:00
|
|
|
|
2017-12-15 20:21:03 +11:00
|
|
|
class RelatedObjectListManager:
|
|
|
|
def __init__(self, field, obj):
|
|
|
|
self.field = field
|
|
|
|
self.obj = obj
|
|
|
|
|
|
|
|
def get_all(self):
|
|
|
|
query = {self.field.related_field: getattr(self.obj, self.field.this_field)}
|
2017-12-15 20:51:57 +11:00
|
|
|
return self.field.related_type.get_all_by_fields(**query)
|
2017-12-15 20:21:03 +11:00
|
|
|
|
|
|
|
class RelatedObjectListField(Field):
|
|
|
|
def __init__(self, object_type=None, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
2017-12-15 20:51:57 +11:00
|
|
|
self.related_type = kwargs['related_type']
|
|
|
|
self.object_type = kwargs['object_type'] if 'object_type' in kwargs else None
|
|
|
|
self.this_field = kwargs['this_field'] if 'this_field' in kwargs else '_id'
|
|
|
|
self.related_field = kwargs['related_field']
|
2017-12-15 20:21:03 +11:00
|
|
|
|
|
|
|
def object_get(self, obj):
|
|
|
|
return RelatedObjectListManager(self, obj)
|
|
|
|
|
|
|
|
def object_set(self, obj, value):
|
|
|
|
raise Exception('Cannot directly set related field')
|
|
|
|
|
2017-12-15 20:51:57 +11:00
|
|
|
def object_init(self, obj, value):
|
|
|
|
pass
|
|
|
|
|
2017-12-15 20:21:03 +11:00
|
|
|
def serialise(self, value, options=SerialiseOptions.DEFAULT):
|
|
|
|
if not options.combine_related:
|
|
|
|
return None
|
|
|
|
return EmbeddedObjectListField(object_type=self.object_type).serialise(value.get_all(), options)
|
|
|
|
|
|
|
|
def deserialise(self, value):
|
|
|
|
if value is None:
|
2017-12-15 20:51:57 +11:00
|
|
|
return None
|
2017-12-15 20:21:03 +11:00
|
|
|
return EosList([EosObject.deserialise_and_unwrap(x, self.object_type) for x in value])
|
|
|
|
|
2017-09-25 17:18:02 +10:00
|
|
|
if is_python:
|
|
|
|
class UUIDField(Field):
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(default=uuid.uuid4, *args, **kwargs)
|
|
|
|
|
2017-12-15 20:21:03 +11:00
|
|
|
def serialise(self, value, options=SerialiseOptions.DEFAULT):
|
2017-09-25 17:18:02 +10:00
|
|
|
return str(value)
|
|
|
|
|
|
|
|
def deserialise(self, value):
|
|
|
|
return uuid.UUID(value)
|
|
|
|
else:
|
|
|
|
UUIDField = PrimitiveField
|
|
|
|
|
2017-11-24 20:26:18 +11:00
|
|
|
class DateTimeField(Field):
|
|
|
|
def pad(self, number):
|
|
|
|
if number < 10:
|
|
|
|
return '0' + str(number)
|
|
|
|
return str(number)
|
|
|
|
|
2017-12-15 20:21:03 +11:00
|
|
|
def serialise(self, value, options=SerialiseOptions.DEFAULT):
|
2017-11-24 20:26:18 +11:00
|
|
|
if value is None:
|
|
|
|
return None
|
|
|
|
|
|
|
|
if is_python:
|
|
|
|
return value.strftime('%Y-%m-%dT%H:%M:%SZ')
|
|
|
|
else:
|
|
|
|
return value.getUTCFullYear() + '-' + self.pad(value.getUTCMonth() + 1) + '-' + self.pad(value.getUTCDate()) + 'T' + self.pad(value.getUTCHours()) + ':' + self.pad(value.getUTCMinutes()) + ':' + self.pad(value.getUTCSeconds()) + 'Z'
|
|
|
|
|
|
|
|
def deserialise(self, value):
|
|
|
|
if value is None:
|
|
|
|
return None
|
|
|
|
|
|
|
|
if is_python:
|
2017-12-13 15:21:22 +11:00
|
|
|
return pytz.utc.localize(datetime.strptime(value, '%Y-%m-%dT%H:%M:%SZ'))
|
2017-11-24 20:26:18 +11:00
|
|
|
else:
|
2017-11-25 23:16:29 +11:00
|
|
|
return __pragma__('js', '{}', 'new Date(value)')
|
2017-11-24 20:26:18 +11:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def now():
|
|
|
|
if is_python:
|
2017-12-13 15:21:22 +11:00
|
|
|
return pytz.utc.localize(datetime.utcnow())
|
2017-11-24 20:26:18 +11:00
|
|
|
else:
|
|
|
|
return __pragma__('js', '{}', 'new Date()')
|
|
|
|
|
2017-09-25 17:18:02 +10:00
|
|
|
# Objects
|
|
|
|
# =======
|
|
|
|
|
|
|
|
class EosObjectType(type):
|
|
|
|
def __new__(meta, name, bases, attrs):
|
|
|
|
cls = type.__new__(meta, name, bases, attrs)
|
2017-12-04 13:52:10 +11:00
|
|
|
cls._name = (cls.__module__ + '.' + cls.__name__).replace('.js.', '.').replace('.python.', '.') #TNYI: qualname
|
2017-09-25 17:18:02 +10:00
|
|
|
if name != 'EosObject':
|
|
|
|
EosObject.objects[cls._name] = cls
|
|
|
|
return cls
|
|
|
|
|
|
|
|
class EosObject(metaclass=EosObjectType):
|
|
|
|
objects = {}
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self._instance = (None, None)
|
|
|
|
self._inited = False
|
|
|
|
|
|
|
|
def post_init(self):
|
|
|
|
self._inited = True
|
|
|
|
|
|
|
|
def recurse_parents(self, cls):
|
2017-11-24 18:09:43 +11:00
|
|
|
#if not isinstance(cls, type):
|
|
|
|
if isinstance(cls, str):
|
2017-12-12 20:32:02 +11:00
|
|
|
cls = EosObject.lookup(cls)
|
2017-09-28 16:46:30 +10:00
|
|
|
|
2017-09-25 17:18:02 +10:00
|
|
|
if isinstance(self, cls):
|
|
|
|
return self
|
|
|
|
if self._instance[0]:
|
|
|
|
return self._instance[0].recurse_parents(cls)
|
|
|
|
return None
|
|
|
|
|
2017-11-24 19:51:10 +11:00
|
|
|
def __eq__(self, other):
|
|
|
|
if not isinstance(other, EosObject):
|
|
|
|
return False
|
|
|
|
return EosObject.serialise_and_wrap(self) == EosObject.serialise_and_wrap(other)
|
|
|
|
|
2017-12-12 20:32:02 +11:00
|
|
|
@staticmethod
|
|
|
|
def lookup(name):
|
|
|
|
if name in EosObject.objects:
|
|
|
|
return EosObject.objects[name]
|
|
|
|
importlib.import_module(name[:name.rindex('.')])
|
|
|
|
return EosObject.objects[name]
|
|
|
|
|
2017-09-25 17:18:02 +10:00
|
|
|
@staticmethod
|
2017-12-15 20:21:03 +11:00
|
|
|
def serialise_and_wrap(value, object_type=None, options=SerialiseOptions.DEFAULT):
|
2017-09-25 17:18:02 +10:00
|
|
|
if object_type:
|
2017-09-28 19:37:10 +10:00
|
|
|
if value:
|
2017-12-15 20:21:03 +11:00
|
|
|
return value.serialise(options)
|
2017-12-15 20:51:57 +11:00
|
|
|
if value:
|
|
|
|
return {'type': value._name, 'value': (value.serialise(options) if value else None)}
|
|
|
|
return None
|
2017-09-25 17:18:02 +10:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def deserialise_and_unwrap(value, object_type=None):
|
|
|
|
if object_type:
|
|
|
|
return object_type.deserialise(value)
|
2017-12-12 20:32:02 +11:00
|
|
|
if value:
|
|
|
|
return EosObject.lookup(value['type']).deserialise(value['value'])
|
|
|
|
return None
|
2017-09-25 17:18:02 +10:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def to_json(value):
|
|
|
|
if is_python:
|
2017-12-07 18:05:38 +11:00
|
|
|
return json.dumps(value, sort_keys=True, separators=(',', ':'))
|
2017-09-25 17:18:02 +10:00
|
|
|
else:
|
|
|
|
return lib.stringify(value)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def from_json(value):
|
|
|
|
if is_python:
|
|
|
|
return json.loads(value)
|
|
|
|
else:
|
|
|
|
return JSON.parse(value)
|
|
|
|
|
|
|
|
class EosList(EosObject):
|
|
|
|
def __init__(self, *args):
|
2017-09-25 17:25:06 +10:00
|
|
|
super().__init__()
|
|
|
|
|
2017-09-25 17:18:02 +10:00
|
|
|
self.impl = list(*args)
|
|
|
|
|
2017-09-28 19:37:10 +10:00
|
|
|
def post_init(self):
|
|
|
|
for i in range(len(self.impl)):
|
|
|
|
val = self.impl[i]
|
2017-11-24 19:37:48 +11:00
|
|
|
# Check if object has writeable attributes
|
|
|
|
if is_python and hasattr(val, '__dict__'):
|
|
|
|
val._instance = (self, i)
|
|
|
|
if not val._inited:
|
|
|
|
val.post_init()
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return '<EosList {}>'.format(repr(self.impl))
|
2017-09-28 19:37:10 +10:00
|
|
|
|
2017-09-25 17:18:02 +10:00
|
|
|
# Lists in JS are implemented as native Arrays, so no cheating here :(
|
|
|
|
def __len__(self):
|
|
|
|
return len(self.impl)
|
|
|
|
def __getitem__(self, idx):
|
|
|
|
return self.impl[idx]
|
|
|
|
def __setitem__(self, idx, val):
|
|
|
|
self.impl[idx] = val
|
2017-09-28 16:46:30 +10:00
|
|
|
val._instance = (self, idx)
|
|
|
|
if not val._inited:
|
|
|
|
val.post_init()
|
2017-09-25 17:18:02 +10:00
|
|
|
def __contains__(self, val):
|
|
|
|
return val in self.impl
|
|
|
|
|
2017-11-24 19:37:48 +11:00
|
|
|
# For sorting, etc.
|
|
|
|
def __eq__(self, other):
|
|
|
|
if isinstance(other, EosList):
|
|
|
|
other = other.impl
|
|
|
|
return self.impl == other
|
|
|
|
def __lt__(self, other):
|
|
|
|
if isinstance(other, EosList):
|
|
|
|
other = other.impl
|
|
|
|
return self.impl < other
|
|
|
|
|
2017-09-25 17:18:02 +10:00
|
|
|
def append(self, value):
|
|
|
|
if isinstance(value, EosObject):
|
|
|
|
value._instance = (self, len(self))
|
|
|
|
if not value._inited:
|
|
|
|
value.post_init()
|
|
|
|
return self.impl.append(value)
|
|
|
|
|
|
|
|
class DocumentObjectType(EosObjectType):
|
|
|
|
def __new__(meta, name, bases, attrs):
|
|
|
|
cls = EosObjectType.__new__(meta, name, bases, attrs)
|
|
|
|
|
|
|
|
# Process fields
|
|
|
|
fields = {}
|
|
|
|
if hasattr(cls, '_fields'):
|
|
|
|
fields = cls._fields.copy() if is_python else Object.create(cls._fields)
|
|
|
|
for attr in list(dir(cls)):
|
|
|
|
if not is_python:
|
|
|
|
# We must skip things with getters or else they will be called here (too soon)
|
|
|
|
if Object.getOwnPropertyDescriptor(cls, attr).js_get:
|
|
|
|
continue
|
|
|
|
|
|
|
|
val = getattr(cls, attr)
|
|
|
|
if isinstance(val, Field):
|
|
|
|
val._instance = (cls, name)
|
2017-12-05 20:10:26 +11:00
|
|
|
|
|
|
|
# Transcrypt does funky things with aliases, which are usually helpful, but not here
|
|
|
|
if not is_python and attr.startswith('py_'):
|
|
|
|
real_attr = attr[3:]
|
|
|
|
else:
|
|
|
|
real_attr = attr
|
|
|
|
|
|
|
|
val.real_name = real_attr # The JSON/Python name
|
|
|
|
val.internal_name = attr # The name that gets passed in as kwargs in Javascript
|
|
|
|
|
|
|
|
fields[real_attr] = val
|
2017-09-25 17:18:02 +10:00
|
|
|
delattr(cls, attr)
|
|
|
|
cls._fields = fields
|
|
|
|
|
|
|
|
# Make properties
|
|
|
|
if is_python:
|
|
|
|
def make_property(name, field):
|
|
|
|
def field_getter(self):
|
2017-12-15 20:21:03 +11:00
|
|
|
return field.object_get(self)
|
2017-09-25 17:18:02 +10:00
|
|
|
def field_setter(self, value):
|
2017-12-15 20:21:03 +11:00
|
|
|
field.object_set(self, value)
|
2017-09-25 17:18:02 +10:00
|
|
|
return property(field_getter, field_setter)
|
|
|
|
|
|
|
|
for attr, val in fields.items():
|
2017-12-05 20:10:26 +11:00
|
|
|
setattr(cls, val.real_name, make_property(val.real_name, val))
|
|
|
|
#if val.real_name != val.internal_name:
|
|
|
|
# setattr(cls, val.internal_name, make_property(val.real_name, val))
|
2017-09-25 17:18:02 +10:00
|
|
|
else:
|
|
|
|
# Handled at instance level
|
|
|
|
pass
|
|
|
|
|
|
|
|
return cls
|
|
|
|
|
|
|
|
class DocumentObject(EosObject, metaclass=DocumentObjectType):
|
2017-12-07 18:05:38 +11:00
|
|
|
_ver = StringField(default='0.3')
|
2017-09-25 17:18:02 +10:00
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__()
|
|
|
|
|
|
|
|
self._field_values = {}
|
|
|
|
|
|
|
|
# Different to Python
|
|
|
|
for attr, val in self._fields.items():
|
|
|
|
if is_python:
|
|
|
|
# Properties handled above
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
def make_property(name, field):
|
2017-12-15 20:21:03 +11:00
|
|
|
# TNYI: Transcrypt doesn't pass self
|
2017-09-25 17:18:02 +10:00
|
|
|
def field_getter():
|
2017-12-15 20:21:03 +11:00
|
|
|
return field.object_get(self)
|
2017-09-25 17:18:02 +10:00
|
|
|
def field_setter(value):
|
2017-12-15 20:21:03 +11:00
|
|
|
field.object_set(self, value)
|
2017-09-25 17:18:02 +10:00
|
|
|
return (field_getter, field_setter)
|
2017-12-05 20:10:26 +11:00
|
|
|
prop = make_property(val.real_name, val)
|
2017-09-25 17:18:02 +10:00
|
|
|
# TNYI: No support for property()
|
2017-12-05 20:10:26 +11:00
|
|
|
Object.defineProperty(self, val.real_name, {
|
2017-09-25 17:18:02 +10:00
|
|
|
'get': prop[0],
|
|
|
|
'set': prop[1]
|
|
|
|
})
|
2017-12-05 20:10:26 +11:00
|
|
|
if val.real_name != val.internal_name:
|
|
|
|
# Allow reference as e.g. both obj.py_name (from Python code) and obj.name (from JS templates)
|
|
|
|
Object.defineProperty(self, val.internal_name, {
|
|
|
|
'get': prop[0],
|
|
|
|
'set': prop[1]
|
|
|
|
})
|
2017-09-25 17:18:02 +10:00
|
|
|
|
2017-12-05 20:10:26 +11:00
|
|
|
if val.internal_name in kwargs:
|
2017-12-15 20:51:57 +11:00
|
|
|
val.object_init(self, kwargs[val.internal_name])
|
2017-09-25 17:18:02 +10:00
|
|
|
else:
|
|
|
|
default = val.default
|
|
|
|
if default is not None and callable(default):
|
|
|
|
default = default()
|
2017-12-15 20:51:57 +11:00
|
|
|
val.object_init(self, default)
|
2017-09-25 17:18:02 +10:00
|
|
|
|
2017-12-15 20:21:03 +11:00
|
|
|
def serialise(self, options=SerialiseOptions.DEFAULT):
|
|
|
|
return {val.real_name: val.serialise(getattr(self, val.real_name), options) for attr, val in self._fields.items() if ((val.is_hashed or not options.for_hash) and (not options.should_protect or not val.is_protected))}
|
2017-09-25 17:18:02 +10:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def deserialise(cls, value):
|
2017-11-23 23:10:57 +11:00
|
|
|
if value is None:
|
|
|
|
return None
|
|
|
|
|
2017-11-23 20:42:22 +11:00
|
|
|
attrs = {}
|
|
|
|
for attr, val in cls._fields.items():
|
2017-12-05 20:10:26 +11:00
|
|
|
if attr in value:
|
|
|
|
attrs[val.internal_name] = val.deserialise(value[val.real_name])
|
2017-11-23 20:42:22 +11:00
|
|
|
return cls(**attrs)
|
2017-09-25 17:18:02 +10:00
|
|
|
|
2017-12-12 20:32:02 +11:00
|
|
|
class TopLevelObjectType(DocumentObjectType):
|
|
|
|
def __new__(meta, name, bases, attrs):
|
|
|
|
cls = DocumentObjectType.__new__(meta, name, bases, attrs)
|
|
|
|
|
|
|
|
# TopLevelObject obviously has no _db_name
|
|
|
|
if cls._name == 'eos.core.objects.TopLevelObject':
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
if '_db_name' not in attrs:
|
|
|
|
cls._db_name = cls._name
|
|
|
|
# If _db_name is False, then explicitly use the _name. Otherwise, inherit.
|
|
|
|
if cls._db_name is not False:
|
|
|
|
for base in bases:
|
|
|
|
if hasattr(base, '_db_name'):
|
|
|
|
cls._db_name = base._db_name
|
|
|
|
break
|
|
|
|
return cls
|
|
|
|
|
|
|
|
class TopLevelObject(DocumentObject, metaclass=TopLevelObjectType):
|
2017-09-25 17:18:02 +10:00
|
|
|
def save(self):
|
|
|
|
#res = db[self._name].replace_one({'_id': self.serialise()['_id']}, self.serialise(), upsert=True)
|
2017-11-27 19:40:01 +11:00
|
|
|
#res = dbinfo.db[self._db_name].replace_one({'_id': self._fields['_id'].serialise(self._id)}, EosObject.serialise_and_wrap(self), upsert=True)
|
|
|
|
dbinfo.provider.update_by_id(self._db_name, self._fields['_id'].serialise(self._id), EosObject.serialise_and_wrap(self))
|
2017-11-22 23:23:24 +11:00
|
|
|
|
2018-01-04 19:40:32 +11:00
|
|
|
def delete(self):
|
|
|
|
dbinfo.provider.delete_by_id(self._db_name, self._fields['_id'].serialise(self._id))
|
|
|
|
|
2017-11-22 23:23:24 +11:00
|
|
|
@classmethod
|
|
|
|
def get_all(cls):
|
2017-11-27 19:40:01 +11:00
|
|
|
return [EosObject.deserialise_and_unwrap(x) for x in dbinfo.provider.get_all(cls._db_name)]
|
2017-11-22 23:23:24 +11:00
|
|
|
|
2017-12-15 20:21:03 +11:00
|
|
|
@classmethod
|
|
|
|
def get_all_by_fields(cls, **fields):
|
2017-12-15 20:51:57 +11:00
|
|
|
for field in fields:
|
|
|
|
if not isinstance(fields[field], str):
|
|
|
|
fields[field] = str(fields[field])
|
2017-12-15 20:21:03 +11:00
|
|
|
return [EosObject.deserialise_and_unwrap(x) for x in dbinfo.provider.get_all_by_fields(cls._db_name, fields)]
|
|
|
|
|
2017-11-22 23:23:24 +11:00
|
|
|
@classmethod
|
|
|
|
def get_by_id(cls, _id):
|
2017-12-12 20:32:02 +11:00
|
|
|
if not isinstance(_id, str):
|
|
|
|
_id = str(_id)
|
2017-11-27 19:40:01 +11:00
|
|
|
return EosObject.deserialise_and_unwrap(dbinfo.provider.get_by_id(cls._db_name, _id))
|
2017-09-25 17:18:02 +10:00
|
|
|
|
|
|
|
class EmbeddedObject(DocumentObject):
|
|
|
|
pass
|
2018-01-11 22:06:21 +11:00
|
|
|
|
|
|
|
# Enums
|
|
|
|
# =====
|
|
|
|
|
|
|
|
class EosEnumType(EosObjectType):
|
|
|
|
def __new__(meta, name, bases, attrs):
|
|
|
|
cls = EosObjectType.__new__(meta, name, bases, attrs)
|
|
|
|
|
|
|
|
cls._values = {}
|
|
|
|
|
|
|
|
for attr in list(dir(cls)):
|
|
|
|
val = getattr(cls, attr);
|
|
|
|
if isinstance(val, int):
|
|
|
|
instance = cls(attr, val)
|
|
|
|
setattr(cls, attr, instance)
|
|
|
|
cls._values[val] = instance
|
|
|
|
|
|
|
|
return cls
|
|
|
|
|
|
|
|
class EosEnum(EosObject, metaclass=EosEnumType):
|
|
|
|
def __init__(self, name, value):
|
|
|
|
super().__init__()
|
|
|
|
self.name = name
|
|
|
|
self.value = value
|
|
|
|
|
|
|
|
def __eq__(self, other):
|
|
|
|
if not isinstance(other, self.__class__):
|
|
|
|
return False
|
|
|
|
return self.value == other.value
|
|
|
|
def __ne__(self, other):
|
|
|
|
if not isinstance(other, self.__class__):
|
|
|
|
return True
|
|
|
|
return self.value != other.value
|
|
|
|
def __gt__(self, other):
|
|
|
|
if not isinstance(other, self.__class__):
|
|
|
|
raise TypeError
|
|
|
|
return self.value > other.value
|
|
|
|
def __lt__(self, other):
|
|
|
|
if not isinstance(other, self.__class__):
|
|
|
|
raise TypeError
|
|
|
|
return self.value < other.value
|
|
|
|
def __ge__(self, other):
|
|
|
|
if not isinstance(other, self.__class__):
|
|
|
|
raise TypeError
|
|
|
|
return self.value >= other.value
|
|
|
|
def __le__(self, other):
|
|
|
|
if not isinstance(other, self.__class__):
|
|
|
|
raise TypeError
|
|
|
|
return self.value <= other.value
|
|
|
|
|
|
|
|
def serialise(self, options=SerialiseOptions.DEFAULT):
|
|
|
|
return self.value
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def deserialise(cls, value):
|
|
|
|
return cls._values[value]
|
|
|
|
|
|
|
|
EnumField = EmbeddedObjectField
|