Advertisement
DoromaAnim

ee

Dec 17th, 2019
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.56 KB | None | 0 0
  1. from sqlalchemy import create_engine
  2. from sqlalchemy.orm import sessionmaker
  3. from sqlalchemy.ext.declarative import declarative_base
  4. from sqlalchemy.orm import relationship
  5. from sqlalchemy import Table, Column, Integer, ForeignKey, String
  6. import sys
  7.  
  8. Base = declarative_base()
  9.  
  10. borrows = Table('Borrows', Base.metadata,
  11. Column('Book_id', Integer, ForeignKey('Book.id')),
  12. Column('Person_id', Integer, ForeignKey('Person.id')))
  13.  
  14.  
  15. class Book(Base):
  16. __tablename__ = 'Book'
  17.  
  18. id = Column(Integer, primary_key=True)
  19. author = Column(String)
  20. title = Column(String)
  21. year = Column(Integer)
  22. quantity = Column(Integer)
  23.  
  24. borrowers = relationship('Person', secondary=borrows,
  25. back_populates='borrowed')
  26.  
  27. def __init__(self, author, title, year, quantity):
  28. self.author = author
  29. self.title = title
  30. self.year = year
  31. self.quantity = quantity
  32.  
  33. def __repr__(self):
  34. return ("autor: {}, tytul: {}, rok: {}, liczba: {}".format(self.author,
  35. self.title, self.year, self.quantity))
  36.  
  37. def __eq__(self, other):
  38. return (self.author == other.author and self.title == other.title and
  39. self.year == other.year)
  40.  
  41.  
  42. class Person(Base):
  43. __tablename__ = 'Person'
  44.  
  45. id = Column(Integer, primary_key=True)
  46. name = Column(String)
  47. email = Column(String)
  48.  
  49. borrowed = relationship(
  50. "Book",
  51. secondary=borrows,
  52. back_populates="borrowers")
  53.  
  54. def __init__(self, name, email):
  55. self.name = name
  56. self.email = email
  57.  
  58. def __eq__(self, other):
  59. return self.name == other.name and self.email == other.email
  60.  
  61. def __repr__(self):
  62. return "nazwa: {}, email: {}".format(self.name, self.email)
  63.  
  64.  
  65. def load_database():
  66. engine = create_engine("sqlite:///wyklad.db", echo=False)
  67. Session = sessionmaker(bind=engine)
  68. Base.metadata.create_all(engine)
  69. session = Session()
  70. return session
  71.  
  72.  
  73. def add_book(session, book):
  74. temp = session.query(Book).filter(Book == book).all()
  75. if len(temp):
  76. temp[0].quantity += book.quantity
  77. else:
  78. session.add(book)
  79. return True
  80.  
  81.  
  82. def add_person(session, person):
  83. if ('@' not in person.email or
  84. session.query(Person).filter(Person == person).count() != 0):
  85. print("Błąd podczas dodawania osoby")
  86. return False
  87. session.add(person)
  88. return True
  89.  
  90.  
  91. def borrow_book(session, book, person):
  92. temp_book = session.query(Book).filter(Book == book).all()
  93. temp_person = session.query(Person).filter(Person == person).all()
  94. if len(temp_book) != 1 or len(temp_person) != 1:
  95. print("Nie ma takiej osoby lub książki")
  96. return False
  97. temp_person = temp_person[0]
  98. temp_book = temp_book[0]
  99. if temp_book.quantity == 0:
  100. print("Nie ma aktualnie więcej egzemplarzy tej książki")
  101. return False
  102.  
  103. if temp_person in temp_book.borrowers:
  104. print("Ta osoba pożyczała już tą książkę")
  105. return False
  106. temp_book.borrowers.append(temp_person)
  107. temp_book.quantity -= 1
  108. return True
  109.  
  110.  
  111. def return_book(session, book, person):
  112. temp_book = session.query(Book).filter(Book == book).all()
  113. temp_person = session.query(Person).filter(Person == person).all()
  114. if len(temp_book) != 1 or len(temp_person) != 1:
  115. print("Nie ma takiej osoby lub książki")
  116. return False
  117. temp_person = temp_person[0]
  118. temp_book = temp_book[0]
  119.  
  120. try:
  121. idx = temp_book.borrowers.index(person)
  122. del(temp_book.borrowers[idx])
  123. temp_book.quantity += 1
  124. except ValueError:
  125. print("Ta osoba nie pożyczała tej książki")
  126. return False
  127. return True
  128.  
  129.  
  130. def exit(session):
  131. session.close()
  132. sys.exit(0)
  133.  
  134.  
  135. def main():
  136. session = load_database()
  137.  
  138. if len(sys.argv) < 1:
  139. print("Zbyt mało argumentów do programu")
  140. exit(session)
  141.  
  142. arglist = sys.argv[1:]
  143. for i in range(len(arglist)):
  144. if arglist[i][0:2] != "--":
  145. print("Nieprawidłowe argumenty do programu")
  146. exit(session)
  147. arglist[i] = arglist[i][2:]
  148.  
  149. arg_dict = {}
  150. for i in arglist[1:]:
  151. i = i.split("=")
  152. if len(i) != 2:
  153. print("Niepoprawne argumenty do programu")
  154. exit(session)
  155. arg_dict[i[0]] = i[1]
  156.  
  157. if arglist[0] == "return_book":
  158. return_book(session,
  159. Book(arg_dict["author"], arg_dict["title"],
  160. int(arg_dict["year"]), 0),
  161. Person(arg_dict["name"], arg_dict["email"]))
  162. elif arglist[0] == "borrow_book":
  163. borrow_book(session,
  164. Book(arg_dict["author"], arg_dict["title"],
  165. int(arg_dict["year"]), 0),
  166. Person(arg_dict["name"], arg_dict["email"]))
  167. elif arglist[0] == "add_book":
  168. add_book(session, Book(arg_dict["author"], arg_dict["title"],
  169. int(arg_dict["year"]), int(arg_dict["quantity"])))
  170. elif arglist[0] == "add_person":
  171. add_person(session, Person(arg_dict["name"], arg_dict["email"]))
  172. elif arglist[0] == "wypisz":
  173. x = session.query(Person).all()
  174. y = session.query(Book).all()
  175. for i in x:
  176. print("Osoba:", i)
  177. for i in y:
  178. print("Ksiazka:", i)
  179. else:
  180. print("Niepoprawny pierwszy argument do programu")
  181. exit(session)
  182.  
  183. session.commit()
  184. session.close()
  185.  
  186.  
  187. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement