class Section(db.Model): __tablename__ = 'sections' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(100)) created_date = db.Column(db.DateTime, default=datetime.now()) course_id = db.Column(db.Integer, db.ForeignKey('courses.id'), nullable=False) quizzes = db.relationship('Quiz', backref='section', lazy=True, cascade="all, delete, delete-orphan") quiz_histories = db.relationship('QuizHistory', backref='section', lazy=True, cascade="all, delete, delete-orphan") sub_sections = db.relationship('SubSection', backref='section', lazy=True, cascade="all, delete, delete-orphan") def insert(self): db.session.add(self) db.session.commit() def update(self): db.session.commit() def delete(self): db.session.delete(self) db.session.commit()
class Grade(db.Model): __tablename__ = 'grades' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(100)) courses = db.relationship('Course', backref='grade', lazy=True, cascade="all, delete, delete-orphan") light_courses = db.relationship('LightCourse', backref='grade', lazy=True, cascade="all, delete, delete-orphan") students = db.relationship('User', backref='grade', lazy=True, cascade="all, delete, delete-orphan") def insert(self): db.session.add(self) db.session.commit() def update(self): db.session.commit() def delete(self): db.session.delete(self) db.session.commit() def __repr__(self): return self.name
class Size(PkModel): __tablename__ = "size" name = db.Column(db.String(100)) product_id = db.Column(db.Integer, db.ForeignKey('product.id'))
class ExampleUserModel(PkModel, UserMixin): """Example user model for testing purposes""" __tablename__ = "testusers" username = db.Column(db.String(100), unique=True, nullable=False) email = db.Column(db.String(120), unique=True, nullable=False)
class Quiz(db.Model): __tablename__ = 'quizes' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(100)) question = db.Column(db.String(100)) section_id = db.Column(db.Integer, db.ForeignKey('sections.id'), nullable=False) # https://stackoverflow.com/questions/60805/getting-random-row-through-sqlalchemy answers = db.relationship('Answer', backref='quiz', lazy=True, cascade="all, delete, delete-orphan", order_by='func.random()') def insert(self): db.session.add(self) db.session.commit() def update(self): db.session.commit() def delete(self): db.session.delete(self) db.session.commit()
class BillingDetail(db.Model): __tablename__ = "billing_details" id = db.Column(db.Integer, primary_key=True) first_name = db.Column(db.String(100)) last_name = db.Column(db.String(100)) country = db.Column(db.String(100)) street = db.Column(db.String(100)) town_city = db.Column(db.String(100)) phone = db.Column(db.String(100)) email = db.Column(db.String(100)) order_notes = db.Column(db.String(100)) order_id = db.Column(db.Integer, db.ForeignKey("orders.id")) def add(self): db.session.add(self) def insert(self): db.session.add(self) db.session.commit() def update(self): db.session.commit() def delete(self): db.session.delete(self) db.session.commit()
class Appointments(db.Model): __tablename__ = "appointments" id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(100)) date = db.Column(db.String(20)) time = db.Column(db.String(20)) active = db.Column(db.String(20))
class Category(db.Model): __tablename__ = "category" id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(100), unique=True) products = db.relationship("Product", backref="categories", lazy=True, cascade="all, delete") def insert(self): """Save category to the database""" db.session.add(self) db.session.commit() def update(self): """Update category""" db.session.commit() def delete(self): """delete category""" db.session.delete(self) db.session.commit() @classmethod def category_exists(cls, name): return db.session.query(exists().where(cls.name == name)).scalar()
class User(UserMixin, PkModel): """The user of the app""" __tablename__ = "users" username = db.Column(db.String(100), unique=True) _password = db.Column(db.String(128), nullable=False) first_name = db.Column(db.String(128)) last_name = db.Column(db.String(128)) is_admin = db.Column(db.Boolean, default=False) email = db.Column(db.String(120), unique=True, nullable=False) date_registered = db.Column(db.DateTime, nullable=False, default=datetime.datetime.now()) is_email_confirmed = db.Column(db.Boolean(), nullable=False, default=False) email_confirm_date = db.Column(db.DateTime) is_customer = db.Column(db.Boolean, default=False) # A user can have many roles and a role can have many users roles = db.relationship( "Role", secondary=role_user_link, backref="users", ) @hybrid_property def password(self): return self._password @password.setter def password(self, plaintext): self._password = generate_password_hash(plaintext, method="sha256") def check_hash(self, password): return check_password_hash(self._password, password) def generate_confirmation_token(self, email): serializer = URLSafeTimedSerializer(app.config["SECRET_KEY"]) return serializer.dumps(email, salt=app.config["PASSWORD_SALT"]) @staticmethod def confirm_mail_token(self, token, expiration=3600): serializer = URLSafeTimedSerializer(app.config["SECRET_KEY"]) try: email = serializer.loads(token, salt=app.config["PASSWORD_SALT"], max_age=expiration) except: return False return email def __repr__(self): return "User: {}".format(self.email)
class Resource(db.Model): __tablename__ = "resources" id = db.Column(db.Integer, primary_key=True) filename = db.Column(db.String(50), nullable=False) type = db.Column(db.String(50), nullable=False) category = db.Column(db.String(50), nullable=False) created_date = db.Column(db.DateTime, default=datetime.datetime.now(), nullable=False) # product_id = db.Column(db.Integer, db.ForeignKey('product.id'), nullable=True) category_id = db.Column(db.Integer, db.ForeignKey('categories.id'), nullable=False) subcategory_id = db.Column(db.Integer, db.ForeignKey('subcategories.id'), nullable=True) def insert(self): db.session.add(self) db.session.commit() def update(self): db.session.commit() def delete(self): db.session.delete(self) db.session.commit()
class SubCategory(PkModel): __tablename__ = "subcategories" name = db.Column(db.String(100), nullable=False) category_id = db.Column(db.Integer, db.ForeignKey("categories.id")) products = db.relationship("Product", backref="subcategory", lazy=True) resources = db.relationship("Resource", backref="resource_subcategory", lazy=True) @classmethod def category_exists(cls, name): return db.session.query( exists().where(cls.name == name.lower())).scalar() @validates("name") def convert_lower(self, key, value): return value.lower() def get_num_products(self): return len(self.products) def get_one_image_url(self): if len(self.products) > 0: product = self.products[0] if len(product.resources) == 0: if len(self.resources) == 0: return url_for('static', filename='default/default_subcategory.jpg') else: resource = self.resources[0] return url_for('static', filename='uploads/subcategory/{}'.format( resource.filename)) else: resource = product.resources[0] return url_for('static', filename='uploads/products/{}'.format( resource.filename)) else: if len(self.resources) == 0: return url_for('static', filename='default/default_subcategory.jpg') else: resource = self.resources[0] return url_for('static', filename='uploads/subcategory/{}'.format( resource.filename))
class Setting(db.Model): __tablename__ = 'settings' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(200)) value = db.Column(db.String(200)) def insert(self): db.session.add(self) db.session.commit() def update(self): db.session.commit() def delete(self): db.session.delete(self) db.session.commit()
class Role(db.Model): __tablename__ = "roles" id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(100)) def insert(self): db.session.add(self) db.session.commit() def update(self): db.session.commit() def delete(self): db.session.delete(self) db.session.commit()
class PkModel(YoModel): """ Base model class that includes CRUD convenience methods, plus adds a 'primary key' column named 'id'. """ __abstract__ = True id = db.Column(db.Integer, primary_key=True) @classmethod def get_by_id(cls, record_id): """Get record by ID. Args: record_id (int): ID of record to get Returns: DB Class object: object identified by record_id if any, None otherwise """ if any(( isinstance(record_id, (str, bytes)) and record_id.isdigit(), isinstance(record_id, (int, float)), )): return cls.query.get(int(record_id)) return None
class Product(db.Model): __tablename__ = "product" barcode = db.Column(db.String(100), primary_key=True) price = db.Column(db.Float) name = db.Column(db.String(100)) description = db.Column(db.String(300)) date = db.Column(db.String(100)) in_stock = db.Column(db.Integer) discontinued = db.Column(db.Boolean) selling_price = db.Column(db.Float) category_name = db.Column(db.String(100), db.ForeignKey("category.name"), nullable=False)
class OrderItem(PkModel): __tablename__ = "order_items" time = db.Column(db.DateTime, default=datetime.now()) quantity = db.Column(db.Integer) color = db.Column(db.String(100)) size = db.Column(db.String(100)) status = db.Column(db.String(120), default="pending") barcode = db.Column(db.String(100), nullable=False) order_id = db.Column(db.Integer, db.ForeignKey('orders.id'), nullable=False) def add(self): db.session.add(self) def insert(self): db.session.add(self) db.session.commit() def update(self): db.session.commit() def delete(self): db.session.delete(self) db.session.commit() def get_product(self): return Product.query.filter_by(barcode=self.barcode).first()
class LearningPath(db.Model): __tablename__ = 'learning_paths' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(100)) courses = db.relationship("Course", secondary=learningpath_subs, cascade="all, delete") def insert(self): db.session.add(self) db.session.commit() def update(self): db.session.commit() def delete(self): db.session.delete(self) db.session.commit()
class Homework(db.Model): __tablename__ = 'homeworks' id = db.Column(db.Integer, primary_key=True) subsection_id = db.Column(db.Integer, db.ForeignKey('sub_sections.id'), nullable=False) filename = db.Column(db.String(100)) # uploads/homework/ def insert(self): db.session.add(self) db.session.commit() def update(self): db.session.commit() def delete(self): db.session.delete(self) db.session.commit()
class Settings(db.Model): __tablename__ = "settings" setting = db.Column(db.String(100), primary_key=True) value = db.Column(db.String(100)) def add(self): db.session.add(self) def insert(self): db.session.add(self) db.session.commit() def update(self): db.session.commit() def delete(self): db.session.delete(self) db.session.commit()
class Page(db.Model): __tablename__ = "pages" id = db.Column(db.Integer, primary_key=True) created_date = db.Column(db.DateTime, default=datetime.now()) title = db.Column(db.String(100)) slug = db.Column(db.String(100)) content = db.Column(db.String(1024)) def insert(self): db.session.add(self) db.session.commit() def update(self): db.session.commit() def delete(self): db.session.delete(self) db.session.commit()
class ContactMessage(db.Model): __tablename__ = "contact" id = db.Column(db.Integer, primary_key=True) created_date = db.Column(db.DateTime, default=datetime.now()) name = db.Column(db.String(100)) email = db.Column(db.String(100)) message = db.Column(db.String(1024)) def insert(self): db.session.add(self) db.session.commit() def update(self): db.session.commit() def delete(self): db.session.delete(self) db.session.commit()
class Answer(db.Model): __tablename__ = 'answers' id = db.Column(db.Integer, primary_key=True) string = db.Column(db.String(100)) correct = db.Column(db.Boolean) quizz_id = db.Column(db.Integer, db.ForeignKey('quizes.id'), nullable=False) def insert(self): db.session.add(self) db.session.commit() def update(self): db.session.commit() def delete(self): db.session.delete(self) db.session.commit()
class PaymentOption(db.Model): __tablename__ = "paymentoptions" id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(300)) text = db.Column(db.String(300)) order_id = db.Column(db.Integer, db.ForeignKey("orders.id")) def insert(self): db.session.add(self) db.session.commit() def update(self): db.session.commit() def delete(self): db.session.delete(self) db.session.commit()
class DeliveryOption(db.Model): __tablename__ = "deliveryoptions" id = db.Column(db.Integer, primary_key=True) option = db.Column(db.String(300)) price = db.Column(db.Float) order_id = db.Column(db.Integer, db.ForeignKey("orders.id")) def insert(self): db.session.add(self) db.session.commit() def update(self): db.session.commit() def delete(self): db.session.delete(self) db.session.commit()
class Coupon(db.Model): __tablename__ = "coupons" id = db.Column(db.Integer, primary_key=True) string = db.Column(db.String(300)) type = db.Column(db.String(300)) # percentage, value value = db.Column(db.String(300)) order_id = db.Column(db.Integer, db.ForeignKey("orders.id")) def insert(self): db.session.add(self) db.session.commit() def update(self): db.session.commit() def delete(self): db.session.delete(self) db.session.commit()
class CertificateRequest(db.Model): __tablename__ = 'certificate_requests' id = db.Column(db.Integer, primary_key=True) course_taker_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False) course_id = db.Column(db.Integer, db.ForeignKey('courses.id'), nullable=False) def insert(self): db.session.add(self) db.session.commit() def update(self): db.session.commit() def delete(self): db.session.delete(self) db.session.commit()
class LightHomeworkSubmission(db.Model): __tablename__ = 'light_homework_submissions' id = db.Column(db.Integer, primary_key=True) course_taker_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False) chapter_id = db.Column(db.Integer, db.ForeignKey('light_chapters.id'), nullable=False) filename = db.Column(db.String(100)) def insert(self): db.session.add(self) db.session.commit() def update(self): db.session.commit() def delete(self): db.session.delete(self) db.session.commit()
class ChapterHistory(db.Model): __tablename__ = 'section_histories' id = db.Column(db.Integer, primary_key=True) person_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False) subsection_id = db.Column(db.Integer, db.ForeignKey('sub_sections.id'), nullable=False) completed = db.Column(db.Boolean, default=True, nullable=True) def insert(self): db.session.add(self) db.session.commit() def update(self): db.session.commit() def delete(self): db.session.delete(self) db.session.commit()
class Course(db.Model): __tablename__ = 'courses' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(100)) created_date = db.Column(db.DateTime, default=datetime.now()) submitted = db.Column(db.Boolean, default=False) teacher_id = db.Column(db.Integer, nullable=False) sections = db.relationship('Section', backref='course', lazy=True, cascade="all, delete, delete-orphan") grade_id = db.Column(db.Integer, db.ForeignKey('grades.id'), nullable=False) def insert(self): db.session.add(self) db.session.commit() def update(self): db.session.commit() def delete(self): db.session.delete(self) db.session.commit()
class Certificate(db.Model): __tablename__ = 'certificates' id = db.Column(db.Integer, primary_key=True) course_taker_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False) course_id = db.Column(db.Integer, db.ForeignKey('courses.id'), nullable=False) date_given = db.Column(db.DateTime, default=datetime.now()) filename = db.Column(db.String(100)) def insert(self): db.session.add(self) db.session.commit() def update(self): db.session.commit() def delete(self): db.session.delete(self) db.session.commit()