diff --git a/Exercices/Amaury/Contacts/app.sql b/Exercices/Amaury/Contacts/app.sql new file mode 100644 index 0000000..c575420 --- /dev/null +++ b/Exercices/Amaury/Contacts/app.sql @@ -0,0 +1,31 @@ +-- Database: app + +-- DROP DATABASE IF EXISTS app; + +CREATE DATABASE app + WITH + OWNER = "user" + ENCODING = 'UTF8' + LC_COLLATE = 'en_US.utf8' + LC_CTYPE = 'en_US.utf8' + TABLESPACE = pg_default + CONNECTION LIMIT = -1; + +-- Table: public.contacts + +-- DROP TABLE IF EXISTS public.contacts; + +CREATE TABLE IF NOT EXISTS public.contacts +( + id integer NOT NULL DEFAULT nextval('contacts_id_seq'::regclass), + firstname character varying(30) COLLATE pg_catalog."default", + lastname character varying(30) COLLATE pg_catalog."default", + email character varying(45) COLLATE pg_catalog."default", + password character varying(100) COLLATE pg_catalog."default", + CONSTRAINT contacts_pkey PRIMARY KEY (id) +) + +TABLESPACE pg_default; + +ALTER TABLE IF EXISTS public.contacts + OWNER to "user"; \ No newline at end of file diff --git a/Exercices/Amaury/Contacts/app/__init__.py b/Exercices/Amaury/Contacts/app/__init__.py new file mode 100644 index 0000000..3f5cc12 --- /dev/null +++ b/Exercices/Amaury/Contacts/app/__init__.py @@ -0,0 +1,13 @@ +from flask import Flask +from flask_debugtoolbar import DebugToolbarExtension +import psycopg2 + +app = Flask("app") +app.config['SECRET_KEY'] = "Rg@O None: + self.id = id + self.firstname = firstname + self.lastname = lastname + self.email = email + self.password = password \ No newline at end of file diff --git a/Exercices/Amaury/Contacts/app/services/__pycache__/contactService.cpython-38.pyc b/Exercices/Amaury/Contacts/app/services/__pycache__/contactService.cpython-38.pyc new file mode 100644 index 0000000..ebfdc94 Binary files /dev/null and b/Exercices/Amaury/Contacts/app/services/__pycache__/contactService.cpython-38.pyc differ diff --git a/Exercices/Amaury/Contacts/app/services/contactService.py b/Exercices/Amaury/Contacts/app/services/contactService.py new file mode 100644 index 0000000..cf36bb0 --- /dev/null +++ b/Exercices/Amaury/Contacts/app/services/contactService.py @@ -0,0 +1,25 @@ +from app import conn +from app.forms.contactForm import ContactForm +from app.models.contact import Contact + +class ContactService: + def __init__(self) -> None: + pass + + def findAll(self): + cur = conn.cursor() + cur.execute("SELECT * FROM contacts;") + datas = [] + for data in cur.fetchall(): + datas.append(Contact(data[0], data[1], data[2], data[3], data[4])) + + return datas + + def insert(self, data: ContactForm): + cur = conn.cursor() + + cur.execute("INSERT INTO contacts(firstname, lastname, email, password) VALUES(%s, %s, %s, %s)", + (str(data.firstname.data), str(data.lastname.data), str(data.email.data), str(data.password.data))) + conn.commit() + + return str(data.firstname.data) + ' ' + str(data.lastname.data) \ No newline at end of file diff --git a/Exercices/Amaury/Contacts/app/templates/page.html b/Exercices/Amaury/Contacts/app/templates/page.html new file mode 100644 index 0000000..e6a3f86 --- /dev/null +++ b/Exercices/Amaury/Contacts/app/templates/page.html @@ -0,0 +1,34 @@ + + + + Contact Application + + + +

Subscribe:

+ +
+ + + + + + +
+

+ {% if data %} + New contact added: {{ data }} + {% endif %} + {% if datas %} + {% for data in datas %} + Identity: {{ data.firstname }} {{ data.lastname }} | Contact: {{ data.email }}
+ {% endfor %} + {% endif %} + {% if errors %} + {% for key, val in errors.items() %} + {{ key }} {{ val }} + {% endfor %} + {% endif %} +

