Ejemplo n.º 1
0
import logging
import os

from flask import Flask, render_template
from flask_compress import Compress
from cache import cache
from app.monitoring.monitoring_controller import monitoring
from app.main.main_conroller import api as main
from app.api import app as apis
from app.log.log import setup_logging
from flask_moment import Moment

moment = Moment()

# Logging
setup_logging()

# Define the WSGI application object
app = Flask(__name__)
Compress(app)

config = {
    "dev": "config.DevConfig",
    "test": "config.TestConfig",
    "alpha": "config.AlphaConfig",
    "real": "config.RealConfig",
}

config_name = os.getenv('FLASK_CONFIGURATION', 'dev')
app.config.from_object(config[config_name])
Ejemplo n.º 2
0
#coding:utf-8
from flask import Flask, render_template, Blueprint
from config import config
from flask_sqlalchemy import SQLAlchemy
from flask_login import LoginManager
from flask_mail import Mail
from flask_moment import Moment
from flask_pagedown import PageDown
import flask_whooshalchemyplus
from flask_cache import Cache

#构造文件导入了大多数正在使用的 Flask 扩展。由于尚未初始化所需的程序实例,所以没有初始化扩展,创建扩展类时没有向构造函数传入参数。
mail = Mail()
monment = Moment()
pagedown = PageDown()

login_manager = LoginManager()
#LoginManager 对象的 session_protection 属性可以设为 None 、 'basic' 或 'strong' ,以提供不同的安全等级防止用户会话遭篡改。设为 'strong' 时,Flask-Login 会记录客户端 IP地址和浏览器的用户代理信息,如果发现异动就登出用户。
login_manager.session_protection = 'strong'
#login_view 属性设置登录页面的端点。登录路由在蓝本auth中定义,因此要在前面加上蓝本的名字auth。
login_manager.login_view = 'auth.login'

db = SQLAlchemy()
#缓存
cache = Cache()


#create_app() 函数就是程序的工厂函数,接受一个参数,是程序使用的配置名
def create_app(config_name):
    app = Flask(__name__)
Ejemplo n.º 3
0
from passlib.hash import sha256_crypt

import holidays
import requests

app = Flask('my-stock')

app.config['SECRET_KEY'] = 'sOtCk!'

app.config['REMEMBER_COOKIE_DURATION'] = timedelta

app.config['MONGO_URI'] = 'mongodb://*****:*****@ds211259.mlab.com:11259/mystock?retryWrites=false'

monent = Moment(app)

Bootstrap(app)

mongo = PyMongo(app)

weather_api = 'c115e005de28c13c6e9ff0ad6d7b14ca'

#col = mongo.db.users

#collection = mongo.db.AccountInformation


def offday():
    current = datetime.now()
    us = holidays.UnitedStates(state='CA')
Ejemplo n.º 4
0
from flask_mail import Mail{% endif %}
{%- if cookiecutter.use_flask_moment == 'y' %}
from flask_moment import Moment{% endif %}
{%- if cookiecutter.use_flask_sqlalchemy == 'y' %}
from flask_sqlalchemy import SQLAlchemy{% endif %}
{%- if cookiecutter.use_flask_wtf == 'y' %}
from flask_wtf.csrf import CSRFProtect{% endif %}

import settings

{%- if cookiecutter.use_flask_bootstrap == 'y' %}
bootstrap = Bootstrap(){% endif %}
{%- if cookiecutter.use_flask_mail == 'y' %}
mail = Mail(){% endif %}
{%- if cookiecutter.use_flask_moment == 'y' %}
moment = Moment(){% endif %}
{%- if cookiecutter.use_flask_sqlalchemy == 'y' %}
db = SQLAlchemy(){% endif %}
{%- if cookiecutter.use_flask_wtf == 'y' %}
csrf = CSRFProtect(){% endif %}

def create_app(env):
    """Factory function responsible for lazy app creation.

    Args:
        environment (str): configuration environment to be used by the app
    
    """
    app = Flask(__name__)
    app.config.from_object(settings.environments[env])
    settings.environments[env].init_app(app)
Ejemplo n.º 5
0
# --*-- coding: utf-8 --*--
from flask import Flask
from config import config
from flask_bootstrap import Bootstrap
from flask_moment import Moment
from flask_nav import Nav
from flask_nav.elements import *
from flask_sqlalchemy import SQLAlchemy
bootstrap = Bootstrap()
monent = Moment()
nav = Nav()
db = SQLAlchemy()


def create_app(config_name):
    app = Flask(__name__)
    app.config.from_object(config[config_name])
    config[config_name].init_app(app)
    monent.init_app(app)

    bootstrap.init_app(app)
    nav.register_element(
        "top",
        Navbar("lifeblog", View(u"首页", 'main.index'),
               View("android", 'main.androidpage'), View(u'关于', 'main.about')))
    nav.init_app(app)
    db.init_app(app)
    from .main import main as main_blueprint
    app.register_blueprint(main_blueprint)
    return app
Ejemplo n.º 6
0
import os
from flask_mail import Mail
from flask_bootstrap import Bootstrap
from flask_moment import Moment
from flask_babel import Babel
from flask import request

app = Flask(__name__)
app.config.from_object(Config)
db = SQLAlchemy(app)
migrate = Migrate(app, db)
login = LoginManager(app)
login.login_view = 'login'
mail = Mail(app)
bootstrap = Bootstrap(app)
moment = Moment(app)
babel = Babel(app)

from app import routes, models, errors

