Example #1
0
def bookimport_post():
    x = web.input(myfile={})
    if 'myfile' in x:
        fname = 'tmp/libros.csv'
        f = open(fname, 'w')
        f.write(x.myfile.file.read())
        f.close()

        grades_db = config.DB.select('grades')
        groups_db = config.DB.select('groups')

        grades_db_list = []
        groups_db_list = []

        new_grades = []
        new_groups = []

        with open(fname, 'rb') as data:
            try:
                # csv.DictReader uses first line in file for column headings by default
                dr = csv.DictReader(data) # comma is default delimiter
                to_db = [(i['titulo'], i['curso'], i['grupo'].upper(), i['editorial'], i['isbn'],
                         i['precio'].replace(',', '.'), i['stock']) for i in dr]

                for gdb in grades_db:
                    grades_db_list.append(gdb['grade'])

                for gdb in groups_db:
                    groups_db_list.append(gdb['groupe'])

                for item in to_db:
                    if item[1] in grades_db_list:
                        pass
                    else:
                        new_grades.append(item[1])
                        grades_db_list.append(item[1])
                    if item[2] in groups_db_list:
                        pass
                    else:
                        new_groups.append(item[2])
                        groups_db_list.append(item[2])

                print new_grades, new_groups

                os.remove(fname)
            except Exception, e:
                os.remove(fname)
                return render.error('csv.DictReader', e)
            else:
                con = sqlite3.connect('db/libros.sqlite')
                con.text_factory = str
                cur = con.cursor()
                try:
                    print to_db
                    cur.executemany('''INSERT INTO books (titulo, curso, grupo, editorial,
                                        isbn, precio, stock)
                                        VALUES (?, ?, ?, ?, ?, ?, ?);''',
                                        to_db)

                    con.commit()

                except Exception, e:
                    return render.error('SQLite', e)

                for grade in new_grades:
                    config.DB.insert('grades', grade=grade)

                for group in new_groups:
                    config.DB.insert('groups', groupe=group)
Example #2
0
def studimport_post():
    x = web.input(myfile={})
    if 'myfile' in x:
        fname = 'tmp/alumnos.csv'
        f = open(fname, 'w')
        f.write(x.myfile.file.read())
        f.close()

        grades_db = config.DB.select('grades')
        groups_db = config.DB.select('groups')

        grades_db_list = []
        groups_db_list = []

        new_grades = []
        new_groups = []

        with open(fname, 'rb') as data:
            try:
                # csv.DictReader uses first line in file for column headings by default
                dr = csv.DictReader(data) # comma is default delimiter
                to_db = [(i['nombre'], i['curso'], i['grupo'].upper(), i['tutor'], i['profesiones'], i['tel1'],
                         i['tel2'], i['mail1'], i['mail2']) for i in dr]
                for gdb in grades_db:
                    grades_db_list.append(gdb['grade'])

                for gdb in groups_db:
                    groups_db_list.append(gdb['groupe'])

                for item in to_db:
                    if item[1] in grades_db_list:
                        pass
                    else:
                        new_grades.append(item[1])
                        grades_db_list.append(item[1])
                    if item[2] in groups_db_list:
                        pass
                    else:
                        new_groups.append(item[2])
                        groups_db_list.append(item[2])

                print new_grades, new_groups
                os.remove(fname)
            except Exception, e:
                os.remove(fname)
                return render.error('csv.DictReader', e)
            else:
                con = sqlite3.connect('db/libros.sqlite')
                con.text_factory = str
                cur = con.cursor()
                try:
                    cur.executemany('''INSERT INTO students (nombre, curso, grupo, tutor,
                                        profesiones, tel1, tel2, mail1, mail2)
                                        VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?);''',
                                        to_db)
                    con.commit()
                except Exception, e:
                    return render.error('SQLite', e)

                for grade in new_grades:
                    config.DB.insert('grades', grade=grade)

                for group in new_groups:
                    config.DB.insert('groups', groupe=group)