Exemplo n.º 1
0
    def lend_book(self, person_id, book_id, amount):
        if not person_id:
            return False, 'Please specify a person id'
        if not book_id:
            return False, 'Please specify a book id'
        if not amount:
            return False, 'Please specify an amount'

        person_id = datatypes.PersonID(person_id)
        book_id = datatypes.BookID(book_id)

        if not person_id.isvalid():
            return False, 'The person ID contains bad elements: '+person_id
        if not book_id.isvalid():
            return False, 'The book ID contains bad elements: '+book_id
        if int(amount) == 0 or not str(amount).isdigit():
            return False, "The amount contains nondigit elements: "+str(amount)

        if not Persons(self.con, self.cur)._person_exists(person_id):
            return False, "No such person in the database: " + person_id
        if not Books(self.con, self.cur)._book_exists(book_id):
            return False, "No such book in the database: " + book_id

        self.cur.execute("""INSERT INTO librarysystem (person_id, book_id, return_date, amount)
                                       VALUES (?, ?, date('now', '+21 day'), ?)""", (person_id, book_id, int(amount)))
        return True, None
Exemplo n.º 2
0
    def delete_person(self, person_id):
        person_id = datatypes.PersonID(person_id)
        if not person_id.isvalid():
            return False, "The person ID contains bad elements: "+person_id, None
        if not self._person_exists(person_id):
            return False, "This person ID does not belong to anybody: "+person_id, None

        _is, msg = Books(self.con, self.cur).lent_books_to(person_id)
        if _is and msg:
            return False, "This person still possesses books", msg

        self.cur.execute("DELETE FROM persons WHERE person_id = ?", (person_id, ))
        return True, None, None
Exemplo n.º 3
0
    def return_book(self, person_id, book_id, amount):
        if not person_id:
            return False, 'Please specify a person id'
        if not book_id:
            return False, 'Please specify a book id'
        if not amount:
            return False, 'Please specify an amount'

        person_id = datatypes.PersonID(person_id)
        book_id = datatypes.BookID(book_id)

        if not person_id.isvalid():
            return False, 'The person ID contains bad elements: '+person_id
        if not book_id.isvalid():
            return False, 'The book ID contains bad elements: '+book_id
        if int(amount) == 0 or not str(amount).isdigit():
            return False, "The amount contains nondigit elements: "+str(amount)

        if not Persons(self.con, self.cur)._person_exists(person_id):
            return False, "No such person in the database: " + person_id
        if not Books(self.con, self.cur)._book_exists(book_id):
            return False, "No such book in the database: " + book_id

        self.cur.execute("SELECT id, amount FROM librarysystem WHERE person_id = ? AND book_id = ? ORDER BY id DESC", (person_id, book_id))

        res = self.cur.fetchall()

        if not res:
            return False, "person '"+str(person_id)+"' has not lent the book '"+str(book_id)+"'"

        amount = int(amount)

        too_much = False
        for _id, lent_amount in res:
            lent_amount = int(lent_amount)
            if too_much:
                amount = too_much
            if lent_amount == amount:
                self.cur.execute("DELETE FROM librarysystem WHERE id = ?", (_id,))
                self.con.commit()
                return True, None
            elif lent_amount > amount:
                self.cur.execute("UPDATE librarysystem SET amount = ? WHERE id = ? ", (int(lent_amount)-int(amount), _id))
                self.con.commit()
                return False, "This is not all what the person took. There are "+str(int(lent_amount)-int(amount))+" more books to bring back"
            else:
                self.cur.execute("DELETE FROM librarysystem WHERE id = ?", (_id,))
                self.con.commit()
                too_much = int(amount) - (lent_amount)
        return False, "The person brought more books than s/he lent. S/he was just supposed to bring back "+str(lent_amount)+" instead of "+str(amount)+"."
Exemplo n.º 4
0
    def add_person(self, person_id, firstname, surname, _class, semester, cellphone, edit_mode=False):

        if not person_id:
            return False, 'Please specify a person id'
        if not firstname:
            return False, 'Please specify a first name'
        if not surname:
            return False, 'Please specify a surname'
        if not _class:
            return False, 'Please specify a class'
        if not semester:
            return False, 'Please specify a semester'

        person_id = datatypes.PersonID(person_id)
        firstname = datatypes.Name(firstname)
        surname = datatypes.Name(surname)
        _class = datatypes._Class(_class)
        semester = datatypes.Semester(semester)
        cellphone = datatypes.Cellphone(cellphone)

        if not person_id.isvalid():
            return False, 'The person ID contains bad elements: '+person_id.value
        if not firstname.isvalid():
            return False, 'The first name contains bad elements: '+firstname.value
        if not surname.isvalid():
            return False, 'The surname contains bad elements: '+surname.value
        if not _class.isvalid():
            return False, 'The class contains bad elements: '+_class.value
        if not semester.isvalid():
            return False, 'The semester contains bad elements: '+semester.value
        if not cellphone.isvalid():
            return False, 'The cellphone contains bad elements: '+cellphone.value

        if self._person_exists(person_id) and not edit_mode:   
            return False, "There is already one person with this person ID in the database: " + person_id

        if edit_mode:
            self.cur.execute("DELETE FROM persons WHERE person_id = ?", (person_id,))

        values = (person_id, firstname, surname, _class, semester, cellphone.database_format())

        self.cur.execute("""INSERT INTO persons (person_id, firstname, surname, class, semester, cellphone)
                                         VALUES (?, ?, ?, ?, ?, ?)""", values)
        return True, None
