DoromaAnim

aaa

Jan 6th, 2020
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.99 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 ("{} {} {} {} {}".format(self.id, 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 "{} {} {}".format(self.id, 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 get_person_list(session):
  136. x = session.query(Person).all()
  137. return [str(i).split() for i in x]
  138.  
  139.  
  140. def get_books_list(session):
  141. x = session.query(Book).all()
  142. return [str(i).split() for i in x]
  143.  
  144.  
  145. def get_borrows_list(session):
  146. x = session.query(borrows).all()
  147. return [[str(i), str(j)] for i, j in x]
  148.  
  149.  
  150. def main():
  151. session = load_database()
  152.  
  153. if len(sys.argv) < 1:
  154. print("Zbyt mało argumentów do programu")
  155. exit(session)
  156.  
  157. arglist = sys.argv[1:]
  158. for i in range(len(arglist)):
  159. if arglist[i][0:2] != "--":
  160. print("Nieprawidłowe argumenty do programu")
  161. exit(session)
  162. arglist[i] = arglist[i][2:]
  163.  
  164. arg_dict = {}
  165. for i in arglist[1:]:
  166. i = i.split("=")
  167. if len(i) != 2:
  168. print("Niepoprawne argumenty do programu")
  169. exit(session)
  170. arg_dict[i[0]] = i[1]
  171.  
  172. if arglist[0] == "return_book":
  173. return_book(session,
  174. Book(arg_dict["author"], arg_dict["title"],
  175. int(arg_dict["year"]), 0),
  176. Person(arg_dict["name"], arg_dict["email"]))
  177. elif arglist[0] == "borrow_book":
  178. borrow_book(session,
  179. Book(arg_dict["author"], arg_dict["title"],
  180. int(arg_dict["year"]), 0),
  181. Person(arg_dict["name"], arg_dict["email"]))
  182. elif arglist[0] == "add_book":
  183. add_book(session, Book(arg_dict["author"], arg_dict["title"],
  184. int(arg_dict["year"]), int(arg_dict["quantity"])))
  185. elif arglist[0] == "add_person":
  186. add_person(session, Person(arg_dict["name"], arg_dict["email"]))
  187. elif arglist[0] == "wypisz":
  188. x = session.query(Person).all()
  189. y = session.query(Book).all()
  190. for i in x:
  191. print(str(i).split())
  192. for i in y:
  193. print(str(i).split(), end=", ")
  194. # get_borrows_list(session)
  195. else:
  196. print("Niepoprawny pierwszy argument do programu")
  197. exit(session)
  198.  
  199. session.commit()
  200. session.close()
  201.  
  202.  
  203. if __name__ == "__main__":
  204. main()
Add Comment
Please, Sign In to add comment