forked from wpoa/open-access-media-importer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
88 lines (73 loc) · 3.11 KB
/
model.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
from sqlalchemy import Column, Integer, UnicodeText, Boolean, ForeignKey
from sqlalchemy.orm import relationship
from sqlalchemy.orm import declarative_base
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy import Table, Column, ForeignKey
from sqlalchemy.orm import relationship
import sys
import importlib
def set_source(source):
source_module = importlib.import_module(f'sources.{source}')
# Define the database engine
engine = create_engine('sqlite:///mydata.sqlite')
# Create a session factory
Session = sessionmaker(bind=engine)
# Create a session object
session = Session()
Base = declarative_base()
Base.metadata.create_all(engine)
class Journal(Base):
__tablename__ = 'journal'
title = Column(UnicodeText, primary_key=True)
articles = relationship("Article", back_populates="journal")
article_category = Table(
'article_category', Base.metadata,
Column('article_title', UnicodeText, ForeignKey('article.title')),
Column('category_name', UnicodeText, ForeignKey('category.name'))
)
class Category(Base):
__tablename__ = 'category'
name = Column(UnicodeText, primary_key=True)
articles = relationship('Article', secondary=article_category, back_populates='categories')
class Article(Base):
__tablename__ = 'article'
name = Column(UnicodeText)
doi = Column(UnicodeText)
title = Column(UnicodeText, primary_key=True)
contrib_authors = Column(UnicodeText, primary_key=True)
abstract = Column(UnicodeText)
year = Column(Integer)
month = Column(Integer, nullable=True)
day = Column(Integer, nullable=True)
url = Column(UnicodeText)
license_url = Column(UnicodeText)
license_text = Column(UnicodeText)
copyright_statement = Column(UnicodeText)
copyright_holder = Column(UnicodeText)
journal_title = Column(UnicodeText, ForeignKey('journal.title'))
journal = relationship("Journal", back_populates="articles")
supplementary_materials = relationship('SupplementaryMaterial', back_populates='article')
categories = relationship('Category', secondary=article_category, back_populates='articles')
def __repr__(self):
return '<Article "%s">' % self.title.encode('utf-8')
class SupplementaryMaterial(Base):
__tablename__ = 'supplementary_material'
#id = Column(Integer, primary_key=True)
label = Column(UnicodeText)
title = Column(UnicodeText)
caption = Column(UnicodeText)
mimetype = Column(UnicodeText)
mime_subtype = Column(UnicodeText)
mimetype_reported = Column(UnicodeText)
mime_subtype_reported = Column(UnicodeText)
url = Column(UnicodeText, primary_key=True)
article_title = Column(UnicodeText, ForeignKey('article.title'))
article = relationship('Article', back_populates='supplementary_materials')
downloaded = Column(Boolean, default=False)
converting = Column(Boolean, default=False)
converted = Column(Boolean, default=False)
uploaded = Column(Boolean, default=False)
def __repr__(self):
return '<SupplementaryMaterial "%s" of Article "%s">' % \
(self.label.encode('utf-8'), self.article.title.encode('utf-8'))