Exemplo n.º 5
0
    def search_person(self, person_id, firstname, surname, _class, semester, cellphone):
        #empty string if none
        res = list() # the result

        if not (person_id or firstname or surname or cellphone or _class or semester):
            return False, "No search parameters given"

        person_id = datatypes.PersonID(person_id)
        firstname = datatypes.Name(firstname)
        surname = datatypes.Name(surname)
        _class = datatypes._Class(_class)
        semester = datatypes.Semester(semester)
        cellphone = datatypes.Cellphone(cellphone)

        if person_id and not person_id.isvalid():
            return False, 'The person ID contains bad elements: '+person_id
        if firstname and not firstname.isvalid():
            return False, 'The first name contains bad elements: '+firstname
        if surname and not surname.isvalid():
            return False, 'The surname contains bad elements: '+surname
        if _class and not _class.isvalid():
            return False, 'The class contains bad elements: '+_class
        if semester and not semester.isvalid():
            return False, 'The semester contains bad elements: '+semester
        if cellphone and not cellphone.isvalid():
            return False, 'The cellphone contains bad elements: '+cellphone

        if person_id:
            self.cur.execute("""SELECT person_id, 
                                       firstname, 
                                       surname, 
                                       class, 
                                       semester, 
                                       cellphone FROM persons WHERE person_id = ?""", (person_id,))
            res.extend(self.cur.fetchall())

        if cellphone:
            self.cur.execute("""SELECT person_id, 
                                       firstname, 
                                       surname, 
                                       class, 
                                       semester, 
                                       cellphone FROM persons WHERE cellphone = ?""", (cellphone.database_format(),))
            res.extend(self.cur.fetchall())

        if firstname and surname:
            self.cur.execute("""SELECT person_id, 
                                       firstname, 
                                       surname, 
                                       class, 
                                       semester, 
                                       cellphone FROM persons WHERE lower(firstname) = ? and lower(surname) = ?""", (firstname.lower(), surname.lower()))
            res.extend(self.cur.fetchall())

        if surname and _class and semester:
            self.cur.execute("""SELECT person_id, 
                                       firstname, 
                                       surname, 
                                       class, 
                                       semester, 
                                       cellphone FROM persons WHERE lower(surname) = ? and class = ? and semester = ?""",
                            (surname.lower(), _class, semester))
            res.extend(self.cur.fetchall())

        if surname and _class:
            self.cur.execute("""SELECT person_id, 
                                       firstname, 
                                       surname, 
                                       class, 
                                       semester, 
                                       cellphone FROM persons WHERE lower(surname) = ? and class = ?""", (surname.lower(), _class))
            res.extend(self.cur.fetchall())

        if firstname and _class and semester:
            self.cur.execute("""SELECT person_id, 
                                       firstname, 
                                       surname, 
                                       class, 
                                       semester, 
                                       cellphone FROM persons WHERE lower(firstname) = ? and class = ? and semester = ?""",
                            (firstname.lower(), _class, semester))
            res.extend(self.cur.fetchall())

        if firstname and _class:
            self.cur.execute("""SELECT person_id, 
                                       firstname, 
                                       surname, 
                                       class, 
                                       semester, 
                                       cellphone FROM persons WHERE lower(firstname) = ? and class = ?""", (firstname.lower(), _class))
            res.extend(self.cur.fetchall())

        if firstname and len(res)<5:
            self.cur.execute("""SELECT person_id, 
                                       firstname, 
                                       surname, 
                                       class, 
                                       semester, 
                                       cellphone FROM persons WHERE lower(firstname) = ?""", (firstname.lower(), ))
            res.extend(self.cur.fetchall())

        if surname and len(res)<5:
            self.cur.execute("""SELECT person_id, 
                                       firstname, 
                                       surname, 
                                       class, 
                                       semester, 
                                       cellphone FROM persons WHERE lower(surname) = ?""", (surname.lower(),))
            res.extend(self.cur.fetchall())

        if _class and semester and len(res)<5:
            self.cur.execute("""SELECT person_id, 
                                       firstname, 
                                       surname, 
                                       class, 
                                       semester, 
                                       cellphone FROM persons WHERE class = ? and semester = ?""", (_class, semester))
            res.extend(self.cur.fetchall())

        if _class and not res:
            self.cur.execute("""SELECT person_id, 
                                       firstname, 
                                       surname, 
                                       class, 
                                       semester, 
                                       cellphone FROM persons WHERE class = ?""", (_class,))
            res.extend(self.cur.fetchall())

        if semester and not res:
            self.cur.execute("""SELECT person_id, 
                                       firstname, 
                                       surname, 
                                       class, 
                                       semester, 
                                       cellphone FROM persons WHERE semester = ?""", (semester,))
            res.extend(self.cur.fetchall())

        result = list()
        for row in res:
            if row[0] not in result:
                result.append(row)
        return True, result