+ + \ No newline at end of file diff --git a/Exercices/Amaury/Contacts/docker-compose.yml b/Exercices/Amaury/Contacts/docker-compose.yml new file mode 100644 index 0000000..84d2368 --- /dev/null +++ b/Exercices/Amaury/Contacts/docker-compose.yml @@ -0,0 +1,19 @@ +version: '3.8' + +services: + db: + image: postgres:13.7 + environment: + POSTGRES_PASSWORD: 1234 + POSTGRES_USER: user + POSTGRES_DB: app + + ports: + - '5435:5432' + + volumes: + - app-volume:/var/lib/postgresql/data + - ./init_db:/init_db + +volumes: + app-volume: \ No newline at end of file diff --git a/Exercices/Amaury/Contacts/server.py b/Exercices/Amaury/Contacts/server.py new file mode 100644 index 0000000..646b6a5 --- /dev/null +++ b/Exercices/Amaury/Contacts/server.py @@ -0,0 +1,5 @@ +import os +from app import app + +port = int(os.environ.get('PORT', 8080)) +app.run('0.0.0.0', port=port) \ No newline at end of file diff --git a/Exercices/Amaury/MVC/app/__init__.py b/Exercices/Amaury/MVC/app/__init__.py index 69cad4d..062ce1a 100644 --- a/Exercices/Amaury/MVC/app/__init__.py +++ b/Exercices/Amaury/MVC/app/__init__.py @@ -1,5 +1,6 @@ from flask import Flask, render_template from flask_debugtoolbar import DebugToolbarExtension +import psycopg2 app = Flask("app") app.config['SECRET_KEY'] = 'random' @@ -7,4 +8,6 @@ toolbar = DebugToolbarExtension(app) +conn = psycopg2.connect(dbname='mvc', user='user', password='1234', host='127.0.0.1', port='5435') + from app.controllers import * \ No newline at end of file diff --git a/Exercices/Amaury/MVC/app/controllers/DbController.py b/Exercices/Amaury/MVC/app/controllers/DbController.py new file mode 100644 index 0000000..7c827ff --- /dev/null +++ b/Exercices/Amaury/MVC/app/controllers/DbController.py @@ -0,0 +1,33 @@ +from requests import request +from app import app +from flask import render_template, request +from app.forms.dbForm import DbForm +from app.services.DbService import DbService + +from app.forms.form import Form + +dbService = DbService() + +class DbController: + @app.route('/db', methods=['GET']) + def getAllDatas(): + datas = dbService.findAll() + return render_template('db/db.html', datas=datas) + + @app.route('/db/', methods=['GET']) + def getOneData(id): + data = dbService.findOne(id) + return render_template('db/info.html', data=data) + + @app.route('/db/add', methods=['GET', 'POST']) + def postTest(): + form = DbForm(request.form) + + if request.method == 'POST': + if form.validate(): + data = DbService.insert(form) + return render_template('db/info.html', data=data) + + return render_template('db/add.html', errors=form.errors) + + return render_template('db/add.html', example="POST") \ No newline at end of file diff --git a/Exercices/Amaury/MVC/app/forms/dbForm.py b/Exercices/Amaury/MVC/app/forms/dbForm.py new file mode 100644 index 0000000..d8e56e6 --- /dev/null +++ b/Exercices/Amaury/MVC/app/forms/dbForm.py @@ -0,0 +1,10 @@ +from flask_wtf import FlaskForm +from wtforms import StringField, IntegerField +from wtforms.validators import DataRequired + +class DbForm(FlaskForm): + class Meta: + csrf = False + + testid = IntegerField("id", validators=[DataRequired()]) + testtext = StringField("text", validators=[DataRequired()]) \ No newline at end of file diff --git a/Exercices/Amaury/MVC/app/models/db.py b/Exercices/Amaury/MVC/app/models/db.py new file mode 100644 index 0000000..a4161d8 --- /dev/null +++ b/Exercices/Amaury/MVC/app/models/db.py @@ -0,0 +1,4 @@ +class Db: + def __init__(self, id: int, text: str) -> None: + self.id = id + self.text = text \ No newline at end of file diff --git a/Exercices/Amaury/MVC/app/services/DbServices.py b/Exercices/Amaury/MVC/app/services/DbServices.py new file mode 100644 index 0000000..164079c --- /dev/null +++ b/Exercices/Amaury/MVC/app/services/DbServices.py @@ -0,0 +1,30 @@ +from app import conn +from app.forms.dbForm import DbForm +from app.models.db import Db + +class DbService: + def __init__(self) -> None: + pass + + def findAll(self): + cur = conn.cursor() + cur.execute("SELECT * FROM dbDatas") + datas = [] + for data in cur.fetchall(): + datas.append(Db(data[0], data[1])) + + return tests + + def findOne(self, testid): + cur = conn.cursor() + cur.execute(f"SELECT * FROM dbDatas WHERE id = {id}") + data = cur.fetchone() + + return Db(data[0], data[1]) + + def insert(self, data: DbForm): + cur = conn.cursor() + cur.execute("INSERT INTO dbDatas(id, contentText) VALUES(" + str(data.id.data) + ", '" + str(data.text.data) + "')") + conn.commit() + + return self.findOne(int(data.id.data)) \ No newline at end of file diff --git a/Exercices/Amaury/MVC/app/templates/db/add.html b/Exercices/Amaury/MVC/app/templates/db/add.html new file mode 100644 index 0000000..b871dd6 --- /dev/null +++ b/Exercices/Amaury/MVC/app/templates/db/add.html @@ -0,0 +1,22 @@ + + + + Amaury MVC + + + +

