def create_app(config_file='settings.py'): argument = argparser_results() DATA_DIR = os.path.abspath(argument['DATA_DIR']) app = Flask(__name__) app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True app.config['SQLALCHEMY_ECHO'] = False app.config['SQLALCHEMY_DATABASE_URI'] = f'sqlite:///{DATA_DIR}/database.db' app.config['RECIPE_FILES'] = f'{DATA_DIR}/_recipes/' app.config['RECIPE_IMAGES'] = f'{DATA_DIR}/_images/' app.config['WHOOSH_INDEX_PATH'] = f'{DATA_DIR}/whooshIndex' app.config['WHOOSH_ANALYZER'] = 'StemmingAnalyzer' # Create Flask secret if not os.path.isfile(f'{DATA_DIR}/saltToTaste.secret'): create_flask_secret(DATA_DIR) app.secret_key = open(f'{DATA_DIR}/saltToTaste.secret', 'r', encoding='utf-16').readline() # Register blueprints app.register_blueprint(main) app.register_blueprint(api, url_prefix='/api') # Create indexes of database tables wa.search_index(app, Recipe) wa.search_index(app, Tag) wa.search_index(app, Ingredient) wa.search_index(app, Direction) wa.search_index(app, Note) # Initalize and create the DB db.init_app(app) db.app = app db.create_all() # Initalize the login manager plugin login_manager.init_app(app) @login_manager.user_loader def load_user(user_id): return User.query.get(int(user_id)) # Import phyiscal recipe files recipe_list = recipe_importer(app.config['RECIPE_FILES']) # Sync physical recipe files with database if not Recipe.query.first(): add_all_recipes(recipe_list) else: add_new_recipes(recipe_list) remove_missing_recipes(recipe_list) update_recipes(recipe_list) db_cleanup() return app
self.eatery_id = eatery_id # set passed eatery_id to object's eatery_id """ Method Name: __repr__ Creation: 1/20/20 Purpose: for displaying in terminal Arguments: self Required Tables: review Return Value: string containing the id """ def __repr__(self): return 'Review ' + str(self.id) wa.search_index(app, Eatery) # Schema For Sending Json Data class EaterySchema(ma.Schema): class Meta: fields = ('id', 'name', 'address', 'contact', 'why_flag', 'flag', 'rating') # format of json data for eateries class ReviewSchema(ma.Schema): class Meta: fields = ('id', 'review_text', 'rating', 'why_flag', 'flag', 'flagged_before', 'eatery_id' ) # format of json data for reviews
id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(120), nullable=False) content = db.Column(db.Text, nullable=False) date_posted = db.Column(db.DateTime, nullable=False, default=datetime.utcnow) user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) def __repr__(self): return f"Post('{self.title}','{self.date_posted}')" likes = db.relationship('PostLike', backref='post', lazy='dynamic') dislikes = db.relationship('PostDisLike', backref='post', lazy='dynamic') class PostLike(db.Model): __tablename__ = 'like_post' id = db.Column(db.Integer, primary_key=True) user_id = db.Column(db.Integer, db.ForeignKey('user.id')) post_id = db.Column(db.Integer, db.ForeignKey('post.id')) class PostDisLike(db.Model): __tablename__ = 'dislike_post' id = db.Column(db.Integer, primary_key=True) user_id = db.Column(db.Integer, db.ForeignKey('user.id')) post_id = db.Column(db.Integer, db.ForeignKey('post.id')) wa.search_index(current_app, Post)
def initiate_index(): flask_whooshalchemy.search_index(app, models.Clause)
def bootstrap(): flask_whooshalchemy.search_index(current_app, Expense)
def create_app(config_file='settings.py'): argument = argparser_results() DATA_DIR = os.path.abspath(argument['DATA_DIR']) app = Flask(__name__) app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True app.config['SQLALCHEMY_ECHO'] = False app.config['SQLALCHEMY_DATABASE_URI'] = f'sqlite:///{DATA_DIR}/database.db' app.config['WHOOSH_INDEX_PATH'] = f'{DATA_DIR}/whooshIndex' app.config['WHOOSH_ANALYZER'] = 'StemmingAnalyzer' app.config['DATA_DIR'] = DATA_DIR app.config['RECIPE_FILES'] = f'{DATA_DIR}/_recipes/' app.config['RECIPE_IMAGES'] = f'{DATA_DIR}/_images/' app.config['CONFIG_INI'] = f'{DATA_DIR}/config.ini' app.jinja_env.filters['capwords'] = capwords if not os.path.isfile(app.config['CONFIG_INI']): create_default_configfile() verify_configfile() config = configparser_results(app.config['CONFIG_INI']) try: app.config['SECRET_KEY'] = config.get('flask', 'secret_key') except TypeError: print('Error: Could not find Flask secret_key in config.ini.') sys.exit() # Register blueprints app.register_blueprint(main) app.register_blueprint(api, url_prefix='/api') # Create indexes of database tables wa.search_index(app, Recipe) wa.search_index(app, Tag) wa.search_index(app, Ingredient) wa.search_index(app, Direction) wa.search_index(app, Note) # Initalize and create the DB db.init_app(app) db.app = app db.create_all() # Initalize the login manager plugin login_manager.init_app(app) login_manager.login_view = 'main.login' login_manager.login_message_category = 'info' @login_manager.user_loader def load_user(user_id): return User.query.get(user_id) # Import phyiscal recipe files recipe_list = recipe_importer(app.config['RECIPE_FILES']) # Sync physical recipe files with database if not Recipe.query.first(): add_all_recipes(recipe_list) else: add_new_recipes(recipe_list) remove_missing_recipes(recipe_list) update_recipes(recipe_list) db_cleanup() return app
if self.author == g.user: return True return False def is_mine(self): if self.receiver == g.user: return True return False def export_data(self): return { 'id': self.id, 'isMe': self.is_me(), 'body': self.body, 'timestamp': self.timestamp, 'author_id': self.author_id, 'message_id': self.message_id, 'sender_image': self.get_author_image(self.author_id), } def import_data(self, data): try: self.body = data['body'] except KeyError as e: raise ValidationError('Invalid customer: missing ' + e.args[0]) return self app = create_app('production') whooshalchemy.search_index(app, User)
def verify_reset_token(token): s =serializer(app.config['SECRET_KEY']) try: user_id = s.loads(token)['user_id'] except: return None return User.query.get(user_id) @staticmethod def get_by_email(email): return User.query.filter_by(email=email).first() def __repr__(self): return f"User('{self.username}', '{self.email}', '{self.image_file}')" class Post(db.Model): __searchable__= ['title', 'content'] id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(100), nullable=False) date_posted = db.Column(db.DateTime, nullable=False, default=datetime.utcnow) content = db.Column(db.Text, nullable=False) user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) def __repr__(self): return f"Post('{self.title}', '{self.date_posted}')" wa.search_index(app, Post) db.create_all()