def daily(self): email = self.request.get('email') days = int(self.request.get('days')) user = User(email=email) settings = Settings.singletonForUser(user) timeZoneDelta = datetime.timedelta(hours=settings.timeZoneOffset) utcDate = datetime.datetime.today() #fixme might be incorrect utcDateTrimmed = datetime.datetime.combine(utcDate, datetime.time(0, 0)) fromDate = utcDateTrimmed - datetime.timedelta( days=days) - timeZoneDelta toDate = utcDateTrimmed - datetime.timedelta(days=days - 1) - timeZoneDelta event_reports.SendEmailDailyReport(user, email, fromDate, toDate)
def SendEmailDailyReport(currentUser, email, fromDate, toDate): #todo refactor cursor_inDateRange = None cursor_outDateRange = None queryEndInDateRange = None queryStartInDateRange = None try: queryEndInDateRange = Event.query( Event.actor == currentUser, (ndb.AND(Event.endTime >= fromDate, Event.endTime < toDate)), ).order(Event.endTime) queryStartInDateRange = Event.query( Event.actor == currentUser, Event.startTime >= fromDate ).order(-Event.startTime) # the only event could be started and not completed in range with max time # ndb.AND(Event.endTime < toDate, # Event.endTime > toDate) except: logging.info('error cant be prepared %s', sys.exc_info()[0]) pass if cursor_inDateRange: queryEndInDateRange.with_cursor(cursor_inDateRange) if cursor_outDateRange: queryStartInDateRange.with_cursor(cursor_outDateRange) settings = Settings.singletonForUser(currentUser) inRangeItems = queryEndInDateRange.fetch(BATCH_SIZE) outRangeItems = queryStartInDateRange.fetch(1) # the only event could be started and not completed in range with max time if (len(outRangeItems) > 0): outRangeItem = outRangeItems[0] if ((outRangeItem.startTime < toDate) and (outRangeItem.endTime > toDate)): outRangeItem.endTime = toDate inRangeItems.append(outRangeItem) preFormatedItems = map(lambda (event): itemToPreFormattedItem(event, settings, fromDate, toDate), inRangeItems) summary = {} markedTime = datetime.timedelta(seconds=0) for item in preFormatedItems: if item.activityCode in summary: summary[item.activityCode] += item.timeSpan else: summary[item.activityCode] = item.timeSpan markedTime += item.timeSpan totalDelta = (toDate - fromDate) htmlOut = StringIO.StringIO() htmlOut.write("<h2>Summary:</h2>") htmlOut.write("<table border=\"1\">") noneTimeDelta = toDate - fromDate - markedTime summary['None'] = noneTimeDelta for (sumKey, sumDelta) in summary.iteritems(): markedTime += sumDelta percent = math.floor(sumDelta.total_seconds() / totalDelta.total_seconds() * 100) htmlOut.write("<tr><td>%s</td><td>%d%%</td><td>%s</td></tr>" % (sumKey, percent, formatTimeDelta(sumDelta))) htmlOut.write("</table>") htmlOut.write("<br/>") htmlOut.write("<h2>Details:</h2>") htmlOut.write("<table border=\"1\">") html_items = map(activityToHtmlRow, preFormatedItems) cvs_items = map(activityToCvs, preFormatedItems) htmlOut.write("\n\r".join(html_items)) cvsOut = StringIO.StringIO() cvsOut.write("\n\r".join(cvs_items)) htmlOut.write("</table>") charOut = StringIO.StringIO() charOut.write(PIE_CHART_TEMPLATE_HEADER) charDataItems = map( summaryItemToDataItemRow , summary.iteritems()) charOut.write("\n\r,".join(charDataItems)) charOut.write(PIE_CHART_TEMPLATE_FOOTHDER) mail.send_mail(sender="*****@*****.**", to=email, subject="Time report: %s - %s" % (fromDate.strftime("%Y-%m-%d"), toDate.strftime("%Y-%m-%d")), body='see html email', html=htmlOut.getvalue(), attachments=[('report.csv', cvsOut.getvalue()), ('chart.html', charOut.getvalue()) ]) htmlOut.close() cvsOut.close() charOut.close() pass
def SendEmailDailyReport(currentUser, email, fromDate, toDate): #todo refactor cursor_inDateRange = None cursor_outDateRange = None queryEndInDateRange = None queryStartInDateRange = None try: queryEndInDateRange = Event.query( Event.actor == currentUser, (ndb.AND(Event.endTime >= fromDate, Event.endTime < toDate)), ).order(Event.endTime) queryStartInDateRange = Event.query( Event.actor == currentUser, Event.startTime >= fromDate ).order( -Event.startTime ) # the only event could be started and not completed in range with max time # ndb.AND(Event.endTime < toDate, # Event.endTime > toDate) except: logging.info('error cant be prepared %s', sys.exc_info()[0]) pass if cursor_inDateRange: queryEndInDateRange.with_cursor(cursor_inDateRange) if cursor_outDateRange: queryStartInDateRange.with_cursor(cursor_outDateRange) settings = Settings.singletonForUser(currentUser) inRangeItems = queryEndInDateRange.fetch(BATCH_SIZE) outRangeItems = queryStartInDateRange.fetch( 1 ) # the only event could be started and not completed in range with max time if (len(outRangeItems) > 0): outRangeItem = outRangeItems[0] if ((outRangeItem.startTime < toDate) and (outRangeItem.endTime > toDate)): outRangeItem.endTime = toDate inRangeItems.append(outRangeItem) preFormatedItems = map( lambda (event): itemToPreFormattedItem(event, settings, fromDate, toDate), inRangeItems) summary = {} markedTime = datetime.timedelta(seconds=0) for item in preFormatedItems: if item.activityCode in summary: summary[item.activityCode] += item.timeSpan else: summary[item.activityCode] = item.timeSpan markedTime += item.timeSpan totalDelta = (toDate - fromDate) htmlOut = StringIO.StringIO() htmlOut.write("<h2>Summary:</h2>") htmlOut.write("<table border=\"1\">") noneTimeDelta = toDate - fromDate - markedTime summary['None'] = noneTimeDelta for (sumKey, sumDelta) in summary.iteritems(): markedTime += sumDelta percent = math.floor(sumDelta.total_seconds() / totalDelta.total_seconds() * 100) htmlOut.write("<tr><td>%s</td><td>%d%%</td><td>%s</td></tr>" % (sumKey, percent, formatTimeDelta(sumDelta))) htmlOut.write("</table>") htmlOut.write("<br/>") htmlOut.write("<h2>Details:</h2>") htmlOut.write("<table border=\"1\">") html_items = map(activityToHtmlRow, preFormatedItems) cvs_items = map(activityToCvs, preFormatedItems) htmlOut.write("\n\r".join(html_items)) cvsOut = StringIO.StringIO() cvsOut.write("\n\r".join(cvs_items)) htmlOut.write("</table>") charOut = StringIO.StringIO() charOut.write(PIE_CHART_TEMPLATE_HEADER) charDataItems = map(summaryItemToDataItemRow, summary.iteritems()) charOut.write("\n\r,".join(charDataItems)) charOut.write(PIE_CHART_TEMPLATE_FOOTHDER) mail.send_mail( sender="*****@*****.**", to=email, subject="Time report: %s - %s" % (fromDate.strftime("%Y-%m-%d"), toDate.strftime("%Y-%m-%d")), body='see html email', html=htmlOut.getvalue(), attachments=[('report.csv', cvsOut.getvalue()), ('chart.html', charOut.getvalue())]) htmlOut.close() cvsOut.close() charOut.close() pass