class User(db.Model, UserMixin): # setting the name to 'users' __tablename__ = 'users' # setting the user id to an integer and making it to a unique primary key which auto-increments id = db.Column(db.Integer, primary_key=True) # setting the profile image to a string of 64 characters using the nullable to false to force the user to insert # an image and if they have none they are given a static preloaded image profile_image = db.Column(db.String(64), nullable=False, default='default_profile.png') # emails and username are strings of 64 characters which are unique and indexed email = db.Column(db.String(64), unique=True, index=True) username = db.Column(db.String(64), unique=True, index=True) # passwords are 128 character strings password_hash = db.Column(db.String(128)) # relationships are set to blog posts by mentioning the particular model they have relationships with, # they are given a reference called author which we use for reference to this relationship to template and they # are lazily loaded posts = db.relationship('BlogPost', backref='author', lazy=True) # we instantiate the User model class def __init__(self, email, username, password): self.email = email self.username = username # here the password_hashed is set to a werkzeug generated representation of the password self.password_hash = generate_password_hash(password) # check password function checks the hashed password and also that we inputted def check_password(self, password): return check_password_hash(self.password_hash, password) # we produce a printed version or representative for debugging purposes def __repr__(self): return f"Username {self.username}"
class User(db.Model,UserMixin): __tablename__ = 'users' id = db.Column(db.Integer, primary_key=True) profile_image = db.Column(db.String(64), nullable=False, default='default_profile.png') email = db.Column(db.String(),unique=True, index=True) username = db.Column(db.String(64),unique=True, index=True) password_hash = db.Column(db.String(128)) posts = db.relationship('BlogPost', backref='author', lazy=True) def __init__(self,email,username,password): self.email = email self.username = username self.password_hash = generate_password_hash(password) def check_password(self,password): return check_password_hash(self.password_hash,password) def __repr__(self): return 'Username {self.username}'
class BlogPost(db.Model): # we set up and reference the relationship between the blog post and a User users = db.relationship(User) # we set up a column for the id of the blog post which is a primary key id = db.Column(db.Integer, primary_key=True) # we set up the user_id which is an integer which is an integer which is a foreign key referencing the id of the # users table user_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False) # we set it to a date time column with the default set to the present date and time date = db.Column(db.DateTime, nullable=False, default=datetime.utcnow) # we set the title to a string of 140 characters which must be filled title = db.Column(db.String(140), nullable=False) # text set to a db text and must be inputted text = db.Column(db.Text, nullable=False) # we then set the title text and userid as a representation attributes of the blog post class def __init__(self, title, text, user_id): self.title = title self.text = text self.user_id = user_id # we then set up a representation of this class which can be used for debugging or testing purposes def __repr__(self): return f"Post Id: {self.id} --- Date: {self.date} --- Title: {self.title}"
class User(db.Model, UserMixin): __tablename__ = 'users' id = db.Column(db.Integer, primary_key=True) profile_image = db.Column(db.String(64), nullable=False, default='default_profile.png') email = db.Column(db.String(64), unique=True, index=True) username = db.Column(db.String(64), unique=True, index=True) password_hash = db.Column(db.String(128)) posts = db.relationship('BlogPost', backref='author', lazy=True) def __init__(self, email, username, password): self.email = email self.username = username self.password_hash = generate_password_hash(password) def check_password(self, password): return check_password_hash(self.password_hash, password) def to_json(self, user_id=None): json_user = { 'url': url_for('users_api.get_user', username=self.username), 'username': self.username, 'posts_url': url_for('users_api.get_user_posts', username=self.username), 'posts_count': len(self.posts) } if user_id is not None and user_id == self.id: json_user['email'] = self.email return json_user def __repr__(self): return f"Username {self.username}"
class BlogPost(db.Model): users = db.relationship(User) id = db.Column(db.Integer, primary_key=True) user_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False) date = db.Column(db.DateTime, nullable=False, default=datetime.utcnow) title = db.Column(db.String(200), nullable=False) text = db.Column(db.Text, nullable=False) def __init__(self, title, text, user_id): self.title = title self.text = text self.user_id = user_id def __repr__(self): return f"Post ID: {self.id} -- Date: {self.date} --- {self.title}"
class BlogPost(db.Model): users = db.relationship(User) id = db.Column(db.Integer, primary_key=True) user_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False) date = db.Column(db.DateTime, nullable=False, default=datetime.utcnow) title = db.Column(db.String(140), nullable=False) text = db.Column(db.Text, nullable=False) def __init__(self, title, text, user_id): self.title = title self.text = text self.user_id = user_id def to_json(self): json_post = { 'url': url_for('blog_posts_api.get_post', post_id=self.id), 'date': self.date, 'title': self.title, 'text': self.text, 'author': self.author.username, 'author_url': url_for('users_api.get_user', username=self.author.username) } return json_post @staticmethod def from_json(json_post, user_id): title = json_post.get('title') text = json_post.get('text') if title is None or title == '': raise ValidationError('Post has no title.') if text is None or text == '': raise ValidationError('Post has no text.') return BlogPost(title=title, text=text, user_id=user_id) def __repr__(self): return f"Post ID: {self.id} -- Date: {self.date} -- {self.title}"
class Doctors(db.Model,UserMixin): __tablename__ = 'Doctors' id = db.Column(db.Integer,primary_key = True) name = db.Column(db.String(20),nullable = False) email = db.Column(db.String(64),unique = True,index = True) visiting_hours = db.Column(db.String(20),nullable = False) qualification = db.Column(db.String(50),nullable = False) specializaton = db.Column(db.String(140),nullable = False) consultation_fees = db.Column(db.Integer, nullable = False) profile_image = db.Column(db.String(64),default = 'default_image.jpg') experience = db.Column(db.Text,nullable = False) contact_number = db.Column(db.String(10),nullable = False) description = db.Column(db.Text,nullable = False) reviews = db.realtionship('Reviews',backref = 'doctor', lazy = True) hospitals = db.realtionship('Hospitals',backref = 'doctor',lazy = True)