def reload_index(): pages = get('courses/{course}/pages'.format(course=COURSE), {}) progressbar = list if pages > 1: progressbar = pbar.ProgressBar(widgets=[pbar.Bar('>'), ' ', pbar.Percentage(), ' ', pbar.ETA(), ' ', pbar.FileTransferSpeed()]).start() num_rows_deleted = Page.query.delete() print "Deleted", num_rows_deleted, "existing rows." db.session.commit() for page in progressbar(pages): url = page['url'] updated_at = page["updated_at"] # TODO: Some logic to check if the page has changed page_content = get('courses/{course}/pages/{url}'.format(course=COURSE, url=url), {}) body = page_content['body'] #.encode('utf8') title = page_content['title'] published = page_content['published'] db.session.add(Page(body=body, url=url, title=title, published=published)) db.session.commit() whooshalchemy.whoosh_index(app, Page) return "Success"
def build_app(app): app.register_blueprint(auth_bundle) app.register_blueprint(home_bundle) app.register_blueprint(api_bundle) app.register_blueprint(profile_bundle) app.register_blueprint(content_bundle) app.register_blueprint(search_bundle) # Config to Flask from objects # app.config.from_object('fedora_college.core.ProductionConfig') app.config.from_object('fedora_college.core.config.DevelopmentConfig') db.init_app(app) # FAS OpenID Instance with app.app_context(): whooshalchemy.whoosh_index(app, Content) whooshalchemy.whoosh_index(app, Media) DebugToolbarExtension(app) admin = Admin( app, 'Auth', index_view=FedoraAdminIndexView()) admin.add_view(FedoraModelView(UserProfile, db.session)) admin.add_view(FedoraModelView(Content, db.session)) admin.add_view(FedoraModelView(Media, db.session)) admin.add_view(FedoraModelView(Tags, db.session)) admin.add_view( FedoraFileView( current_app.config['STATIC_FOLDER'], name='Static Files' ) ) current_app.config['fas'] = FAS(app)
def app_factory(config): # create app app = Flask(__name__) app.config.from_object(config) # database db.init_app(app) whoosh_index(app, Post) # flask-login login_manager = LoginManager(app) login_manager.user_loader(load_user) # flask-wtf csrf protection CsrfProtect(app) # jinja2 config jinja.init_app(app) # blueprints from blog import blog app.register_blueprint(blog) from api import api app.register_blueprint(api, url_prefix='/api') return app
def configure_extensions(app): """ Configures the extensions """ # Flask-Plugins plugin_manager.init_app(app) # Flask-SQLAlchemy db.init_app(app) # Flask-Migrate migrate.init_app(app, db) # Flask-Mail mail.init_app(app) # Flask-Cache cache.init_app(app) # Flask-Debugtoolbar debugtoolbar.init_app(app) # Flask-Themes themes.init_themes(app, app_identifier="flaskbb") # Flask-And-Redis redis.init_app(app) # Flask-WhooshAlchemy with app.app_context(): whoosh_index(app, Post) whoosh_index(app, Topic) whoosh_index(app, Forum) whoosh_index(app, Category) whoosh_index(app, User) # Flask-Login login_manager.login_view = app.config["LOGIN_VIEW"] login_manager.refresh_view = app.config["REAUTH_VIEW"] login_manager.anonymous_user = Guest @login_manager.user_loader def load_user(id): """ Loads the user. Required by the `login` extension """ unread_count = db.session.query(db.func.count(PrivateMessage.id)).\ filter(PrivateMessage.unread == True, PrivateMessage.user_id == id).subquery() u = db.session.query(User, unread_count).filter(User.id == id).first() if u: user, user.pm_unread = u return user else: return None login_manager.init_app(app)
def create_app(): app = Flask(__name__) from .main import main as main_blueprint app.register_blueprint(main_blueprint) from .auth import auth as auth_blueprint app.register_blueprint(auth_blueprint, url_prefix="/auth") app.config.from_object(Config) db.init_app(app) login_manager.init_app(app) create_admin(app, db) whooshalchemy.whoosh_index(app, Post) return app
def generate_application(config=None): app = Flask(__name__) app.config.from_object('config') app.jinja_env.add_extension('jinja2.ext.do') DB.init_app(app) # blueprint registration app.register_blueprint(blueprint) # whoosh registration whooshalchemy.whoosh_index(app, Book) whooshalchemy.whoosh_index(app, Author) # admin registration below admin = Admin(app, name="IDB", template_mode='bootstrap3') path1 = op.join(op.dirname(__file__), 'static') admin.add_view(FileAdmin(path1, '/static/', name='Static Files')) admin.add_view(ModelView(TeamMember, DB.session)) admin.add_view(ModelView(Author, DB.session)) admin.add_view(ModelView(Book, DB.session)) return app
def create_app(config_name): app = Flask(__name__) app.config.from_object(config[config_name]) # Register our blueprints from .cast import cast as cast_blueprint from .auth import auth as auth_blueprint from .api_1_0 import api as api_blueprint from .api_1_1 import api_1_1 as api_blueprint_1_1 from .admin import admin as admin_blueprint from .dj import dj as dj_blueprint app.register_blueprint(cast_blueprint) app.register_blueprint(auth_blueprint, url_prefix="/auth") app.register_blueprint(api_blueprint, url_prefix="/api") app.register_blueprint(api_blueprint_1_1, url_prefix="/api1.1") app.register_blueprint(admin_blueprint, url_prefix="/admin") app.register_blueprint(dj_blueprint, url_prefix="/dj") # Initialize any extensions we are using bootstrap.init_app(app) login_manager.init_app(app) moment.init_app(app) def nl2br(value): return value.replace("\n", "<br>\n") app.jinja_env.filters["nl2br"] = nl2br @app.route("/robots.txt") def robots_from_static(): return send_from_directory(os.path.join(app.root_path, "static"), "robots.txt") @app.before_request def before_request(): g.next_cast = Cast.query.order_by(Cast.cast_number.desc()).first() msgs = Announcement.query.all() if msgs: for msg in msgs: flash("%s" % msg.message, "%sannouncement" % msg.id) whooshalchemy.whoosh_index(app, Pick) return app
def rebuild_index(model): primary_field = model.pure_whoosh.primary_key_name searchables = model.__searchable__ index_writer = whooshalchemy.whoosh_index(app, model) query = model.query.all() with index_writer.writer() as writer: for post in query: index_attrs = {} for field in searchables: index_attrs[field] = unicode(getattr(post, field)) index_attrs[primary_field] = unicode(getattr(post, primary_field)) writer.update_document(**index_attrs)
def rebuild_index(model): """Rebuild search index of Flask-SQLAlchemy model""" log("Rebuilding {0} index...".format(model.__name__)) primary_field = model.pure_whoosh.primary_key_name searchables = model.__searchable__ index_writer = whooshalchemy.whoosh_index(app, model) # Fetch all data entries = model.query.all() entry_count = 0 with index_writer.writer() as writer: for entry in entries: index_attrs = {} for field in searchables: index_attrs[field] = unicode(getattr(entry, field)) index_attrs[primary_field] = unicode(getattr(entry, primary_field)) writer.update_document(**index_attrs) entry_count += 1 log("Rebuilt {0} {1} search index entries.".format(str(entry_count), model.__name__))
def _configure_whoosh(app): whooshalchemy.whoosh_index(app, Tender) whooshalchemy.whoosh_index(app, Winner)
#!flask/bin/python from app import app from flask.ext.admin import Admin from flask import g from flask.ext import admin from flask.ext.admin.contrib import sqla from flask.ext.admin import expose from app.models import * import flask.ext.whooshalchemy as whooshalchemy class FedoraModelView(sqla.ModelView): column_display_pk = True column_display_pk = True app.config['WHOOSH_BASE'] = '' whooshalchemy.whoosh_index(app, product) admin = Admin(app) admin.add_view(FedoraModelView(path, db.session)) admin.add_view(FedoraModelView(faq, db.session)) admin.add_view(FedoraModelView(product, db.session)) admin.add_view(FedoraModelView(rack, db.session)) app.run(debug=True, host='0.0.0.0')
from celery import Celery from flask import Flask, jsonify, render_template, request, send_file import flask.ext.whooshalchemy as whooshalchemy from cal.schema import db, Event, User # noqa from cal.fb import update_fb_events from cal.ics import to_icalendar from cal.engineeringevents import update_engineering_events # noqa # Initialize the app app = Flask(__name__) app.config.from_object('config') db.init_app(app) whooshalchemy.whoosh_index(app, Event) # Initialize celery celery = Celery(app.import_name, broker=app.config['CELERY_BROKER_URL']) celery.conf.update(app.config) TaskBase = celery.Task class ContextTask(TaskBase): abstract = True def __call__(self, *args, **kwargs): with app.app_context(): return TaskBase.__call__(self, *args, **kwargs) celery.Task = ContextTask
from main import main app.register_blueprint(main, url_prefix='/main') from auth import auth app.register_blueprint(auth, url_prefix="/auth") from api_1_0 import api app.register_blueprint(api, url_prefix="/api/v1.0") return app app = create_app(config_name = 'default') #to search from .models import News,Article,Picture,Interaction,Tag whooshalchemy.whoosh_index(app, News) whooshalchemy.whoosh_index(app, Article) whooshalchemy.whoosh_index(app, Picture) whooshalchemy.whoosh_index(app, Interaction) whooshalchemy.whoosh_index(app, Tag) def make_celery(app): celery = Celery(app.import_name, broker=app.config['CELERY_BROKER_URL']) celery.conf.update(app.config) TaskBase = celery.Task class ContextTask(TaskBase): abstract = True def __call__(self, *args, **kwargs): with app.app_context(): return TaskBase.__call__(self, *args, **kwargs) celery.Task = ContextTask
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 class Post(db.Model): __searchable__ = ['body'] id = db.Column(db.Integer, primary_key=True) body = db.Column(db.String(140)) timestamp = db.Column(db.DateTime) user_id = db.Column(db.Integer, db.ForeignKey('user.id')) language = db.Column(db.String(5)) def __repr__(self): return '<Post %r>' % self.body whooshalchemy.whoosh_index(app, Post)
}) @post_load def make_client(self, data): return Client(**data) class MenuSchema(ma.ModelSchema): class Meta: model = Menu # fields = ("end_date",) # end_date = fields.Method("formate_date", dump_only=True) def formate_date(self, menu): return menu.end_time clients = ma.Nested(ClientSchema, many=True, exclude=('menu',), dump_only=True, ) recipes = ma.Nested(RecipeSchema, many=True) # Smart hyperlinking _links = ma.Hyperlinks({ 'self': ma.URLFor('api._get_menu', menu_id='<id>'), 'collection': ma.URLFor('api._get_menus') }) if enable_search: whooshalchemy.whoosh_index(app, Recipe)
class Professor(db.Model): __searchable__ = ['name'] id = db.Column(db.Integer, primary_key = True) name = db.Column(db.String(64)) rating = db.Column(db.Integer) college = db.Column(db.String(120), default = "Universidade Federal da Bahia") wasAcceptedByAdmin = db.Column(db.Boolean, default = False) posts = db.relationship('Post', backref = 'about', lazy = 'dynamic') department_id = db.Column(db.Integer, db.ForeignKey('department.id')) def media_final(self): total = self.posts.count() acum = 0 #7 e a nota padrao if total == 0: return 7 for p in self.posts: acum += p.nota_ponderada() print(acum) return float(acum) / float(total) def __repr__(self): return '<Professor %r>' % (self.first_name) whooshalchemy.whoosh_index(app, Professor)
post_id (str): post (sqlalchemy relationship): post_id (str): time_stamp (datetime): modified (datetime): created (datetime): """ self.id = id self.body = body self.ups = ups self.downs = downs self.post_id = post_id self.created = datetime.utcfromtimestamp(create_utc) self.college = college def get_text(self): return self.body def __repr__(self): """ String representation of a comment object""" return '<Comment %s>' % self.body[:10] def __hash__(self): return hash(self.id + str(self.created)) def __eq__(self, other): return self.id == other.id whoosh.whoosh_index(app, Post) whoosh.whoosh_index(app, Comment)
#!/usr/bin/env python2 import os import sys from flask.ext.whooshalchemy import whoosh_index from nhtg15_webapp import app from nhtg15_webapp import models sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0) atatime = 512 with app.APP.app_context(): index = whoosh_index(app.APP, models.Area) searchable = models.Area.__searchable__ total = int(models.Area.query.order_by(None).count()) print 'total rows: {}'.format(total) done = 0 writer = index.writer(limitmb=10000, procs=16, multisegment=True) for p in models.Area.query.yield_per(atatime): record = dict([(s, p.__dict__[s]) for s in searchable]) record.update({'id': unicode(p.id)}) # id is mandatory, or whoosh won't work writer.add_document(**record) done += 1 if done % atatime == 0:
category_id = json_courses.get('category_id') credit = json_courses.get('credit') # 二级课程分类和学分分类可选 type_id = json_courses.get('type_id') subcategory_id = json_courses.get('sub_category_id') return Courses(name=name, teacher=teacher, category_id=category_id, type_id=type_id, subcategory_id=subcategory_id) def __repr__(self): return '<Courses %r>' % self.name whooshalchemy.whoosh_index(app, Courses) # CourseCategories # 1 公共课 # 2 通识课 # 3 专业课 # 4 素质课 class CourseCategories(db.Model): # __table_args__ = {'mysql_charset': 'utf8'} __tablename__ = 'category' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(30)) courses = db.relationship("Courses", backref="category", lazy="dynamic",
es.refresh('catalog') product_created.connect(add_product_index_to_es, app) class Category(db.Model): __searchable__ = ['name'] id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(100)) def __init__(self, name): self.name = name def __repr__(self): return '<Category %d>' % self.id whooshalchemy.whoosh_index(app, Category) def add_category_index_to_es(sender, category): es.index('catalog', 'category', { 'name': category.name, }) es.refresh('catalog') category_created.connect(add_category_index_to_es, app)
backref=db.backref('j_projects', lazy='dynamic'), lazy='dynamic') r_students = db.relationship('Student', secondary=r_projects, backref=db.backref('r_projects', lazy='dynamic'), lazy='dynamic') def __repr__(self): return '<Project %r>' % self.name # Whoosh whooshalchemy.whoosh_index(app, Student) whooshalchemy.whoosh_index(app, Project) # Forms class LoginForm(Form): email = StringField( 'Email', validators=[ Required(message='Your email address is required.'), Length(1, 64), Email(message='This email address is invalid.') ]) password = PasswordField('Password') remember_me = BooleanField('Keep me logged in')
return nickname version = 2 while True: new_nickname = nickname + str(version) if User.query.filter_by(nickname=new_nickname).first() is None: break version += 1 return new_nickname def __repr__(self): return '<User %r>' % (self.nickname) class Shop(db.Model): __searchable__ = ['name'] id = db.Column(db.Integer, primary_key = True) name = db.Column(db.String(140), index=True) items = db.relationship('Item', backref='author', lazy='dynamic') def __repr__(self): return '<Shop %r>' % (self.name) class Item(db.Model): id = db.Column(db.Integer, primary_key = True) name = db.Column(db.String(140), index=True) shop_id = db.Column(db.Integer, db.ForeignKey('shop.id')) def __repr__(self): return '<Item %r>' % (self.name) # for search whooshalchemy.whoosh_index(application, Shop)
.all()) @property def tier1_hours(self): if len(self.units) == 0: return 0 return (db.session.query(db.func.sum(Unit.tier1)) .join(Unit.courses) .filter(Course.id == self.id) .first())[0] @property def tier2_hours(self): if len(self.units) == 0: return 0 return (db.session.query(db.func.sum(Unit.tier2)) .join(Unit.courses) .filter(Course.id == self.id) .first())[0] def __repr__(self): # pragma: no cover return '<Course: {0.abbr} - {0.title} {0.program}>'.format(self) whoosh_index(app, Area) whoosh_index(app, Unit) whoosh_index(app, Outcome) whoosh_index(app, Program) whoosh_index(app, Course)
import coverage COV = coverage.coverage(branch=True,include=basedir + '/app/*') COV.start() from ooiservices.app import create_app, db from flask.ext.script import Manager, Shell, Server, prompt_bool from flask.ext.migrate import Migrate, MigrateCommand import flask.ext.whooshalchemy as whooshalchemy from ooiservices.app.models import PlatformDeployment, User, UserScope from datetime import datetime app = create_app('LOCAL_DEVELOPMENT') manager = Manager(app) migrate = Migrate(app,db) app.config['WHOOSH_BASE'] = 'ooiservices/whoosh_index' whooshalchemy.whoosh_index(app, PlatformDeployment) ##------------------------------------------------------------------ ## M@Campbell 02/10/2015 ## ## Helper function to build index of models that are loaded manually (from .sql file) # # Usage: # From shell: # > from ooiservices.manage import rebuild_index # > rebuild_index(model_name) ##------------------------------------------------------------------ def rebuild_index(model): import whoosh import flask_whooshalchemy """Rebuild search index of Flask-SQLAlchemy model"""
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 class Post(db.Model): __searchable__ = ['body'] id = db.Column(db.Integer, primary_key=True) body = db.Column(db.String(140)) timestamp = db.Column(db.DateTime) user_id = db.Column(db.Integer, db.ForeignKey('user.id')) def __repr__(self): return '<Post %r>' % self.body wha.whoosh_index(app, Post)
db.func.count(Article.id).label('count')).filter( Category.id == Article.category_id).group_by( Category.name).all() return art # top 5 @classmethod def top(cls, num): art = db.session.query(Article).order_by( Article.numlook.desc()).limit(num) return art # 本月新建 whooshalchemy.whoosh_index(blog, Article) '''上传附件''' class Uploads(db.Model): id = db.Column(db.Integer, primary_key=True) file_name = db.Column(db.String(50)) file_url = db.Column(db.String(50)) use_url = db.Column(db.String(50)) file_type = db.Column(db.String(4)) upload_date = db.Column(db.DateTime) upload_user = db.Column(db.Integer) @classmethod def get_imgs(cls): imgs = Uploads.query.filter_by(file_type='img').order_by(
return area @staticmethod def get_by_code(code): area = Area.query.filter(Area.code == code).first() if not area: return None return area @staticmethod def get_by_name(name): area = Area.query.filter(Area.name == name).first() if not area: return None return area def get_locations(): for location in self.locations: yield location for child in self.children: for location in child.get_locations: yield location whooshalchemy.whoosh_index(app.APP, Area)
def search_results(query): whooshalchemy.whoosh_index(app, Post) results = Post.query.whoosh_search(query, MAX_SEARCH_RESULTS).all() return render_template('search_results.html', query=query, results=results)
from frmwk import orm_db from frmwk import flask_framework from config import WHOOSH_ENABLED class Post(orm_db.Model): __searchable__ = ['body'] id = orm_db.Column(orm_db.Integer, primary_key=True) body = orm_db.Column(orm_db.String(140)) timestamp = orm_db.Column(orm_db.DateTime) user_id = orm_db.Column(orm_db.Integer, orm_db.ForeignKey('authenticateduser.id')) language = orm_db.Column(orm_db.String(5)) def __repr__(self): # pragma: no cover return '<Post %r>' % (self.body) if WHOOSH_ENABLED: import flask.ext.whooshalchemy as whooshalchemy whooshalchemy.whoosh_index(flask_framework, Post)
id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(120)) latitude = db.Column(db.String( 12)) # -90 tp 90 degrees south pole to north pole db.DECIMAL(6,4) longitude = db.Column( db.String(12)) # -180 to 180 degress west to east db.DECIMAL(7,4) events = db.relationship('Event', backref='venu', lazy='dynamic') # sqlite 3 does not support this constraint __table_args__ = (db.UniqueConstraint('latitude', 'longitude'), ) @staticmethod def check_saved_location(name, latitude, longitude): location = Location.query.filter_by(latitude=latitude, longitude=longitude).first() if location is None: location = Location(name=name, latitude=latitude, longitude=longitude) db.session.add(location) db.session.commit() return location # return False # line for test else: return location # return True # line for test def __repr__(self): return '<Location %r>' % (self.name) if enable_search: whooshalchemy.whoosh_index(app, Event)
city = db.Column(db.String(64)) area = db.Column(db.String(120), nullable=True) #area is things like fishing, retaurant, hiking, etc. def get_all(self): aois = AreaOfInterests.query.all() def __repr__(self): return '<AreaOfInterests %r %r %r %r %r %r>' % ( self.id, self.user_id, self.country, self.state, self.city, self.area) if enable_search: whooshalchemy.whoosh_index(app, AreaOfInterests) class Ratings(db.Model): id = db.Column(db.Integer, primary_key=True) rater_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) rated_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) rates = db.Column(db.Float, nullable=False) comment = db.Column(db.Text, nullable=True) timestamp = db.Column(db.DateTime) rater = db.relationship('User', foreign_keys='Ratings.rater_id') rated = db.relationship('User', foreign_keys='Ratings.rated_id') def get_rater(self): return User.query.filter_by(id=self.rater_id).first()
from Content import Content from app import app import flask.ext.whooshalchemy as whooshalchemy whooshalchemy.whoosh_index(app, Content)
class Journal(Base): """this is the model for journals """ __tablename__ = 'journal' __searchable__ = ['body', 'title', 'tags'] # journal details id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(200), nullable=False) body = db.Column(db.String(1000), nullable=False) tags = db.Column(db.String(50), db.ForeignKey('tags.tagname'), nullable=True) user_id = db.Column(db.Integer, db.ForeignKey('user.id')) user = db.relationship('User') def __init__(self, title, body, tags, user_id): self.title = title self.body = body self.tags = tags self.user_id = user_id def __repr__(self): return '<Journal %r>' % self.title if enable_search: whooshalchemy.whoosh_index(app, Journal)
from flask import Flask, render_template, request, jsonify import flask.ext.whooshalchemy as whooshalchemy from schema import Professor, Course, Review, db app = Flask(__name__) app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///culpa.db" app.config["WHOOSH_BASE"] = "whoosh/" db.init_app(app) import compare whooshalchemy.whoosh_index(app, Course) whooshalchemy.whoosh_index(app, Professor) whooshalchemy.whoosh_index(app, Review) @app.route("/", methods=["GET", "POST"]) def index(): return render_template("index.html") @app.route("/class_search/", methods=["POST"]) def class_search(): courses = Course.query.whoosh_search(request.form["search"]).limit(5).all() results = [course.serialize for course in courses] return jsonify(data=results) @app.route("/prof_search/", methods=["POST"]) def prof_search(): profs = Professor.query.whoosh_search(
from spot import plate from spot import db import flask.ext.whooshalchemy as whooshalchemy class Country(db.Model): id = db.Column(db.Integer, primary_key = True) country_name = db.Column(db.String(64), unique = True) codes = db.relationship('Code', backref='country', lazy='dynamic') mission = db.Column(db.String(64), unique = True) website = db.Column(db.String(64), unique = True) def __repr__(self): return '<%r>' % (self.country_name) class Code(db.Model): __searchable__ = ['scramble'] id = db.Column(db.Integer, primary_key = True) scramble = db.Column(db.String(64), unique = True) country_id = db.Column(db.Integer, db.ForeignKey('country.id')) def __repr__(self): return '<%r>' % (self.scramble) whooshalchemy.whoosh_index(plate, Code)
username = db.Column(db.String(164)) password_hash = db.Column(db.String(164)) book = db.relationship('Book', backref="user", lazy="dynamic") @login_manager.user_loader def load_user(user_id): """flask-login要求实现的用户加载回调函数 依据用户的unicode字符串的id加载用户""" return User.query.get(int(user_id)) @property def password(self): """将密码方法设为User类的属性""" raise AttributeError('无法读取密码原始值!') @password.setter def password(self, password): """设置密码散列值""" self.password_hash = generate_password_hash(password) def verify_password(self, password): """验证密码散列值""" return check_password_hash(self.password_hash, password) def __repr__(self): return "%r :The instance of class User" % self.username if enable_search: whooshalchemy.whoosh_index(app, Book)
if sys.version_info >= (3, 0): enable_search = False else: enable_search = True import flask.ext.whooshalchemy as whooshalchemy class ESLPodcast(db.Model): __searchable__ = ['title'] id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(100)) href = db.Column(db.String(256)) def __repr__(self): return '<ESL Podcast %r>' % self.title class EnglishCafe(db.Model): id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(100)) href = db.Column(db.String(256)) def __repr__(self): return '<English Cafe %r>' % self.title if enable_search: whooshalchemy.whoosh_index(app, ESLPodcast)
class Operon(db.Model): id = db.Column(db.Integer, primary_key=True) operon_type = db.Column(db.String(50), default='not_stated', unique=False) operon_number = db.Column(db.Integer, unique=False) subunit_list = db.Column(db.String(200), unique=False) add_prot_list = db.Column(db.String(200), default='', unique=False) atpase_id = db.Column(db.Integer, db.ForeignKey('atpases.id')) organism_id = db.Column(db.Integer, db.ForeignKey('organisms.id')) sequence = db.relationship('Sequence', backref='operon', lazy='dynamic') def __repr__(self): return '<Operon %r>' % (self.number) class Sequence(db.Model): id = db.Column(db.Integer, primary_key=True) subunit_name = db.Column(db.String(20), unique=False) subunit_id_ncbi = db.Column(db.String(50), unique=False) sequence = db.Column(db.String(500), unique=False) start = db.Column(db.Integer, unique=False) stop = db.Column(db.Integer, unique=False) direction = db.Column(db.String(10), unique=False) # added V2 seq_comment = db.Column(db.String(500), unique=False) operon_id = db.Column(db.Integer, db.ForeignKey('operon.id')) def __repr__(self): return '<Sequence %r>' % (self.sequence) whooshalchemy.whoosh_index(app, Organism)
@classmethod def by_name(cls, name): name = ' '.join([w.capitalize() for w in name.split(' ')]) return cls.query.filter_by(name=name).first() @classmethod def exists(cls, name): return name != '' and cls.by_name(name) class Book(db.Model): __searchable__ = ['name'] id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(100), unique=True, index=True) created = db.Column(db.DateTime, default=datetime.utcnow) authors = db.relationship('Author', secondary=authors, backref = db.backref('books', lazy='dynamic')) def __init__(self, name, authors): self.name = name.strip() self.authors = authors self.created = datetime.utcnow() def __repr__(self): return '<Book %r>' % self.name if WHOOSH_ENABLED: import flask.ext.whooshalchemy as whooshalchemy whooshalchemy.whoosh_index(app, Book) whooshalchemy.whoosh_index(app, Author)
def serialize(self): return { 'comment_id' : self.comment_id, 'comment' : self.comment, 'invited_time' : self.invited_time.strftime(DATE_FORMAT), 'commented_time' : self.commented_time.strftime(DATE_FORMAT), 'customer_id' : self.customer_id, 'expert_id' : self.expert_id, 'is_commented' : self.is_commented } """ Represents the InstructionModule object. """ class Instruction(db.Model): __tablename__ = 'instruction' instruction_id = db.Column(db.Integer, primary_key=True) header = db.Column(db.String(100)) image_url = db.Column(db.String(250)) def serialize(self): return { "instruction_id": self.instruction_id, "header": self.header, "image_url": INSTRUCTION_IMAGE_PATH + self.image_url } if enable_search: whooshalchemy.whoosh_index(app, Topic)
migrate = Migrate(app, db) manager = Manager(app) import sys #Enable Whoosh-Alchemy if python version is lower than 3 if sys.version_info >= (3, 0): enable_search = False else: enable_search = True if enable_search is True: import flask.ext.whooshalchemy as whooshalchemy whooshalchemy.whoosh_index(app, BlogPost) @manager.command def adduser(email, username, admin=False): """Register a new user.""" from blogapp.models import User from getpass import getpass password = getpass() password2 = getpass(prompt='Confirm: ') if password != password2: import sys sys.exit('Error: passwords do not match.') db.create_all() user = User(email=email, username=username, password=password, is_admin=admin) db.session.add(user)
class Campaign(db.Model): id = db.Column(db.Integer, primary_key = True) creator_id = db.Column(db.SmallInteger) name = db.Column(db.String(64), index = True, unique = True) funnel_ids = db.Column(db.String(500), default="NONE") class Funnel(db.Model): __searchable__ = ['name'] id = db.Column(db.Integer, primary_key = True) campaign_id = db.Column(db.SmallInteger) name = db.Column(db.String(64), index = True, unique = True) product = db.Column(db.String(120)) content_ids = db.Column(db.String(500)) whooshalchemy.whoosh_index(app, Funnel) class LandingPage(db.Model): __searchable__ = ['page_name'] id = db.Column(db.Integer, primary_key = True) uploader_id = db.Column(db.SmallInteger) page_name = db.Column(db.String(64), index = True, unique = True) page_type = db.Column(db.String(120)) visibility = db.Column(db.SmallInteger, default = VISIBILE) product = db.Column(db.String(120)) variants = db.Column(db.String(120)) test_pos = db.Column(db.Integer, default =-1) test_id = db.Column(db.Integer) whooshalchemy.whoosh_index(app, LandingPage)
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_houses(self): return House.query.join(followers, (followers.c.followed_id == House.user_id)).filter(followers.c.follower_id == self.id).order_by(House.timestamp.desc()) def __repr__(self): # pragma: no cover return '<User %r>' % (self.nickname) class House(db.Model): __searchable__ = ['body'] id = db.Column(db.Integer, primary_key = True) body = db.Column(db.String(140)) timestamp = db.Column(db.DateTime) user_id = db.Column(db.Integer, db.ForeignKey('user.id')) language = db.Column(db.String(5)) def __repr__(self): # pragma: no cover return '<House %r>' % (self.body) if WHOOSH_ENABLED: import flask.ext.whooshalchemy as whooshalchemy whooshalchemy.whoosh_index(app, House)
__tablename__ = 'ask_reply' id = db.Column(db.Integer, primary_key = True) parent_id = db.Column(db.Integer) body = db.Column(db.Text()) timestamp = db.Column(db.DateTime) user_id = db.Column(db.Integer, db.ForeignKey(User.id)) question_id = db.Column(db.Integer, db.ForeignKey(Question.id)) votes = db.relationship("Vote", primaryjoin= "and_(Reply.id==Vote.type_id, Vote.type=={0})".format(TYPE_REPLY), foreign_keys=[Vote.type_id], backref = 'reply', lazy = 'dynamic') flags = db.relationship("Flag", backref = 'reply', lazy = 'dynamic') def slug(self): return Question.query.get(self.question_id).slug def __repr__(self): return '<Reply %r>' % (self.id) class Flag(db.Model): __tablename__ = 'ask_reply_flag' reply_id = db.Column(db.Integer, db.ForeignKey(Reply.id), primary_key = True) user_id = db.Column(db.Integer, db.ForeignKey(User.id), primary_key = True) def __repr__(self): return '<Flag %r by user:%r>' % (self.reply_id, self.user_id) # For full text search support whooshalchemy.whoosh_index(app, Question)
ThingsType = db.Column(db.String(20)) Type = db.Column(db.String(10)) Content = db.Column(db.String(400)) ImgPath = db.Column(db.String(150)) Reward = db.Column(db.Integer) #ThumbnailPath = db.Column(db.String(150)) LostStatus = db.Column(db.Boolean,default=True) ContactWay = db.Column(db.String(100)) Verify = db.Column(db.Boolean,default=False) SubTime=db.Column(db.String(30)) class AdminUser(db.Model): __tablename__ = "AdminUsers" Id = db.Column(db.Integer, primary_key=True) UserId = db.Column(db.String(50)) PassWord = db.Column(db.String(50)) RegTime = db.Column(db.String(50)) whooshalchemy.whoosh_index(app, UserData) if __name__ == '__main__': db.create_all() admin = AdminUser() admin.UserId = "20144483" admin.PassWord = "******" db.session.add(admin) db.session.commit() db.session.close()
from recruit_app.app import create_app from recruit_app.settings import DevConfig, ProdConfig from recruit_app.blacklist.models import BlacklistCharacter as Model from flask.ext.whooshalchemy import whoosh_index sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0) atatime = 512 if __name__ == '__main__': if os.environ.get("RECRUIT_APP_ENV") == 'prod': app = create_app(ProdConfig) else: app = create_app(DevConfig) with app.app_context(): index = whoosh_index(app, Model) searchable = Model.__searchable__ print 'counting rows...' total = int(Model.query.order_by(None).count()) done = 0 print 'total rows: {}'.format(total) writer = index.writer(limitmb=10000, procs=16, multisegment=True) for p in Model.query.yield_per(atatime): record = dict([(s, p.__dict__[s]) for s in searchable]) record.update({'id': unicode(p.id) }) # id is mandatory, or whoosh won't work writer.add_document(**record) done += 1 if done % atatime == 0: print 'c {}/{} ({}%)'.format( done, total, round((float(done) / total) * 100, 2)),
''' __author__ = 'Edna Donoughe' import unittest import json from base64 import b64encode from flask import url_for from ooiservices.app import create_app, db from ooiservices.app.models import PlatformDeployment, InstrumentDeployment, Stream, StreamParameter, VocabNames from ooiservices.app.models import Organization, User, UserScope import flask.ext.whooshalchemy as whooshalchemy import datetime as dt app = create_app('TESTING_CONFIG') app.config['WHOOSH_BASE'] = 'ooiservices/whoosh_index' whooshalchemy.whoosh_index(app, PlatformDeployment) ''' These tests are additional to the normal testing performed by coverage; each of these tests are to validate model logic outside of db management. ''' class UserTestCase(unittest.TestCase): def setUp(self): self.app = app self.app_context = self.app.app_context() self.app_context.push() db.create_all() test_username = '******' test_password = '******' Organization.insert_org() User.insert_user(username=test_username, password=test_password)
else: self.image_path = news_source.image_path def __repr__(self): return '<NewsSummary {0}>'.format(self.title) @event.listens_for(NewsSummary, "after_insert") def update_url(mapper, connection, target): url_max_length = 30 news_summary_table = mapper.local_table tokenizer = Tokenizer() title_clean = tokenizer.strip_all_punctuation(target.title.lower()) title_clean = tokenizer.remove_stopwords( title_clean) if len(title_clean) > url_max_length else title_clean while len(title_clean) > url_max_length: title_clean = ' '.join( title_clean.split(' ')[:-1]) # Remove last word from title title_clean = title_clean.replace(' ', '-') url_hash = Hashids(min_length=4).encode(target.id) url = title_clean + '-' + url_hash connection.execute(news_summary_table.update().values(url=url).where( news_summary_table.c.id == target.id)) # Add the summary table to the search index whooshalchemy.whoosh_index(app, NewsSummary) whooshalchemy.whoosh_index(app, NewsSource)
url = db.Column(db.String(200), primary_key=True) title = db.Column(db.String(300)) description = db.Column(db.String(1000)) subjects = db.Column(db.String(1000)) page_content = db.Column(db.String(10000)) links = db.Column(db.String(1000)) link_titles = db.Column(db.String(1000)) def __init__(self, url): self.url = url def __repr__(self): return '<Conditions %r>' % self.url whooshalchemy.whoosh_index(app, Conditions) @app.route("/") def index(): return render_template('index.html') @app.route("/conditions") def search(): q = request.args.get('q') extractor_result = ConceptExtractor(q) query_terms = list(extractor_result.terms) combined_query_terms = ', '.join(query_terms) ce_query_results = Conditions.query.whoosh_search(combined_query_terms, 3).all()
@property def outcomes(self): return (Outcome.query.join(Outcome.unit, Unit.courses).filter( Course.id == self.id).order_by(Outcome.mastery, Outcome.tier, Outcome.id).all()) @property def tier1_hours(self): if len(self.units) == 0: return 0 return (db.session.query(db.func.sum(Unit.tier1)).join( Unit.courses).filter(Course.id == self.id).first())[0] @property def tier2_hours(self): if len(self.units) == 0: return 0 return (db.session.query(db.func.sum(Unit.tier2)).join( Unit.courses).filter(Course.id == self.id).first())[0] def __repr__(self): # pragma: no cover return '<Course: {0.abbr} - {0.title} {0.program}>'.format(self) whoosh_index(app, Area) whoosh_index(app, Unit) whoosh_index(app, Outcome) whoosh_index(app, Program) whoosh_index(app, Course)
title = db.Column(db.String(256)) description = db.Column(db.Text) code = db.Column(db.Text) timestamp = db.Column(db.DateTime) ref_count = db.Column(db.Integer) access = db.Column(db.Boolean) creator_id = db.Column(db.Integer) topic_id = db.Column(db.Integer, db.ForeignKey('topic.id')) def __init__(self, title, description, code, timestamp, topic, creator_id, access=ACCESS_PRIVATE): self.title = title self.description = description self.code = code self.timestamp = timestamp self.ref_count = 1 self.access = access self.creator_id = creator_id self.topic = topic def inc_ref(self): self.ref_count += 1; def dec_ref(self): self.ref_count -= 1; def __repr__(self): return '<ID:%r, title:%r, description:%r, code:%r, timestamp:%r, ref_count:%r, access:%r, creator_id:%r>' % \ (self.id, self.title, self.description, self.code, self.timestamp, self.ref_count, self.access, self.creator_id) whooshalchemy.whoosh_index(app, Snippet)
import os import sys file_dir = os.path.dirname(__file__) sys.path.append(file_dir) from app import create_app, db from app.models import Dream from flask_script import Manager, Shell from flask_migrate import Migrate, MigrateCommand import flask.ext.whooshalchemy as whooshalchemy from config import config app = create_app(os.getenv('FLASK_CONFIG') or 'default') manager = Manager(app) migrate = Migrate(app,db) with app.app_context(): whooshalchemy.whoosh_index(app, Dream) def make_shell_context(): return dict(app=app, db=db, Dream=Dream) manager.add_command("shell", Shell(make_context=make_shell_context)) manager.add_command('db', MigrateCommand) if __name__ == '__main__': manager.run()
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()) @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 class Post(db.Model): __searchable__=['body'] id = db.Column(db.Integer, primary_key=True) body = db.Column(db.String(140)) timestamp = db.Column(db.DateTime) user_id = db.Column(db.Integer, db.ForeignKey('user.id')) def __rep__(self): return '<Post %r>' % (self.body) if enable_search: whooshalchemy.whoosh_index(app, Post)
import sys if sys.version_info >= (3, 0): enable_search = False else: enable_search = True import flask.ext.whooshalchemy as whooshalchemy from flask.ext.admin.contrib import sqla from flask.ext.admin import Admin import logging from logging.handlers import RotatingFileHandler app = Flask(__name__) app.config.from_object('config') db = SQLAlchemy(app) oid = OpenID(app, os.path.join(basedir, 'tmp')) from models import Goods, User from app import views, models class FedoraModelView(sqla.ModelView): column_display_pk = True column_display_pk = True admin = Admin(app) admin.add_view(FedoraModelView(User, db.session)) admin.add_view(FedoraModelView(Goods, db.session)) whooshalchemy.whoosh_index(app, Goods)