def construct(self, data, deleted=False, subitems=None): # populates the model with the data internal_objects = {} for k, v in data.items(): k_splitted = k.split('€$$€') if len(k_splitted) == 1: # check if its named different in the database than the orm if k not in self.__class__.attr_names.keys(): for orm, db in self.__class__.attr_names.items(): if k == db: k = orm break # get the recomposed value field_class = getattr(self.__class__, k) v = field_class.recompose(v) if field_class in [ForeignKey, ManyToManyField]: pass setattr(self, k, v) else: # itself or empty dict internal_objects[k_splitted[0]] = internal_objects.get( k_splitted[0], {}) # update the new value internal_objects[k_splitted[0]].update({k_splitted[1]: v}) if internal_objects: for attr_name, data in internal_objects.items(): if hasattr(self, attr_name): if getattr(self, attr_name): field = getattr(self.__class__, attr_name) model = get_model(field.foreign_key) setattr(self, attr_name, model().construct(data)) else: for subitem in subitems: if "fields" not in subitem: continue for join in subitem["fields"]: if join['right_table'] == attr_name: field = getattr(self.__class__, join['orm_fieldname']) model = get_model(field.foreign_key) setattr(self, join['orm_fieldname'], model().construct(data)) break self.deleted = deleted return self
def set_many2many(cls, field, table_name, my_column, other_column, direct=False): other_model = get_model(other_column) queryset = ModelManager(other_model, field=field) queryset.set_orm(cls.objects.orm) def m2m_set(self): queryset.query = [{ 'action': 'db__select_m2m', 'select': '*', 'm2m_tablename': table_name, 'other_tablename': other_column, 'otherdb_pk': other_model.db_pk, 'id_data': '{}={}'.format(my_column, getattr(self, self.orm_pk)), }] return queryset method_name = (direct and field.field_name or '{}_set'.format(other_column.lower())) setattr(cls, method_name, m2m_set)
def set_many2many(cls, field, table_name, my_column, other_column, direct=False): other_model = get_model(other_column) queryset = ModelManager(other_model, field=field) queryset.set_orm(cls.objects.orm) def m2m_set(self): queryset.query = [{ "action": "db__select_m2m", "select": "*", "m2m_tablename": table_name, "other_tablename": other_column, "otherdb_pk": other_model.db_pk, "id_data": "{}={}".format(my_column, getattr(self, self.orm_pk)), }] return queryset method_name = (direct and field.field_name or "{}_set".format(other_column.lower())) setattr(cls, method_name, m2m_set)
def fk_set(self): model = get_model(model_name) return model.objects.filter( **{field_name: getattr(self, self.orm_pk)})
from asyncorm.application.configure import get_model from asyncorm.exceptions import AsyncOrmSerializerError from asyncorm.serializers import ModelSerializer, SerializerMethod from asyncorm.test_case import AsyncormTestCase from tests.app_1.models import Author, Book from tests.app_1.serializer import BookSerializer # You can get the book by model_name Book2 = get_model("Book") # And get the author by module.model_name Author2 = get_model("app_1.Author") class SerializerTests(AsyncormTestCase): async def test_serialize_wrong_model(self): # complains if we try to serialize an incorrect model with self.assertRaises(AsyncOrmSerializerError) as exc: author = Author() BookSerializer().serialize(author) self.assertIn("That object is not an instance of", exc.exception.args[0]) async def test_serialize_correct(self): # the inverse relation is correctly set book = await Book.objects.filter(id__lt=100)[0] serialized_book = BookSerializer().serialize(book) self.assertEqual(serialized_book.get("name"), book.name)
def test_get_model_model_does_not_exist(self): with self.assertRaises(AppError) as exc: get_model('Tato') self.assertIn('The model does not exists', exc.exception.args[0])
from asyncorm.application.configure import get_model, orm_app, configure_orm from asyncorm.exceptions import ModelError, AppError from tests.test_helper import AioTestCase Book = get_model('Book') db_config = { 'database': 'asyncorm', 'host': 'localhost', 'user': '******', 'password': '******', } class ModuleTests(AioTestCase): def test_ormconfigure_no_models(self): orm = configure_orm({'db_config': db_config, 'apps': None}) with self.assertRaises(AppError) as exc: orm.get_model('here.there.what') self.assertTrue('There are no apps declared in the orm' == exc.exception.args[0]) def test_ormconfigure_no_db_config(self): with self.assertRaises(AppError) as exc: configure_orm({'apps': ['tests.testapp', 'tests.testapp2']}) self.assertIn('Imposible to configure without database', exc.exception.args[0])
from asyncorm.application.configure import get_model from asyncorm.exceptions import FieldError, ModelError from tests.testapp.models import Book, Author from tests.testapp2.models import Developer, Client, Organization from tests.test_helper import AioTestCase # You can get the book by model_name Book2 = get_model('Book') # And get the author by module.model_name Author2 = get_model('testapp.Author') class ModelTests(AioTestCase): def test_class__init__(self): # classmethods tests # no matter how you import them they are the same object self.assertTrue(Author is Author2) self.assertTrue(Book is Book2) self.assertEqual(Book().cls_tablename(), 'library') self.assertEqual(Author().cls_tablename(), 'Author') def test_get_fields(self): fields = Book.get_fields() self.assertEqual(len(fields), 7) self.assertEqual( sorted(list(fields.keys())), sorted([ 'id', 'content', 'name', 'author', 'date_created', 'price',
from asyncorm.application.configure import get_model, orm_app, configure_orm from asyncorm.exceptions import ModelError, AppError from tests.test_helper import AioTestCase Book = get_model("Book") db_config = { "database": "asyncorm", "host": "localhost", "user": "******", "password": "******", } class ModuleTests(AioTestCase): def test_ormconfigure_no_models(self): orm = configure_orm({"db_config": db_config, "apps": None}) with self.assertRaises(AppError) as exc: orm.get_model("here.what") self.assertTrue( "There are no apps declared in the orm" == exc.exception.args[0]) def test_ormconfigure_no_db_config(self): with self.assertRaises(AppError) as exc: configure_orm({"apps": ["tests.testapp", "tests.testapp2"]}) self.assertIn("Imposible to configure without database", exc.exception.args[0])
from asyncorm.application.configure import get_model from asyncorm.exceptions import SerializerError from asyncorm.serializers import ModelSerializer, SerializerMethod from tests.testapp.serializer import BookSerializer from tests.testapp.models import Book, Author from tests.test_helper import AioTestCase # You can get the book by model_name Book2 = get_model("Book") # And get the author by module.model_name Author2 = get_model("testapp.Author") class SerializerTests(AioTestCase): async def test_serialize_wrong_model(self): # complains if we try to serialize an incorrect model with self.assertRaises(SerializerError) as exc: author = Author() BookSerializer().serialize(author) self.assertIn("That object is not an instance of", exc.exception.args[0]) async def test_serialize_correct(self): # the inverse relation is correctly set book = await Book.objects.filter(id__lt=100)[0] serialized_book = BookSerializer().serialize(book) self.assertEqual(serialized_book.get("name"), book.name)
def test_get_model_model_does_not_exist(self): with self.assertRaises(AsyncOrmModelNotDefined) as exc: get_model("Tato") self.assertIn("The model does not exists", exc.exception.args[0])