Skip to content
This repository has been archived by the owner on Jul 30, 2022. It is now read-only.

Commit

Permalink
Merge branch 'one-email-per-item'
Browse files Browse the repository at this point in the history
  • Loading branch information
mhucka committed Nov 20, 2019
2 parents 77c9126 + f061d30 commit 01a6ccf
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 26 deletions.
6 changes: 6 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
Change log for Lost It!
=======================

Version 1.5.0
-------------

* At the request of Donna W., send one message per item lost instead of sending the entire list of lost items in one email message.


Version 1.4.5
-------------

Expand Down
2 changes: 1 addition & 1 deletion codemeta.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"codeRepository": "https://github.com/caltechlibrary/lostit",
"issueTracker": "https://github.com/caltechlibrary/lostit/issues",
"license": "https://github.com/caltechlibrary/lostit/blob/master/LICENSE",
"version": "1.4.4",
"version": "1.5.0",
"author": [
{
"@type": "Person",
Expand Down
44 changes: 20 additions & 24 deletions lostit/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,15 +184,15 @@ def run(self):

# Send mail, if requested.
if send_mail and num_new > 0:
tracer.update('Sending mail')
subject = 'Lost It! reports {} lost item{}'.format(
num_new, 's' if num_new > 1 else '')
body = email_body(new_records, google.spreadsheet_url(sid))
sender = accesser.user + '@caltech.edu'
password = accesser.password
mailer = Mailer(mail_server, mail_port)
mailer.send(sender, password, recipients, subject, body)

tracer.update('Sending email for each lost item')
for num, record in enumerate(new_records, 1):
subject = 'Lost It! "{}"'.format(record.item_title)
body = email_body(record, google.spreadsheet_url(sid))
sender = accesser.user + '@caltech.edu'
password = accesser.password
mailer = Mailer(mail_server, mail_port)
mailer.send(sender, password, recipients, subject, body)
tracer.update('Sent {}'.format(num))
except (KeyboardInterrupt, UserCancelled) as err:
tracer.stop('Quitting.')
controller.stop()
Expand All @@ -210,24 +210,20 @@ def run(self):
tracer.stop('Stopping due to error')
controller.stop()
else:
tracer.stop('Done')
tracer.update('Done. You can quit when ready.')
controller.stop()


# Miscellaneous utilities.
# .............................................................................

def email_body(records, sheet_url):
def email_body(record, sheet_url):
# Helper function
def value(field):
return '-- none --' if field == '' else field

if __debug__: log('formatting email body')
summary = ''
num_records = len(records)
joke = random_pun()
for rec in records:
summary += '''
summary = '''
Title: {}
Author: {}
Call #: {}
Expand All @@ -238,20 +234,20 @@ def value(field):
Requester email: {}
Patron type: {}
'''.format(value(rec.item_title), value(rec.item_author),
value(rec.item_call_number), value(rec.item_barcode),
value(rec.item_location_code), value(rec.item_location_name),
value(rec.requester_name), value(rec.requester_email),
value(rec.requester_type))
'''.format(value(record.item_title), value(record.item_author),
value(record.item_call_number), value(record.item_barcode),
value(record.item_location_code), value(record.item_location_name),
value(record.requester_name), value(record.requester_email),
value(record.requester_type))
joke = random_pun()
return '''
Lost It! was just run and it discovered {} new lost item{} recorded in TIND:
Lost It! was just run and it discovered a new lost item recorded in TIND:
{}
Here is the URL for the spreadsheet of lost items:
{}
{}
'''.format(num_records, 's' if num_records > 1 else '', summary, sheet_url,
"---\nAnd here is a random pun:\n" + joke if joke else '')
'''.format(summary, sheet_url, "---\nAnd here is a random pun:\n" + joke if joke else '')


def random_pun():
Expand Down
2 changes: 1 addition & 1 deletion lostit/__version__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# @website https://github.com/caltechlibrary/lostit
# =============================================================================

__version__ = '1.4.5'
__version__ = '1.5.0'
__title__ = 'lostit'
__name__ = 'Lost It!'
__description__ = '''A program for Caltech Library's Circulation team to easily report lost items.'''
Expand Down
10 changes: 10 additions & 0 deletions lostit/email.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
'''

from email.message import EmailMessage
from ratelimit import limits, sleep_and_retry
from smtplib import SMTP
import ssl

Expand All @@ -25,6 +26,12 @@

# Exported classes.
# .............................................................................
# As of 2019-11-20, Caltech uses office365 as its mail server, and Microsoft's
# documentation states that the server allows a max of 30 msgs/minute:
# https://docs.microsoft.com/en-us/office365/servicedescriptions/exchange-online-service-description/exchange-online-limits#receiving-and-sending-limits
#
# Archived version of the web page above:
# https://web.archive.org/web/20191120212839/https://docs.microsoft.com/en-us/office365/servicedescriptions/exchange-online-service-description/exchange-online-limits?redirectedfrom=MSDN

class Mailer():
'''Simple interface for Lost It! to send mail.'''
Expand All @@ -35,6 +42,8 @@ def __init__(self, server, port):
self._smtp_port = port


@sleep_and_retry
@limits(calls = 30, period = 60)
def send(self, sender, password, recipients, subject, body):
'''Send a message with the given subject and body.'''

Expand All @@ -54,4 +63,5 @@ def send(self, sender, password, recipients, subject, body):
smtp.login(sender, password)
smtp.sendmail(sender, recipients, msg.as_string())
except Exception as ex:
import pdb; pdb.set_trace()
raise ServiceFailure(str(ex))
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ nameparser>=1.0.3
oauth2client>=4.1.3
plac>=1.0.0
Pypubsub>=4.0.0
ratelimit>=2.2.1
requests>=2.21.0
setuptools>=41.0.1
termcolor>=1.1.0
Expand Down

0 comments on commit 01a6ccf

Please sign in to comment.