コード例 #1
0
ファイル: models.py プロジェクト: zhaohuizhang/3GBBlog
class User(db.Model):
	"""docstring for User"""
	id = db.Column(db.Integer, primary_key = True)
	nickname = db.Column(db.String(64), index = True, unique = True)
	email = db.Column(db.String(120),index = True, unique = True)
	role = db.Column(db.SmallInteger, default = ROLE_USER)
	posts = db.relationship('Post', backref = 'author', lazy = 'dynamic')
	about_me = db.Column(db.String(140))
	last_seen = db.Column(db.DateTime)
	followed = db.relationship('User',
		secondary = followers,
		primaryjoin = (followers.c.follower_id == id),
		secondaryjoin = (followers.c.followed_id == id),
		backref = db.backref('followers', lazy = 'dynamic'),
		lazy = 'dynamic')

	@staticmethod
	def make_unique_nickname(nickname):
		if User.query.filter_by(nickname = nickname).first() == None:
			return nickname
		version = 2
		while True:
			new_nickname = nickname + str(version)
			if User.query.filter_by(nickname = new_nickname).first() == None:
				break
			version += 1
		return new_nickname

	def is_authenticated(self):
		return True

	def is_active(self):
		return True

	def is_anonymous(self):
		return True

	def get_id(self):
		return unicode(self.id)

	def avatar(self, size):
		return 'http://www.gravatar.com/avatar/' + md5(self.email).hexdigest() + '?d=mm&s=' + str(size)

	def follow(self, user):
		if not self.is_following(user):
			self.followed.append(user)
			return self
	
	def unfollow(self, user):
		if self.is_following(user):
			self.followed.remove(user)
			return self

	def is_following(self, user):
		return self.followed.filter(followers.c.followed_id == user.id).count() > 0	

	def followed_posts(self):
		return Post.query.join(followers, (followers.c.followed_id == Post.user_id)).filter(followers.c.follower_id == self.id).order_by(Post.timestamp.desc())

	def __repr__(self):
		return '<User %r>' % (self.nickname)
コード例 #2
0
ファイル: models.py プロジェクト: IlyaTroshchynskyi/blog
from app import db
from datetime import datetime
import re
from flask_security import UserMixin, RoleMixin


def slugify(string):
    pattern = r'[^\w+]'
    return re.sub(pattern, '-', string) #все что не равно буквами и цифрами заменяем на тире для урла


post_tags = db.Table('post_tags',
                     db.Column('post_id', db.Integer, db.ForeignKey('post.id')),
                     db.Column('tag_id', db.Integer, db.ForeignKey('tag.id'))
                     )

# 'post.id'- путь к свойству айди в таблице пост


class Post(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(140))
    slug = db.Column(db.String(140), unique=True)
    body = db.Column(db.Text)
    created = db.Column(db.DateTime, default=datetime.now())

    def __init__(self, *args, **kwargs):
        super(Post, self).__init__(*args, **kwargs) # вызываем конструктор класса Модель
        self.generate_slug()

    tags = db.relationship('Tag', secondary=post_tags, backref=db.backref('posts', lazy='dynamic'))
コード例 #3
0
class WechatUser(db.Model):
    __tablename__ = 'wechatusers'

    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    openid = db.Column(db.String(32), unique=True, nullable=False)
    nickname = db.Column(db.String(32), nullable=True)
    realname = db.Column(db.String(32), nullable=True)
    classname = db.Column(db.String(32), nullable=True)
    sex = db.Column(db.SmallInteger, default=0, nullable=False)
    province = db.Column(db.String(20), nullable=True)
    city = db.Column(db.String(20), nullable=True)
    country = db.Column(db.String(20), nullable=True)
    headimgurl = db.Column(db.String(150), nullable=True)
    regtime = db.Column(db.DateTime, default=datetime.utcnow, nullable=False)
    user_setting = db.Column(db.String(100))

    user_group = db.relationship('Group',
                                 secondary=registrations,
                                 backref=db.backref('wechatusers',
                                                    lazy='dynamic'),
                                 lazy='dynamic')

    #  phone_number = db.Column(db.String(32), nullable=True)
    #  eamil = db.Column(db.String(32), nullable=True)

    def __init__(self,
                 openid,
                 nickname=None,
                 realname=None,
                 classname=None,
                 sex=None,
                 province=None,
                 city=None,
                 country=None,
                 headimgurl=None,
                 regtime=None):
        self.openid = openid
        self.nickname = nickname
        self.realname = realname
        self.classname = classname
        self.sex = sex
        self.province = province
        self.city = city
        self.country = country
        self.headimgurl = headimgurl
        self.regtime = regtime

    def __repr__(self):
        return '<openid %r>' % self.openid

    def save(self):
        db.session.add(self)
        db.session.commit()
        return self

    def update(self):
        db.session.commit()
        return self

    @staticmethod
    def on_created(target, value, oldvalue, initiator):
        group = Group.query.filter_by(name='全体用户').first()
        if group is None:
            new_group = Group()
            new_group.name = '全体用户'
            new_group.save()
        #  user.user_group.append(new_group)
        target.user_group.append(Group.query.filter_by(name='全体用户').first())
