Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from sqlalchemy import create_engine
- from sqlalchemy.orm import sessionmaker
- from sqlalchemy.ext.declarative import declarative_base
- from sqlalchemy.orm import relationship
- from sqlalchemy import Table, Column, Integer, ForeignKey, String
- import sys
- Base = declarative_base()
- borrows = Table('Borrows', Base.metadata,
- Column('Book_id', Integer, ForeignKey('Book.id')),
- Column('Person_id', Integer, ForeignKey('Person.id')))
- class Book(Base):
- __tablename__ = 'Book'
- id = Column(Integer, primary_key=True)
- author = Column(String)
- title = Column(String)
- year = Column(Integer)
- quantity = Column(Integer)
- borrowers = relationship('Person', secondary=borrows,
- back_populates='borrowed')
- def __init__(self, author, title, year, quantity):
- self.author = author
- self.title = title
- self.year = year
- self.quantity = quantity
- def __repr__(self):
- return ("autor: {}, tytul: {}, rok: {}, liczba: {}".format(self.author,
- self.title, self.year, self.quantity))
- def __eq__(self, other):
- return (self.author == other.author and self.title == other.title and
- self.year == other.year)
- class Person(Base):
- __tablename__ = 'Person'
- id = Column(Integer, primary_key=True)
- name = Column(String)
- email = Column(String)
- borrowed = relationship(
- "Book",
- secondary=borrows,
- back_populates="borrowers")
- def __init__(self, name, email):
- self.name = name
- self.email = email
- def __eq__(self, other):
- return self.name == other.name and self.email == other.email
- def __repr__(self):
- return "nazwa: {}, email: {}".format(self.name, self.email)
- def load_database():
- engine = create_engine("sqlite:///wyklad.db", echo=False)
- Session = sessionmaker(bind=engine)
- Base.metadata.create_all(engine)
- session = Session()
- return session
- def add_book(session, book):
- temp = session.query(Book).filter(Book == book).all()
- if len(temp):
- temp[0].quantity += book.quantity
- else:
- session.add(book)
- return True
- def add_person(session, person):
- if ('@' not in person.email or
- session.query(Person).filter(Person == person).count() != 0):
- print("Błąd podczas dodawania osoby")
- return False
- session.add(person)
- return True
- def borrow_book(session, book, person):
- temp_book = session.query(Book).filter(Book == book).all()
- temp_person = session.query(Person).filter(Person == person).all()
- if len(temp_book) != 1 or len(temp_person) != 1:
- print("Nie ma takiej osoby lub książki")
- return False
- temp_person = temp_person[0]
- temp_book = temp_book[0]
- if temp_book.quantity == 0:
- print("Nie ma aktualnie więcej egzemplarzy tej książki")
- return False
- if temp_person in temp_book.borrowers:
- print("Ta osoba pożyczała już tą książkę")
- return False
- temp_book.borrowers.append(temp_person)
- temp_book.quantity -= 1
- return True
- def return_book(session, book, person):
- temp_book = session.query(Book).filter(Book == book).all()
- temp_person = session.query(Person).filter(Person == person).all()
- if len(temp_book) != 1 or len(temp_person) != 1:
- print("Nie ma takiej osoby lub książki")
- return False
- temp_person = temp_person[0]
- temp_book = temp_book[0]
- try:
- idx = temp_book.borrowers.index(person)
- del(temp_book.borrowers[idx])
- temp_book.quantity += 1
- except ValueError:
- print("Ta osoba nie pożyczała tej książki")
- return False
- return True
- def exit(session):
- session.close()
- sys.exit(0)
- def main():
- session = load_database()
- if len(sys.argv) < 1:
- print("Zbyt mało argumentów do programu")
- exit(session)
- arglist = sys.argv[1:]
- for i in range(len(arglist)):
- if arglist[i][0:2] != "--":
- print("Nieprawidłowe argumenty do programu")
- exit(session)
- arglist[i] = arglist[i][2:]
- arg_dict = {}
- for i in arglist[1:]:
- i = i.split("=")
- if len(i) != 2:
- print("Niepoprawne argumenty do programu")
- exit(session)
- arg_dict[i[0]] = i[1]
- if arglist[0] == "return_book":
- return_book(session,
- Book(arg_dict["author"], arg_dict["title"],
- int(arg_dict["year"]), 0),
- Person(arg_dict["name"], arg_dict["email"]))
- elif arglist[0] == "borrow_book":
- borrow_book(session,
- Book(arg_dict["author"], arg_dict["title"],
- int(arg_dict["year"]), 0),
- Person(arg_dict["name"], arg_dict["email"]))
- elif arglist[0] == "add_book":
- add_book(session, Book(arg_dict["author"], arg_dict["title"],
- int(arg_dict["year"]), int(arg_dict["quantity"])))
- elif arglist[0] == "add_person":
- add_person(session, Person(arg_dict["name"], arg_dict["email"]))
- elif arglist[0] == "wypisz":
- x = session.query(Person).all()
- y = session.query(Book).all()
- for i in x:
- print("Osoba:", i)
- for i in y:
- print("Ksiazka:", i)
- else:
- print("Niepoprawny pierwszy argument do programu")
- exit(session)
- session.commit()
- session.close()
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement