def perform_request_send(uid, msg_to_user="", msg_to_group="", msg_subject="", msg_body="", msg_send_year=0, msg_send_month=0, msg_send_day=0, ln=CFG_SITE_LANG, use_email_address = 0): """ send a message. if unable return warnings to write page @param uid: id of user from (int) @param msg_to_user: comma separated usernames (recipients) (str) @param msg_to_group: comma separated groupnames (recipeints) (str) @param msg_subject: subject of message (str) @param msg_body: body of message (str) @param msg_send_year: send this message on year x (int) @param msg_send_month: send this message on month y (int) @param msg_send_day: send this message on day z (int) @param ln: language @return: (body with warnings, title, navtrail) """ _ = gettext_set_language(ln) def strip_spaces(text): """suppress spaces before and after x (str)""" return text.strip() # wash user input users_to = map(strip_spaces, msg_to_user.split(CFG_WEBMESSAGE_SEPARATOR)) groups_to = map(strip_spaces, msg_to_group.split(CFG_WEBMESSAGE_SEPARATOR)) if users_to == ['']: users_to = [] if groups_to == ['']: groups_to = [] warnings = [] infos = [] problem = None users_to_str = CFG_WEBMESSAGE_SEPARATOR.join(users_to) groups_to_str = CFG_WEBMESSAGE_SEPARATOR.join(groups_to) send_on_date = get_datetext(msg_send_year, msg_send_month, msg_send_day) if (msg_send_year == msg_send_month == msg_send_day == 0): status = CFG_WEBMESSAGE_STATUS_CODE['NEW'] else: status = CFG_WEBMESSAGE_STATUS_CODE['REMINDER'] if send_on_date == datetext_default: warning = \ _("The chosen date (%(x_year)i/%(x_month)i/%(x_day)i) is invalid.") warning = warning % {'x_year': msg_send_year, 'x_month': msg_send_month, 'x_day': msg_send_day} warnings.append(warning) problem = True if not(users_to_str or groups_to_str): # <=> not(users_to_str) AND not(groups_to_str) warnings.append(_("Please enter a user name or a group name.")) problem = True if len(msg_body) > CFG_WEBMESSAGE_MAX_SIZE_OF_MESSAGE: warnings.append(_("Your message is too long, please shorten it. Maximum size allowed is %i characters.") % \ (CFG_WEBMESSAGE_MAX_SIZE_OF_MESSAGE,)) problem = True if use_email_address == 0: users_dict = db.get_uids_from_nicks(users_to) users_to = users_dict.items() # users_to=[(nick, uid),(nick2, uid2)] elif use_email_address == 1: users_dict = db.get_uids_from_emails(users_to) users_to = users_dict.items() # users_to=[(email, uid),(email2, uid2)] groups_dict = db.get_gids_from_groupnames(groups_to) groups_to = groups_dict.items() gids_to = [] for (group_name, group_id) in groups_to: if not(group_id): warnings.append(_("Group %s does not exist.") % \ (escape_html(group_name))) problem = 1 else: gids_to.append(group_id) # Get uids from gids uids_from_group = db.get_uids_members_of_groups(gids_to) # Add the original uids, and make sure there is no double values. tmp_dict = {} for uid_receiver in uids_from_group: tmp_dict[uid_receiver] = None for (user_nick, user_id) in users_to: if user_id: if user_id not in tmp_dict: uids_from_group.append(user_id) tmp_dict[user_id] = None else: if type(user_nick) == int or \ type(user_nick) == str and user_nick.isdigit(): user_nick = int(user_nick) if db.user_exists(user_nick) and user_nick not in tmp_dict: uids_from_group.append(user_nick) tmp_dict[user_nick] = None else: warnings.append(_("User %s does not exist.")% \ (escape_html(user_nick))) problem = True if problem: body = webmessage_templates.tmpl_write(msg_to=users_to_str, msg_to_group=groups_to_str, msg_subject=msg_subject, msg_body=msg_body, msg_send_year=msg_send_year, msg_send_month=msg_send_month, msg_send_day=msg_send_day, warnings=warnings, ln=ln) title = _("Write a message") navtrail = get_navtrail(ln, title) return (body, title, navtrail) else: msg_id = db.create_message(uid, users_to_str, groups_to_str, msg_subject, msg_body, send_on_date) uid_problem = db.send_message(uids_from_group, msg_id, status) if len(uid_problem) > 0: usernames_problem_dict = db.get_nicks_from_uids(uid_problem) usernames_problem = usernames_problem_dict.values() def listing(name1, name2): """ name1, name2 => 'name1, name2' """ return str(name1) + ", " + str(name2) warning = _("Your message could not be sent to the following recipients as it would exceed their quotas:") + " " warnings.append(warning + reduce(listing, usernames_problem)) if len(uids_from_group) != len(uid_problem): infos.append(_("Your message has been sent.")) else: db.check_if_need_to_delete_message_permanently([msg_id]) body = perform_request_display(uid, warnings, infos, ln) title = _("Your Messages") return (body, title, get_navtrail(ln))
def perform_request_send(uid, msg_to_user="", msg_to_group="", msg_subject="", msg_body="", msg_send_year=0, msg_send_month=0, msg_send_day=0, ln=CFG_SITE_LANG, use_email_address=0): """ send a message. if unable return warnings to write page @param uid: id of user from (int) @param msg_to_user: comma separated usernames (recipients) (str) @param msg_to_group: comma separated groupnames (recipeints) (str) @param msg_subject: subject of message (str) @param msg_body: body of message (str) @param msg_send_year: send this message on year x (int) @param msg_send_month: send this message on month y (int) @param msg_send_day: send this message on day z (int) @param ln: language @return: (body with warnings, title, navtrail) """ _ = gettext_set_language(ln) def strip_spaces(text): """suppress spaces before and after x (str)""" return text.strip() # wash user input users_to = map(strip_spaces, msg_to_user.split(CFG_WEBMESSAGE_SEPARATOR)) groups_to = map(strip_spaces, msg_to_group.split(CFG_WEBMESSAGE_SEPARATOR)) if users_to == ['']: users_to = [] if groups_to == ['']: groups_to = [] warnings = [] infos = [] problem = None users_to_str = CFG_WEBMESSAGE_SEPARATOR.join(users_to) groups_to_str = CFG_WEBMESSAGE_SEPARATOR.join(groups_to) send_on_date = get_datetext(msg_send_year, msg_send_month, msg_send_day) if (msg_send_year == msg_send_month == msg_send_day == 0): status = CFG_WEBMESSAGE_STATUS_CODE['NEW'] else: status = CFG_WEBMESSAGE_STATUS_CODE['REMINDER'] if send_on_date == datetext_default: warning = \ _("The chosen date (%(x_year)i/%(x_month)i/%(x_day)i) is invalid.") warning = warning % { 'x_year': msg_send_year, 'x_month': msg_send_month, 'x_day': msg_send_day } warnings.append(warning) problem = True if not (users_to_str or groups_to_str): # <=> not(users_to_str) AND not(groups_to_str) warnings.append(_("Please enter a user name or a group name.")) problem = True if len(msg_body) > CFG_WEBMESSAGE_MAX_SIZE_OF_MESSAGE: warnings.append(_("Your message is too long, please edit it. Maximum size allowed is %i characters.") % \ (CFG_WEBMESSAGE_MAX_SIZE_OF_MESSAGE,)) problem = True if use_email_address == 0: users_dict = db.get_uids_from_nicks(users_to) users_to = users_dict.items() # users_to=[(nick, uid),(nick2, uid2)] elif use_email_address == 1: users_dict = db.get_uids_from_emails(users_to) users_to = users_dict.items() # users_to=[(email, uid),(email2, uid2)] groups_dict = db.get_gids_from_groupnames(groups_to) groups_to = groups_dict.items() gids_to = [] for (group_name, group_id) in groups_to: if not (group_id): warnings.append(_("Group %s does not exist.") % \ (escape_html(group_name))) problem = 1 else: gids_to.append(group_id) # Get uids from gids uids_from_group = db.get_uids_members_of_groups(gids_to) # Add the original uids, and make sure there is no double values. tmp_dict = {} for uid_receiver in uids_from_group: tmp_dict[uid_receiver] = None for (user_nick, user_id) in users_to: if user_id: if user_id not in tmp_dict: uids_from_group.append(user_id) tmp_dict[user_id] = None else: if type(user_nick) == int or \ type(user_nick) == str and user_nick.isdigit(): user_nick = int(user_nick) if db.user_exists(user_nick) and user_nick not in tmp_dict: uids_from_group.append(user_nick) tmp_dict[user_nick] = None else: warnings.append(_("User %s does not exist.")% \ (escape_html(user_nick))) problem = True if problem: body = webmessage_templates.tmpl_write(msg_to=users_to_str, msg_to_group=groups_to_str, msg_subject=msg_subject, msg_body=msg_body, msg_send_year=msg_send_year, msg_send_month=msg_send_month, msg_send_day=msg_send_day, warnings=warnings, ln=ln) title = _("Write a message") navtrail = get_navtrail(ln, title) return (body, title, navtrail) else: msg_id = db.create_message(uid, users_to_str, groups_to_str, msg_subject, msg_body, send_on_date) uid_problem = db.send_message(uids_from_group, msg_id, status) if len(uid_problem) > 0: usernames_problem_dict = db.get_nicks_from_uids(uid_problem) usernames_problem = usernames_problem_dict.values() def listing(name1, name2): """ name1, name2 => 'name1, name2' """ return str(name1) + ", " + str(name2) warning = _( "Your message could not be sent to the following recipients due to their quota:" ) + " " warnings.append(warning + reduce(listing, usernames_problem)) if len(uids_from_group) != len(uid_problem): infos.append(_("Your message has been sent.")) else: db.check_if_need_to_delete_message_permanently([msg_id]) body = perform_request_display(uid, warnings, infos, ln) title = _("Your Messages") return (body, title, get_navtrail(ln))
def perform_borrower_loans(uid, barcode, borrower_id, request_id, ln=CFG_SITE_LANG): """ Display all the loans and the requests of a given borrower. @param barcode: identify the item. Primary key of crcITEM. @type barcode: string @param borrower_id: identify the borrower. Primary key of crcBORROWER. @type borrower_id: int @param request_id: identify the request: Primary key of crcLOANREQUEST @type request_id: int @return body(html) """ infos = [] is_borrower = db.is_borrower(db.get_invenio_user_email(uid)) loans = db.get_borrower_loans(is_borrower) requests = db.get_borrower_requests(is_borrower) tmp_date = datetime.date.today() + datetime.timedelta(days=30) new_due_date = get_datetext(tmp_date.year, tmp_date.month, tmp_date.day) #renew loan if barcode: recid = db.get_id_bibrec(barcode) queue = db.get_queue_request(recid) if len(queue) != 0: infos.append("It is not possible to renew your loan for " \ "<strong>" + book_title_from_MARC(recid) + "</strong>. Another user " \ "is waiting for this book.") else: db.update_due_date(barcode, new_due_date) infos.append("Your loan has been renewed with sucess.") #cancel request if request_id: db.cancel_request(request_id, 'cancelled') make_copy_available(request_id) #renew all loans elif borrower_id: list_of_recids = db.get_borrower_recids(borrower_id) for (recid) in list_of_recids: queue = db.get_queue_request(recid[0]) #check if there are requests if len(queue) != 0: infos.append("It is not possible to renew your loan for " \ "<strong>" + book_title_from_MARC(recid) + "</strong>. Another user" \ " is waiting for this book.") else: db.update_due_date_borrower(borrower_id, new_due_date) infos.append("All loans have been renewed with success.") body = bibcirculation_templates.tmpl_yourloans(loans=loans, requests=requests, borrower_id=is_borrower, infos=infos, ln=ln) return body
def perform_new_request_send( uid, recid, from_year, from_month, from_day, to_year, to_month, to_day, barcode, ln=CFG_SITE_LANG ): """ @param recid: recID - CDS Invenio record identifier @param ln: language of the page """ request_from = get_datetext(from_year, from_month, from_day) request_to = get_datetext(to_year, to_month, to_day) nb_requests = db.get_number_requests_per_copy(barcode) is_on_loan = db.is_item_on_loan(barcode) if nb_requests == 0 and is_on_loan is not None: status = "waiting" elif nb_requests == 0 and is_on_loan is None: status = "pending" else: status = "waiting" user = collect_user_info(uid) is_borrower = db.is_borrower(user["email"]) if is_borrower != 0: address = db.get_borrower_address(user["email"]) if address != 0: db.new_hold_request(is_borrower, recid, barcode, request_from, request_to, status) db.update_item_status("requested", barcode) send_email( fromaddr=CFG_BIBCIRCULATION_LIBRARIAN_EMAIL, toaddr=CFG_SITE_SUPPORT_EMAIL, subject="Hold request for books confirmation", content=hold_request_mail(recid, is_borrower), attempt_times=1, attempt_sleeptime=10, ) if CFG_CERN_SITE == 1: message = bibcirculation_templates.tmpl_message_request_send_ok_cern(ln=ln) else: message = bibcirculation_templates.tmpl_message_request_send_ok_other(ln=ln) else: if CFG_CERN_SITE == 1: email = user["email"] result = get_user_info_from_ldap(email) try: ldap_address = result["physicalDeliveryOfficeName"][0] except KeyError: ldap_address = None if ldap_address is not None: db.add_borrower_address(ldap_address, email) db.new_hold_request(is_borrower, recid, barcode, request_from, request_to, status) db.update_item_status("requested", barcode) send_email( fromaddr=CFG_BIBCIRCULATION_LIBRARIAN_EMAIL, toaddr=CFG_SITE_SUPPORT_EMAIL, subject="Hold request for books confirmation", content=hold_request_mail(recid, is_borrower), attempt_times=1, attempt_sleeptime=10, ) message = bibcirculation_templates.tmpl_message_request_send_ok_cern(ln=ln) else: message = bibcirculation_templates.tmpl_message_request_send_fail_cern(ln=ln) else: message = bibcirculation_templates.tmpl_message_request_send_fail_other(ln=ln) else: if CFG_CERN_SITE == 1: result = get_user_info_from_ldap(email=user["email"]) try: name = result["cn"][0] except KeyError: name = None try: email = result["mail"][0] except KeyError: email = None try: phone = result["telephoneNumber"][0] except KeyError: phone = None try: address = result["physicalDeliveryOfficeName"][0] except KeyError: address = None try: mailbox = result["postOfficeBox"][0] except KeyError: mailbox = None if address is not None: db.new_borrower(name, email, phone, address, mailbox, "") is_borrower = db.is_borrower(email) db.new_hold_request(is_borrower, recid, barcode, request_from, request_to, status) db.update_item_status("requested", barcode) send_email( fromaddr=CFG_BIBCIRCULATION_LIBRARIAN_EMAIL, toaddr=CFG_SITE_SUPPORT_EMAIL, subject="Hold request for books confirmation", content=hold_request_mail(recid, is_borrower), attempt_times=1, attempt_sleeptime=10, ) message = bibcirculation_templates.tmpl_message_request_send_ok_cern(ln=ln) else: message = bibcirculation_templates.tmpl_message_request_send_fail_cern(ln=ln) else: message = bibcirculation_templates.tmpl_message_request_send_ok_other(ln=ln) body = bibcirculation_templates.tmpl_new_request_send(message=message, ln=ln) return body