コード例 #1
0
ファイル: models.py プロジェクト: debu999/flaskmicroframework
class Bookmark(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    url = db.Column(db.Text, nullable=False)
    date = db.Column(db.DateTime, default=datetime.utcnow)
    description = db.Column(db.String(300))
    bkmrkusername = db.Column(db.String(400))
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
    _tags = db.relationship("Tag", secondary=tags, backref=db.backref("bookmarks", lazy="dynamic"))

    @property
    def tags(self):
        return ",".join([t.name for t in self._tags])

    @tags.setter
    def tags(self, string):
        if string:
            self._tags = [Tag.get_or_create(name) for name in string.split(",")]


    @classmethod
    def newbookmarks(cls, num):
        return cls.query.filter_by(user=current_user).order_by(desc(cls.date)).limit(num)

    def __repr__(self):
        return "<Bookmark '{}': '{}'>".format(self.url, self.description)
コード例 #2
0
ファイル: models.py プロジェクト: milagan/flask-web-template
class Bookmark(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    url = db.Column(db.Text, nullable=False)
    date = db.Column(db.DateTime, default=datetime.utcnow)
    description = db.Column(db.String(300))
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
    _tags = db.relationship('Tag',
                            secondary=tags,
                            lazy='joined',
                            backref=db.backref('bookmarks', lazy='dynamic'))

    @staticmethod
    def newest(num):
        return Bookmark.query.order_by(desc(Bookmark.date)).limit(num)

    @property
    def tags(self):
        return ",".join([t.name for t in self._tags])

    @tags.setter
    def tags(self, string):
        if string:
            self._tags = [
                Tag.get_or_create(name) for name in string.split(',')
            ]
        else:
            self._tags = []

    def __repr__(self):
        return "<Bookmark '{}': '{}'>".format(self.description, self.url)
コード例 #3
0
class Bookmark(db.Model):
    id_bookmark = db.Column(db.Integer, primary_key=True)
    nm_url = db.Column(db.Text, nullable=False)
    dt_bookmark = db.Column(db.DateTime, default=datetime.utcnow)
    nm_description = db.Column(db.String(300))
    id_user = db.Column(db.Integer,
                        db.ForeignKey('user.id_user'),
                        nullable=False)
    _tags = db.relationship('Tag',
                            secondary=tags,
                            lazy='joined',
                            backref=db.backref('bookmarks', lazy='dynamic'))

    @property
    def tags(self):
        return ','.join(tag.nm_tag for tag in self._tags)

    @tags.setter
    def tags(self, sTags):
        if sTags:
            self._tags = [Tag.get_or_create(t) for t in sTags.split(',')]
        else:
            self._tags = []

    @staticmethod
    def newest(num):
        return Bookmark.query.order_by(desc(Bookmark.dt_bookmark)).limit(num)

    def __repr__(self):
        return "<Bookmark '{}': '{}'>".format(self.nm_description, self.nm_url)
コード例 #4
0
class Bookmark(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    url = db.Column(db.Text, nullable=False)
    date = db.Column(db.DateTime, default=datetime.now)
    description = db.Column(db.String(300))
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)

    @staticmethod
    def newest(num):
        return Bookmark.query.order_by(desc(Bookmark.date)).limit(num)

    def __repr__(self):
        return "<Bookmark '{}': '{}'".format(self.description, self.url)
コード例 #5
0
ファイル: models.py プロジェクト: davedavis/thermos
class Bookmark(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    url = db.Column(db.Text, nullable=False)
    date = db.Column(db.DateTime, default=datetime.utcnow)
    description = db.Column(db.String(300))
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
    # Set the relationship with the Tag class, need to use a string as it's not defined until later. ToDo: Move it up
    # and replace it with the class name for future proofing. The second argument to the relationship call is the
    # argument 'secondary' which tells the relationship to use our junction table called "tags" above. We also defined
    # a backref called 'bookmarks' which will add an attribute called bookmarks to the other side of the relationship.
    # So each tag will get a bookmarks attribute containing a list of the associated bookmarks. Dynamic loading in case
    # there are a large number of bookmarks associated with each tag.
    # Underscore as I don't want to access this directly from other classes.
    _tags = db.relationship('Tag',
                            secondary=tags,
                            lazy='joined',
                            backref=db.backref('bookmarks', lazy='dynamic'))

    @staticmethod
    def newest(num):
        return Bookmark.query.order_by(desc(Bookmark.date)).limit(num)

    # In the view and the form, we handle the list of tags as a comma separated string. So it's convenient to create a
    # tag property that provides a list of strings as well. The getter takes the contents of the _tags list which holds
    # actual Tag model objects. Then it takes the name from each and joins it into a string. So when we ask for the
    # value of the tags property on a bookmark, we get a string, with a list of tag names.
    @property
    def tags(self):
        return ",".join([t.name for t in self._tags])

    # When we pass a string with a list of tags to be set to this property, we need to find out for each of those tags
    # whether it already exists in the database. If it doesn't, we need to insert a new tag into the tag table and then
    # add the new model object to the tag list for this bookmark. If it does exist, we can simply retrieve it and put it
    # in the list. So if it exists, we create a new method get_or_create that takes the name of a tag and returns a tag
    # model instance by either creating or retrieving a tag with that name. We then do a for loop over all the words in
    # the string we received and then call the method on each of those words. The resulting list is a list of tag model
    # objects and we can assign that to the _tags attribute. Assigning a list of tag objects is all we have to do and
    # SQLAlchemy will take it from there and create all the relevant rows in the database.
    @tags.setter
    def tags(self, string):
        # Check if the tags are a string
        if string:
            self._tags = [
                Tag.get_or_create(name) for name in string.split(',')
            ]
        # If not, set the list ot an empty list, otherwise, it will always fail the test.
        else:
            self._tags = []

    def __repr__(self):
        return "Bookmark '{}': '{}'>".format(self.description, self.url)
コード例 #6
0
ファイル: models.py プロジェクト: milagan/flask-web-template
from datetime import datetime

from flask_login import UserMixin
from sqlalchemy import desc
from werkzeug.security import generate_password_hash, check_password_hash

from thermos import db

tags = db.Table(
    'bookmark_tag', db.Column('tag_id', db.Integer, db.ForeignKey('tag.id')),
    db.Column('bookmark_id', db.Integer, db.ForeignKey('bookmark.id')))


class Bookmark(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    url = db.Column(db.Text, nullable=False)
    date = db.Column(db.DateTime, default=datetime.utcnow)
    description = db.Column(db.String(300))
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
    _tags = db.relationship('Tag',
                            secondary=tags,
                            lazy='joined',
                            backref=db.backref('bookmarks', lazy='dynamic'))

    @staticmethod
    def newest(num):
        return Bookmark.query.order_by(desc(Bookmark.date)).limit(num)

    @property
    def tags(self):
        return ",".join([t.name for t in self._tags])
コード例 #7
0
from datetime import datetime

from sqlalchemy import desc
from flask_login import UserMixin
from werkzeug.security import check_password_hash, generate_password_hash

from thermos import db

tags = db.Table(
    "bookmark_tag",
    db.Column("tag_id", db.Integer, db.ForeignKey("tag.id")),
    db.Column("bookmark_id", db.Integer, db.ForeignKey("bookmark.id")),
)


class Bookmark(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    url = db.Column(db.Text, nullable=False)
    date = db.Column(db.DateTime, default=datetime.utcnow)
    description = db.Column(db.String(300))
    user_id = db.Column(db.Integer, db.ForeignKey("user.id"), nullable=False)
    _tags = db.relationship(
        "Tag",
        secondary=tags,
        lazy="joined",
        backref=db.backref("bookmarks", lazy="dynamic"),
    )

    @staticmethod
    def newest(num):
        return Bookmark.query.order_by(desc(Bookmark.date)).limit(num)
コード例 #8
0
from datetime import datetime
from sqlalchemy import desc
from flask_login import UserMixin
from werkzeug.security import generate_password_hash, check_password_hash
from thermos import db

tags = db.Table(
    'bookmark_tag',
    db.Column('id_tad',
              db.Integer,
              db.ForeignKey('tag.id_tag'),
              nullable=False),
    db.Column('id_bookmark',
              db.Integer,
              db.ForeignKey('bookmark.id_bookmark'),
              nullable=False))


class Bookmark(db.Model):
    id_bookmark = db.Column(db.Integer, primary_key=True)
    nm_url = db.Column(db.Text, nullable=False)
    dt_bookmark = db.Column(db.DateTime, default=datetime.utcnow)
    nm_description = db.Column(db.String(300))
    id_user = db.Column(db.Integer,
                        db.ForeignKey('user.id_user'),
                        nullable=False)
    _tags = db.relationship('Tag',
                            secondary=tags,
                            lazy='joined',
                            backref=db.backref('bookmarks', lazy='dynamic'))