class Lesson(db.Model): id = db.Column(db.Integer, primary_key=True) user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) average_mark = db.Column(db.String(1000), nullable=False) all_marks = db.Column(db.String(1000), nullable=False) homework = db.Column(db.String(1000), nullable=False) def __repr__(self): return '<Lesson >'.format(self.id, self.login, self.password, self.user_id) @staticmethod def add(user_id, average_mark, all_marks, homework): lesson = Lesson(user_id=user_id, average_mark=average_mark, all_marks=all_marks, homework=homework) db.session.add(lesson) db.session.commit() @staticmethod def delete(lesson): db.session.delete(lesson) db.session.commit()
class News(db.Model): id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(80), unique=False, nullable=False) content = db.Column(db.String(80), unique=False, nullable=True) user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) user = db.relationship('User', backref=db.backref('product_list', lazy=True)) def __repr__(self): return '<Products {} {} {}>'.format(self.id, self.title, self.user_id) @staticmethod def add(title, content, user): news = News(title=title, content=content, user=user) db.session.add(news) db.session.commit() return news @staticmethod def delete(obj): db.session.delete(obj) db.session.commit() @property def serialize(self): """Return object data in easily serializable format""" return { 'id': self.id, 'title': self.title, 'content': self.content, 'user_id': self.user_id }
class Bookmark(db.Model): # User ID in the app database id = db.Column(db.Integer, primary_key=True) # Binding a bookmark to a specific book, which is in the "now on reading" list book_id = db.Column(db.Integer, db.ForeignKey('book.id'), nullable=False) book = db.relationship('Book', backref=db.backref('bookmark_list', lazy=True)) # The page where the bookmark is located page = db.Column(db.String(1000), unique=True, nullable=False) # The name of the bookmark which is specified by the user title = db.Column(db.String(1000), nullable=False) # Table view as a class def __repr__(self): return '<Bookmark {} {} {} {}>'.format(self.id, self.book_id, self.page, self.title) # Add new bookmark function @staticmethod def add(book, bookmark_page, bookmark_title): bookmark = Bookmark(book=book, page=bookmark_page, title=bookmark_title) db.session.add(bookmark) db.session.commit() # Delete bookmark function @staticmethod def delete(bookmark): db.session.delete(bookmark) db.session.commit()
class User(db.Model): # User ID in the app database id = db.Column(db.Integer, primary_key=True) # User ID given by Alice alice_id = db.Column(db.String(1000), unique=True, nullable=False) # The user name that he specifies by himself name = db.Column(db.String(1000), nullable=False) # Table view as a class def __repr__(self): return '<User {} {} {}>'.format(self.id, self.alice_id, self.name) # Add new user function (you must specify the id) @staticmethod def add(user_alice_id, user_name): user = User(alice_id=user_alice_id, name=user_name) db.session.add(user) db.session.commit() # Delete user function @staticmethod def delete(user): db.session.delete(user) db.session.commit()
class Book(db.Model): # User ID in the app database id = db.Column(db.Integer, primary_key=True) # Binding a book to a specific user user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) user = db.relationship('User', backref=db.backref('book_list', lazy=True)) # Book title title = db.Column(db.String(1000), nullable=False) # Book author author = db.Column(db.String(1000), nullable=False) # The status of the book: # 0 - book in the list of wishful books # 1 - book in the list of now on reading books # 2 - book in the list of already read books status = db.Column(db.Integer, nullable=False) # Table view as a class def __repr__(self): return '<Book {} {} {} {} {}>'.format(self.id, self.user_id, self.title, self.author, self.status) # Add new book function (you must specify the id) @staticmethod def add(user, book_title, book_author, book_status): book = Book(user=user, title=book_title, author=book_author, status=book_status) db.session.add(book) db.session.commit() # The function of changing the status of the book @staticmethod def change_status(book, book_new_status): book.status = book_new_status db.session.commit() # Delete book from all lists function @staticmethod def delete(book): db.session.delete(book) db.session.commit()
class User(db.Model): id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(80), unique=True, nullable=False) password = db.Column(db.String(80), unique=False, nullable=False) def __repr__(self): return '<User {} {}>'.format(self.id, self.username) @staticmethod def add(username, password): user = User(username=username, password=password) db.session.add(user) db.session.commit() @staticmethod def delete(user): db.session.delete(user) db.session.commit()
class Region(db.Model): id = db.Column(db.Integer, primary_key=True) regionid = db.Column(db.Integer, nullable=False) def __repr__(self): return '<Region {} {}>'.format(self.id, self.regionid) @staticmethod def add(regionid): region = Region(regionid=regionid) db.session.add(region) db.session.commit() @staticmethod def delete(region): db.session.delete(region) db.session.commit()
class User(db.Model): id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(80), unique=True, nullable=False) password = db.Column(db.String(80), unique=False, nullable=False) # будем хранить хэш пароля admin = db.Column(db.Boolean, unique=False, nullable=False) def __repr__(self): return '<User {} {}>'.format(self.id, self.username) @staticmethod def add(username, password, admin): user = User(username=username, password=password, admin=admin) db.session.add(user) db.session.commit() @staticmethod def delete(obj): db.session.delete(obj) db.session.commit()
class City(db.Model): id = db.Column(db.Integer, primary_key=True) region_id = db.Column(db.Integer, nullable=False) cityid = db.Column(db.Integer, nullable=False) name = db.Column(db.String(1000), nullable=False) def __repr__(self): return '<City {} {} {}>'.format(self.id, self.name, self.cityid) @staticmethod def add(cityid, name, region_id): city = City(cityid=cityid, name=name, region_id=region_id) db.session.add(city) db.session.commit() @staticmethod def delete(city): db.session.delete(city) db.session.commit()
class School(db.Model): id = db.Column(db.Integer, primary_key=True) city_id = db.Column(db.Integer, nullable=False) schoolid = db.Column(db.Integer, nullable=False) name = db.Column(db.String(1000), nullable=False) def __repr__(self): return '<School {} {} {}>'.format(self.id, self.name, self.schoolid) @staticmethod def add(schoolid, name, city_id): school = School(schoolid=schoolid, name=name, city_id=city_id) db.session.add(school) db.session.commit() @staticmethod def delete(school): db.session.delete(school) db.session.commit()
class Post(db.Model): id = db.Column(db.Integer, primary_key=True) from_who = db.Column(db.String(80), unique=False, nullable=False) to = db.Column(db.String(80), unique=False, nullable=False) message = db.Column(db.String(1000), unique=False, nullable=True) new = db.Column(db.Boolean, unique=False, nullable=False) user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) user = db.relationship('User', backref=db.backref('messages', lazy=True)) def __repr__(self): return '<Message {} {} {}>'.format(self.id, self.message, self.user_id) @staticmethod def add(from_who, to, message, user): message = Post(from_who=from_who, to=to, message=message, new=True, user=user) db.session.add(message) db.session.commit() return message @staticmethod def delete(obj): db.session.delete(obj) db.session.commit()
class Character(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(80), unique=True, nullable=False) title = db.Column(db.String(80), unique=False, nullable=False) city = db.Column(db.String(80), unique=False, nullable=True) # пусть текст можно будет оставить пустым age = db.Column(db.Integer, unique=False, nullable=True) info = db.Column(db.String(1000), unique=False, nullable=True) ispublic = db.Column(db.Boolean) user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) user = db.relationship('User', backref=db.backref('character_list', lazy=True)) # ссылка на модель (класс) выше # для User возвращает список его новостей по .user_character def __repr__(self): return '<Character {} {} {}>'.format(self.id, self.title, self.user_id) @staticmethod def add(name, title, city, age, info, ispublic, user): character = Character(name=name, title=title, city=city, age=age, info=info, ispublic=ispublic, user=user) db.session.add(character) db.session.commit() return character @staticmethod def delete(obj): db.session.delete(obj) db.session.commit() @property def serialize(self): """Return object data in easily serializable format""" return { 'id': self.id, 'title': self.title, 'name': self.name, 'city': self.city, 'info': self.info, 'age': self.age, 'ispublic': self.ispublic, 'user_id': self.user_id }
class News(db.Model): id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(80), unique=False, nullable=False) content = db.Column( db.String(80), unique=False, nullable=True) # пусть текст можно будет оставить пустым user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) # user_id - вторичный ключ, который связывает 2 таблицы # Пользователь пишет Новость(их может быть несколько), Новость принадлежит Пользователю, свзяь Один-Ко-Многим user = db.relationship('User', backref=db.backref('news_list', lazy=True)) # ссылка на модель (класс) выше # для User возвращает список его новостей по .user_news def __repr__(self): return '<News {} {} {}>'.format(self.id, self.title, self.user_id) @staticmethod def add(title, content, user): news = News(title=title, content=content, user=user) db.session.add(news) db.session.commit() return news @staticmethod def delete(obj): db.session.delete(obj) db.session.commit() @property def serialize(self): """Return object data in easily serializable format""" return { 'id': self.id, 'title': self.title, 'content': self.content, 'user_id': self.user_id }
class User(db.Model): id = db.Column(db.Integer, primary_key=True) user_id = db.Column(db.String(1000), nullable=False) login = db.Column(db.String(1000), nullable=False, unique=True) password = db.Column(db.String(1000), nullable=False) region_id = db.Column(db.Integer, nullable=False) city_name = db.Column(db.String(1000), nullable=False) city_id = db.Column(db.Integer, nullable=False) school_name = db.Column(db.String(1000), nullable=False) school_id = db.Column(db.Integer, nullable=False) def __repr__(self): return '<User {} {} {} {} {} {} {}>'.format( self.id, self.login, self.password, self.user_id, self.region_id, self.city_id, self.school_id) @staticmethod def add(user_id, login, password, region_id, city_id, school_id): user = User(user_id=user_id, login=login, password=password, region_id=region_id, city_id=city_id, school_id=school_id) db.session.add(user) db.session.commit() @staticmethod def delete(user): db.session.delete(user) db.session.commit()
class Surveys(db.Model): id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(80), unique=False, nullable=False) category = db.Column(db.String(80), unique=False, nullable=False) publicity_check = db.Column(db.Boolean, unique=False, default=False) on_admin_check = db.Column(db.Boolean, unique=False, default=True) yes_ans = db.Column(db.Integer, default=0) no_ans = db.Column(db.Integer, default=0) user = db.relationship('User', backref=db.backref('survey_list', lazy=True)) user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) voted_users_id = db.Column(db.String, default='') def __repr__(self): return '<Surveys {} {} {} {} {} {} {} {} {}>'.format(self.id, self.title, self.category, self.publicity_check, self.on_admin_check, self.yes_ans, self.no_ans, self.user_id, self.voted_users_id) @staticmethod def add(title, category, publicity_check, on_admin_check, user): survey = Surveys(title=title, category=category, publicity_check=publicity_check, on_admin_check=on_admin_check, user=user) db.session.add(survey) db.session.commit() @staticmethod def delete(survey): db.session.delete(survey) db.session.commit() @staticmethod def show(survey): survey.publicity_check = True db.session.commit() @staticmethod def hide(survey): survey.publicity_check = False db.session.commit() @staticmethod def mark_as_checked(survey): survey.on_admin_check = False db.session.commit() @staticmethod def plus_yes(survey): survey.yes_ans += 1 db.session.commit() @staticmethod def plus_no(survey): survey.no_ans += 1 db.session.commit() @staticmethod def vote_add(survey, user_id): survey.voted_users_id += ' ' + str(user_id) db.session.commit() @property def serialize(self): return { 'id': self.id, 'title': self.title, 'category': self.category, 'on_admin_check': self.on_admin_check, 'yes_ans': self.yes_ans, 'no_ans': self.no_ans, 'publicity_check': self.publicity_check, 'user_id': self.user_id }