コード例 #1
0
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()
コード例 #2
0
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
        }
コード例 #3
0
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()
コード例 #4
0
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
        }
コード例 #5
0
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()
コード例 #6
0
ファイル: models.py プロジェクト: NotSoundBanana/Flask-Web
class Books(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(80), unique=False, nullable=False)
    author = db.Column(db.String(80), unique=False, nullable=False)
    content = db.Column(db.String(80), unique=False, nullable=False)
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
    # user_id - вторичный ключ, который связывает 2 таблицы
    # Пользователь пишет Новость(их может быть несколько), Новость принадлежит Пользователю, свзяь Один-Ко-Многим
    user = db.relationship('User', backref=db.backref('books_list', lazy=True))
    link = db.Column(db.String, unique=False, nullable=False)

    # ссылка на модель (класс) выше
    # для User возвращает список его новостей по .user_news

    def __repr__(self):
        return '<Books {} {} {}>'.format(self.id, self.title, self.user_id)

    @staticmethod
    def add(title, author, content, link, user):
        book = Books(title=title,
                     author=author,
                     content=content,
                     link=link,
                     user=user)
        db.session.add(book)
        db.session.commit()
        return book

    @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,
            'author': self.author,
            'content': self.content,
            'link': self.link,
            'user_id': self.user_id
        }
コード例 #7
0
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
        }