Example #1
0
    def test_constraint_creation(self):
        class FKC_a(TestModel):
            name = CharField()

        fkc_deferred = DeferredRelation()

        class FKC_b(TestModel):
            fkc_a = ForeignKeyField(fkc_deferred)

        fkc_deferred.set_model(FKC_a)

        with test_db.transaction() as txn:
            FKC_b.drop_table(True)
            FKC_a.drop_table(True)
            FKC_a.create_table()
            FKC_b.create_table()

            # Foreign key constraint is not enforced.
            fb = FKC_b.create(fkc_a=-1000)
            fb.delete_instance()

            # Add constraint.
            test_db.create_foreign_key(FKC_b, FKC_b.fkc_a)

            def _trigger_exc():
                with test_db.savepoint() as s1:
                    fb = FKC_b.create(fkc_a=-1000)

            self.assertRaises(IntegrityError, _trigger_exc)

            fa = FKC_a.create(name='fa')
            fb = FKC_b.create(fkc_a=fa)
            txn.rollback()
Example #2
0
    def test_constraint_creation(self):
        class FKC_a(TestModel):
            name = CharField()

        fkc_deferred = DeferredRelation()

        class FKC_b(TestModel):
            fkc_a = ForeignKeyField(fkc_deferred)

        fkc_deferred.set_model(FKC_a)

        with test_db.transaction() as txn:
            FKC_b.drop_table(True)
            FKC_a.drop_table(True)
            FKC_a.create_table()
            FKC_b.create_table()

            # Foreign key constraint is not enforced.
            fb = FKC_b.create(fkc_a=-1000)
            fb.delete_instance()

            # Add constraint.
            test_db.create_foreign_key(FKC_b, FKC_b.fkc_a)

            def _trigger_exc():
                with test_db.savepoint() as s1:
                    fb = FKC_b.create(fkc_a=-1000)

            self.assertRaises(IntegrityError, _trigger_exc)

            fa = FKC_a.create(name='fa')
            fb = FKC_b.create(fkc_a=fa)
            txn.rollback()
Example #3
0
    def test_doc_example(self):
        db = database_initializer.get_in_memory_database()
        TweetDeferred = DeferredRelation()

        class Base(Model):
            class Meta:
                database = db

        class User(Base):
            username = CharField()
            favorite_tweet = ForeignKeyField(TweetDeferred, null=True)

        class Tweet(Base):
            user = ForeignKeyField(User)
            message = TextField()

        TweetDeferred.set_model(Tweet)
        with db.transaction():
            User.create_table()
            Tweet.create_table()

        # SQLite does not support alter + add constraint.
        self.assertRaises(
            OperationalError,
            lambda: db.create_foreign_key(User, User.favorite_tweet))
Example #4
0
    def test_multiple_refs(self):
        person_ref = DeferredRelation()
        class Relationship(TestModel):
            person_from = ForeignKeyField(person_ref, related_name='f1')
            person_to = ForeignKeyField(person_ref, related_name='f2')
        class SomethingElse(TestModel):
            person = ForeignKeyField(person_ref)

        class Person(TestModel):
            name = CharField()
        person_ref.set_model(Person)

        p1 = Person(id=1, name='p1')
        p2 = Person(id=2, name='p2')
        p3 = Person(id=3, name='p3')
        r = Relationship(person_from=p1, person_to=p2)
        s = SomethingElse(person=p3)

        self.assertEqual(r.person_from.name, 'p1')
        self.assertEqual(r.person_to.name, 'p2')
        self.assertEqual(s.person.name, 'p3')
Example #5
0
    def test_doc_example(self):
        db = database_initializer.get_in_memory_database()
        TweetDeferred = DeferredRelation()

        class Base(Model):
            class Meta:
                database = db
        class User(Base):
            username = CharField()
            favorite_tweet = ForeignKeyField(TweetDeferred, null=True)
        class Tweet(Base):
            user = ForeignKeyField(User)
            message = TextField()
        TweetDeferred.set_model(Tweet)
        with db.transaction():
            User.create_table()
            Tweet.create_table()

        # SQLite does not support alter + add constraint.
        self.assertRaises(
            OperationalError,
            lambda: db.create_foreign_key(User, User.favorite_tweet))
Example #6
0
    def test_multiple_refs(self):
        person_ref = DeferredRelation()

        class Relationship(TestModel):
            person_from = ForeignKeyField(person_ref, related_name='f1')
            person_to = ForeignKeyField(person_ref, related_name='f2')

        class SomethingElse(TestModel):
            person = ForeignKeyField(person_ref)

        class Person(TestModel):
            name = CharField()

        person_ref.set_model(Person)

        p1 = Person(id=1, name='p1')
        p2 = Person(id=2, name='p2')
        p3 = Person(id=3, name='p3')
        r = Relationship(person_from=p1, person_to=p2)
        s = SomethingElse(person=p3)

        self.assertEqual(r.person_from.name, 'p1')
        self.assertEqual(r.person_to.name, 'p2')
        self.assertEqual(s.person.name, 'p3')
Example #7
0
 class CircularRef1(Model):
     circ_ref2 = ForeignKeyField(DeferredRelation('circularref2'),
                                 null=True)
Example #8
0
        self.change = change

    def __getattr__(self, attr):
        if attr == "id":
            return self.change.logbook.id
        if attr == "revision_n":
            return list(self.change.logbook.changes).index(self.change)

        if attr in ("name", "description", "template", "attributes",
                    "archived", "parent_id"):
            return self.change.get_old_value(attr)

        return getattr(self.change.logbook, attr)


DeferredEntry = DeferredRelation()

# class EntrySearch(FTS5Model):
#     entry = ForeignKeyField(DeferredEntry)
#     content = SearchField()


class MLStripper(HTMLParser):
    def __init__(self):
        self.reset()
        self.strict = False
        self.convert_charrefs = True
        self.fed = []

    def handle_data(self, d):
        self.fed.append(d)
Example #9
0
		return self.first_name + ' ' + self.last_name if self.first_name and self.last_name else self.username

class UserRoles(BaseModel):
	user = ForeignKeyField(User, related_name='roles')
	role = ForeignKeyField(Role, related_name='users')
	name = property(lambda self: self.role.name)
	description = property(lambda self: self.role.description)

class MessageType(BaseModel):
	name = CharField()
	constructor = CharField()

class ConversationType(BaseModel):
	name = CharField()

DeferredLastMessage = DeferredRelation()

class Conversation(BaseModel):
	conversation_type = ForeignKeyField(ConversationType)
	last_message = ForeignKeyField(DeferredLastMessage, null=True)
	file = CharField(null=True)

class Quotation(BaseModel):
	parameters = JSONField(null=False)
	creator = ForeignKeyField(User, null=False, related_name='creator')
	receiver = ForeignKeyField(User, null=False, related_name='receiver')

class TextContent(BaseModel):
	text = CharField(null = False)

class MessageContent(BaseModel):