models.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. """Database models"""
  2. from sqlalchemy import Column, ForeignKey, Integer, String, \
  3. DateTime, BigInteger, Date
  4. from sqlalchemy.orm import relationship, backref
  5. from sqlalchemy.ext.declarative import declarative_base
  6. Base = declarative_base()
  7. class PlayerResidency(Base):
  8. """Model for player residency"""
  9. __tablename__ = 'player_residency'
  10. player_id = Column(BigInteger, ForeignKey('player.id'), primary_key=True)
  11. region_id = Column(Integer, ForeignKey('region.id'), primary_key=True)
  12. from_date_time = Column(DateTime, primary_key=True)
  13. until_date_time = Column(DateTime)
  14. class PlayerLocation(Base):
  15. """Model for player location"""
  16. __tablename__ = 'player_location'
  17. player_id = Column(BigInteger, ForeignKey('player.id'), primary_key=True)
  18. region_id = Column(Integer, ForeignKey('region.id'), primary_key=True)
  19. from_date_time = Column(DateTime, primary_key=True)
  20. until_date_time = Column(DateTime)
  21. class StateRegion(Base):
  22. """Model for state region"""
  23. __tablename__ = 'state_region'
  24. state_id = Column(Integer, ForeignKey('state.id'), primary_key=True)
  25. region_id = Column(Integer, ForeignKey('region.id'), primary_key=True)
  26. from_date_time = Column(DateTime, primary_key=True)
  27. until_date_time = Column(DateTime)
  28. class StateWorkPermit(Base):
  29. """Model for state work permit"""
  30. __tablename__ = 'state_work_permit'
  31. state_id = Column(Integer, ForeignKey('state.id'), primary_key=True)
  32. player_id = Column(BigInteger, ForeignKey('player.id'), primary_key=True)
  33. from_date_time = Column(DateTime, primary_key=True)
  34. until_date_time = Column(DateTime)
  35. class State(Base):
  36. """Model for state"""
  37. __tablename__ = 'state'
  38. id = Column(Integer, primary_key=True)
  39. name = Column(String)
  40. regions = relationship(
  41. 'Region',
  42. secondary='state_region',
  43. backref=backref('states', lazy='dynamic'),
  44. lazy='dynamic'
  45. )
  46. capital_id = Column(Integer, ForeignKey('region.id'))
  47. capital = relationship(
  48. 'Region',
  49. backref=backref('state_capital', lazy='dynamic')
  50. )
  51. class Region(Base):
  52. """Model for region"""
  53. __tablename__ = 'region'
  54. id = Column(Integer, primary_key=True)
  55. name = Column(String)
  56. class Player(Base):
  57. """Model for player"""
  58. __tablename__ = 'player'
  59. id = Column(BigInteger, primary_key=True)
  60. name = Column(String)
  61. nation = Column(String)
  62. registration_date = Column(Date)
  63. residencies = relationship(
  64. 'Region',
  65. secondary='player_residency',
  66. backref=backref('residents', lazy='dynamic'),
  67. lazy='dynamic',
  68. order_by='desc(PlayerResidency.from_date_time)'
  69. )
  70. locations = relationship(
  71. 'Region',
  72. secondary='player_location',
  73. backref=backref('citizens', lazy='dynamic'),
  74. lazy='dynamic',
  75. order_by='desc(PlayerLocation.from_date_time)'
  76. )
  77. state_work_permits = relationship(
  78. 'State',
  79. secondary='state_work_permit',
  80. lazy='dynamic',
  81. order_by='desc(StateWorkPermit.from_date_time)'
  82. )