{{ example }}

+ +
+ + + + +
+ {% if errors %} + {% for key, val in errors.items() %} + {{ key }} {{ val }} + {% endfor %} + {% endif %} + + \ No newline at end of file diff --git a/Exercices/Amaury/MVC/app/templates/db/db.html b/Exercices/Amaury/MVC/app/templates/db/db.html new file mode 100644 index 0000000..33083be --- /dev/null +++ b/Exercices/Amaury/MVC/app/templates/db/db.html @@ -0,0 +1,14 @@ + + + + Amaury MVC + + + + {% for data in datas %} +
+ {{ data.text }} +
+ {% endfor %} + + \ No newline at end of file diff --git a/Exercices/Amaury/MVC/app/templates/db/info.html b/Exercices/Amaury/MVC/app/templates/db/info.html new file mode 100644 index 0000000..4f6b42f --- /dev/null +++ b/Exercices/Amaury/MVC/app/templates/db/info.html @@ -0,0 +1,10 @@ + + + + Amaury MVC + + + +

{{ data.id }} {{ data.text }}

+ + \ No newline at end of file diff --git a/Exercices/Amaury/MVC/docker-compose.yml b/Exercices/Amaury/MVC/docker-compose.yml new file mode 100644 index 0000000..45f2474 --- /dev/null +++ b/Exercices/Amaury/MVC/docker-compose.yml @@ -0,0 +1,19 @@ +version: '3.3' + +services: + db: + image: postgres:13.7 + environment: + POSTGRES_PASSWORD: 1234 + POSTGRES_USER: user + POSTGRES_DB: mvc + + ports: + - '5435:5432' + + volumes: + - app-volume:/var/lib/postgresql/data + - ./init_db:/init_db + +volumes: + app-volume: \ No newline at end of file diff --git a/Exercices/Amaury/MVC/sql.sql b/Exercices/Amaury/MVC/sql.sql new file mode 100644 index 0000000..0147e68 --- /dev/null +++ b/Exercices/Amaury/MVC/sql.sql @@ -0,0 +1,7 @@ +CREATE TABLE dbDatas( + id SERIAL PRIMARY KEY + contentText VARCHAR(81) +); + + + diff --git a/Exercices/Claire/03_MVC/app/__init__.py b/Exercices/Claire/03_MVC/app/__init__.py index 69cad4d..5bca0e9 100644 --- a/Exercices/Claire/03_MVC/app/__init__.py +++ b/Exercices/Claire/03_MVC/app/__init__.py @@ -1,5 +1,6 @@ from flask import Flask, render_template from flask_debugtoolbar import DebugToolbarExtension +import psycopg2 app = Flask("app") app.config['SECRET_KEY'] = 'random' @@ -7,4 +8,6 @@ toolbar = DebugToolbarExtension(app) -from app.controllers import * \ No newline at end of file +conn = psycopg2.connect(dbname='app', user='app', password='1234', host='127.0.0.1', port='5435') + +from app.controllers import * diff --git a/Exercices/Claire/03_MVC/app/controllers/ContactController.py b/Exercices/Claire/03_MVC/app/controllers/ContactController.py index ad4782a..f6f295b 100644 --- a/Exercices/Claire/03_MVC/app/controllers/ContactController.py +++ b/Exercices/Claire/03_MVC/app/controllers/ContactController.py @@ -1,25 +1,30 @@ from app import app -from flask import render_template, request +from flask import render_template, request, redirect, url_for from app.forms.ContactForm import ContactForm +from app.services.ContactService import ContactService + +contactService = ContactService() class ContactController: - @app.route('/contact', methods=['GET']) - def contactForm(): - # if request.method == 'POST': - return render_template('contact/contactForm.html', contact="GET") + @app.route('/contacts', methods=['GET']) + def getAllContacts(): + contacts = contactService.findAll() + return render_template('contact/contactList.html', contacts=contacts) + + # @app.route('/test/', methods=['GET']) + # def getOneTests(testid): + # test = testService.findOne(testid) + # return render_template('test/info.html', test=test) - @app.route('/contact', methods=['POST']) - def contactPostForm(): + @app.route('/contacts/add', methods=['GET', 'POST']) + def insertContact(): form = ContactForm(request.form) - if form.validate(): - contact = {} - contact["first_name"] = form.first_name.data - contact["last_name"] = form.last_name.data - contact["email"] = form.email.data - contact["descr"] = form.descr.data - return render_template('contact/contactDone.html', contact=contact) + if request.method == 'POST': + if form.validate(): + contactService.insert(form) + return redirect(url_for('getAllContacts')) - return render_template('contact/contactForm.html', error=True) + return render_template('contact/contactForm.html', errors=form.errors) diff --git a/Exercices/Claire/03_MVC/app/forms/ContactForm.py b/Exercices/Claire/03_MVC/app/forms/ContactForm.py index da5841e..8119fc2 100644 --- a/Exercices/Claire/03_MVC/app/forms/ContactForm.py +++ b/Exercices/Claire/03_MVC/app/forms/ContactForm.py @@ -1,6 +1,6 @@ from flask_wtf import FlaskForm from wtforms import StringField, SubmitField -from wtforms.validators import DataRequired, Email +from wtforms.validators import DataRequired class ContactForm(FlaskForm): @@ -10,5 +10,4 @@ class Meta: first_name = StringField("first_name", validators=[DataRequired()]) last_name = StringField("last_name", validators=[DataRequired()]) email = StringField("email", validators=[DataRequired()]) - descr = StringField("descr") submit = SubmitField('Submit') diff --git a/Exercices/Claire/03_MVC/app/models/Contact.py b/Exercices/Claire/03_MVC/app/models/Contact.py new file mode 100644 index 0000000..3ae1bfa --- /dev/null +++ b/Exercices/Claire/03_MVC/app/models/Contact.py @@ -0,0 +1,33 @@ +class Contact: + + def __init__(self, first_name, last_name, email): + self.__first_name = first_name + self.__last_name = last_name + self.__email = email + + @property + def first_name(self): + return self.__first_name + + @first_name.setter + def first_name(self, value): + self.__first_name = value + + @property + def last_name(self): + return self.__last_name + + @last_name.setter + def last_name(self, value): + self.__last_name = value + + @property + def email(self): + return self.__email + + @email.setter + def email(self, value): + self.__email = value + + def __str__(self): + return f"{self.first_name} {self.last_name} [{self.email}]" diff --git a/Exercices/Claire/03_MVC/app/services/ContactService.py b/Exercices/Claire/03_MVC/app/services/ContactService.py new file mode 100644 index 0000000..53c687e --- /dev/null +++ b/Exercices/Claire/03_MVC/app/services/ContactService.py @@ -0,0 +1,21 @@ +from app import conn +from app.models.Contact import Contact +from app.forms.ContactForm import ContactForm + + +class ContactService: + def __init__(self) -> None: + pass + + def findAll(self): + cur = conn.cursor() + cur.execute("SELECT firstName, lastName, email FROM contacts;") + contacts = [] + for contact in cur.fetchall(): + contacts.append(Contact(contact[0], contact[1], contact[2])) + return contacts + + def insert(self, data: ContactForm): + cur = conn.cursor() + cur.execute(f"INSERT INTO contacts(firstName, lastName, email) VALUES('{data.first_name.data}', '{data.last_name.data}', '{data.email.data}')") + conn.commit() diff --git a/Exercices/Claire/03_MVC/app/templates/contact/contactForm.html b/Exercices/Claire/03_MVC/app/templates/contact/contactForm.html index 03c09e2..b84ea03 100644 --- a/Exercices/Claire/03_MVC/app/templates/contact/contactForm.html +++ b/Exercices/Claire/03_MVC/app/templates/contact/contactForm.html @@ -6,14 +6,13 @@

Contact us !

- {% if error %} -

Invalid data !

+ {% if errors %} +

{{ errors }}

{% endif %}
-
diff --git a/Exercices/Claire/03_MVC/app/templates/contact/contactList.html b/Exercices/Claire/03_MVC/app/templates/contact/contactList.html new file mode 100644 index 0000000..986979c --- /dev/null +++ b/Exercices/Claire/03_MVC/app/templates/contact/contactList.html @@ -0,0 +1,17 @@ + + + + All contacts + + + +

All contacts:

+ +
    + {% for contact in contacts %} +
  • {{ contact }}
  • + {% endfor %} +
+ + + \ No newline at end of file diff --git a/Exercices/Claire/03_MVC/sql.txt b/Exercices/Claire/03_MVC/sql.txt new file mode 100644 index 0000000..a7c2bc5 --- /dev/null +++ b/Exercices/Claire/03_MVC/sql.txt @@ -0,0 +1,11 @@ +CREATE DATABASE technobelapp; +\c technobelapp +ALTER USER postgres PASSWORD 'postgres'; +CREATE TABLE contacts ( contactID SERIAL NOT NULL, + firstName varchar(55) NOT NULL, + lastName varchar(55) NOT NULL, + email varchar(255) NOT NULL, + PRIMARY KEY(contactID) +); +SELECT * FROM contacts; +INSERT INTO contacts(firstName, lastName, email) VALUES ('Claire', 'Bretton', 'clbr@odoo.com'); \ No newline at end of file