示例#1
0
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
示例#2
0
import os
from datetime import datetime
from flask import current_app, Blueprint, render_template, request, send_file, safe_join, abort, session, url_for, redirect, flash, send_from_directory
from flask_login import login_user, logout_user, current_user, login_required
from werkzeug.utils import secure_filename
from saltToTaste.extensions import db
from saltToTaste.models import Recipe, Tag, Direction, Ingredient, Note
from saltToTaste.forms import RecipeForm
from saltToTaste.search_handler import search_parser
from saltToTaste.recipe_handler import add_recipe_file
from saltToTaste.database_handler import get_recipes, get_recipe_by_title_f, add_recipe
from saltToTaste.parser_handler import argparser_results

argument = argparser_results()
DATA_DIR = os.path.abspath(argument['DATA_DIR'])

main = Blueprint('main', __name__)


@main.route("/", methods=['GET', 'POST'])
def index():
    recipes = sorted(get_recipes(), key=lambda i: i['title'])

    if request.method == 'POST':
        search_data = request.form.getlist('taggles[]')
        if search_data:
            results = search_parser(search_data)
            return render_template("index.html", recipes=results)

    return render_template("index.html", recipes=recipes)