Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from flask import Flask
- from flaskext.sqlalchemy import SQLAlchemy
- from datetime import datetime
- from sqlalchemy.ext.declarative import declarative_base
- from sqlalchemy import MetaData
- from sqlalchemy.orm.collections import attribute_mapped_collection
- from sqlalchemy import orm
- metadata = MetaData()
- Base = declarative_base(metadata=metadata)
- app = Flask(__name__)
- app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////tmp/test.db'
- db = SQLAlchemy(app)
- class User(db.Model):
- id = db.Column(db.Integer, primary_key=True)
- username = db.Column(db.String(80), unique=True)
- password = db.Column(db.String(80))
- email = db.Column(db.String(120))
- date_joined = db.Column(db.DateTime)
- submissions = db.relationship('Submission', backref=db.backref('user', lazy='dynamic'))
- comments = db.relationship('Comment', backref=db.backref('user', lazy='dynamic'))
- karma = db.Column(db.Integer)
- def __init__(self, username, password, email, date_joined=None, karma=0):
- self.username = username
- self.password = password
- self.email = email
- if date_joined is None:
- date_joined = datetime.utcnow()
- self.date_joined = date_joined
- self.karma = karma
- def __repr__(self):
- return '<User %r>' % self.username
- class Submission(db.Model):
- id = db.Column(db.Integer, primary_key=True)
- user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
- date_submitted = db.Column(db.DateTime)
- title = db.Column(db.String(100))
- body = db.Column(db.Text)
- link = db.Column(db.Text)
- points = db.Column(db.Integer)
- comments = db.relationship('Comment', backref=db.backref('submission', lazy='dynamic'))
- def __init__(self, user_id, title, date_submitted=None, body=None, link=None, points=1):
- self.user_id = user_id
- self.title = title
- if date_submitted is None:
- date_submitted = datetime.utcnow()
- self.body = body
- self.link = link
- self.points = points
- def __repr__(self):
- return '<Submission %r>' % self.title
- class Comment(Base):
- __tablename__ = 'comment'
- id = db.Column(db.Integer, primary_key=True)
- user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
- submission_id = db.Column(db.Integer, db.ForeignKey('submission.id'))
- parent_id = db.Column(db.Integer, db.ForeignKey('comment.id'))
- children = db.relation('Comment', backref=db.backref('parent', remote_side=[id]))
- date_submitted = db.Column(db.DateTime)
- body = db.Column(db.Text)
- points = db.Column(db.Integer)
- def __init__(self, user_id, submission_id, body, parent_id=None, date_submitted=None, points=1):
- self.user_id = user_id
- self.submission_id = submission_id
- self.body = body
- self.parent_id = parent_id
- if date_submitted is None:
- date_submitted = datetime.utcnow()
- points = points
- def append(self, user_id, submission_id, body, parent_id=None, date_submitted=None, points=1):
- if parent_id is None:
- parent_id = self.id
- new_comment = Comment(user_id, submission_id, body, parent_id, date_submitted, points)
- self.children[new_comment.id] = new_comment
- class Points(db.Model):
- id = db.Column(db.Integer, primary_key=True)
- value = db.Column(db.Integer)
- user_id_voted = db.Column(db.Integer)
- user_id_received = db.Column(db.Integer)
- comment_id = db.Column(db.Integer)
- submission_id = db.Column(db.Integer)
- date_counted = db.Column(db.DateTime)
- def __init__(self, value, user_id_voted, user_id_received, comment_id=None, submission_id=None, date_counted=None):
- self.value = value
- self.user_id_voted = user_id_voted
- self.user_id_received = user_id_received
- self.comment_id = comment_id
- self.submission_id = submission_id
- if date_counted is None:
- self.date_counted = datetime.utcnow()
- 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