def send_privacy_report():
    # retrieve person objects
    msgt('(1) retrieving Person objects')
    lst = Person.objects.filter(visible=True)
    msg('number found: %s' % lst.count())
    
    # filter out objects without privacy information
    msgt('(2) Filtering people without privacy info')    
    no_info = filter(lambda x: x.harvardpersoninfo_set.count()==0, lst)
    no_info = filter(lambda x: not x.email == '*****@*****.**', no_info)
    
    msg('people w/o privacy info: %s' % len(no_info))

    # contruct an email
    msgt('(3) Preparing email with results')
    
    admin_emails = get_admin_email_addresses()    
    from_email = admin_emails[0]
    to_addresses = admin_emails
    
    for email in DirectoryNotificationEmail.objects.all().values_list('email', flat=True):
        to_addresses.append(email)
    
    if len(no_info) == 0:
        subject = 'Personnel Privacy Data: Looks Good!' 
        mail_msg = """Every person has a privacy record. (%s)""" % get_datetime_now()
    else:
        subject = 'Personnel Privacy Data: %s people do not have info' % len(no_info)
        
        person_lst = '\n\n'.join(map(lambda x: '- %s - %s'  % (x, \
'https://webapps.sciences.fas.harvard.edu/mcb/mcb-control-panel/person/person/%s/' % x.id), no_info ))
        
        mail_msg = """The following people lack privacy records: 
        
%s

(time %s)""" % (person_lst,   get_datetime_now() )
    
    msg('from: %s' % from_email)
    msg('to_addresses: %s' % to_addresses)
    msg('mail_msg: %s' % mail_msg)

    msgt('(4) Sending email with results')
    
    send_message(from_email,\
                to_addresses, \
                subject=subject,\
                text_content=mail_msg,\
                html_content=mail_msg.replace('\n', '<br />'))
        
    msg('Email sent!')
    msgt('Done')
def send_privacy_report():
    # contruct an email
    msgt("(3) Preparing email with results")

    admin_emails = get_admin_email_addresses()

    from_email = admin_emails[0]
    to_addresses = admin_emails
    if len(no_info) == 0:
        subject = "Personnel Privacy Data: Looks Good!"
        mail_msg = """Every person has a privacy record. (%s)""" % get_datetime_now()
    else:
        subject = "Personnel Privacy Data: %s people do not have info" % len(no_info)

        person_lst = "\n\n".join(
            map(
                lambda x: "- %s - %s"
                % (x, "https://webapps.sciences.fas.harvard.edu/mcb/mcb-control-panel/person/person/%s/" % x.id),
                no_info,
            )
        )

        mail_msg = """The following people lack privacy records: 
        
%s

(time %s)""" % (
            person_lst,
            get_datetime_now(),
        )

    msg("from: %s" % from_email)
    msg("to_addresses: %s" % to_addresses)
    msg("mail_msg: %s" % mail_msg)

    msgt("(4) Sending email with results")

    send_message(
        from_email, to_addresses, subject=subject, text_content=mail_msg, html_content=mail_msg.replace("\n", "<br />")
    )

    msg("Email sent!")
    msgt("Done")
Пример #3
0
def view_directory_excel_file(request, mcb_lab_personnel_only=False):
    """From the Django admin view of a Person->Lab object, generate an Excel spreadsheet"""
    if not (request.user.is_authenticated() and request.user.is_staff):  
        return Http404('404 - Page Not Found')
    
    sheet_title = slugify('MCB People')
    
    if mcb_lab_personnel_only:
        sheet_title = slugify('MCB Lab Personnel')
        people = Person.objects.select_related('office', 'building', 'primary_lab', 'title'\
                , 'appointment', 'affiliation',  'secondary_labs', 'grad_program'\
                , 'grad_year'\
                ).filter( Q(primary_lab__affiliation__id__exact=MCB_AFFILIATION_ID) \
                        |Q(primary_lab__name=ROTATION_LAB_NAME)\
                ).order_by('primary_lab', 'lname', 'fname')
        
    elif request.method == 'GET':  
        kwargs = {}
        for x,y in request.GET.iteritems():
            if not str(x) in ['ot', 'o', 'q']:
                kwargs.update({str(x):str(y)})
        #print kwargs
        try:
            people = Person.objects.select_related('office', 'building', 'primary_lab', 'title', 'appointment', 'affiliation',  'secondary_labs', 'grad_program', 'grad_year').filter(**kwargs).order_by('lname','fname')
        except:
            people = Person.objects.select_related('office', 'building', 'primary_lab', 'title', 'appointment', 'affiliation',  'secondary_labs', 'grad_program', 'grad_year').all().order_by('lname','fname')    
    else:
        people = Person.objects.select_related('office', 'building', 'primary_lab', 'title', 'appointment', 'affiliation', 'secondary_labs', 'grad_program', 'grad_year').filter(visible=True).order_by('lname', 'fname')
   
    if people.count() == 0:
        return HttpResponse('Sorry!  No people were found for this list.<br /><br />Please press the back button on your browser.')
    
    book = xlwt.Workbook(encoding="utf-8")
    # With a workbook object made we can now add some sheets.
    sheet1 = book.add_sheet(sheet_title)

    date_obj = get_datetime_now()
    info_line = "Generated on %s" % (date_obj.strftime('%m/%d/%Y - %I:%M %p'))

    sheet1 = make_person_roster(sheet1, info_line, people)

    # create response object
    response = HttpResponse(mimetype='application/ms-excel')
    response['Content-Disposition'] = 'attachment; filename=mcb_listing_%s.xls' % ( date_obj.strftime('%m%d-%I-%M%p-%S').lower())
    
    # send .xls spreadsheet to response stream
    book.save(response)
    return response
    def send_update_report(self):

        msgt('Preparing email with report')
        admin_emails = get_admin_email_addresses()    
        # add susan, kim, and natacha to email notice
        for additional_email in '[email protected] [email protected] [email protected]'.split():
            admin_emails.append(additional_email)
        
        from_email = admin_emails[0]
        to_addresses = admin_emails
        
        if len(self.msg_lines) == 0:
            subject = 'Privacy Data Updates: Looks Good! %s records checked' % self.num_records_updated 
            mail_msg = """Existing privacy records updated.\n\n%s records checked.  No errors.\n\n(%s)""" % (self.num_records_updated,get_datetime_now())
        else:
            subject = 'Privacy Data Updates: %s error notes' % len(self.msg_lines)
            mail_msg = """%s records checked. The following records had error messages: 

    %s

    (time %s)""" % (self.num_records_updated, '\n\n'.join(self.msg_lines),   get_datetime_now() )

        mail_msg += """
        
Note: This is a weekly check to ensure that people in the MCB directory are still in the HU's central directory.  
        
The possible reasons for a person failing the check are:
        
(1) The person has left the University
(2) The last name of the person has changed
"""

        msg('from: %s' % from_email)
        msg('to_addresses: %s' % to_addresses)
        msg('mail_msg: %s' % mail_msg)

        msgt('(4) Sending email with results')

        send_message(from_email,\
               to_addresses, \
               subject=subject,\
               text_content=mail_msg,\
               html_content=mail_msg.replace('\n', '<br />'))

        msg('Email sent!')
        msgt('Done')