コード例 #4
0
ファイル: models.py プロジェクト: zhaohuizhang/3GBBlog
#!/usr/bin/python
# -*- coding:utf-8 -*-

from app import db, app 
from hashlib import md5
import flask.ext.whooshalchemy as whooshalchemy

ROLE_USER = 0
ROLE_ADMIN = 1

followers = db.Table('followers',
	db.Column('follower_id', db.Integer, db.ForeignKey('user.id')),
	db.Column('followed_id', db.Integer, db.ForeignKey('user.id')),
	)

class User(db.Model):
	"""docstring for User"""
	id = db.Column(db.Integer, primary_key = True)
	nickname = db.Column(db.String(64), index = True, unique = True)
	email = db.Column(db.String(120),index = True, unique = True)
	role = db.Column(db.SmallInteger, default = ROLE_USER)
	posts = db.relationship('Post', backref = 'author', lazy = 'dynamic')
	about_me = db.Column(db.String(140))
	last_seen = db.Column(db.DateTime)
	followed = db.relationship('User',
		secondary = followers,
		primaryjoin = (followers.c.follower_id == id),
		secondaryjoin = (followers.c.followed_id == id),
		backref = db.backref('followers', lazy = 'dynamic'),
		lazy = 'dynamic')
コード例 #5
0
ファイル: models.py プロジェクト: Yansre/YuBlog
class Comment(db.Model):
    __tablename__ = 'comments'
    id = db.Column(db.Integer, primary_key=True)
    comment = db.Column(db.Text)
    author = db.Column(db.String(25))
    email = db.Column(db.String(255))
    website = db.Column(db.String(255), nullable=True)
    isReply = db.Column(db.Boolean, default=False)
    replyTo = db.Column(db.Integer, nullable=True)
    disabled = db.Column(db.Boolean, default=False)
    timestamp = db.Column(db.DateTime,
                          index=True,
                          default=datetime.datetime.now)

    post_id = db.Column(db.Integer, db.ForeignKey('posts.id'))
    page_id = db.Column(db.Integer, db.ForeignKey('pages.id'))

    @property
    def strptime(self):
        return datetime.datetime.strftime(self.timestamp, '%Y-%m-%d')

    @property
    def body_to_html(self):
        html = markdown_to_html(self.comment)
        return html

    # 获取Gravatar头像
    def gravatar(self, size):
        return 'http://www.gravatar.com/avatar/' + md5(
            self.email.encode('utf-8')).hexdigest() + '?d=mm&s=' + str(size)

    def __repr__(self):
        return '<Comment %r>' % (self.comment)
コード例 #6
0
class NewsLink(db.Model):
    __tablename__ = 'newslinks'

    id = db.Column(db.Integer, primary_key=True)
    news_site_one = db.Column(db.String(25), unique=True)
    news_site_two = db.Column(db.String(25), unique=True)
    news_site_three = db.Column(db.String(25), unique=True)
    news_site_four = db.Column(db.String(25), unique=True)
    news_site_five = db.Column(db.String(25), unique=True)
    news_url_one = db.Column(db.String(25), unique=True)
    news_url_two = db.Column(db.String(25), unique=True)
    news_url_three = db.Column(db.String(25), unique=True)
    news_url_four = db.Column(db.String(25), unique=True)
    brand_url_five = db.Column(db.String(25), unique=True)

    organisation_id = db.Column(db.Integer, db.ForeignKey('organisations.id', ondelete="CASCADE"), nullable=False)
    owner_organisation = db.Column(db.String(128))
    
    created_at = db.Column(db.DateTime, default=db.func.now())
    updated_at = db.Column(db.DateTime, default=db.func.now(), onupdate=db.func.now())

    def __repr__(self):
        return u'<{self.__class__.__name__}: {self.id}>'.format(self=self)
コード例 #7
0
ファイル: models.py プロジェクト: Sibirtsev/py-library
class Tag(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    tag = db.Column(db.String(100))

    def __repr__(self):
        return '<Tag {}>'.format(self.tag.encode('utf8'))
コード例 #8
0
ファイル: models.py プロジェクト: Sibirtsev/py-library
class Publisher(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(255))
コード例 #9
0
ファイル: models.py プロジェクト: Sibirtsev/py-library
    def is_active(self):
        return True

    def is_anonymous(self):
        return False

    def get_id(self):
        return unicode(self.id)

    def __repr__(self):
        return '<User login={}, role={}>'.format(self.login, self.role)


books_by_author = db.Table('book_by_author',
                           db.Column('book_id', db.Integer, db.ForeignKey('book.id')),
                           db.Column('author_id', db.Integer, db.ForeignKey('author.id')))

books_by_tag = db.Table('book_by_tag',
                        db.Column('book_id', db.Integer, db.ForeignKey('book.id')),
                        db.Column('tag_id', db.Integer, db.ForeignKey('tag.id')))


class Book(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    isbn = db.Column(db.String(13), index=True)
    title = db.Column(db.String(255))
    description = db.Column(db.Text())
    publisher = db.relationship('Publisher', backref=db.backref('books', lazy='dynamic'))
    authors = db.relationship("Author", secondary=books_by_author,
                              backref=db.backref('books', lazy='dynamic'))