-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtables.py
79 lines (66 loc) · 2.37 KB
/
tables.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
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.ext.hybrid import hybrid_property
from sqlalchemy.orm import column_property, relationship
from sqlalchemy import func
from sqlalchemy import (
Column,
String,
Integer,
ForeignKey,
DateTime,
Table)
Base = declarative_base()
class CommonColumns(Base):
__abstract__ = True
_created = Column(DateTime, default=func.now())
_updated = Column(DateTime, default=func.now(), onupdate=func.now())
_etag = Column(String(40))
@hybrid_property
def _id(self):
"""
Eve backward compatibility
"""
return self.id
def jsonify(self):
"""
Used to dump related objects to json
"""
relationships = inspect(self.__class__).relationships.keys()
mapper = inspect(self)
attrs = [a.key for a in mapper.attrs if \
a.key not in relationships \
and not a.key in mapper.expired_attributes]
attrs += [a.__name__ for a in inspect(self.__class__).all_orm_descriptors if a.extension_type is hybrid.HYBRID_PROPERTY]
return dict([(c, getattr(self, c, None)) for c in attrs])
powerups = Table('powerups',
Base.metadata,
Column('character_id', Integer, ForeignKey('character.id')),
Column('powerup_id', Integer, ForeignKey('powerup.id'))
)
class Character(CommonColumns):
__tablename__ = 'character'
id = Column(Integer, primary_key=True, autoincrement=True)
name = Column(String(80), unique=True)
description = Column(String)
powerups = relationship('Powerup',
secondary=powerups,
backref='character')
def __init__(self, name, description):
self.name = name
self.description = description
def __repr__(self):
return "<Character %r>" % self.name
def __unicode__(self):
return self.name
@classmethod
def from_tuple(cls, data):
""" Helper method to populate the database """
return cls(name=data[0], description=data[1])
class Powerup(CommonColumns):
__tablename__ = 'powerup'
id = Column(Integer, primary_key=True, autoincrement=True)
name = Column(String(80), unique=True)
def __init__(self, name):
self.name = name
def __repr__(self):
return "<Powerup %s>" % self.name