Guest User

Untitled

a guest
Dec 11th, 2017
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.00 KB | None | 0 0
  1. from flask import Flask
  2. from flaskext.sqlalchemy import SQLAlchemy
  3. from datetime import datetime
  4. from sqlalchemy.ext.declarative import declarative_base
  5. from sqlalchemy import MetaData
  6. from sqlalchemy.orm.collections import attribute_mapped_collection
  7. from sqlalchemy import orm
  8.  
  9. metadata = MetaData()
  10. Base = declarative_base(metadata=metadata)
  11.  
  12.  
  13. app = Flask(__name__)
  14. app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////tmp/test.db'
  15. db = SQLAlchemy(app)
  16.  
  17.  
  18. class User(db.Model):
  19. id = db.Column(db.Integer, primary_key=True)
  20. username = db.Column(db.String(80), unique=True)
  21. password = db.Column(db.String(80))
  22. email = db.Column(db.String(120))
  23. date_joined = db.Column(db.DateTime)
  24. submissions = db.relationship('Submission', backref=db.backref('user', lazy='dynamic'))
  25. comments = db.relationship('Comment', backref=db.backref('user', lazy='dynamic'))
  26. karma = db.Column(db.Integer)
  27.  
  28. def __init__(self, username, password, email, date_joined=None, karma=0):
  29. self.username = username
  30. self.password = password
  31. self.email = email
  32. if date_joined is None:
  33. date_joined = datetime.utcnow()
  34. self.date_joined = date_joined
  35. self.karma = karma
  36.  
  37. def __repr__(self):
  38. return '<User %r>' % self.username
  39.  
  40.  
  41. class Submission(db.Model):
  42. id = db.Column(db.Integer, primary_key=True)
  43. user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
  44. date_submitted = db.Column(db.DateTime)
  45. title = db.Column(db.String(100))
  46. body = db.Column(db.Text)
  47. link = db.Column(db.Text)
  48. points = db.Column(db.Integer)
  49. comments = db.relationship('Comment', backref=db.backref('submission', lazy='dynamic'))
  50.  
  51. def __init__(self, user_id, title, date_submitted=None, body=None, link=None, points=1):
  52. self.user_id = user_id
  53. self.title = title
  54. if date_submitted is None:
  55. date_submitted = datetime.utcnow()
  56. self.body = body
  57. self.link = link
  58. self.points = points
  59.  
  60. def __repr__(self):
  61. return '<Submission %r>' % self.title
  62.  
  63.  
  64. class Comment(Base):
  65.  
  66. __tablename__ = 'comment'
  67.  
  68. id = db.Column(db.Integer, primary_key=True)
  69. user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
  70. submission_id = db.Column(db.Integer, db.ForeignKey('submission.id'))
  71. parent_id = db.Column(db.Integer, db.ForeignKey('comment.id'))
  72. children = db.relation('Comment', backref=db.backref('parent', remote_side=[id]))
  73. date_submitted = db.Column(db.DateTime)
  74. body = db.Column(db.Text)
  75. points = db.Column(db.Integer)
  76.  
  77. def __init__(self, user_id, submission_id, body, parent_id=None, date_submitted=None, points=1):
  78. self.user_id = user_id
  79. self.submission_id = submission_id
  80. self.body = body
  81. self.parent_id = parent_id
  82. if date_submitted is None:
  83. date_submitted = datetime.utcnow()
  84. points = points
  85.  
  86. def append(self, user_id, submission_id, body, parent_id=None, date_submitted=None, points=1):
  87. if parent_id is None:
  88. parent_id = self.id
  89. new_comment = Comment(user_id, submission_id, body, parent_id, date_submitted, points)
  90. self.children[new_comment.id] = new_comment
  91.  
  92.  
  93. class Points(db.Model):
  94. id = db.Column(db.Integer, primary_key=True)
  95. value = db.Column(db.Integer)
  96. user_id_voted = db.Column(db.Integer)
  97. user_id_received = db.Column(db.Integer)
  98. comment_id = db.Column(db.Integer)
  99. submission_id = db.Column(db.Integer)
  100. date_counted = db.Column(db.DateTime)
  101.  
  102. def __init__(self, value, user_id_voted, user_id_received, comment_id=None, submission_id=None, date_counted=None):
  103. self.value = value
  104. self.user_id_voted = user_id_voted
  105. self.user_id_received = user_id_received
  106. self.comment_id = comment_id
  107. self.submission_id = submission_id
  108. if date_counted is None:
  109. self.date_counted = datetime.utcnow()
  110.  
  111.  
  112. sqlalchemy.exc.InvalidRequestError: When initializing mapper Mapper|Submission|submission, expression 'CommentsTreeNode' failed to locate a name ("name 'CommentsTreeNode' is not defined"). If this is a class name, consider adding this relationship() to the <class 'schema.Submission'> class after both dependent classes have been defined.
Add Comment
Please, Sign In to add comment