def __init__(self, key, type, user_value, global_value, description, can_override): ''' Args: key -- str. type -- instance of ConfigurationValueType. user_value -- serialized value defined per user. Can be None. global_value -- serialized value defined not per user. Cannot be None. Extra fields: value -- *deserialized* value associated with key. Value of global_value or user_value, respectively to can_override. If user_value is None, then value == global_value. ''' self.logger = get_logger('config.option') assert isinstance(key, basestring) assert isinstance(type, ConfigurationValueType) assert isinstance(can_override, bool) assert global_value self.key = key self.type = type self.user_value = Config.Option.deserialize(user_value, self.type) self.global_value = Config.Option.deserialize(global_value, self.type) if can_override and (self.user_value is not None): self.value = self.user_value else: self.value = self.global_value self.can_override = can_override self.description = description
def send_prolongation_request(request, rental_id, requester): ''' Desc: Sends prolongation request to librarians. Args: request -- http request - needed for messages rental_id -- uint requester -- instance of User, ''' prol_logger = utils.get_logger('prolong') prol_logger.info("prolong_request()....") assert isinstance(rental_id, int) assert isinstance(requester, User) try: r = Rental.objects.get(pk=rental_id, reservation__for_whom=requester.id) max_period = max_prolongation_period(r) prol_logger.info("prolong_request() preconditions valid, ;; rental %d ;; found ." % rental_id) if max_period < 1: prol_logger.error("prolong_request() cannot prolong -- someone else reserved/prolonged or too early.") messages.error(request, "You CANNOT prolong this rental -- probably someone else reserved it or it is too early for prolongation.") else: prol_logger.info("prolong_request() request sent successfully.") mail.request_prolongation(requester=requester, rental=r) messages.info(request, "Request was sent to librarians, please WAIT FOR APPROVAL. Prolongation possible for %d days." % max_period) except Rental.DoesNotExist: messages.error(request, "Rental not found, unable to request for prolong.")
def __init__(self, key, type, user_value, global_value, description, can_override): ''' Args: key -- str. type -- instance of ConfigurationValueType. user_value -- serialized value defined per user. Can be None. global_value -- serialized value defined not per user. Cannot be None. Extra fields: value -- *deserialized* value associated with key. Value of global_value or user_value, respectively to can_override. If user_value is None, then value == global_value. ''' self.logger = get_logger('config.option') assert isinstance(key, basestring) assert isinstance(type, ConfigurationValueType) assert isinstance(can_override, bool) assert global_value self.key = key self.type = type self.user_value = Config.Option.deserialize(user_value, self.type) self.global_value = Config.Option.deserialize( global_value, self.type) if can_override and (self.user_value is not None): self.value = self.user_value else: self.value = self.global_value self.can_override = can_override self.description = description
def __init__(self): self.logger = get_logger('config') self.supported_types = ['int', 'bool', 'unicode', 'list_groupnames']
def show_user_rentals(request, user_id=False): ''' Desc: Shows user's current rentals. Allows returning books. ''' if not user_id: user = request.user else: try: user = User.objects.get(id=user_id) except User.DoesNotExist: return render_not_found(request, item_name='User') post = request.POST # a shorthand context = { 'first_name' : user.first_name, 'last_name' : user.last_name, 'email' : user.email, 'reader' : user, } # Return button clicked for a rental: # but we care about it only if he is supposed to if request.user.has_perm('baseapp.change_rental') and\ request.method == 'POST' and\ 'returned' in post: try: until_when = return_rental(librarian=request.user, rental_id=post['returned']) context['message'] = 'Successfully returned' messages.info(request, 'Successfully returned') except Rental.DoesNotExist: return render_not_found(request, item_name='Rental') except PermissionDenied: # this might happen if user doesn't have 'change_rental' permission return render_forbidden # logger for prolongation prol_logger = utils.get_logger('prolong') # sending prolongation request to librarians if request.method == 'POST' and 'prolong_request' in post: rental_id = int(post['prolong_rental_id']) prol_logger.info("request ;; user %d %s ;; rental_id %d", user.id, user.get_full_name(), rental_id) send_prolongation_request(request, rental_id, user) # prolonging rental if request.method == 'POST' and 'prolong' in post and request.user.has_perm('baseapp.change_rental'): rental_id = int(post['prolong_rental_id']) requester = user prolongator = request.user prol_logger.info("prolonging ;; prolongator %d %s ;; requester %d %s ;; rental_id %d", prolongator.id, prolongator.get_full_name(), requester.id, requester.get_full_name(), rental_id) prolong_rental(request, rental_id, requester, prolongator) # find user's rentals user_rentals = Rental.objects.filter(reservation__for_whom=user.id).filter(end_date__isnull=True) # and put them in a dict: days_left_to_enable_prolongation = Config().get_int('min_days_left_to_enable_prolongation') rent_list = [] for r in user_rentals: max_period = max_prolongation_period(r) days_left_to_rend = (r.reservation.end_date - utils.today()).days enabled_prolongation = days_left_to_rend <= days_left_to_enable_prolongation can_request_prolongation = enabled_prolongation and (request.user==r.reservation.for_whom) # owner of reservation can_prolong = enabled_prolongation \ and max_period > 0 \ and request.user.has_perm('baseapp.change_rental') \ and (request.user in r.reservation.book_copy.location.get_all_maintainers()) # msg('enabled_prolongation: %d' % enabled_prolongation) # msg('can_request_prolongation: %d' % can_request_prolongation) # msg('can_prolong: %d' % can_prolong) # msg('days_left_to_enable_prolongation: %d' % days_left_to_enable_prolongation) # msg('max_period: %d' % max_period) kopy = r.reservation.book_copy book = kopy.book rent_list.append({ 'id' : r.id, 'shelf_mark' : kopy.shelf_mark, 'title' : book.title, 'kopy' : kopy, 'book' : book, 'authors' : list(sorted([a.name for a in book.author.all()])), 'from_date' : r.start_date.date(), 'to_date' : r.reservation.end_date, 'returnable' : request.user in kopy.location.get_all_maintainers(), 'rental' : r, 'can_request_prolongation' : can_request_prolongation, 'can_prolong' : can_prolong, 'prolongation_length' : max_period, 'cannot_request_nor_prolong' : not (can_request_prolongation or can_prolong), }) # put rentals into context context['rows'] = rent_list # if user can change rentals then he can return books template = 'user_rentals_return_allowed.html' if request.user.has_perm('baseapp.change_rental') else 'user_rentals.html' return render_response(request, template, context)
def prolong_rental(request, rental_id, requester, prolongator): ''' Desc: Prolongs rental if possible. Args: request rental_id requester -- User. Owner of reservation and rental (one who is in possesion of book) prolongator ''' prol_logger = utils.get_logger('prolong') prol_logger.info("prolong_rental()....") assert isinstance(rental_id, int) assert isinstance(requester, User) assert isinstance(prolongator, User) r = None try: r = Rental.objects.get(pk=rental_id, reservation__for_whom=requester) except Rental.DoesNotExist: messages.error(request, "Rental wasn't found, unable to prolong.") return prol_logger.info("prolong_rental() preconditions valid, rental present.") days_left_to_enable_prolongation = Config().get_int('min_days_left_to_enable_prolongation') days_left_to_rend = (r.reservation.end_date - utils.today()).days enabled_prolongation = days_left_to_rend <= days_left_to_enable_prolongation max_period = max_prolongation_period(r) can_prolong = enabled_prolongation and max_period > 0 and prolongator.has_perm('baseapp.change_rental') prol_logger.info("prolong_rental() " + ";; rental_id %d " + ";; days_left_to_enable_prolongation %d " + ";; days_left_to_rend %d " + ";; enabled_prolongation %d " + ";; max_period %d " + ";; can_prolong %d " % r.id, days_left_to_enable_prolongation, days_left_to_rend, enabled_prolongation, max_period, can_prolong ) if prolongator not in r.reservation.book_copy.location.get_all_maintainers(): prol_logger.error("prolong_rental() prolongator not a location maintainer.") messages.error(request, "You cannot prolong this book - it's not from location you maintain.") return if not enabled_prolongation: prol_logger.error("prolong_rental() prolongator not enabled.") messages.error(request, "It is TOO EARLY to prolong this rental: please wait for %d days." % (days_left_to_rend - days_left_to_enable_prolongation)) return if max_period <= 0: prol_logger.error("prolong_rental() max_period less then zero.") messages.error(request, "Rental cannot be prolonged - probably someone reserved this book.") return if can_prolong: import datetime as dt old_end_date = r.reservation.end_date new_end_date = r.reservation.end_date + dt.timedelta(max_period) r.reservation.end_date = new_end_date r.reservation.save() prol_logger.info("prolong_rental() prolonged ;; rental_id %d " + ";; old_end_date %s " + ";; new_end_date %s " % r.id, str(old_end_date), str(new_end_date) ) mail.prolongated(r, prolongator, extra={'old_end_date': old_end_date, 'new_end_date': new_end_date, }) messages.info(request, "Prolonged for %d days." % max_period) else: prol_logger.error("prolong_rental() you cannot prolong the rental.") messages.error(request, "You cannot prolong the rental.")