Advertisement
Guest User

Untitled

a guest
Jan 15th, 2016
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.47 KB | None | 0 0
  1. from sqlalchemy import *
  2. from sqlalchemy.orm import sessionmaker
  3. from sqlalchemy.ext.declarative import declarative_base
  4. from sqlalchemy.dialects.postgresql import JSON
  5. from sqlalchemy.orm import relationship
  6.  
  7. from sqlalchemy.ext.declarative import declared_attr
  8.  
  9. ################################################################
  10. # DPIS database model
  11. ################################################################
  12.  
  13. Base = declarative_base()
  14.  
  15. class Country(Base):
  16. """ Managed through Kotti """
  17.  
  18. __tablename__ = 'countries'
  19.  
  20. dpis_id = Column(Integer, primary_key=True)
  21. title = Column(Unicode(80))
  22. regions = relationship("Region",
  23. primaryjoin="Country.dpis_id==Region.country_id")
  24.  
  25.  
  26. class Region(Base):
  27. """ Managed through Kotti """
  28. __tablename__ = 'regions'
  29. dpis_id = Column(Integer, primary_key=True)
  30. country_id = Column(Integer, ForeignKey('countries.dpis_id'))
  31. title = Column(Unicode(80))
  32. parishes = relationship("Parish",
  33. primaryjoin="Region.dpis_id==Parish.region_id")
  34.  
  35.  
  36. class Parish(Base):
  37. """ Managed through Kotti """
  38. __tablename__ = 'parishes'
  39. dpis_id = Column(Integer, primary_key=True)
  40. region_id = Column(Integer, ForeignKey('regions.dpis_id'))
  41. title = Column(Unicode(80))
  42. schools = relationship("School",
  43. primaryjoin="Parish.dpis_id==School.parish_id")
  44.  
  45.  
  46. ################################################################
  47. # School <-1:N-> Student <-1:N-> Score
  48. # are managed within Sqlalchemy as a bidirectional relation
  49. ################################################################
  50.  
  51.  
  52. class School(Base):
  53. """ Managed through Kotti """
  54. __tablename__ = 'schools'
  55. dpis_id = Column(Integer, primary_key=True)
  56. parish_id = Column(Integer, ForeignKey('parishes.dpis_id'))
  57. title = Column(Unicode(80))
  58.  
  59. # all students of this school
  60. students = relationship("Student",
  61. primaryjoin="School.dpis_id==Student.school_id")
  62.  
  63.  
  64. class Student(Base):
  65. """ Managed by system, no integration with Kotti """
  66. __tablename__ = 'students'
  67.  
  68. dpis_id = Column(Integer, primary_key=True)
  69. school_id = Column(Integer, ForeignKey('schools.dpis_id'))
  70. firstname = Column(Unicode(80))
  71. lastname = Column(Unicode(80))
  72. address = Column(Unicode(80))
  73. city = Column(Unicode(80))
  74. birth = Column(Date)
  75. gender = Column(Unicode(4))
  76.  
  77. # back-reference to school
  78. school = relationship(School, primaryjoin=school_id==School.dpis_id)
  79. # all scores of this student
  80. scores = relationship("Score",
  81. primaryjoin="Student.dpis_id==Score.student_id")
  82.  
  83.  
  84. class Score(Base):
  85. __tablename__ = 'scores'
  86.  
  87. dpis_id = Column(Integer, primary_key=True)
  88. student_id = Column(Integer, ForeignKey('students.dpis_id'))
  89. year = Column(Integer, index=True)
  90. subject = Column(Unicode(80), index=True)
  91. score = Column(Integer, index=True)
  92. # back reference to student
  93. student = relationship(Student, primaryjoin="Score.student_id==Student.dpis_id")
  94.  
  95.  
  96. class Person(Base):
  97. """ A Person represents someone how can login into the system
  98. for doing *something*.
  99. """
  100.  
  101. __tablename__ = 'dpis_users'
  102.  
  103. dpis_id = Column(Integer, primary_key=True)
  104. title = Column(Unicode(80))
  105. firstname = Column(Unicode(80))
  106. lastname = Column(Unicode(80))
  107. username = Column(Unicode(32)) # Kotti login name
  108. password = Column(Unicode(32)) # Hashed Kotti password?
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement