Showing posts with label CGI. Show all posts
Showing posts with label CGI. Show all posts

Sunday, May 15, 2016

Python CGI Database (4 of 4)

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sqlite3
import cgi

print "Content-Type: application/xhtml+xml; charset=utf-8\n"

print '<!DOCTYPE html>'
print '<html xmlns="http://www.w3.org/1999/xhtml" lang="fr">'
print '<head>'
print '    <title>eCarnet - Employées d un service</title>'
print '</head>'
print '<body>'

form = cgi.FieldStorage()
id_service = str(form["matricule"].value)
nom = str(form["nom"].value)
prenom = str(form["prenom"].value)
matricule = str(form["matricule"].value)
tel_fixe = str(form["tel_fixe"].value)

db_connection = sqlite3.connect('database.test-01.db')
db_connection.row_factory = sqlite3.Row
cursor = db_connection.cursor()
cursor.execute("INSERT INTO employe(matricule, prenom, nom, tel_fixe, id_service) VALUES (?, ?, ?, ?, ?)", (matricule, prenom, nom, tel_fixe, id_service))
db_connection.commit()
db_connection.close()

print '    <h1>Ajout de l employé - ' + prenom + ' ' + nom + ' -</h1>'
print '    <p>' + prenom + ' ' + nom + ' a bien été ajouté dans la base de données</p>'
print '    <p><a href="eCarnet_home.py">Retour au eCarnet.</a></p>'
print ' </body>'
print ' </html>'

Python CGI Database (3 of 4)

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

import sqlite3
import cgi

print "Content-Type: application/xhtml+xml; charset=utf-8\n"

print '<!DOCTYPE html>'
print '<html xmlns="http://www.w3.org/1999/xhtml" lang="fr">'
print '<head>'
print '    <title>eCarnet - Employées d un service</title>'
print '</head>'
print '<body>'

form = cgi.FieldStorage()
service_id = str(form["service"].value)
db_connection = sqlite3.connect('database.test-01.db')
db_connection.row_factory = sqlite3.Row
cursor = db_connection.cursor()
cursor.execute("SELECT emplacement FROM service WHERE id=" + service_id)
row = cursor.fetchone()
service_nom = str(row['emplacement'])

print '    <h1>Employés du service - ' + service_nom + ' -</h1>'

cursor.execute("SELECT prenom, nom, tel_fixe FROM employe WHERE id_service=" + service_id)
rows = cursor.fetchall()

print '    <ol>'
for row in rows:
    print '        <li>' + row['prenom'] + ', ' +row['nom'] + ', ' + row['tel_fixe'] + '</li>'
print '    </ol>'
print '</body>'
print '</html>'

db_connection.close()

Python CGI Database (2 of 4)

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sqlite3

print "Content-Type: application/xhtml+xml; charset=utf-8\n"

print '<!DOCTYPE html>'
print '<html xmlns="http://www.w3.org/1999/xhtml" lang="fr">'
print '<head>'
print '    <title>eCarnet - Home</title>'
print '</head>'
print '<body>'
print '    <h1>Bienvenue sur eCarnet de ULB</h1>'
print '    <h2>Employés</h2>'

# Connection à la base de données
#db_connection = sqlite3.connect('database.sqlite3')
db_connection = sqlite3.connect('database.test-01.db')
db_connection.row_factory = sqlite3.Row
cursor = db_connection.cursor()

# Sélection des enregistrements
cursor.execute("SELECT prenom, nom, tel_fixe FROM employe")

# Création de la liste des employées
rows = cursor.fetchall()
print '    <ol>'
for row in rows:
    print'        <li>' +row['prenom'] + ' ' +row['nom'] + ', ' +row['tel_fixe'] + '  </li>'
print '    </ol>'
   
# Formulaire de recherche des employés d'un service   
print '    <h2>Employés par service</h2>'
print '    <form action="eCarnet_service.py" method="get">'
print '    <p><select name="service">'
cursor.execute ("SELECT id, nom, emplacement From service")
rows = cursor.fetchall()
for row in rows:
    print '            <option value="' + str(row['id']) + '">' + row['emplacement'] + '</option>'
print '    </select>'
print '        <input type="submit" value="Lister" /></p>'
print '    </form>'

# Formulaire d'ajout d'un nouvel employé   
print '    <h2>Ajouter un nouvel employé</h2>'
print '    <form action="eCarnet_add_employee.py" method="get">'
print '    <p>Prénom : <input type="text" name="prenom" /></p>'
print '    <p>Nom : <input type="text" name="nom" /></p>'
print '    <p>Matricule : <input type="text" name="matricule" /></p>'
print '    <p>Tèl. fixe : <input type="text" name="tel_fixe" /></p>'
print '    <p>Service : <select name="service">'
for row in rows:
    print '            <option value="' + str(row['id']) + '">' + row['emplacement'] + '</option>'
print '        </select>'
print '        <input type="submit" value="Ajouter" /></p>'
print '    </form>'  
print '</body>'
print '</html>'

db_connection.close()

Python CGI Database (1 of 4)

<?xml version=“1.0” encoding=“utf-8”?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="fr">
    <head>
        <meta charset="UTF-8">
        <!--<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />-->
        <title>Université libre de Bruxelles</title>
    </head>
    <body>
    <img src="logo_ulb.png" alt="Logo de l'ULB" width="656" height="80" />
    <h1>Bienvenue sur le site de l'ULB</h1>
    <section>
        <h2>Que voulez-vous consulter ?</h2>
        <ul>
            <li><a href="status.html">Les statuts de l'université</a></li>
            <li><a href="/cgi-bin/eCarnet_home.py">Le carnet d'adresses des employés</a></li>
            <li><a href="ca.html">La composition du Conseil d'Aministration</a></li>
        </ul>
    </section>
    </body>
</html>

Python CGI Web Server

#! Path to Python.exe "C:\Web development\Python27\python.exe"

from BaseHTTPServer import HTTPServer
from CGIHTTPServer import CGIHTTPRequestHandler

httpd = HTTPServer(('', 8080), CGIHTTPRequestHandler)
print("Demarrage du serveur web...")
httpd.serve_forever()