def borrow(self): # Delete Old Borrow self.delete() # Create New Borrow Record borrow = Borrow(readers=self.reader, deadline=self.getDeadline()) # Set Borrowed Item setattr(borrow, self.item.__tablename__, self.item) # Number of Borrowed Items count = session.query(Borrow).filter_by( reader_id=self.reader.id).count() # Check If Number of Borrowed Items Less Than 5 if count < 5: if self.reader: # Insert Data To DB session.merge(borrow) session.commit() self.extendButton() # Reset messagebox.showinfo('Kölcsönzés', 'Kikölcsönözve.') else: messagebox.showerror( 'Kölcsönzés', 'A maxiumum kölcsönözhető dokumentumok meghaladva.') # RESET SELECT PERIOD FIELD self.entries['period'].set('')
def events(): for row in Import.readcsv(Import.openfile()): day, type, readerCode, documentCode, *_ = row # Borrow deadline deadline = datetime.now().date() + timedelta(days=int(day)) # Borrowing reader reader = session.query(Reader).filter(Reader.code==readerCode).first() # Borrowed document documents = Import.getDocuments(documentCode) # Simulate borrow if type == 'KOLCSONZES': borrow = Borrow(deadline=deadline, readers=reader, **documents) Import.add(borrow) # Simulate extend if type == 'HOSSZABBITAS': borrow = Import.getBorrow(documents, documentCode) borrow.deadline = deadline borrow.extended = Borrow.extended+1 Import.add(borrow) # Simulate return if type == 'VISSZA': borrow = Import.getBorrow(documents, documentCode) Import.delete(borrow) # Simulation complete message messagebox.showinfo('Történések', 'Szimuláció kész.')
def fill(self): # RESET TREEVIEW self.tree.delete(*self.tree.get_children()) # GET ALL BORROWS borrows = session.query(Borrow).order_by(Borrow.deadline.desc()).all() # INSERT BORROWED ITEMS INTO TREEVIEW if borrows: for borrow in borrows: # --- Borrowed Item Types types = [borrow.books, borrow.magazines, borrow.media] # --- Determine Borrowed Item Type title = next(x for x in types if x is not None).title # --- Determine If Borrow Deadline Has Passed tags = tuple() if borrow.deadline < datetime.now().date(): tags = ('expired') # --- Display Borrowed Item self.tree.insert('', 0, text=title, tags=tags, values=(borrow.readers.name, borrow.deadline, borrow.extended))
def getSelected(self): try: index = self._getSelectedIndex() id = self.getAll()[index][0] return session.query( self.model).filter(self.model.id == id).first() except: return self.model()
def select(self): # Fetch Reader ID From Dialog readerId = ReadersDialog(self.frame).show() # Fetch Reader By ID self.reader = session.query(Reader).get(readerId) # Set Reader Name On Button self.borrower.config(text=self.reader.name) # Reset self.borrowButton()
def write(model, callback, file=None, mode='w', count=None): # Open File filename = file or Export.savefile() with open(filename, mode, newline='', encoding='utf-8') as file: # All Readers results = session.query(model) # CSV Writer outcsv = csv.writer(file, delimiter=';') # Number of Rows in Total count != False and outcsv.writerow(count or [results.count()]) # Write Rows to File [callback(outcsv, row) for row in results.all()] # Clode File Handler file.close() return filename
def fill(self): # RESET TREEVIEW self.tree.delete(*self.tree.get_children()) # SEARCH QUERY query = self.search.getQuery() # GET ALL READERS readers = session.query(Reader.id, Reader.code, Reader.name) readers = readers.filter(Reader.name.like('%{0}%'.format(query))).all() # Insert Readers into Treeview if readers: for reader in readers: self.tree.insert('', 0, text=reader.code, values=(reader.id, reader.name))
def documents(): # TOTAL NUMBER OF DOCUMENTS count = sum([ session.query(model).count() for model in [Book, Magazine, Medium] ]) # EXPORT ALL DOCUMENT TYPES: # books, magazines, media file = Export.write(Book, Export._books, count=[count]) file = Export.write(Magazine, Export._magazines, mode='a', count=False, file=file) file = Export.write(Medium, Export._media, mode='a', count=False, file=file)
def getAll(self): # GET ALL READERS result = session.query(self.model.id, self.model.title) result = result.filter(self.model.code.like('%{0}%'.format( self.query))).all() return result
def _readers(outcsv, reader): # Number of Borrowed Documents count = session.query(Borrow).filter_by(reader_id=reader.id).count() # Write Data Row outcsv.writerow([reader.code, reader.name, count])
def getBorrow(documents, documentCode): condition = getattr(Borrow, next(iter(documents))).has(code=documentCode) return session.query(Borrow).filter(condition).first()
def getDocuments(documentCode): documents = {} for model in [Book, Magazine, Medium]: documents[model.__tablename__] = session.query(model).filter(model.code==documentCode).first() filter(None, documents) return documents
def selected(self): return session.query(Reader).filter( Reader.id == self._selectedId()).first()
def get(self): # Return Current Item's Borrow Record table = getattr(Borrow, self.item.__tablename__) return session.query(Borrow).filter( table.has(id=self.item.id)).join(Reader).first()