cineclub-script/scripts/cineclubBlogSQ.py

270 lines
7.8 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Feb 1 22:49:56 2018
@author: alice
2018-02-09 01:48:22 +01:00
2019-11-21 19:10:15 +01:00
TODO :
2018-02-09 01:48:22 +01:00
* simplifier l'input autant que possible
* remplir
* débug
* documenter
* publication FB
"""
import sqlite3
2021-11-28 03:07:41 +01:00
from config import db_path
2018-02-02 21:04:51 +01:00
from datetime import date
2018-02-08 00:25:44 +01:00
import locale
locale.setlocale(locale.LC_ALL, 'fr_FR.utf8')
#def adapt_datetime(ts):
# return time.mktime(ts.timetuple())
#sqlite3.register_adapter(datetime.date, adapt_datetime)
class filmSQ():
2019-11-21 19:10:15 +01:00
def __init__(self, dic):
"""ceci est une aide"""
2018-02-08 00:25:44 +01:00
if type(dic) == type(""):
self.idN = dic
2021-11-28 03:07:41 +01:00
conn = sqlite3.connect(db_path)
conn.row_factory = sqlite3.Row
c = conn.cursor()
try:
c.execute("""SELECT i FROM films WHERE idN = ?""",(dic,))
r = c.fetchone()
self.i = r[0]
conn.commit()
2019-11-21 19:10:15 +01:00
except Exception as e:
conn.rollback()
raise e
finally:
conn.close()
elif type(dic) == type(1):
self.i = dic
2021-11-28 03:07:41 +01:00
conn = sqlite3.connect(db_path)
conn.row_factory = sqlite3.Row
c = conn.cursor()
try:
c.execute("""SELECT idN FROM films WHERE i = ?""",(dic,))
r = c.fetchone()
self.idN = r[0]
conn.commit()
except Exception as e:
conn.rollback()
raise e
finally:
conn.close()
2018-02-08 00:25:44 +01:00
else:
2021-11-28 03:07:41 +01:00
conn = sqlite3.connect(db_path)
2018-02-08 00:25:44 +01:00
conn.row_factory = sqlite3.Row
c = conn.cursor()
2019-11-21 19:10:15 +01:00
2018-02-08 00:25:44 +01:00
try:
2019-11-21 19:10:15 +01:00
c.execute('INSERT INTO films VALUES (:i, :idN, :date, :nom, :realisateur, :duree, :synopsis, :pays, :annee, :youtube, :couleur, :image, :formatCopie, :langST)', dic)
2018-02-08 00:25:44 +01:00
acteursToSq = [{'idFilm' : dic['i'], 'acteur' : a} for a in dic['acteurs']]
c.executemany('INSERT INTO acteurs VALUES (NULL, :idFilm, :acteur)', acteursToSq)
2019-11-21 19:10:15 +01:00
2018-02-08 00:25:44 +01:00
conn.commit()
except Exception as e:
conn.rollback()
raise e
finally:
conn.close()
#id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT [PRIMARY KEY]
2019-11-21 19:10:15 +01:00
2018-02-08 00:25:44 +01:00
self.__setattr__('idN', dic['idN'])
2019-11-21 19:10:15 +01:00
def __setattr__(self, nom_attr, val_attr):
2018-02-02 21:04:51 +01:00
if nom_attr == 'idN':
2018-02-08 00:25:44 +01:00
object.__setattr__(self, 'idN', val_attr)
else:
2018-02-08 00:25:44 +01:00
try:
2021-11-28 03:07:41 +01:00
conn = sqlite3.connect(db_path)
2018-02-08 00:25:44 +01:00
conn.row_factory = sqlite3.Row
c = conn.cursor()
2019-11-21 19:10:15 +01:00
2018-02-08 00:25:44 +01:00
if nom_attr == 'acteurs':
c.execute("""DELETE FROM acteurs WHERE iFilm =?""", (self.i,))
acteursToSq = [{'iFilm' : self.i, 'acteur' : a} for a in val_attr]
c.executemany('INSERT INTO acteurs VALUES (NULL, :iFilm, :acteur)', acteursToSq)
2018-02-08 00:25:44 +01:00
else:
c.execute('UPDATE films SET %s = ? WHERE idN = ?' % nom_attr, (val_attr, self.idN))
conn.commit()
except Exception as e:
conn.rollback()
raise e
finally:
conn.close()
2019-11-21 19:10:15 +01:00
def __getattr__(self, nom):
2021-11-28 03:07:41 +01:00
conn = sqlite3.connect(db_path, detect_types=sqlite3.PARSE_DECLTYPES)
2018-02-09 01:48:22 +01:00
conn.row_factory = sqlite3.Row
2018-02-08 00:25:44 +01:00
c = conn.cursor()
2018-02-07 13:05:47 +01:00
try:
2018-02-02 21:04:51 +01:00
if nom == 'acteurs':
2018-02-08 00:25:44 +01:00
c.execute("""SELECT name FROM acteurs WHERE iFilm=?""", (self.i,))
2018-02-02 21:04:51 +01:00
r = c.fetchall()
res = [a[0] for a in r]
else:
2018-02-08 00:25:44 +01:00
c.execute("""SELECT %s FROM films WHERE idN=:idN""" % nom, {"idN":self.idN})
2018-02-02 21:04:51 +01:00
r = c.fetchone()
res = r[0]
2018-02-08 00:25:44 +01:00
conn.commit()
2018-02-07 20:46:38 +01:00
2018-02-08 00:25:44 +01:00
except Exception as e:
conn.rollback()
raise e
2018-02-07 13:05:47 +01:00
finally:
2018-02-02 21:04:51 +01:00
conn.close()
2019-11-21 19:10:15 +01:00
return res
2019-11-21 19:10:15 +01:00
def __str__(self):
2018-02-07 13:05:47 +01:00
try:
2021-11-28 03:07:41 +01:00
conn = sqlite3.connect(db_path)
2018-02-08 00:25:44 +01:00
#conn.row_factory = sqlite3.Row
2018-02-07 13:05:47 +01:00
c = conn.cursor()
2018-02-08 00:25:44 +01:00
c.execute('SELECT idN, nom, date FROM films WHERE idN = ?', (self.idN))
2018-02-07 13:05:47 +01:00
r = c.fetchone()
s = r[0] + ' : ' + r[1] + ' le ' + r[2]
2018-02-08 00:25:44 +01:00
conn.commit()
except Exception as e:
conn.rollback()
raise e
2018-02-07 13:05:47 +01:00
finally:
conn.close()
2019-11-21 19:10:15 +01:00
return s
2018-02-09 01:48:22 +01:00
def maxId():
try:
2021-11-28 03:07:41 +01:00
conn = sqlite3.connect(db_path)
2019-11-21 19:10:15 +01:00
2018-02-09 01:48:22 +01:00
conn.row_factory = sqlite3.Row
c = conn.cursor()
c.execute('SELECT MAX(i) FROM films')
r = c.fetchone()
except Exception as e:
conn.rollback()
raise e
finally:
conn.close()
2019-11-21 19:10:15 +01:00
2018-02-09 01:48:22 +01:00
return r[0]
2019-11-21 19:10:15 +01:00
2018-02-09 01:48:22 +01:00
def printAll():
try:
2021-11-28 03:07:41 +01:00
conn = sqlite3.connect(db_path)
2019-11-21 19:10:15 +01:00
2018-02-09 01:48:22 +01:00
conn.row_factory = sqlite3.Row
c = conn.cursor()
c.execute('SELECT idN, nom, date, i FROM films ORDER BY i ASC')
2018-02-09 01:48:22 +01:00
r = c.fetchall()
for movie in r:
print(movie[3], ' - ', movie[0], ' : ', movie[1], ' le ', movie[2])
2018-02-09 01:48:22 +01:00
except Exception as e:
conn.rollback()
raise e
finally:
conn.close()
pass
2019-11-21 19:10:15 +01:00
def makeListActors():
l = []
2021-11-25 00:26:15 +01:00
new = input("Entrez le nom des acteurs en appuyant sur Entrée entre chaque. Pour terminer, appuyer sur Entrée avec une ligne vide.\n")
2019-11-21 19:10:15 +01:00
if (new == -1):
return -1
while(new != ""):
l += [new]
new = input()
pass
return l
def input_date():
ok = False
d = input("date de la séance (format JJ/MM/AAAA ) ")
while not ok:
try:
d = d.split("/")
res = date(int(d[2]), int(d[1]), int(d[0]))
ok = True
except IndexError:
print("Veuillez entrer la date au format JJ/MM/AAA")
return res
2019-11-21 19:10:15 +01:00
def input_duree():
ok = False
while not ok:
try:
res = eval(input("durée du film (en minutes) "))
2019-04-01 19:45:00 +02:00
ok = True
except (NameError, SyntaxError):
print("le format n'est pas correct. Ne rentrez que des chiffres svp")
pass
pass
return res
def input_color():
ok = False
while not ok:
d = input("le film est-il en couleur (oui/non) ? ")
if d in ["True", "true", "oui", "Oui", "y", "o", "Y", "y", "O", "yes", "Yes"]:
res = True
ok = True
elif d in ["False", "false", "non", "Non", "n", "N", "no", "No"]:
res = False
ok = True
return res
def newFilm():
"""Interface d'ajout d'un nouveau film"""
2019-11-21 19:10:15 +01:00
dic = {}
i = filmSQ.maxId()
ok = False
dic['i'] = i+1
dic['idN'] = input("identifiant du film ? ")
2019-04-01 14:33:33 +02:00
dic['date'] = filmSQ.input_date()
dic['nom'] = input("titre ? ")
dic['realisateur'] = input("realisateur ? ")
dic['acteurs'] = filmSQ.makeListActors()
dic['synopsis'] = input("Donnez ici un synopsis rapide du film ")
dic['pays'] = input("pays de diffusion du film ? ")
dic['annee'] = input("année de sortie du film (format AAAA) ? ")
2019-04-01 14:33:33 +02:00
dic['duree'] = filmSQ.input_duree();
dic['youtube'] = input("adresse youtube de la bande-annonce ? ")
dic['image'] = input("url d'une affiche du film ? ")
2019-04-01 14:33:33 +02:00
dic['couleur'] = filmSQ.input_color();
dic['formatCopie'] = input("format de la copie ? ")
dic['langST'] = input("langue et sous-titre : VF/VOSTFR ? ")
2019-11-21 19:10:15 +01:00
seance = filmSQ(dic)
return seance
def strListe(liste):
res = ""
for l in liste[:-1]:
res += l +', '
if liste != []:
res += liste[-1]
return res