def download_all_holiday_data(request, who=None): '''Endpoint which creates a CSV file for all holiday data for a single employee. :param who: The ID of the person the report should be generated for if the user is not in the span of control for the administrator then no report will be generated.''' if not who: raise Http404 auth_user = Tbluser.objects.get(id=request.session.get("user_id")) try: target_user = auth_user.get_subordinates().get(id=who) except Tbluser.DoesNotExist: raise Http404 buf = StringIO() buf.write("\xef\xbb\xbf") csvfile = UnicodeWriter(buf) csvfile.writerow(TrackingEntry.headings()) for entry in TrackingEntry.objects.filter(user_id=who): csvfile.writerow(entry.display_as_csv()) response = HttpResponse(buf.getvalue(), mimetype="text/csv") response['Content-Disposition'] = \ 'attachment;filename=AllHolidayData_%s.csv' % target_user.id return response
def download_all_holiday_data(request, who=None): # pragma: no cover '''Endpoint which creates a CSV file for all holiday data for a single employee. :param who: The ID of the person the report should be generated for if the user is not in the span of control for the administrator then no report will be generated.''' if not who: raise Http404 auth_user = Tbluser.objects.get(id=request.session.get("user_id")) try: target_user = auth_user.get_subordinates().get(id=who) except Tbluser.DoesNotExist: raise Http404 buf = StringIO() buf.write("\xef\xbb\xbf") csvfile = UnicodeWriter(buf) csvfile.writerow(TrackingEntry.headings()) for entry in TrackingEntry.objects.filter(user_id=who): csvfile.writerow(entry.display_as_csv()) response = HttpResponse(buf.getvalue(), mimetype="text/csv") response['Content-Disposition'] = \ 'attachment;filename=AllHolidayData_%s.csv' % target_user.id return response
def holidays_for_yearmonth(request, year=None): '''Endpoint which creates a CSV file for all holidays per month in a year :param year: Year for the report.''' if not year: raise Http404 auth_user = Tbluser.objects.get(id=request.session.get("user_id")) buf = StringIO() buf.write("\xef\xbb\xbf") csvfile = UnicodeWriter(buf) csvfile.writerow( ["Name"] + [MONTH_MAP[n][1] for n in range(0,12)] + ["Used", "Remaining"] ) for user in auth_user.get_subordinates(): row = [user.name()] total = 0 for month in range(1,13): e = TrackingEntry.objects.filter(user_id=user.id, entry_date__year=year, entry_date__month=month, daytype="HOLIS").count() total += e row.append(e) row.append(["%d" % total, "%d" % user.holiday_balance]) csvfile.writerow(row) response = HttpResponse(buf.getvalue(), mimetype="text/csv") response['Content-Disposition'] = \ 'attachment;filename=Holidays_for_year%s.csv' % year return response
def holidays_for_yearmonth(request, year=None): # pragma: no cover '''Endpoint which creates a CSV file for all holidays per month in a year :param year: Year for the report.''' if not year: raise Http404 auth_user = Tbluser.objects.get(id=request.session.get("user_id")) buf = StringIO() buf.write("\xef\xbb\xbf") csvfile = UnicodeWriter(buf) csvfile.writerow( ["Name"] + [MONTH_MAP[n][1] for n in range(0,12)] + ["Used", "Remaining"] ) for user in auth_user.get_subordinates(): row = [user.name()] total = 0 for month in range(1,13): e = TrackingEntry.objects.filter(user_id=user.id, entry_date__year=year, entry_date__month=month, daytype="HOLIS").count() total += e row.append(e) row.append(["%d" % total, "%d" % user.holiday_balance]) csvfile.writerow(row) response = HttpResponse(buf.getvalue(), mimetype="text/csv") response['Content-Disposition'] = \ 'attachment;filename=Holidays_for_year%s.csv' % year return response
def all_team(request, year=None, month=None, team=None): # pragma: no cover if not year or not month or not team: raise Http404 buf = StringIO() buf.write("\xef\xbb\xbf") csvfile = UnicodeWriter(buf) csvfile.writerow(TrackingEntry.headings()) for user in Tbluser.objects.filter(market=team): for entry in TrackingEntry.objects.filter( entry_date__year=year, entry_date__month=month, user_id=user.id): csvfile.writerow(entry.display_as_csv()) response = HttpResponse(buf.getvalue(), mimetype="text/csv") response['Content-Disposition'] = \ 'attachment;filename=AllHolidayData_%s_%s_%s.csv' % (year, month, team) return response
def ot_by_year(request, year=None): '''Endpoint which creates a CSV file for all OT in a year. :param year: The year for the report.''' auth_user = Tbluser.objects.get(id=request.session.get("user_id")) buf = StringIO() buf.write("\xef\xbb\xbf") csvfile = UnicodeWriter(buf) csvfile.writerow( ["Name", "Team"] + [MONTH_MAP[n][1] for n in range(0,12)] ) total_balance = 0 balances = { n: 0 for n in range(1, 13) } for user in auth_user.get_subordinates(): row = [user.name(), user.process] for month in range(1, 13): balance = user.get_total_balance(ret='flo', year=year, month=month) balances[month] += balance row.append("%.2f" % balance if balance != 0.0 else "-") csvfile.writerow(row) totalrow = ["Total", "Total"] [totalrow.append("%.2f" % balances[n]) for n in range(1,13)] csvfile.writerow(totalrow) response = HttpResponse(buf.getvalue(), mimetype="text/csv") response['Content-Disposition'] = \ 'attachment;filename=OT_By_Year_%s_%s.csv' % (year, month) return response
def ot_by_year(request, year=None): # pragma: no cover '''Endpoint which creates a CSV file for all OT in a year. :param year: The year for the report.''' auth_user = Tbluser.objects.get(id=request.session.get("user_id")) buf = StringIO() buf.write("\xef\xbb\xbf") csvfile = UnicodeWriter(buf) csvfile.writerow( ["Name", "Team"] + [MONTH_MAP[n][1] for n in range(0,12)] ) total_balance = 0 balances = { n: 0 for n in range(1, 13) } for user in auth_user.get_subordinates(): row = [user.name(), user.process] for month in range(1, 13): balance = user.get_total_balance(ret='flo', year=year, month=month) balances[month] += balance row.append('="%.2f"' % balance if balance != 0.0 else "-") csvfile.writerow(row) totalrow = ["Total", "Total"] [totalrow.append('="%.2f"' % balances[n]) for n in range(1,13)] csvfile.writerow(totalrow) response = HttpResponse(buf.getvalue(), mimetype="text/csv") response['Content-Disposition'] = \ 'attachment;filename=OT_By_Year_%s_%s.csv' % (year, month) return response
def yearmonthhol(request, year=None, month=None): '''Endpoint which creates a CSV file for all holiday data within a specific month. :param year: The year for the report. :param month: The month for the report. :note: Both year and mont are required.''' auth_user = Tbluser.objects.get(id=request.session.get("user_id")) buf = StringIO() buf.write("\xef\xbb\xbf") csvfile = UnicodeWriter(buf) csvfile.writerow(TrackingEntry.headings()) for user in auth_user.get_subordinates(): for entry in TrackingEntry.objects.filter( entry_date__year=year, entry_date__month=month, user_id=user.id): csvfile.writerow(entry.display_as_csv()) response = HttpResponse(buf.getvalue(), mimetype="text/csv") response['Content-Disposition'] = \ 'attachment;filename=HolidayData_%s_%s.csv' % (year, month) return response
def yearmonthhol(request, year=None, month=None): # pragma: no cover '''Endpoint which creates a CSV file for all holiday data within a specific month. :param year: The year for the report. :param month: The month for the report. :note: Both year and mont are required.''' auth_user = Tbluser.objects.get(id=request.session.get("user_id")) buf = StringIO() buf.write("\xef\xbb\xbf") csvfile = UnicodeWriter(buf) csvfile.writerow(TrackingEntry.headings()) for user in auth_user.get_subordinates(): for entry in TrackingEntry.objects.filter( entry_date__year=year, entry_date__month=month, user_id=user.id): csvfile.writerow(entry.display_as_csv()) response = HttpResponse(buf.getvalue(), mimetype="text/csv") response['Content-Disposition'] = \ 'attachment;filename=HolidayData_%s_%s.csv' % (year, month) return response
def ot_by_month(request, year=None, month=None): '''Endpoint which creates a CSV file for all OT in a given month :param year: The year for the report. :param month: The month for the report. :note: Both year and mont are required.''' auth_user = Tbluser.objects.get(id=request.session.get("user_id")) buf = StringIO() buf.write("\xef\xbb\xbf") csvfile = UnicodeWriter(buf) csvfile.writerow( ["Name", "Team", MONTH_MAP[int(month)-1][1]] ) total_balance = 0 for user in auth_user.get_subordinates(): balance = user.get_total_balance(ret='flo', year=year, month=month) total_balance += balance csvfile.writerow([user.name(), user.process, "%.2f" % balance]) csvfile.writerow(["Total", "Total", "%.2f" % total_balance]) response = HttpResponse(buf.getvalue(), mimetype="text/csv") response['Content-Disposition'] = \ 'attachment;filename=OT_By_Month_%s_%s.csv' % (year, month) return response
def ot_by_month(request, year=None, month=None): # pragma: no cover '''Endpoint which creates a CSV file for all OT in a given month :param year: The year for the report. :param month: The month for the report. :note: Both year and mont are required.''' auth_user = Tbluser.objects.get(id=request.session.get("user_id")) buf = StringIO() buf.write("\xef\xbb\xbf") csvfile = UnicodeWriter(buf) csvfile.writerow( ["Name", "Team", MONTH_MAP[int(month)-1][1]] ) total_balance = 0 for user in auth_user.get_subordinates(): balance = user.get_total_balance(ret='flo', year=year, month=month) total_balance += balance csvfile.writerow([user.name(), user.process, '="%.2f"' % balance]) csvfile.writerow(["Total", "Total", '="%.2f"' % total_balance]) response = HttpResponse(buf.getvalue(), mimetype="text/csv") response['Content-Disposition'] = \ 'attachment;filename=OT_By_Month_%s_%s.csv' % (year, month) return response
def report_for_account(account, now, send=True): '''Sends all overtime reports to the managers of an account for a given date.''' DAYS_SHORT = [element[0] for element in WORKING_CHOICES if element[0] != "SATUR"] + ["ROVER"] buff = StringIO() buff.write("\xef\xbb\xbf") csvout = UnicodeWriter(buff, delimiter=';') users = Tbluser.objects.filter(market=account, disabled=False).order_by("lastname") # generate the dates for this month c = calendar.Calendar() dates = filter( lambda date: date.month == now.month, c.itermonthdates(now.year, now.month) ) csvout.writerow( # write out the top heading ["Date"] + [user.rev_name() for user in users] ) csvout.writerow( # write out the settlement period row. ["Settlement Period"] + [user.job_code[-1] if user.job_code else "" for user in users] ) csvout.writerow( # write out the e-mail row. ["EmployeeID"] + [user.user_id for user in users] ) csvout.writerow( # write out the total balances. ["Balance"] + ['="'+str(user.get_total_balance(ret='flo', year=now.year,month=now.month))+'"' for user in users] ) for date in dates: current_line = [str(date)] for user in users: try: entry = TrackingEntry.objects.get( user_id=user.id, entry_date=date ) except TrackingEntry.DoesNotExist: current_line.append("") continue if entry.daytype not in DAYS_SHORT: current_line.append("") continue if entry.is_linked(): current_line.append("") continue # if the entry is a return for overtime entry, we display # the user's shiftlength as a negative value since that's # what the value should be. value = round_down(entry.time_difference() if entry.daytype != "ROVER" else -entry.total_working_time()) current_line.append('="'+str(value)+'"' if value != 0 else "") csvout.writerow(current_line) csvfile = buff.getvalue() if send: message = mail.EmailMessage(from_email="*****@*****.**") message.body = \ "Hi,\n\n" \ "Please see attached your month end overtime report for " \ "your team.\n\n" \ "If there are any errors, please inform the administrator " \ "of the timetracker immediately.\n\n" \ "Regards,\n" \ "Timetracker team" message.attach( "overtimereport.csv", csvfile, "application/octet-stream" ) message.to = ["*****@*****.**"] + \ Tbluser.administrator_emails_for_account(account) message.subject = "End of month Overtime Totals." message.send() else: response = HttpResponse(csvfile, mimetype="text/csv") response['Content-Disposition'] = \ 'attachment;filename=MEC_OT_Report_%s.csv' % now return response
def report_for_account(account, now, send=True): '''Sends all overtime reports to the managers of an account for a given date.''' DAYS_SHORT = [ element[0] for element in WORKING_CHOICES if element[0] != "SATUR" ] + ["ROVER"] buff = StringIO() buff.write("\xef\xbb\xbf") csvout = UnicodeWriter(buff, delimiter=';') users = Tbluser.objects.filter(market=account, disabled=False).order_by("lastname") # generate the dates for this month c = calendar.Calendar() dates = filter(lambda date: date.month == now.month, c.itermonthdates(now.year, now.month)) csvout.writerow( # write out the top heading ["Date"] + [user.rev_name() for user in users]) csvout.writerow( # write out the settlement period row. ["Settlement Period"] + [user.job_code[-1] if user.job_code else "" for user in users]) csvout.writerow( # write out the e-mail row. ["EmployeeID"] + [user.user_id for user in users]) csvout.writerow( # write out the total balances. ["Balance"] + [ '="' + str( user.get_total_balance( ret='flo', year=now.year, month=now.month)) + '"' for user in users ]) for date in dates: current_line = [str(date)] for user in users: try: entry = TrackingEntry.objects.get(user_id=user.id, entry_date=date) except TrackingEntry.DoesNotExist: current_line.append("") continue if entry.daytype not in DAYS_SHORT: current_line.append("") continue if entry.is_linked(): current_line.append("") continue # if the entry is a return for overtime entry, we display # the user's shiftlength as a negative value since that's # what the value should be. value = round_down(entry.time_difference( ) if entry.daytype != "ROVER" else -entry.total_working_time()) current_line.append('="' + str(value) + '"' if value != 0 else "") csvout.writerow(current_line) csvfile = buff.getvalue() if send: message = mail.EmailMessage(from_email="*****@*****.**") message.body = \ "Hi,\n\n" \ "Please see attached your month end overtime report for " \ "your team.\n\n" \ "If there are any errors, please inform the administrator " \ "of the timetracker immediately.\n\n" \ "Regards,\n" \ "Timetracker team" message.attach("overtimereport.csv", csvfile, "application/octet-stream") message.to = ["*****@*****.**"] + \ Tbluser.administrator_emails_for_account(account) message.subject = "End of month Overtime Totals." message.send() else: response = HttpResponse(csvfile, mimetype="text/csv") response['Content-Disposition'] = \ 'attachment;filename=MEC_OT_Report_%s.csv' % now return response
def send_report_for_account(account, now): '''Sends all overtime reports to the managers of an account for a given date.''' message = mail.EmailMessage(from_email="*****@*****.**") message.body = \ "Hi,\n\n" \ "Please see attached your month end overtime report for " \ "your team.\n\n" \ "If there are any errors, please inform the administrator " \ "of the timetracker immediately.\n\n" \ "Regards,\n" \ "Timetracker team" buff = StringIO() buff.write("\xef\xbb\xbf") csvout = UnicodeWriter(buff, delimiter=';') users = filter( lambda user: user.get_total_balance(ret='num') == 0, Tbluser.objects.filter(market=account, disabled=False) ) # generate the dates for this month c = calendar.Calendar() dates = filter( lambda date: date.month == now.month, c.itermonthdates(now.year, now.month) ) csvout.writerow( # write out the top heading ["Date"] + [user.rev_name() for user in users] ) csvout.writerow( # write out the settlement period row. ["Settlement Period"] + [user.job_code[-1] if user.job_code else "" for user in users] ) csvout.writerow( # write out the e-mail row. ["EmployeeID"] + [user.user_id for user in users] ) csvout.writerow( # write out the total balances. ["Balance"] + [str(user.get_total_balance(ret='num')) for user in users] ) for date in dates: current_line = [str(date)] for user in users: try: entry = TrackingEntry.objects.get(user_id=user.id, entry_date=date) except TrackingEntry.DoesNotExist: current_line.append("") continue if entry.is_overtime(): current_line.append(0-entry.time_difference()) else: current_line.append("") csvout.writerow(current_line) csvfile = buff.getvalue() message.attach( "overtimereport.csv", csvfile, "application/octet-stream" ) message.to = ["*****@*****.**"] + \ Tbluser.administrator_emails_for_account(account) message.subject = "End of month Overtime Totals." message.send()