# -*- coding: utf-8 -*-

from flask import Blueprint, Flask, request, flash, render_template, url_for, session, redirect, abort, make_response, send_file, escape, flash, abort, send_file, send_from_directory
from werkzeug.utils import secure_filename
from PIL import Image
import time
import sqlite3
import os


mygallery = Blueprint('mygallery', __name__, template_folder='templates')

app = Flask( 'pywallter' )
app.config.from_pyfile('config.py')


#### Variables ####################################################################################

DOSSIER_PERSO= app.config['DOSSIER_APP']

extensionimg = app.config['EXT_IMG']

DATABASE = app.config['DATABASE']
##################################################################################################

@mygallery.route( '/gallery/')
def gallery():
    if 'username' in session :
        UTILISATEUR='%s' % escape(session['username'])
        THUMBNAILS=DOSSIER_PERSO + UTILISATEUR + '/images/thumbnails/'
        fichiers = [fich for fich in os.listdir(THUMBNAILS)]
        return render_template('gallery.html',
                               section='Gallery',
                               THUMBNAILS=THUMBNAILS,
                               fichiers=fichiers)
    else :
       return redirect(url_for('loginlogout.login'), code=401)

@mygallery.route('/myfiles/images/<filename>')
def myimg(filename):
    if 'username' in session :
        UTILISATEUR='%s' % escape(session['username'])
        return send_from_directory(
            os.path.join(DOSSIER_PERSO, UTILISATEUR, 'images'), filename )
    else :
        return redirect(url_for('loginlogout.login'), code=401)

@mygallery.route('/myfiles/images/thumbnails/<filename>')
def mythumbnails(filename):
    if 'username' in session :
        UTILISATEUR='%s' % escape(session['username'])
        return send_from_directory(
            os.path.join(DOSSIER_PERSO, UTILISATEUR, 'images/thumbnails'), filename )
    else :
        return redirect(url_for('loginlogout.login'), code=401)