2020-11-29 02:05:19 +01:00
# -*- coding: utf-8 -*-
2023-11-02 08:03:21 +01:00
from flask import Blueprint , render_template , session , redirect , url_for , request , flash , abort , Flask
2020-11-29 02:05:19 +01:00
import time
2023-11-02 08:03:21 +01:00
from markupsafe import escape
2020-11-29 02:05:19 +01:00
import sqlite3
2022-07-05 05:41:20 +02:00
from markdown import markdown
2022-07-10 15:09:03 +02:00
postit = Blueprint ( ' post-it ' , __name__ , template_folder = ' templates ' )
2020-11-29 02:05:19 +01:00
2022-08-06 18:22:24 +02:00
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 ' ]
2022-08-06 23:49:16 +02:00
BASE_URL = app . config [ ' BASE_URL ' ]
2022-08-06 18:22:24 +02:00
##################################################################################################
2020-11-29 02:05:19 +01:00
2022-07-10 15:09:03 +02:00
@postit.route ( ' /post-it/ ' , methods = [ ' GET ' , ' POST ' ] )
2020-11-29 02:05:19 +01:00
def racine_blog ( ) :
if ' username ' in session :
UTILISATEUR = ' %s ' % escape ( session [ ' username ' ] )
if request . method == ' POST ' :
title = request . form [ ' title ' ]
2022-08-07 08:32:21 +02:00
content = request . form [ ' content ' ]
2020-11-29 02:05:19 +01:00
#category = request.form['category']
status = request . form [ ' status ' ]
2023-07-06 05:07:34 +02:00
post_date = time . strftime ( " % A %d % B % Y % H: % M: % S " )
2022-08-06 18:22:24 +02:00
conn = sqlite3 . connect ( DATABASE ) # Connexion la base de donne
2020-11-29 02:05:19 +01:00
cursor = conn . cursor ( ) # Création de l'objet "curseur"
2023-07-06 05:07:34 +02:00
cursor . execute ( """ INSERT INTO posts(title, content, time, author, status) VALUES(?, ?, ?, ?, ?) """ , ( title , content , post_date , UTILISATEUR , status ) ) # Insérer des valeurs
2020-11-29 02:05:19 +01:00
conn . commit ( )
2023-07-06 05:07:34 +02:00
cursor . execute ( """ SELECT avatar FROM users WHERE name=? """ , ( UTILISATEUR , ) )
user_info = cursor . fetchone ( )
cursor . execute ( """ SELECT title, content, time, author, status FROM posts where author=? """ , ( UTILISATEUR , ) )
list_posts = cursor . fetchall ( )
2020-11-29 02:05:19 +01:00
conn . close ( )
2023-07-06 05:07:34 +02:00
posts = list ( )
id = 0
for post in list_posts :
posts . append ( dict ( title = post [ 0 ] , id_postit = id , content = markdown ( post [ 1 ] ) , time = post [ 2 ] , author = post [ 3 ] , status = post [ 4 ] , avatar = user_info [ 0 ] ) )
id = id + 1
2020-11-29 02:05:19 +01:00
return render_template ( ' blog.html ' , posts = posts )
else :
2022-08-06 18:22:24 +02:00
conn = sqlite3 . connect ( DATABASE ) # Connexion à la base de donnée
2020-11-29 02:05:19 +01:00
cursor = conn . cursor ( ) # Création de l'objet "curseur"
2023-07-06 05:07:34 +02:00
cursor . execute ( """ SELECT avatar FROM users WHERE name=? """ , ( UTILISATEUR , ) )
user_info = cursor . fetchone ( )
cursor . execute ( """ SELECT title, content, time, author, status FROM posts WHERE author=? """ , ( UTILISATEUR , ) )
list_posts = cursor . fetchall ( )
2020-11-29 02:05:19 +01:00
conn . close ( )
2023-07-06 05:07:34 +02:00
posts = list ( )
id = 0
for post in list_posts :
posts . append ( dict ( title = post [ 0 ] , id_postit = id , content = markdown ( post [ 1 ] ) , time = post [ 2 ] , author = post [ 3 ] , status = post [ 4 ] , avatar = user_info [ 0 ] ) )
id = id + 1
2022-07-11 00:36:31 +02:00
return render_template ( ' blog.html ' , section = ' Post-it ' , posts = posts )
2020-11-29 02:05:19 +01:00
else :
2022-08-06 23:49:16 +02:00
return redirect ( BASE_URL , code = 401 )
2020-11-29 02:05:19 +01:00
2023-07-06 05:07:34 +02:00
@postit.route ( ' /delete/<title>/<time> ' )
def delete ( title , time ) :
2020-11-29 02:05:19 +01:00
if ' username ' in session :
2022-08-06 18:22:24 +02:00
conn = sqlite3 . connect ( DATABASE ) # Connexion à la base de donnée
2020-11-29 02:05:19 +01:00
cursor = conn . cursor ( ) # Création de l'objet "curseur"
2023-07-06 05:07:34 +02:00
cursor . execute ( """ DELETE FROM posts WHERE title=? AND time=? """ , ( title , time ) )
2020-11-29 02:05:19 +01:00
conn . commit ( )
conn . close ( )
2022-07-10 15:09:03 +02:00
return redirect ( url_for ( ' post-it.racine_blog ' ) )
2020-11-29 02:05:19 +01:00
else :
2022-08-06 23:49:16 +02:00
return redirect ( BASE_URL , code = 401 ) # sinon on redirige vers login
2020-11-29 02:05:19 +01:00
2023-07-06 05:07:34 +02:00
@postit.route ( ' /edit/<title>/<time> ' , methods = [ ' GET ' , ' POST ' ] )
def edit ( title , time ) :
2020-11-29 02:05:19 +01:00
if ' username ' in session :
if request . method == ' POST ' :
newtitle = request . form [ ' title ' ]
2022-08-07 08:32:21 +02:00
newcontent = request . form [ ' content ' ]
2020-11-29 02:05:19 +01:00
newstatus = request . form [ ' status ' ]
2022-08-06 18:22:24 +02:00
conn = sqlite3 . connect ( DATABASE )
2020-11-29 02:05:19 +01:00
cursor = conn . cursor ( )
2023-07-06 05:07:34 +02:00
cursor . execute ( """ UPDATE posts SET title=?, content=?, status=? WHERE title=? AND time=? """ ,
( newtitle , newcontent , newstatus , title , time ) )
2020-11-29 02:05:19 +01:00
conn . commit ( )
conn . close ( )
2022-07-10 15:09:03 +02:00
return redirect ( url_for ( ' post-it.racine_blog ' ) )
2020-11-29 02:05:19 +01:00
else :
2022-08-06 18:22:24 +02:00
conn = sqlite3 . connect ( DATABASE ) # Connexion à la base de donnée
2020-11-29 02:05:19 +01:00
cursor = conn . cursor ( ) # Création de l'objet "curseur"
2023-07-06 05:52:10 +02:00
cursor . execute ( """ SELECT title, content, status FROM posts WHERE title=? AND time =? """ , ( title , time ) )
2022-08-06 18:22:24 +02:00
oldpost = cursor . fetchone ( )
2020-11-29 02:05:19 +01:00
conn . close ( )
2022-07-11 00:36:31 +02:00
return render_template ( ' postedit.html ' ,
section = ' Post-it ' ,
oldpost = oldpost )
2020-11-29 02:05:19 +01:00
else :
2022-08-06 18:22:24 +02:00
2022-08-06 23:49:16 +02:00
return redirect ( BASE_URL , code = 401 )
2022-08-06 18:22:24 +02:00
2023-07-06 05:07:34 +02:00
2022-08-06 18:22:24 +02:00
@postit.route ( ' /postit/board ' , methods = [ ' GET ' ] )
def viewsheet ( ) :
if ' username ' in session :
conn = sqlite3 . connect ( DATABASE ) # Connexion à la base de donnée
cursor = conn . cursor ( ) # Création de l'objet "curseur"
2023-07-06 05:07:34 +02:00
cursor . execute ( """ SELECT title, content, time, author, status FROM posts WHERE status= ' public ' """ )
list_posts = cursor . fetchall ( )
posts = list ( )
id = 0
for post in list_posts :
author = post [ 3 ]
cursor . execute ( """ SELECT avatar FROM users WHERE name=? """ , ( author , ) )
tmp = cursor . fetchone ( )
if tmp != None :
author_avatar = tmp [ 0 ]
else :
author_avatar = tmp
posts . append ( dict ( title = post [ 0 ] , id_postit = id , content = markdown ( post [ 1 ] ) , time = post [ 2 ] , author = post [ 3 ] , status = post [ 4 ] , avatar = author_avatar ) )
id = id + 1
2022-08-06 18:22:24 +02:00
conn . close ( )
2023-07-06 05:07:34 +02:00
2022-08-06 18:22:24 +02:00
return render_template ( ' board.html ' , section = ' Post-it ' , posts = posts )
else :
2022-08-06 23:49:16 +02:00
return redirect ( BASE_URL , code = 401 )