-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
57 lines (46 loc) · 1.68 KB
/
models.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
from sqlalchemy import Column, Integer, String, Text, ForeignKey, Table
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.ext.hybrid import hybrid_property
from sqlalchemy.orm import relationship
Base = declarative_base()
authorArticle_table = Table('authorArticle', Base.metadata,
Column('author_id', Integer, ForeignKey('author.id')),
Column('article_id', Integer, ForeignKey('article.id'))
)
class Author(Base):
__tablename__ = 'author'
id = Column(Integer, primary_key=True, autoincrement=True)
firstName = Column(String(50))
lastName = Column(String(50))
category = Column(Text)
articles = relationship(
"Article",
secondary=authorArticle_table,
back_populates="authors"
)
def __repr__(self):
"""Show this object (database record)"""
return "Author(%d, %s, %s)" % (
self.id, self.firstName, self.lastName)
@hybrid_property
def fullName(self):
return self.firstName + " " + self.lastName
@hybrid_property
def articleAbstracts(self):
abstracts = [article.abstract for article in self.articles]
return abstracts
class Article(Base):
__tablename__ = 'article'
id = Column(Integer, primary_key=True, autoincrement=True)
category = Column(Text)
title = Column(Text)
abstract = Column(Text)
authors = relationship(
"Author",
secondary=authorArticle_table,
back_populates="articles"
)
def __repr__(self):
"""Show this object (database record)"""
return "Author(%d, %s, %s)" % (
self.id, self.firstName, self.lastName)