# Path: utils/database.py
# class Database:
#     """
#     This class handles :
#     - Provides a link and a cursor to the database as class attributes
#     Usage :
#     - Database.on()
#     - Database.CURSOR.execute(query, params)
#     - ... etc
#     - Database.off()
#     Available class attributes :
#     - LINK
#     - CURSOR
#     - FILE (path to the database file)
#     """
#     DATABASE_PRODUCTION = 'database.db'
#     """ Normal database filename """
#     DATABASE_TEST = 'database_test.db'
#     """ Test database filename : this database is temporary """
#     TEST_MODE = False
#     """ Does it need to use the test database ? """
# 
#     @classmethod
#     def on(cls):
#         """
#         Connect to the database
#         :rtype: bool
#         """
#         # Don't reconnect if already connected
#         if cls.is_ready():
#             return False
# 
#         # Filename
#         cls.FILE = cls.DATABASE_PRODUCTION
# 
#         if cls.TEST_MODE:
#             cls.FILE = cls.DATABASE_TEST
# 
#         # Connect
#         cls.LINK = sqlite3.connect(cls.FILE)
#         cls.LINK.row_factory = sqlite3.Row
#         cls.CURSOR = cls.LINK.cursor()
# 
#         return True
# 
#     @classmethod
#     def off(cls):
#         """
#         Ends connection with the database.
#         :rtype: bool
#         """
#         if cls.LINK:
#             cls.LINK.close()
#         cls.LINK = None
#         cls.CURSOR = None
#         return True
# 
#     @classmethod
#     def is_ready(cls):
#         """
#         Is the connection open ?
#         :rtype: bool
#         """
#         return hasattr(cls, 'LINK') and cls.LINK != None


import os
import random
import string
import hashlib
import json
import utils
import models
from flask import Flask, render_template, session, request, redirect, url_for
from utils import Database
    # Define Folders
    #
    path = os.path.abspath(os.path.dirname(__file__))
    path = path.replace('controllers', '')
    webapp_folders = {
        'static': path + '/assets/static',
        'templates': path + '/assets/templates',
        'products': path  + '/assets/products'
    }
    del path

    #
    # Flask init
    #
    webapp = Flask(__name__,
                   static_url_path='/static',
                   static_folder=webapp_folders['static'],
                   template_folder=webapp_folders['templates'])
    webapp.config['SECRET_KEY'] = webapp_secret_key

#-----------------------------------------------------------------------------
# Views
#-----------------------------------------------------------------------------
@webapp.route('/', methods=['GET', 'POST'])
def login():
    """
    Landing page.
    Launches and handles login form if needed.
    """
    # Database access + loads basic info

# Path: utils/database.py
# class Database:
#     """
#     This class handles :
#     - Provides a link and a cursor to the database as class attributes
#     Usage :
#     - Database.on()
#     - Database.CURSOR.execute(query, params)
#     - ... etc
#     - Database.off()
#     Available class attributes :
#     - LINK
#     - CURSOR
#     - FILE (path to the database file)
#     """
#     DATABASE_PRODUCTION = 'database.db'
#     """ Normal database filename """
#     DATABASE_TEST = 'database_test.db'
#     """ Test database filename : this database is temporary """
#     TEST_MODE = False
#     """ Does it need to use the test database ? """
# 
#     @classmethod
#     def on(cls):
#         """
#         Connect to the database
#         :rtype: bool
#         """
#         # Don't reconnect if already connected
#         if cls.is_ready():
#             return False
# 
#         # Filename
#         cls.FILE = cls.DATABASE_PRODUCTION
# 
#         if cls.TEST_MODE:
#             cls.FILE = cls.DATABASE_TEST
# 
#         # Connect
#         cls.LINK = sqlite3.connect(cls.FILE)
#         cls.LINK.row_factory = sqlite3.Row
#         cls.CURSOR = cls.LINK.cursor()
# 
#         return True
# 
#     @classmethod
#     def off(cls):
#         """
#         Ends connection with the database.
#         :rtype: bool
#         """
#         if cls.LINK:
#             cls.LINK.close()
#         cls.LINK = None
#         cls.CURSOR = None
#         return True
# 
#     @classmethod
#     def is_ready(cls):
#         """
#         Is the connection open ?
#         :rtype: bool
#         """
#         return hasattr(cls, 'LINK') and cls.LINK != None


# Path: controllers/web.py
import os
import random
import string
import hashlib
import json
import utils
import models
from flask import Flask, render_template, session, request, redirect, url_for
from utils import Database

    # Define Folders
    #
    path = os.path.abspath(os.path.dirname(__file__))
    path = path.replace('controllers', '')
    webapp_folders = {
        'static': path + '/assets/static',
        'templates': path + '/assets/templates',
        'products': path  + '/assets/products'
    }
    del path

    #
    # Flask init
    #
    webapp = Flask(__name__,
                   static_url_path='/static',
                   static_folder=webapp_folders['static'],
                   template_folder=webapp_folders['templates'])
    webapp.config['SECRET_KEY'] = webapp_secret_key

#-----------------------------------------------------------------------------
# Views
#-----------------------------------------------------------------------------
@webapp.route('/', methods=['GET', 'POST'])
def login():
    """
    Landing page.
    Launches and handles login form if needed.
    """
    # Database access + loads basic info

    Database.on()