cineclub-script/scripts/cineclubBlogSQ.py

273 lines
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
2022-03-03 18:03:17 +01:00
2018-02-08 00:25:44 +01:00
locale.setlocale(locale.LC_ALL, 'fr_FR.utf8')
2022-03-03 18:03:17 +01:00
# def adapt_datetime(ts):
# return time.mktime(ts.timetuple())
2022-03-03 18:03:17 +01:00
# 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:
2022-03-03 18:03:17 +01:00
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:
2022-03-03 18:03:17 +01:00
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:
2022-03-03 18:03:17 +01:00
c.execute(
2023-09-02 20:25:53 +02:00
'INSERT INTO films VALUES (:i, :idN, :date, :nom, :realisateur, :duree, :synopsis, :pays, :annee, :youtube, :couleur, :image, :formatCopie, :langST, :idImdb)',
2022-03-03 18:03:17 +01:00
dic)
2019-11-21 19:10:15 +01:00
2022-03-03 18:03:17 +01:00
acteursToSq = [{'idFilm': dic['i'], 'acteur': a} for a in dic['acteurs']]
2018-02-08 00:25:44 +01:00
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()
2022-03-03 18:03:17 +01:00
# 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
2022-03-03 18:03:17 +01:00
def __setattr__(self, attr_name, attr_value):
if attr_name == 'idN':
object.__setattr__(self, 'idN', attr_value)
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
2022-03-03 18:03:17 +01:00
if attr_name == 'acteurs':
c.execute("""DELETE FROM acteurs WHERE iFilm =?""", (self.i,))
2022-03-03 18:03:17 +01:00
acteursToSq = [{'iFilm': self.i, 'acteur': a} for a in attr_value]
c.executemany('INSERT INTO acteurs VALUES (NULL, :iFilm, :acteur)', acteursToSq)
2018-02-08 00:25:44 +01:00
else:
2022-03-03 18:03:17 +01:00
c.execute('UPDATE films SET %s = ? WHERE idN = ?' % attr_name, (attr_value, self.idN))
2018-02-08 00:25:44 +01:00
conn.commit()
except Exception as e:
conn.rollback()
raise e
finally:
conn.close()
2019-11-21 19:10:15 +01:00
2022-03-03 18:03:17 +01:00
def __getattr__(self, attr_name):
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:
2022-03-03 18:03:17 +01:00
if attr_name == '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:
2022-03-03 18:03:17 +01:00
c.execute("""SELECT %s FROM films WHERE idN=:idN""" % attr_name, {"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)
2022-03-03 18:03:17 +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
2022-03-03 18:03:17 +01:00
def max_id():
2018-02-09 01:48:22 +01:00
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
2022-03-03 18:03:17 +01:00
def list_all():
2018-02-09 01:48:22 +01:00
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
2022-03-03 18:03:17 +01:00
def make_actors_list():
l = []
2022-03-03 18:03:17 +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
2022-03-03 18:03:17 +01:00
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
2022-03-03 18:03:17 +01:00
def input_duration():
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
2022-03-03 18:03:17 +01:00
def new_film():
"""Interface d'ajout d'un nouveau film"""
2019-11-21 19:10:15 +01:00
dic = {}
2022-03-03 18:03:17 +01:00
i = filmSQ.max_id()
ok = False
2022-03-03 18:03:17 +01:00
dic['i'] = i + 1
dic['idN'] = input("identifiant du film ? ")
2023-09-02 17:29:06 +02:00
dic['idImdb'] = input("identifiant IMDB du film ? ")
2019-04-01 14:33:33 +02:00
dic['date'] = filmSQ.input_date()
dic['nom'] = input("titre ? ")
dic['realisateur'] = input("realisateur ? ")
2022-03-03 18:03:17 +01:00
dic['acteurs'] = filmSQ.make_actors_list()
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) ? ")
2022-03-03 18:03:17 +01:00
dic['duree'] = filmSQ.input_duration();
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();
2023-09-02 20:25:53 +02:00
dic['formatCopie'] = input("VFformat 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]:
2022-03-03 18:03:17 +01:00
res += l + ', '
if liste != []:
res += liste[-1]
return res