if not app.debug:
    if app.config['MAIL_SERVER']:
        auth = None
        if app.config['MAIL_USERNAME'] or app.config['MAIL_PASSWORD']:
            auth = (app.config['MAIL_USERNAME'], app.config['MAIL_PASSWORD'])
        secure = None
        if app.config['MAIL_USE_TLS']:
            secure = ()
        mail_handler = SMTPHandler(
            mailhost=(app.config['MAIL_SERVER'], app.config['MAIL_PORT']),
            fromaddr='no-reply@' + app.config['MAIL_SERVER'],
Ejemplo n.º 7
0
def create_app(test_config=None):
    # create and configure the app --heroku config --app
    # testing again
    app = Flask(__name__)
    CORS(app)
    moment = Moment(app)
    app.config.from_object('config')
    setup_db(app)
    cors = CORS(app, resources={r"/api/*": {"origins": "*"}})

    def paginate_actors(request, selection):
        actors_per_page = 10
        page = request.args.get('page', 1, type=int)
        start = (page - 1) * actors_per_page
        end = start + actors_per_page

        actors = [actor.format() for actor in selection]
        current_actors = actors[start:end]

        return current_actors

    def paginate_movies(request, selection):
        movies_per_page = 10
        page = request.args.get('page', 1, type=int)
        start = (page - 1) * movies_per_page
        end = start + movies_per_page

        movies = [movie.format() for movie in selection]
        current_movies = movies[start:end]

        return current_movies

    @app.after_request
    def add_cors_headers(response):
        response.headers.add('Access-Control-Allow-Headers',
                             'Content-Type, Authorization')
        response.headers.add('Access-Control-Allow-Methods',
                             'GET, PUT, POST, PATCH, DELETE, OPTIONS')
        return response

    @app.route('/actors', methods=['GET'])
    @requires_auth('get:actors')
    def get_actors(jwt):
        selection = list(Actor.query.order_by(Actor.id).all())
        current_actors = paginate_actors(request, selection)

        return jsonify({
            'success': True,
            'actors': current_actors,
            'total_actors': len(selection)
        })

    @app.route('/movies', methods=['GET'])
    @requires_auth('get:movies')
    def get_movies(jwt):
        selection = list(Movie.query.order_by(Movie.id).all())
        current_movies = paginate_movies(request, selection)

        return jsonify({
            'success': True,
            'movies': current_movies,
            'total_movies': len(selection)
        })

    @app.route('/actors', methods=['POST'])
    @requires_auth('post:actor')
    def add_actor(jwt):
        body = request.get_json()
        new_name = body.get('name', None)
        new_age = body.get('age', None)
        new_gender = body.get('gender', None)

        try:
            actor = Actor(name=new_name, age=new_age,
                          gender=new_gender)
            actor.insert()

            selection = (Actor.query.order_by(Actor.id).all())
            current_actors = paginate_actors(request, selection)

            return jsonify({
                'success': True,
                'created': actor.id,
                'actors': current_actors,
                'total_actors': len(selection)
            })
        except Exception:
            abort(422)

    @app.route('/movies', methods=['POST'])
    @requires_auth('post:movie')
    def add_movie(jwt):
        body = request.get_json()
        new_title = body.get('title', None)
        new_city = body.get('city', None)
        new_release_date = body.get('release_date', None)

        try:
            movie = Movie(title=new_title, city=new_city,
                          release_date=new_release_date)
            movie.insert()

            selection = list(Movie.query.order_by(Movie.id).all())
            current_movies = paginate_movies(request, selection)

            return jsonify({
                'success': True,
                'created': movie.id,
                'movies': current_movies,
                'total_movies': len(selection)
            })

        except Exception:
            abort(422)

    @app.route('/actors/<int:actor_id>', methods=['DELETE'])
    @requires_auth('delete:actor')
    def delete_actor(jwt, actor_id):
        try:
            actor = Actor.query.filter(Actor.id ==
                                       actor_id).first_or_404()
            actor.delete()

            selection = list(Actor.query.order_by(Actor.id).all())
            current_actors = paginate_actors(request, selection)

            return jsonify({
                'success': True,
                'deleted_actor_id': actor.id,
                'actors': current_actors,
                'total_actors': len(selection)
            })

        except Exception:
            abort(422)

    @app.route('/movies/<int:movie_id>', methods=['DELETE'])
    @requires_auth('delete:movie')
    def delete_movie(jwt, movie_id):
        try:

            movie = Movie.query.filter(Movie.id ==
                                       movie_id).first_or_404()
            movie.delete()

            selection = list(Movie.query.order_by(Movie.id).all())
            current_movies = paginate_movies(request, selection)

            return jsonify({
                'success': True,
                'deleted_movie_id': movie.id,
                'movies': current_movies,
                'total_movies': len(selection)
            })
        except Exception:
            abort(422)

    @app.route('/actors/<int:actor_id>', methods=['PATCH'])
    @requires_auth('patch:actor')
    def edit_actor(jwt, actor_id):
        try:
            actor = Actor.query.filter(Actor.id ==
                                       actor_id).first_or_404()
            body = request.get_json()

            actor.name = body.get('name', None)
            actor.age = body.get('age', None)
            actor.gender = body.get('gender', None)

            actor.update()

            selection = list(Actor.query.order_by(Actor.id).all())
            current_actors = paginate_actors(request, selection)

            return jsonify({
                'success': True,
                'editied_actor_id': actor.id,
                'actors': current_actors,
                'total_actors': len(selection)
            })

        except Exception:
            abort(422)

    @app.route('/movies/<int:movie_id>', methods=['PATCH'])
    @requires_auth('patch:movie')
    def edit_movie(jwt, movie_id):
        try:
            movie = Movie.query.filter(Movie.id ==
                                       movie_id).first_or_404()
            body = request.get_json()
            movie.title = body.get('title', None)
            movie.city = body.get('city', None)
            movie.release_date = body.get('release_date', None)

            movie.update()

            selection = list(Movie.query.order_by(Movie.id).all())
            current_movies = paginate_movies(request, selection)

            return jsonify({
                'success': True,
                'edited_movie_id': movie.id,
                'movies': current_movies,
                'total_movies': len(selection)
            })
        except Exception:
            abort(422)

    @app.route('/searchActors', methods=['POST'])
    @requires_auth('search:actor')
    def search_actor(jwt):
        try:

            body = request.get_json()

            if ('name' in body):
                name = body.get('name', None)
                selection = Actor.query.filter(Actor.name.ilike
                                               (f'%{name}%')).all()
                current_actors = paginate_actors(request,
                                                 selection)
            if ('age' in body):
                age = body.get('age', None)
                selection = Actor.query.filter(Actor.age ==
                                               (age))
                current_actors = paginate_actors(request,
                                                 selection)

            return jsonify({
                'success': True,
                'matches': current_actors,
                'number_of_matches': len(current_actors)
            })
        except Exception:
            abort(422)

    @app.route('/searchMovies', methods=['POST'])
    @requires_auth('search:movie')
    def search_movie(jwt):
        # try:
        body = request.get_json()

        if('title' in body):
            title = body.get('title', None)
            selection = Movie.query.filter(
                Movie.title.ilike(f'%{title}%')).all()
            current_movies = paginate_movies(request,
                                             selection)
        if('city' in body):
            city = body.get('city')
            selection = Movie.query.filter
            (Movie.city.ilike(f'%{city}%')).all()
            current_movies = paginate_movies(request,
                                             selection)
        return jsonify({
            'success': True,
            'matches': current_movies,
            'number_of_matches': len(current_movies)
        })
        # except Exception:
        # abort(422)

    @app.route('/')
    def main_page():
        return jsonify({
            'success': False,
            'message': 'please give the right authorization'
        })

    @app.errorhandler(422)
    def unprocessable(error):
        return jsonify({
            "success": False,
            "error": 422,
            "message": "unprocessable"
        }), 422

    @app.errorhandler(404)
    def resource_not_found(error):
        return jsonify({
            "success": False,
            "error": 404,
            "message": "resource not found"
        }), 404

    @app.errorhandler(500)
    def Internal_server_error(error):
        return jsonify({
            "success": False,
            "error": 500,
            "message": "Internal Server Error"
        }), 500

    @app.errorhandler(AuthError)
    def handle_Auth_Error(error):
        return jsonify({
            "success": False,
            "error": 401,
            "message": "AuthError"
        }), 401

    return app
Ejemplo n.º 8
0
from flask_sqlalchemy import SQLAlchemy # ORM模型扩展库
from flask_migrate import Migrate  # 模型迁移
from flask_mail import Mail  # 邮件发送扩展库
from flask_login import LoginManager  # pip install flask-login 处理用户登录的第三方扩展库
from flask_moment import Moment  # 格式化时间显示的扩展库
# 导入头像上传
from flask_uploads import IMAGES,UploadSet,configure_uploads,patch_request_class


# 实例化
bootstrap = Bootstrap() # bootstrap扩展库
db = SQLAlchemy() # ORM模型扩展库
migrate = Migrate() # 模型迁移
mail = Mail()  # 邮件发送
login_manger = LoginManager() # 处理登陆的第三方扩展库
moment = Moment()  # 实例化格式化时间显示的扩展库
file = UploadSet('photos',IMAGES)

# 加载app
def init_app(app):
    bootstrap.init_app(app)
    db.init_app(app)
    migrate.init_app(app=app,db=db)
    mail.init_app(app)
    moment.init_app(app)

    login_manger.init_app(app)
    login_manger.login_view = 'user.login' # 当你没有登录访问了需要登录的路由的时候 进行登录的端点
    login_manger.login_message = '您还没有登录 请登陆后在访问'  # 提示信息
    login_manger.session_protection = 'strong' # 设置session的保护级别
Ejemplo n.º 9
0
def create_app(test_config=None):
    """ create and configure the app """
    app = Flask(__name__)
    setup_db(app)
    moment = Moment(app)
    CORS(app)
    app.jinja_env.filters['datetime'] = format_datetime

    # CORS Headers

    @app.after_request
    def after_request(response):
        response.headers.add('Access-Control-Allow-Headers',
                             'Content-Type,Authorization,true')
        response.headers.add('Access-Control-Allow-Methods',
                             'GET,PUT,POST,DELETE,OPTIONS')
        return response

    # @TODO: Define ERROR handlers functions

    @app.errorhandler(404)
    def not_found(error):
        """ Returns 404 not found Error """
        return jsonify({
            "success": False,
            "error": 404,
            "message": "Resource Not found"
        }), 404

    @app.errorhandler(400)
    def bad_request(error):
        """ Returns 400 Bad Request Error """
        return jsonify({
            "success": False,
            "error": 400,
            "message": "Bad Request"
        }), 400

    @app.errorhandler(422)
    def unprocessable_entity(error):
        """ Returns 422 Unprocessable Request Error """
        return jsonify({
            "success": False,
            "error": 422,
            "message": "Unprocessable Request"
        }), 422

    #  Venues
    @app.route('/')
    def index():
        return render_template('pages/home.html')

    #  ----------------------------------------------------------------

    @app.route('/venues')
    def venues():
        # @TODO: replace with real venues data.
        #   num_shows should be aggregated based on number of upcoming shows per venue.
        # Gathering data from database
        try:
            cities = City.query.all()
            data = [{"city": city.name,
                     "state": city.states.name,
                     "venues": [{"id": venue.id,
                                 "name": venue.name,
                                 "num_upcoming_shows":
                                     len([show for show in venue.shows
                                          if show.start_time > CURRENT_DATE])
                                 }
                                for venue in city.venues]
                     } for city in cities]

            return render_template('pages/venues.html', areas=data)
        except None:
            abort(404)

    @app.route('/venues/search', methods=['POST'])
    def search_venues():
        # @TODO: implement search on venues with partial string search. Ensure it is case-insensitive.
        # seach for Hop should return "The Musical Hop".
        # search for "Music" should return "The Musical Hop" and "Park Square Live Music & Coffee"
        try:
            venue_hint = "%{}%".format(request.form.get("search_term"))
            search_result = Venue.query.filter(Venue.name.ilike(venue_hint)).all()
            # print(search_result[0])
            data = [{"id": venue.id,
                     "name": venue.name} for venue in search_result]
            # Returning the response object
            response = {
                "data": data,
                "count": len(search_result)
            }
            return render_template('pages/search_venues.html', results=response,
                                   search_term=request.form.get('search_term', ''))
        except None:
            abort(404)

    @app.route('/venues/<int:venue_id>')
    def show_venue(venue_id):
        # shows the venue page with the given venue_id
        # @TODO: replace with real venue data from the venues table, using venue_id
        try:
            venue = Venue.query.filter(Venue.id == venue_id).one_or_none()
            # print(artist.shows)
            genres = [genre.name for genre in venue.genres]
            shows = [show for show in venue.shows]
            past_shows = [{"artist_id": show.artist_id,
                           "artist_name": show.artists.name,
                           "artist_image_link": show.artists.image_link,
                           "start_time": format_datetime(str(show.start_time), format="full")
                           } for show in shows
                          if show.start_time < CURRENT_DATE]
            upcoming_shows = [{"artist_id": show.artist_id,
                               "artist_name": show.artists.name,
                               "artist_image_link": show.artists.image_link,
                               "start_time": format_datetime(str(show.start_time), format="full")
                               } for show in shows
                              if show.start_time > CURRENT_DATE]
            # print(genres)
            data = {"id": venue.id,
                    "name": venue.name,
                    "genres": genres,
                    "address": venue.venue_address[0].address,
                    "city": venue.cities[0].name,
                    "state": venue.cities[0].states.name,
                    "phone": venue.venue_address[0].phone,
                    "website": venue.website,
                    "facebook_link": venue.facebook_link,
                    "seeking_talent": venue.seeking_talent,
                    "seeking_description": venue.seeking_description,
                    "image_link": venue.image_link,
                    "past_shows": past_shows,
                    "upcoming_shows": upcoming_shows,
                    "past_shows_count": len(past_shows),
                    "upcoming_shows_count": len(upcoming_shows)
                    }

            return render_template('pages/show_venue.html', venue=data)
        except None:
            abort(404)

    #  Create Venue
    #  ----------------------------------------------------------------

    @app.route('/venues/create', methods=['GET'])
    def create_venue_form():
        form = VenueForm()
        return render_template('forms/new_venue.html', form=form)

    @app.route('/venues/create', methods=['POST'])
    def create_venue_submission():
        # @TODO: insert form data as a new Venue record in the db, instead
        # Querying venues and all related models to make sure we are not inserting
        # records that already exist in our database.
        venues = Venue.query.all()
        cities = City.query.all()
        states = State.query.all()
        genres = Genre.query.all()

        try:
            if request.method == 'POST':
                # Gather all data from the front end forms
                form_genres = request.form.getlist('genres')
                form_name = request.form.get('name')
                form_city = request.form.get('city')
                form_state = request.form.get('state')
                form_address = request.form.get('address')
                form_phone = request.form.get('phone')
                form_facebook_link = request.form.get('facebook_link')

                if len(form_genres) > 5:
                    flash('Cannot select more than 5 genres')
                    return redirect(url_for('create_venue_form'))
                else:
                    # Check if the Venue is already present on the database
                    check_venue = [venue for venue in venues if form_name.lower() == venue.name.lower()]
                    check_city = [city for city in cities if form_city.lower() == city.name.lower()]
                    check_states = [state for state in states if form_state.lower() == state.name.lower()]
                    check_genres = [genre for genre in genres if genre.name.lower()
                                    in list(map(str.lower, form_genres))]
                    # print(check_states)

                    if check_venue:
                        # If venue already exists flash message to the client
                        # Else create new venue
                        flash('Venue ' + request.form['name'] + ' already exists!')
                    else:
                        venue = Venue(venue_id=None, name=form_name, image_link=None,
                                      facebook_link=form_facebook_link, website=None,
                                      seeking_talent=None, seeking_description=None)
                        venue.insert()
                        if check_city:
                            for city in check_city:
                                venue.cities.append(city)
                        else:
                            city = City
                            if check_states:
                                print(check_states)
                                for state in check_states:
                                    city = City(city_id=None, name=form_city, state_id=state.id)
                            else:
                                state = State(id=None, name=form_state)
                                state.insert()
                                state = State.query.filter(State.name.lower() == form_state.lower()).one_or_none()
                                city = City(city_id=None, name=form_city, state_id=state.id)
                            venue.cities.append(city)

                        if check_genres:
                            # The genres are already set up by default
                            # There is no need to create a new on
                            # The genres will be appended into the venue
                            for genre in check_genres:
                                venue.genres.append(genre)
                        # After all checks
                        # Update the early create venue
                        # No need to check for address because if the venue is new
                        # The address will also be new
                        address = Venue_Address(id=None, address=form_address, phone=form_phone)
                        venue.venue_address.append(address)
                        venue.update()
                        # @TODO: modify data to be the data object returned from db insertion

                        # on successful db insert, flash success
                        flash('Venue ' + venue.name + ' was successfully created!')
                    return render_template('pages/home.html')

            else:
                # @TODO: on unsuccessful db insert, flash an error instead.
                flash('An error occurred. Venue ' + request.form.get('name') + ' could not be created.')
                # see: http://flask.pocoo.org/docs/1.0/patterns/flashing/
                return render_template('pages/home.html')
        except None:
            abort(422)

    @app.route('/venues/<venue_id>', methods=['POST'])
    def delete_venue(venue_id):
        # @TODO: Complete this endpoint for taking a venue_id, and using
        # SQLAlchemy ORM to delete a record. Handle cases where the session commit could fail.
        # print("Hello")
        venue = Venue.query.filter(Venue.id == venue_id).one_or_none()

        if venue:
            venue.delete()
            flash('Venue ' + venue.name + ' was successfully deleted!')
            return redirect(url_for('venues'))
        else:
            flash('Venue ' + venue.name + ' does not exist!')
            return render_template('pages/home.html')
        # BONUS CHALLENGE: Implement a button to delete a Venue on a Venue Page, have it so that
        # clicking that button delete it from the db then redirect the user to the homepage

    #  Artists
    #  ----------------------------------------------------------------
    @app.route('/artists')
    def artists():
        # @TODO: replace with real data returned from querying the database
        try:
            artist_list = Artist.query.all()
            serialized_artist_list = [artist.serialize() for artist in artist_list]
            # print(serialized_artist_list)
            data = [{"id": row["id"], "name": row["name"]} for row in serialized_artist_list]
            return render_template('pages/artists.html', artists=data)
        except None:
            abort(404)

    @app.route('/artists/search', methods=['POST'])
    def search_artists():
        # @TODO: implement search on artists with partial string search. Ensure it is case-insensitive.
        # seach for "A" should return "Guns N Petals", "Matt Quevado", and "The Wild Sax Band".
        # search for "band" should return "The Wild Sax Band".
        # Getting current data so that can compare it to the shows start time
        try:
            artist_hint = "%{}%".format(request.form.get("search_term"))
            search_result = Artist.query.filter(Artist.name.ilike(artist_hint)).all()
            # Since we can get more than one artist we only get the
            # number of upcoming shows if we get one artist instead of
            # a list of artists
            # If the search_result has more than one artist
            # upcoming_shows list will still empty
            # Else if the result is only one artist
            # Then the upcoming_shows list shall be fulfilled with the shows
            # print(search_result[0])
            data = [{"id": artist.id,
                     "name": artist.name} for artist in search_result]
            # Returning the response object
            response = {
                "data": data,
                "count": len(search_result)
            }

            return render_template('pages/search_artists.html', results=response,
                                   search_term=request.form.get('search_term', ''))
        except None:
            abort(404)

    @app.route('/artists/<int:artist_id>')
    def show_artist(artist_id):
        # shows the artist page with the given artist_id
        # @TODO: replace with real artist data from the artists table, using artist_id
        try:
            artist = Artist.query.filter(Artist.id == artist_id).one_or_none()
            # print(artist.shows)
            if artist:
                genres = [genre.name for genre in artist.genres]
                shows = [show for show in artist.shows]
                past_shows = [{"venue_id": show.venue_id,
                               "venue_name": show.venues.name,
                               "venue_image_link": show.venues.image_link,
                               "start_time": format_datetime(str(show.start_time), format="full")
                               } for show in shows
                              if show.start_time < CURRENT_DATE]
                upcoming_shows = [{"venue_id": show.venue_id,
                                   "venue_name": show.venues.name,
                                   "venue_image_link": show.venues.image_link,
                                   "start_time": format_datetime(str(show.start_time), format="full")
                                   } for show in shows
                                  if show.start_time > CURRENT_DATE]
                # print(genres)
                data = {"id": artist.id,
                        "name": artist.name,
                        "genres": genres,
                        "city": artist.city,
                        "state": artist.state,
                        "phone": artist.phone,
                        "website": artist.website,
                        "facebook_link": artist.facebook_link,
                        "seeking_venue": artist.seeking_venue,
                        "seeking_description": artist.seeking_description,
                        "image_link": artist.image_link,
                        "past_shows": past_shows,
                        "upcoming_shows": upcoming_shows,
                        "past_shows_count": len(past_shows),
                        "upcoming_shows_count": len(upcoming_shows)
                        }

                return render_template('pages/show_artist.html', artist=data)
            else:
                flash('Artist with ID ' + artist_id + ' do not exist!')
                return render_template('pages/home.html')
        except None:
            abort(404)

    #  Update
    #  ----------------------------------------------------------------
    @app.route('/artists/<int:artist_id>/edit', methods=['GET'])
    def edit_artist(artist_id):
        form = ArtistForm()
        artist = {
            "id": 4,
            "name": "Guns N Petals",
            "genres": ["Rock n Roll"],
            "city": "San Francisco",
            "state": "CA",
            "phone": "326-123-5000",
            "website": "https://www.gunsnpetalsband.com",
            "facebook_link": "https://www.facebook.com/GunsNPetals",
            "seeking_venue": True,
            "seeking_description": "Looking for shows to perform at in the San Francisco Bay Area!",
            "image_link": "https://images.unsplash.com/photo-1549213783-8284d0336c4f?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=300&q=80"
        }
        # @TODO: populate form with fields from artist with ID <artist_id>
        return render_template('forms/edit_artist.html', form=form, artist=artist)

    @app.route('/artists/<int:artist_id>/edit', methods=['POST'])
    def edit_artist_submission(artist_id):
        # @TODO: take values from the form submitted, and update existing
        # artist record with ID <artist_id> using the new attributes

        return redirect(url_for('show_artist', artist_id=artist_id))

    @app.route('/venues/<int:venue_id>/edit', methods=['GET'])
    def edit_venue(venue_id):
        form = VenueForm()
        venue = {
            "id": 1,
            "name": "The Musical Hop",
            "genres": ["Jazz", "Reggae", "Swing", "Classical", "Folk"],
            "address": "1015 Folsom Street",
            "city": "San Francisco",
            "state": "CA",
            "phone": "123-123-1234",
            "website": "https://www.themusicalhop.com",
            "facebook_link": "https://www.facebook.com/TheMusicalHop",
            "seeking_talent": True,
            "seeking_description": "We are on the lookout for a local artist to play every two weeks. Please call us.",
            "image_link": "https://images.unsplash.com/photo-1543900694-133f37abaaa5?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=400&q=60"
        }
        # @TODO: populate form with values from venue with ID <venue_id>
        return render_template('forms/edit_venue.html', form=form, venue=venue)

    @app.route('/venues/<int:venue_id>/edit', methods=['POST'])
    def edit_venue_submission(venue_id):
        # @TODO: take values from the form submitted, and update existing
        # venue record with ID <venue_id> using the new attributes
        return redirect(url_for('show_venue', venue_id=venue_id))

    #  Create Artist
    #  ----------------------------------------------------------------

    @app.route('/artists/create', methods=['GET'])
    def create_artist_form():
        form = ArtistForm()
        return render_template('forms/new_artist.html', form=form)

    @app.route('/artists/create', methods=['POST'])
    def create_artist_submission():
        # called upon submitting the new artist listing form
        # @TODO: insert form data as a new Artist record in the db, instead
        # @TODO: modify data to be the data object returned from db insertion
        artists = Artist.query.all()
        genres = Genre.query.all()

        try:
            if request.method == 'POST':
                # Gather all data from the front end forms
                form_genres = request.form.getlist('genres')
                form_name = request.form.get('name')
                form_city = request.form.get('city')
                form_state = request.form.get('state')
                form_image = request.form.get('image_link')
                form_phone = request.form.get('phone')
                form_facebook_link = request.form.get('facebook_link')

                if len(form_genres) > 10:
                    flash('Cannot select more than 5 genres')
                    return redirect(url_for('create_artist_form'))
                else:
                    # Check if the Artist is already present on the database
                    check_artist = [artist for artist in artists if form_name.lower() == artist.name.lower()]
                    check_genres = [genre for genre in genres if genre.name.lower()
                                    in list(map(str.lower, form_genres))]
                    # print(check_states)

                    if check_artist:
                        # If Artist already exists flash message to the client
                        # Else create new venue
                        flash('Artist ' + form_name + ' already exists!')
                    else:
                        artist = Artist(artist_id=None, name=form_name, city=form_city,
                                        state=form_state, phone=form_phone,
                                        image_link=form_image, facebook_link=form_facebook_link,
                                        website=None, seeking_venue=None, seeking_description=None)
                        artist.insert()

                        if check_genres:
                            # The genres are already set up by default
                            # There is no need to create a new on
                            # The genres will be appended into the artist
                            for genre in check_genres:
                                artist.genres.append(genre)
                        # After all checks
                        artist.update()
                        # @TODO: modify data to be the data object returned from db insertion

                        # on successful db insert, flash success
                        flash('Artist ' + artist.name + ' was successfully created!')
                    return render_template('pages/home.html')

            else:
                # @TODO: on unsuccessful db insert, flash an error instead.
                flash('An error occurred. Venue ' + request.form.get('name') + ' could not be created.')
                # see: http://flask.pocoo.org/docs/1.0/patterns/flashing/
                return render_template('pages/home.html')
        except None:
            abort(422)
        # on successful db insert, flash success
        flash('Artist ' + request.form['name'] + ' was successfully created!')
        # @TODO: on unsuccessful db insert, flash an error instead.
        # e.g., flash('An error occurred. Artist ' + data.name + ' could not be listed.')
        return render_template('pages/home.html')

    #  Shows
    #  ----------------------------------------------------------------

    @app.route('/shows')
    def shows():
        # displays list of shows at /shows
        # @TODO: replace with real venues data.
        #       num_shows should be aggregated based on number of upcoming shows per venue.
        shows = Show.query.all()

        data = [{"venue_id": shows[i].venue_id,
                 "venue_name": shows[i].venues.name,
                 "artist_id": shows[i].artist_id,
                 "artist_name": shows[i].artists.name,
                 "artist_image_link": shows[i].artists.image_link,
                 "ticket_price": shows[i].ticket_price,
                 "start_time": format_datetime(str(shows[i].start_time), format="full")} for i in range(len(shows))]

        return render_template('pages/shows.html', shows=data)

    @app.route('/shows/create')
    def create_shows():
        # renders form. do not touch.
        form = ShowForm()
        return render_template('forms/new_show.html', form=form)

    @app.route('/shows/create', methods=['POST'])
    def create_show_submission():
        # called to create new shows in the db, upon submitting new show listing form
        # @TODO: insert form data as a new Show record in the db, instead
        try:
            if request.method == 'POST':
                artist_id = request.form.get('artist_id')
                venue_id = request.form.get('venue_id')
                start_time = request.form.get('start_time')
                ticket_price = request.form.get('ticket_price')
                # print(artist_id, venue_id, start_time, ticket_price)
                show = Show(show_id=None, artist_id=artist_id, venue_id=venue_id,
                            start_time=start_time, ticket_price=ticket_price)
                show.insert()
                # on successful db insert, flash success
                # @TODO: on successful db insert, flash an error instead.
                flash('Show was successfully created!')
                # e.g., flash('An error occurred. Show could not be listed.')
                # see: http://flask.pocoo.org/docs/1.0/patterns/flashing/
                return redirect(url_for('shows'))
            else:
                # on successful db insert, flash success
                # @TODO: on unsuccessful db insert, flash an error instead.
                flash('Show was not created!')
                # e.g., flash('An error occurred. Show could not be listed.')
                # see: http://flask.pocoo.org/docs/1.0/patterns/flashing/
                return redirect(url_for('create_shows'))

        except None:
            abort(401)

    return app
Ejemplo n.º 10
0
        'sqlite:///' + os.path.join(basedir, 'data.sqlite')
app.config['SQLALCHEMY_COMMIT_ON_TEARDOWN'] = True

app.config['MAIL_SERVER'] = 'smtp.126.com'
app.config['MAIL_PORT'] = 25
app.config['MAIL_USE_TLS'] = True
app.config['MAIL_USERNAME'] = os.environ.get('MAIL_USERNAME')
app.config['MAIL_PASSWORD'] = os.environ.get('MAIL_PASSWORD')

app.config['FLASKY_MAIL_SUBJECT_PREFIX'] = '[Flasky]'
app.config['FLASKY_MAIL_SENDER'] = 'OrchidRock <*****@*****.**>'
app.config['FLASKY_ADMIN'] = os.environ.get('FLASKY_ADMIN')

manager = Manager(app)
bootstrap = Bootstrap(app)
monment = Moment(app)
db = SQLAlchemy(app)
migrate = Migrate(app, db)
mail = Mail(app)


def make_shell_context():
    """TODO: Docstring for make_shell_context.
    :returns: TODO

    """
    return dict(app=app, db=db, User=User, Role=Role)


def send_async_email(app, msg):
    with app.app_context():
Ejemplo n.º 11
0
# thread
if async_mode == 'eventlet':
    import eventlet
    eventlet.monkey_patch()
elif async_mode == 'gevent':
    from gevent import monkey
    monkey.patch_all()


app = Flask(__name__)
login_manager = LoginManager()
db = SQLAlchemy()
socketio = SocketIO()
oauth = OAuth(app)
redis = Redis()
moment = Moment()

with app.app_context():

    config_name = os.getenv('FLASK_CONFIG') or 'development'
    app.config.from_object(config[config_name])
    config[config_name].init_app(app)

    db.app = app
    db.init_app(app)

    login_manager.init_app(app)
    login_manager.session_protection = 'strong'
    login_manager.login_view = 'login'
    login_manager.login_message = u'请先登陆系统,若遗忘密码,请联系管理员'
    login_manager.login_message_category = 'warning'
Ejemplo n.º 12
0
def setup_db(app):
    moment = Moment(app)
    app.config.from_object('config')
    db = SQLAlchemy(app)
    migrate = Migrate(app, db)
Ejemplo n.º 13
0
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_bcrypt import Bcrypt
from flask_login import LoginManager
from flask_moment import Moment
from flask_mail import Mail
from flaskblog.config import Config

db = SQLAlchemy()
bcrypt = Bcrypt()  # for incription and decription
moment = Moment()  # for local time on the user posted
login_manager = LoginManager()  # login required
login_manager.login_view = 'users.login'  # makes sure that when login required, takes user to login page
login_manager.login_message_category = 'info'  # nice bootstrap blue color

mail = Mail()


def create_app(config_class=Config):
    app = Flask(__name__)
    app.config.from_object(Config)

    db.init_app(app)
    bcrypt.init_app(app)  # incription and decription
    moment.init_app(app)  # local time on the user posted
    login_manager.init_app(app)  # login required
    mail.init_app(app)

    # import applications such as users and then register them with the application
    from flaskblog.users.routes import users
    from flaskblog.posts.routes import posts
Ejemplo n.º 14
0
#!/usr/bin/env python3
# _*_ coding:utf-8 _*_
from datetime import datetime
from flask import Flask, render_template
from flask_bootstrap import Bootstrap
from flask_moment import Moment

__author__ = 'CityManager'

app = Flask(__name__)
Bootstrap(app)  # 使用flask_bootstrap扩展
Moment(app)


@app.route('/micros/')
def learn_micros():
    return render_template('common.html', comments=range(1, 10))


@app.route('/include/')
def learn_include():
    return render_template('base.html', comments=range(1, 10))


@app.route('/extends/')
def learn_extend():
    return render_template('use_base.html', comments=range(1, 10))


@app.route('/flask_bootstrap/')
def use_flask_bootstrap():
Ejemplo n.º 15
0
from flask_migrate import Migrate
from flask_login import LoginManager
import logging
from logging.handlers import SMTPHandler, RotatingFileHandler
import os
from flask_mail import Mail
from flask_bootstrap import Bootstrap
from flask_moment import Moment

application = Flask(__name__)
application.config.from_object(Config)
db = SQLAlchemy(application)
migrate = Migrate(application, db)
mail = Mail(application)
bootstrap = Bootstrap(application)
moment = Moment(application)

login = LoginManager(application)
login.login_view = 'login'


from app import routes, models, errors

if not application.debug:
    if application.config['MAIL_SERVER']:
        auth = None
        if application.config['MAIL_USERNAME'] or application.config['MAIL_PASSWORD']:
            auth = (application.config['MAIL_USERNAME'],
                    application.config['MAIL_PASSWORD'])
        secure = None
        if application.config['MAIL_USE_TLS']:
Ejemplo n.º 16
0
from flask import Flask
from flask_bootstrap import Bootstrap
from flask_mail import Mail
from flask_moment import Moment
from flask_sqlalchemy import SQLAlchemy
from flask_login import LoginManager
from flask_pagedown import PageDown
from config import config

bootstrap = Bootstrap()  # bootstrap 框架
mail = Mail()
moment = Moment()  # 日期和时间 Moment.js
db = SQLAlchemy()

login_manager = LoginManager()
# 可能会有 session 异常, 导致用户登录无法记住
# login_manager.session_protection = 'strong'
login_manager.login_view = 'auth.login'  # 设置登录页面端点

pagedown = PageDown()


# 工厂函数
def create_app(config_name):
    app = Flask(__name__)
    app.config.from_object(config[config_name])
    config[config_name].init_app(app)

    # 初始化扩展程序
    bootstrap.init_app(app)
    mail.init_app(app)
Ejemplo n.º 17
0
#!/usr/bin/env python3
#coding=utf-8
#author="yexiaozhu"

from flask import Flask
from flask_pagedown import PageDown
from flask_bootstrap import Bootstrap
from flask_mail import Mail
from flask_moment import Moment
from flask_sqlalchemy import SQLAlchemy
from flask_login import LoginManager
from config import config

bootstrap = Bootstrap()
mail = Mail()
moment = Moment()
db = SQLAlchemy()
pagedown = PageDown()

login_manager = LoginManager()
login_manager.session_protection = 'strong'
login_manager.login_view = 'auth.login'


def create_app(config_name):
    app = Flask(__name__)
    app.config.from_object(config[config_name])
    config[config_name].init_app(app)

    bootstrap.init_app(app)
    mail.init_app(app)
Ejemplo n.º 18
0
def create_app(test_config=None):
    # create and configure the app
    app = Flask(__name__, template_folder='./templates')
    CORS(app)
    moment = Moment(app)
    setup_db(app)

    cors = CORS(app, resources={r"/api/*": {"origins": "*"}})
    '''
  @TODO: Use the after_request decorator to set Access-Control-Allow
  '''
    @app.after_request
    def after_request(response):
        response.headers.add('Access-Contro-Allow-Headers',
                             'Content-Type ,Authorization')
        response.headers.add('Access-Contro-Allow-Headers',
                             'GET, POST ,PATCH , DELETE ,OPTIONS')
        response.headers.add('Access-Control-Allow-Origin',
                             'http://*****:*****@app.route('/hello', methods=['GET'])
    def test_api():
        return jsonify({'success': True, 'hello': "hello there "})

    #login page API
    @app.route('/login', methods=['GET', 'POST'])
    def index():
        return render_template("index.html")

    #login page call back API
    @app.route('/user-page', methods=['GET', 'POST'])
    def user_logged():
        return render_template("logged.html")

    #list all the actors
    @app.route('/actors', methods=['GET'])
    @requires_auth('get:actors')
    def get_actors(self):
        actors = Actor.query.all()
        formatted_actors = [actor.format() for actor in actors]
        if len(formatted_actors) == 0:
            abort(404)

        return jsonify({
            'success': True,
            'actors': formatted_actors,
            'total_actors': len(formatted_actors)
        })

    # adding a new actor
    @app.route('/actors', methods=['post'])
    @requires_auth('post:actors')
    def add_new_actor(self):
        body = request.get_json()
        name = body.get('name', None)
        age = body.get('age', None)
        gender = body.get('gender', None)

        actor = Actor(name=name, age=age, gender=gender)
        actor.insert()
        actors = Actor.query.all()
        formatted_actors = [actor.format() for actor in actors]

        return jsonify({
            'success': True,
            'actors': formatted_actors,
            'total_actors': len(formatted_actors),
            'created': actor.id
        })

    # updating  a specifc actor
    @app.route('/actors/<int:id>', methods=['PATCH'])
    @requires_auth('patch:actors')
    def update_actor(id):
        actor = Actor.query.filter(Actor.id == id).one_or_none()
        if actor is None:
            abort(404)
        body = request.get_json()
        actor.name = body.get('name', actor.name)
        actor.age = body.get('age', actor.age)
        actor.gender = body.get('gender', actor.gender)
        actor.update()
        actors = Actor.query.all()
        formatted_actors = [actor.format() for actor in actors]

        return jsonify({
            'success': True,
            'actors': formatted_actors,
            'modified_actor': id
        })

    # deleting a specifc actor
    @app.route('/actors/<int:id>', methods=['DELETE'])
    @requires_auth('delete:actors')
    def delete_actor(id):
        selected_actor = Actor.query.get(id)
        if selected_actor is None:
            abort(404)
        selected_actor.delete()

        return jsonify({'success': True, 'deleted': id})

    #listing all the movies
    @app.route('/movies', methods=['GET'])
    @requires_auth('get:movies')
    def get_movies(self):
        movies = Movie.query.all()
        formatted_movies = [movie.format() for movie in movies]
        if len(formatted_movies) == 0:
            abort(404)

        return jsonify({
            'success': True,
            'movies': formatted_movies,
            'total_movies': len(formatted_movies)
        })

    # adding a new movie
    @app.route('/movies', methods=['post'])
    @requires_auth('post:movies')
    def add_new_movie(self):
        body = request.get_json()
        title = body.get('title', None)
        release_date = body.get('release_date', None)
        actors = Actor.query.filter(Actor.id.in_(body.get('actors',
                                                          None))).all()
        print(actors)
        movie = Movie(title=title, release_date=release_date)
        movie.actors = actors
        movie.insert()
        movies = Movie.query.all()
        formatted_movies = [movie.format() for movie in movies]

        return jsonify({
            'success': True,
            'movies': formatted_movies,
            'total_movies': len(formatted_movies),
            'created': movie.id
        })

    #editing an exist movie
    @app.route('/movies/<int:id>', methods=['PATCH'])
    @requires_auth('patch:movies')
    def update_movie(id):
        movie = Movie.query.filter(Movie.id == id).one_or_none()
        if movie is None:
            abort(404)

        body = request.get_json()
        movie.title = body.get('title', None)
        movie.release_date = body.get('release_date', None)
        movie.actors = Actor.query.filter(
            Actor.id.in_(body.get('actors', None))).all()
        movie.update()
        movies = Movie.query.all()
        formatted_movies = [movie.format() for movie in movies]

        return jsonify({
            'success': True,
            'movies': formatted_movies,
            'modified_movie': id
        })

    # deleting a specifc movie
    @app.route('/movies/<int:id>', methods=['DELETE'])
    @requires_auth('delete:movies')
    def delete_movie(id):
        selected_movie = Movie.query.get(id)
        if selected_movie is None:
            abort(404)

        selected_movie.delete()

        return jsonify({'success': True, 'deleted': id})

    #Error Handeling
    @app.errorhandler(404)
    def unprocessable(error):
        return jsonify({
            "success": False,
            "error": 404,
            "message": "resource not found "
        }), 404

    @app.errorhandler(400)
    def bad_request(error):
        return jsonify({
            "success": False,
            "error": 400,
            "message": "bad request "
        }), 400

    @app.errorhandler(405)
    def method_not_found(error):
        return jsonify({
            "success": False,
            "error": 405,
            "message": "Method not found "
        }), 405

    @app.errorhandler(AuthError)
    def handle_auth_error(ex):
        response = jsonify(ex.error)
        response.status_code = ex.status_code
        return response

    return app
Ejemplo n.º 19
0
def moment(app):
    moment = Moment()
    moment.init_app(app)
    yield moment
Ejemplo n.º 20
0
def create_app(test_config=None):
    app = Flask(__name__)
    setup_db(app)
    moment = Moment(app)

    #----------------------------------------------------------------------------#
    # Filters.
    #----------------------------------------------------------------------------#

    def format_datetime(value, format='medium'):
        date = dateutil.parser.parse(value)
        if format == 'full':
            format = "EEEE MMMM, d, y 'at' h:mma"
        elif format == 'medium':
            format = "EE MM, dd, y h:mma"
        return babel.dates.format_datetime(date, format)

    app.jinja_env.filters['datetime'] = format_datetime

    #----------------------------------------------------------------------------#
    # Controllers.
    #----------------------------------------------------------------------------#

    @app.route('/')
    def index():
        return render_template('pages/home.html')

    #  Venues
    #  ----------------------------------------------------------------

    @app.route('/venues')
    def venues():
        # TODO: replace with real venues data.
        # num_shows should be aggregated based on number of upcoming shows per venue.

        city_groub = db.session.query(Venue).distinct(Venue.state,
                                                      Venue.city).all()

        data = []

        for city_l in city_groub:
            val_dic = {}
            venues = db.session.query(Venue).group_by(Venue.id, Venue.city,
                                                      Venue.state).all()
            for venue in venues:
                if venue.city == city_l.city:
                    val_dic['city'] = city_l.city
                    val_dic['state'] = city_l.state
                venue_i = db.session.query(Venue).filter(
                    Venue.city == city_l.city).all()
                ven_ls = []
                for ven_last in venue_i:
                    ven_ls.append(ven_last)
                val_dic['venues'] = ven_ls
            data.append(val_dic)

        return render_template('pages/venues.html', areas=data)

    @app.route('/venues/search', methods=['POST'])
    def search_venues():
        # TODO: implement search on artists with partial string search. Ensure it is case-insensitive.
        # seach for Hop should return "The Musical Hop".
        # search for "Music" should return "The Musical Hop" and "Park Square Live Music & Coffee"
        searsh_tearm = request.form.get('search_term')
        search_like = "%{}%".format(searsh_tearm)
        response = db.session.query(Venue).filter(
            Venue.name.ilike(search_like)).all()
        count = db.session.query(Venue).filter(
            Venue.name.ilike(search_like)).count()
        return render_template('pages/search_venues.html',
                               results=response,
                               count=count,
                               search_term=request.form.get('search_term', ''))

    @app.route('/venues/<int:venue_id>')
    def show_venue(venue_id):
        # shows the venue page with the given venue_id
        # TODO: replace with real venue data from the venues table, using venue_id

        venue = db.session.query(Venue).filter(Venue.id == venue_id).first()

        past_shows = db.session.query(Artist, Show).join(Show).join(Venue).\
            filter(
                Show.venue_id == venue_id,
                Show.artist_id == Artist.id,
                Show.date < datetime.now()
            ).\
            all()
        upcoming_shows = db.session.query(Artist, Show).join(Show).join(Venue).\
            filter(
            Show.venue_id == venue_id,
            Show.artist_id == Artist.id,
            Show.date > datetime.now()
        ).\
            all()

        data = {
            'id':
            venue.id,
            'name':
            venue.name,
            'city':
            venue.city,
            'state':
            venue.state,
            'address':
            venue.address,
            'phone':
            venue.phone,
            'genres':
            list(venue.genres),
            'image_link':
            venue.image_link,
            'facebook_link':
            venue.facebook_link,
            'seeking_talent':
            venue.seeking_talent,
            'seeking_description':
            venue.seeking_description,
            'past_shows': [{
                'artist_id': artist.id,
                "artist_name": artist.name,
                "artist_image_link": artist.image_link,
                "start_time": show.date.strftime("%m/%d/%Y, %H:%M")
            } for artist, show in past_shows],
            'upcoming_shows': [{
                'artist_id':
                artist.id,
                'artist_name':
                artist.name,
                'artist_image_link':
                artist.image_link,
                'start_time':
                show.date.strftime("%m/%d/%Y, %H:%M")
            } for artist, show in upcoming_shows],
            'past_shows_count':
            len(past_shows),
            'upcoming_shows_count':
            len(upcoming_shows)
        }

        return render_template('pages/show_venue.html', venue=data)

    #  Create Venue
    #  ----------------------------------------------------------------

    @app.route('/venues/create', methods=['GET'])
    def create_venue_form():
        form = VenueForm()
        return render_template('forms/new_venue.html', form=form)

    @app.route('/venues/create', methods=['POST'])
    def create_venue_submission():
        # TODO: insert form data as a new Venue record in the db, instead
        # TODO: modify data to be the data object returned from db insertion
        error = False
        try:
            # insert & Commit
            form = VenueForm(request.form)

            venue = Venue(name=form.name.data,
                          city=form.city.data,
                          state=form.state.data,
                          address=form.address.data,
                          phone=form.phone.data,
                          image_link=form.image_link.data,
                          facebook_link=form.facebook_link.data,
                          website=form.website.data,
                          seeking_talent=form.seeking_talent.data,
                          seeking_description=form.seeking_description.data,
                          genres=list(form.genres.data))
            db.session.add(venue)
            db.session.commit()
        except:
            # error Or Not
            error = True
            db.session.rollback()
            print(sys.exc_info())
        finally:
            # dissmis
            db.session.close()
        if error:
            abort(400)
        else:
            # on successful db insert, flash success
            flash('Venue ' + request.form['name'] +
                  ' was successfully listed!')
            # TODO: on unsuccessful db insert, flash an error instead.
            # e.g., flash('An error occurred. Venue ' + data.name + ' could not be listed.')
            # see: http://flask.pocoo.org/docs/1.0/patterns/flashing/
            return render_template('pages/home.html')

    @app.route('/venues/<int:venue_id>', methods=['DELETE'])
    def delete_venue(venue_id):
        # TODO: Complete this endpoint for taking a venue_id, and using
        # SQLAlchemy ORM to delete a record. Handle cases where the session commit could fail.
        error = False
        try:
            # db.session.query(Venue).filter(id=venue_id).delete()
            Show.query.filter_by(venue_id=venue_id).delete()
            Venue.query.filter_by(id=venue_id).delete()
            db.session.commit()
        except:
            error = True
            db.session.rollback()
            print(sys.exc_info())
        finally:
            db.session.close()
        # BONUS CHALLENGE: Implement a button to delete a Venue on a Venue Page, have it so that
        # clicking that button delete it from the db then redirect the user to the homepage
        if error:
            abort(400)
        else:
            flash('Venue was successfully Deleted!')
            # TODO: on unsuccessful db insert, flash an error instead.
            # e.g., flash('An error occurred. Venue ' + data.name + ' could not be listed.')
            # see: http://flask.pocoo.org/docs/1.0/patterns/flashing/
            return render_template('pages/home.html')

    #  Artists
    #  ----------------------------------------------------------------
    @app.route('/artists')
    def artists():
        # TODO: replace with real data returned from querying the database
        data = db.session.query(Artist).all()
        return render_template('pages/artists.html', artists=data)

    @app.route('/artists/search', methods=['POST'])
    def search_artists():
        # TODO: implement search on artists with partial string search. Ensure it is case-insensitive.
        # seach for "A" should return "Guns N Petals", "Matt Quevado", and "The Wild Sax Band".
        # search for "band" should return "The Wild Sax Band".
        searsh_tearm = request.form.get('search_term')
        search_like = "%{}%".format(searsh_tearm)
        response = db.session.query(Artist).filter(
            Artist.name.ilike(search_like)).all()
        count = db.session.query(Artist).filter(
            Artist.name.ilike(search_like)).count()
        return render_template('pages/search_artists.html',
                               results=response,
                               count=count,
                               search_term=request.form.get('search_term', ''))

    @app.route('/artists/<int:artist_id>')
    def show_artist(artist_id):
        # shows the venue page with the given venue_id
        # TODO: replace with real venue data from the venues table, using venue_id

        artist = db.session.query(Artist).filter(
            Artist.id == artist_id).first()

        past_shows = db.session.query(Venue, Show).join(Show).join(Artist).\
            filter(
            Show.artist_id == artist_id,
            Show.venue_id == Venue.id,
            Show.date < datetime.now()
        ).\
            all()
        upcoming_shows = db.session.query(Venue, Show).join(Show).join(Artist).\
            filter(
            Show.artist_id == artist_id,
            Show.venue_id == Venue.id,
            Show.date > datetime.now()
        ).\
            all()

        data = {
            'id':
            artist.id,
            'name':
            artist.name,
            'city':
            artist.city,
            'state':
            artist.state,
            'phone':
            artist.phone,
            'genres':
            artist.genres,
            'image_link':
            artist.image_link,
            'facebook_link':
            artist.facebook_link,
            'seeking_venue':
            artist.seeking_venue,
            'seeking_description':
            artist.seeking_description,
            'past_shows': [{
                'venue_id': venue.id,
                "venue_name": venue.name,
                "venue_image_link": venue.image_link,
                "start_time": show.date.strftime("%m/%d/%Y, %H:%M")
            } for venue, show in past_shows],
            'upcoming_shows': [{
                'venue_id':
                venue.id,
                'venue_name':
                venue.name,
                'venue_image_link':
                venue.image_link,
                'start_time':
                show.date.strftime("%m/%d/%Y, %H:%M")
            } for venue, show in upcoming_shows],
            'past_shows_count':
            len(past_shows),
            'upcoming_shows_count':
            len(upcoming_shows)
        }

        return render_template('pages/show_artist.html', artist=data)

    #  Update
    #  ----------------------------------------------------------------
    @app.route('/artists/<int:artist_id>/edit', methods=['GET'])
    def edit_artist(artist_id):
        form = ArtistForm()

        # TODO: populate form with fields from artist with ID <artist_id>
        artist = db.session.query(Artist).filter(
            Artist.id == artist_id).first()
        return render_template('forms/edit_artist.html',
                               form=form,
                               artist=artist)

    @app.route('/artists/<int:artist_id>/edit', methods=['POST'])
    def edit_artist_submission(artist_id):
        # TODO: take values from the form submitted, and update existing
        # artist record with ID <artist_id> using the new attributes
        error = False
        try:
            # update & Commit
            artist = db.session.query(Artist).get(artist_id)
            artist.name = request.form['name']
            artist.city = request.form['city']
            artist.state = request.form['state']
            artist.phone = request.form['phone']
            artist.genres = request.form.getlist('genres')
            artist.image_link = request.form['image_link']
            artist.facebook_link = request.form['facebook_link']
            artist.website = request.form['website']
            artist.seeking_venue = request.form['seeking_venue']
            artist.seeking_description = request.form['seeking_description']
            db.session.commit()
        except:
            # error Or Not
            error = True
            db.session.rollback()
            print(sys.exc_info())
        finally:
            # dissmis
            db.session.close()
        if error:
            abort(400)
        else:
            # on successful db insert, flash success
            flash('Artist ' + request.form['name'] +
                  ' was successfully Updated!')
            # TODO: on unsuccessful db insert, flash an error instead.
            # e.g., flash('An error occurred. Venue ' + data.name + ' could not be listed.')
            # see: http://flask.pocoo.org/docs/1.0/patterns/flashing/
            return redirect(url_for('show_artist', artist_id=artist_id))

    @app.route('/venues/<int:venue_id>/edit', methods=['GET'])
    def edit_venue(venue_id):
        form = VenueForm()

        # TODO: populate form with values from venue with ID <venue_id>
        venue = db.session.query(Venue).filter(Venue.id == venue_id).first()

        return render_template('forms/edit_venue.html', form=form, venue=venue)

    @app.route('/venues/<int:venue_id>/edit', methods=['POST'])
    def edit_venue_submission(venue_id):
        # TODO: take values from the form submitted, and update existing
        # venue record with ID <venue_id> using the new attributes
        error = False
        try:
            # update & Commit
            venue = db.session.query(Venue).get(venue_id)
            venue.name = request.form['name']
            venue.city = request.form['city']
            venue.state = request.form['state']
            venue.address = request.form['address']
            venue.phone = request.form['phone']
            venue.genres = request.form.getlist('genres')
            venue.image_link = request.form['image_link']
            venue.facebook_link = request.form['facebook_link']
            venue.website = request.form['website']
            venue.seeking_talent = request.form['seeking_talent']
            venue.seeking_description = request.form['seeking_description']
            db.session.commit()
        except:
            # error Or Not
            error = True
            db.session.rollback()
            print(sys.exc_info())
        finally:
            # dissmis
            db.session.close()
        if error:
            abort(400)
        else:
            # on successful db insert, flash success
            flash('Venue ' + request.form['name'] +
                  ' was successfully Updated!')
        return redirect(url_for('show_venue', venue_id=venue_id))

    #  Create Artist
    #  ----------------------------------------------------------------

    @app.route('/artists/create', methods=['GET'])
    def create_artist_form():
        form = ArtistForm()
        return render_template('forms/new_artist.html', form=form)

    @app.route('/artists/create', methods=['POST'])
    def create_artist_submission():
        # called upon submitting the new artist listing form
        # TODO: insert form data as a new Venue record in the db, instead
        # TODO: modify data to be the data object returned from db insertion
        error = False
        try:
            # insert & Commit
            form = ArtistForm(request.form)

            artist = Artist(name=form.name.data,
                            city=form.city.data,
                            state=form.state.data,
                            phone=form.phone.data,
                            genres=form.genres.data,
                            image_link=form.image_link.data,
                            facebook_link=form.facebook_link.data,
                            website=form.website.data,
                            seeking_description=form.seeking_description.data,
                            seeking_venue=form.seeking_venue.data)
            db.session.add(artist)
            db.session.commit()
        except:
            # error Or Not
            error = True
            db.session.rollback()
            print(sys.exc_info())
        finally:
            # dissmis
            db.session.close()
        if error:
            abort(400)
        else:
            flash('Artist ' + request.form['name'] +
                  ' was successfully listed!')
            return render_template('pages/home.html')

    #  Shows
    #  ----------------------------------------------------------------

    @app.route('/shows')
    def shows():
        # displays list of shows at /shows
        # TODO: replace with real venues data.
        # num_shows should be aggregated based on number of upcoming shows per venue.

        shows = db.session.query(Venue, Artist, Show).\
          select_from(Show).\
          join(Artist).\
          join(Venue).\
            filter(
            Show.artist_id == Artist.id,
            Show.venue_id == Venue.id,
        ).\
          all()

        data = [{
            'venue_id': venue.id,
            "venue_name": venue.name,
            'artist_id': artist.id,
            "artist_name": artist.name,
            "artist_image_link": artist.image_link,
            "start_time": show.date.strftime("%m/%d/%Y, %H:%M")
        } for venue, artist, show in shows]

        return render_template('pages/shows.html', shows=data)

    @app.route('/shows/create')
    def create_shows():
        # renders form. do not touch.
        form = ShowForm()
        return render_template('forms/new_show.html', form=form)

    @app.route('/shows/create', methods=['POST'])
    def create_show_submission():
        # called to create new shows in the db, upon submitting new show listing form
        # TODO: insert form data as a new Show record in the db, instead
        error = False
        try:
            # insert & Commit
            form = ShowForm(request.form)

            show = Show(artist_id=form.artist_id.data,
                        venue_id=form.venue_id.data,
                        date=form.start_time.data)
            db.session.add(show)
            db.session.commit()
        except:
            # error Or Not
            error = True
            db.session.rollback()
            print(sys.exc_info())
        finally:
            # dissmis
            db.session.close()

        if error:
            abort(400)
        else:
            flash('Show was successfully listed!')
            return render_template('pages/home.html')

        # on successful db insert, flash success
        # TODO: on unsuccessful db insert, flash an error instead.
        # e.g., flash('An error occurred. Show could not be listed.')
        # see: http://flask.pocoo.org/docs/1.0/patterns/flashing/
        # return render_template('pages/home.html')

    @app.errorhandler(404)
    def not_found_error(error):
        return render_template('errors/404.html'), 404

    @app.errorhandler(500)
    def server_error(error):
        return render_template('errors/500.html'), 500

    if not app.debug:
        file_handler = FileHandler('error.log')
        file_handler.setFormatter(
            Formatter(
                '%(asctime)s %(levelname)s: %(message)s [in %(pathname)s:%(lineno)d]'
            ))
        app.logger.setLevel(logging.INFO)
        file_handler.setLevel(logging.INFO)
        app.logger.addHandler(file_handler)
        app.logger.info('errors')

    return app