def return_rental(librarian, rental_id): ''' Desc: librarian receives book from rental Args: librarian - User instance: accepts returnal rental_id - id of rental being ended ''' returned_rental = get_object_or_404(Rental, id=rental_id) # not everyone can return book if not librarian.has_perm('baseapp.change_rental'): raise PermissionDenied if not librarian in returned_rental.reservation.book_copy.location.get_all_maintainers(): raise PermissionDenied('One can only rent/return from location one maintains') # can't return a rental twice if returned_rental.end_date is not None: raise Rental.DoesNotExist('Rental already returned') # actual returning returned_rental.who_received = librarian returned_rental.end_date = utils.now() returned_rental.save() mark_available(returned_rental.reservation.book_copy) # someone might be waiting for that book
def generate_new_password(self): ''' Generates new password and returns it. ''' from datetime import datetime from hashlib import md5 value_to_hash = repr(utils.now()) + self.user.email new_password = md5(value_to_hash).hexdigest() return new_password
def cancel_reservation(reservation, user): if reservation.for_whom != user and not user.has_perm("baseapp.change_reservation") or\ user == reservation.for_whom and not user.has_perm("baseapp.change_own_reservation"): raise PermissionDenied if reservation.rental_set.count() > 0: raise PermissionDenied('The reservation has already been purchased!') reservation.who_cancelled = user reservation.when_cancelled = utils.now() reservation.save()
def cancel_reservation(reservation, canceller): ''' Desc: Cancels reservation. Arg: reservation - instance of Reservation. Reservation to be cancelled. canceller - instance of User. User who cancels the reservation. ''' assert isinstance(reservation, Reservation) assert isinstance(canceller, User) reservation.when_cancelled = utils.now() reservation.who_cancelled = request.user reservation.save()
def rent(reservation, librarian): ''' Desc: Librarian rents book indicated by reservation. Return value says when it is supposed to be returned ''' if not librarian.has_perm('baseapp.add_rental'): raise PermissionDenied if not librarian in reservation.book_copy.location.get_all_maintainers(): raise PermissionDenied('One can only rent from location one maintains') if not is_reservation_rentable(reservation): raise PermissionDenied('Reservation not rentable.') max_end_date = utils.today() + timedelta(reservation_status(reservation)-1) # return one day before it's reserved if max_end_date < reservation.end_date: reservation.end_date = max_end_date reservation.save() rental = Rental(reservation=reservation, who_handed_out=librarian, start_date=utils.now()) rental.save() mail.made_rental(rental) return reservation.end_date