From 91107d71bf8289264dd30f385b324420c441015b Mon Sep 17 00:00:00 2001 From: Miguel Angel Astor Romero Date: Mon, 2 Oct 2017 05:35:38 -0400 Subject: [PATCH] Added rudimentary support for differente subjects and helper scripts. --- code.py | 80 ++++++++++++++++++++++++++++++++++----- db_schema.sql | 32 +++++++++++++--- generate_report.py | 40 ++++++++++++++++++++ insert_students.py | 50 +++++++++++++++++++++++++ templates/index.html | 44 ++-------------------- templates/subject.html | 85 ++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 275 insertions(+), 56 deletions(-) create mode 100644 generate_report.py create mode 100644 insert_students.py create mode 100644 templates/subject.html diff --git a/code.py b/code.py index a356c91..6b0ebf7 100755 --- a/code.py +++ b/code.py @@ -4,23 +4,61 @@ import web urls = ( - '/', 'Index' + '/', 'Index', + '/[0-9]+', 'Subject' ) render = web.template.render('templates/') db = web.database(dbn = 'mysql', user = '', pw = '', db = '') +COOKIE_SUBJECT = "weblabs_cookie_subject" + def _get_schedule_list(): - schedules = db.query("SELECT sched_id, description FROM schedules WHERE sched_id < 8 ORDER BY sched_id ASC") + cookie = web.cookies().get(COOKIE_SUBJECT) + + if cookie is None or int(cookie) <= 1: + return [] + + schedules = db.query("SELECT sched_id, description FROM schedules WHERE sched_id > 1 AND subject_id = " + str(cookie) + " " + + "ORDER BY sched_id ASC") + + lst = [] + for s in schedules: + lst.append((s['sched_id'], s['description'])) + + return lst + +def _get_subject_list(): + subjects = db.query("SELECT subject_id, name FROM subjects WHERE subject_id > 1 ORDER BY subject_id ASC") lst = [] - for s in schedules: - lst.append((s['sched_id'], s['description'])) + for s in subjects: + lst.append((s['subject_id'], s['name'])) return lst class Index: + form = web.form.Form( + web.form.Dropdown( + 'asignatura', + _get_subject_list(), + description = "Asignatura inscrita" + ), + web.form.Button('Seleccionar') + ) + + def GET(self): + return render.index(self.form()) + + def POST(self): + form = self.form() + form.validates() + subject_id = form.d.asignatura + web.setcookie(COOKIE_SUBJECT, subject_id) + raise web.seeother('/' + str(subject_id)) + +class Subject: form = web.form.Form( web.form.Textbox( @@ -38,21 +76,45 @@ class Index: ), web.form.Dropdown( 'horario', - _get_schedule_list(), + [], description = "Horario a inscribir:" ), web.form.Button('Registrar horario') ) - + def GET(self): + cookie = web.cookies().get(COOKIE_SUBJECT) + + def validate_cookie(cookie): + found = False + + subjects = db.query("SELECT subject_id FROM subjects WHERE subject_id > 1 ORDER BY subject_id ASC") + for s in subjects: + if int(cookie) == int(s['subject_id']): + found = True + break + + return found + + if cookie is None or not validate_cookie(cookie): + raise web.seeother('/') + + + schedules = _get_schedule_list() + form = self.form() + form.horario.args = schedules + schedules = db.query( "SELECT schedules.sched_id, schedules.description, schedules.capacity, rooms.name " + "FROM schedules " + "INNER JOIN rooms ON schedules.room_id = rooms.room_id " + - "ORDER BY schedules.sched_id ASC" + "AND schedules.subject_id = " + str(cookie) + " " + + "ORDER BY schedules.sched_id ASC " ) - return render.index(schedules, self.form(), None) + subject_name = db.query("SELECT name FROM subjects WHERE subject_id = " + str(cookie)) + + return render.subject(subject_name[0]['name'], schedules, form, None) def POST(self): schedules = db.query( @@ -67,7 +129,7 @@ class Index: if not form.validates(): return render.index( schedules, - self.form, + self.form(), "No deje los campos vacíos.
La cédula debe ser un número." ) diff --git a/db_schema.sql b/db_schema.sql index 4c61370..ebf94a2 100644 --- a/db_schema.sql +++ b/db_schema.sql @@ -15,6 +15,21 @@ /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; /*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */; +-- +-- Table structure for table `subjects` +-- + +DROP TABLE IF EXISTS `subjects`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `subjects` ( + `subject_id` int(10) unsigned NOT NULL AUTO_INCREMENT, + `name` varchar(100), + PRIMARY KEY (`subject_id`) +) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1; +/*!40101 SET character_set_client = @saved_cs_client */; +/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */; + -- -- Table structure for table `rooms` -- @@ -26,7 +41,7 @@ CREATE TABLE `rooms` ( `room_id` int(10) unsigned NOT NULL AUTO_INCREMENT, `name` varchar(5) DEFAULT NULL, PRIMARY KEY (`room_id`) -) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1; +) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1; /*!40101 SET character_set_client = @saved_cs_client */; -- @@ -41,10 +56,12 @@ CREATE TABLE `schedules` ( `description` varchar(30) DEFAULT NULL, `capacity` int(11) NOT NULL, `room_id` int(10) unsigned NOT NULL, + `subject_id` int(10) unsigned NOT NULL, PRIMARY KEY (`sched_id`), KEY `fk_room` (`room_id`), - CONSTRAINT `schedules_ibfk_1` FOREIGN KEY (`room_id`) REFERENCES `rooms` (`room_id`) ON DELETE CASCADE ON UPDATE CASCADE -) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=latin1; + CONSTRAINT `schedules_ibfk_1` FOREIGN KEY (`room_id`) REFERENCES `rooms` (`room_id`) ON DELETE CASCADE ON UPDATE CASCADE, + CONSTRAINT `schedules_ibfk_2` FOREIGN KEY (`subject_id`) REFERENCES `subjects` (`subject_id`) ON DELETE CASCADE ON UPDATE CASCADE +) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1; /*!40101 SET character_set_client = @saved_cs_client */; -- @@ -58,7 +75,7 @@ CREATE TABLE `sections` ( `section_id` int(10) unsigned NOT NULL AUTO_INCREMENT, `section` varchar(2) DEFAULT NULL, PRIMARY KEY (`section_id`) -) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1; +) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1; /*!40101 SET character_set_client = @saved_cs_client */; -- @@ -76,12 +93,15 @@ CREATE TABLE `students` ( `email` varchar(100) NOT NULL, `class_id` int(10) unsigned NOT NULL, `schedule_id` int(10) unsigned NOT NULL, + `subject_id` int(10) unsigned NOT NULL, PRIMARY KEY (`student_id`), KEY `fk_class` (`class_id`), KEY `fk_sched` (`schedule_id`), + KEY `fk_subject` (`subject_id`), CONSTRAINT `students_ibfk_1` FOREIGN KEY (`class_id`) REFERENCES `sections` (`section_id`) ON DELETE CASCADE ON UPDATE CASCADE, - CONSTRAINT `students_ibfk_2` FOREIGN KEY (`schedule_id`) REFERENCES `schedules` (`sched_id`) ON DELETE CASCADE ON UPDATE CASCADE -) ENGINE=InnoDB AUTO_INCREMENT=103 DEFAULT CHARSET=latin1; + CONSTRAINT `students_ibfk_2` FOREIGN KEY (`schedule_id`) REFERENCES `schedules` (`sched_id`) ON DELETE CASCADE ON UPDATE CASCADE, + CONSTRAINT `students_ibfk_3` FOREIGN KEY (`subject_id`) REFERENCES `subjects` (`subject_id`) ON DELETE CASCADE ON UPDATE CASCADE +) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1; /*!40101 SET character_set_client = @saved_cs_client */; /*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */; diff --git a/generate_report.py b/generate_report.py new file mode 100644 index 0000000..5c9aa8c --- /dev/null +++ b/generate_report.py @@ -0,0 +1,40 @@ +#! /usr/bin/env python +# _*_ coding: UTF-8 _*_ + +import web + +SUBJECTS = [("6001", 3), ("6004", 2)] + +SECCTION_IDS = {"C1": 1, + "C2": 2, + "C3": 3, + "C4": 4} + +QUERY = "SELECT students.id_card AS cedula, students.first_name AS nombre, students.last_name AS apellido, students.email AS email, sections.section AS seccion, schedules.description AS horario, rooms.name AS salon FROM students INNER JOIN schedules ON schedules.sched_id = students.schedule_id INNER JOIN rooms ON schedules.room_id = rooms.room_id INNER JOIN sections ON students.class_id = sections.section_id AND students.class_id = $sect AND students.subject_id = $subj;" + +def main(): + db = web.database(dbn = 'mysql', user = 'root', pw = 'Familylost9989*', db = 'labs') + + for s in SUBJECTS: + for c in SECCTION_IDS.keys(): + with open(s[0] + "_" + c + ".csv", "w") as f: + values = {"sect": SECCTION_IDS[c], + "subj": s[1]} + students = db.query(QUERY, values) + + f.write("Cedula, Nombres, Apellidos, E-mail, Seccion, Horario, Salon,\n") + + for student in students: + out_str = "" + out_str += unicode(student['cedula']) + ", " + out_str += unicode(student['nombre']) + ", " + out_str += unicode(student['apellido']) + ", " + out_str += unicode(student['email']) + ", " + out_str += unicode(student['seccion']) + ", " + out_str += unicode(student['horario']) + ", " + out_str += unicode(student['salon']) + ", " + + f.write(out_str.encode('utf8') + "\n") + +if __name__ == '__main__': + main() diff --git a/insert_students.py b/insert_students.py new file mode 100644 index 0000000..d4fd240 --- /dev/null +++ b/insert_students.py @@ -0,0 +1,50 @@ +#! /usr/bin/env python +# _*_ coding: UTF-8 _*_ + +import pandas +import web + +FILES = [("/home/miky/Documentos/listado_6001_C1.xls", 3), + ("/home/miky/Documentos/listado_6001_C2.xls", 3), + ("/home/miky/Documentos/listado_6001_C3.xls", 3), + ("/home/miky/Documentos/listado_6001_C4.xls", 3), + ("/home/miky/Documentos/listado_6004_C1.xls", 2), + ("/home/miky/Documentos/listado_6004_C2.xls", 2)] + +SHEET_NAME = "Sheet1" + +SECCTION_IDS = {"C1": 1, + "C2": 2, + "C3": 3, + "C4": 4} + +QUERY = "INSERT INTO students(id_card, first_name, last_name, email, class_id, schedule_id, subject_id) VALUES($id, $fn, $ln, $ml, $cl, 1, $sj)" + +def main(): + db = web.database(dbn = 'mysql', user = 'root', pw = 'Familylost9989*', db = 'labs') + + for f in FILES: + xls = pandas.ExcelFile(f[0]) + df = xls.parse(SHEET_NAME) + + subject_id = f[1] + section_id = SECCTION_IDS[df.iloc[7, 1]] + + row = 10 + while True: + try: + values = {"id": int(df.iloc[row, 2]), + "fn": unicode(df.iloc[row, 3]), + "ln": unicode(df.iloc[row, 4]), + "ml": unicode(df.iloc[row, 5]), + "cl": section_id, + "sj": subject_id} + + db.query(QUERY, vars = values) + + row += 1 + except IndexError: + break + +if __name__ == '__main__': + main() diff --git a/templates/index.html b/templates/index.html index 38991a3..e6ffa33 100644 --- a/templates/index.html +++ b/templates/index.html @@ -1,10 +1,10 @@ -$def with (schedules, form, error_text) +$def with (form) - GD-OECI :: Semestre 2-2015 - Inscripción de laboratorios. + ICARO :: Semestre 1-2017 - Inscripción de laboratorios. @@ -18,8 +18,7 @@ $def with (schedules, form, error_text)
@@ -28,49 +27,12 @@ $def with (schedules, form, error_text)
- - -
-

- Datos para la inscripción. -

-
- - -
-

Horarios disponibles

-
- - - $if error_text is not None: -
-

$:error_text

-
$:form.render()
- -
-
    - $for sched in schedules: - $if sched.sched_id < 8: -
  • - $sched.description -
    - Capacidad: 15 -
    - Disponibles: - $sched.capacity -
    - Sala: - $sched.name -
  • -
-
- diff --git a/templates/subject.html b/templates/subject.html new file mode 100644 index 0000000..08b626c --- /dev/null +++ b/templates/subject.html @@ -0,0 +1,85 @@ +$def with (name, schedules, form, error_text) + + + + + + ICARO :: Semestre 1-2017 - Inscripción de laboratorios. + + + + + + + + + +
+ + + + + + + + + + + + + + + +
+
+ +
+
+
+
+

+ Datos para la inscripción. +

+
+
+
+

Horarios disponibles

+
+
+ $if error_text is not None: +
+

$:error_text

+
+
+ $:form.render() +
+
+
+
    + $for sched in schedules: + $if sched.sched_id > 1: +
  • + $sched.description +
    + Capacidad: 15 +
    + Disponibles: + $sched.capacity +
    + Sala: + $sched.name +
  • +
+
+
+
+
+

+ Laboratorio ICARO, Escuela de Computación, Facultad de Ciencias, Universidad Central de Venezuela. +

+
+ +