def monthly_time_statistics_csv(request, year, month): year = int(year) if year else None month = int(month) if month else None if year and month: rows = [] rows.append([u"#Datum", u"Präsenzzeit Start", u"Präsenzzeit Ende", u"Total Pause", u"Total Präsenzzeit"]) current_date = date(year, month, 1) one_day = timedelta(days = 1) while current_date.month == month: presence_start = get_presence_start(request.user, current_date) presence_end = get_presence_end(request.user, current_date) if presence_start and presence_end: presence_duration = presence_end - presence_start else: presence_duration = timedelta() presence_date = presence_start.replace(hour = 0, minute = 0, second = 0, microsecond = 0) if presence_start is not None else None days_total = get_days_total(request.user, presence_date) if presence_date else timedelta() days_breaks = presence_duration - days_total if days_total else timedelta() current_date_as_string = djangoDate(current_date, "d.m.Y") presence_start_as_string = djangoDate(presence_start, "H:i") if presence_start else "00:00" presence_end_as_string = djangoDate(presence_end, "H:i") if presence_end else "00:00" days_total_as_string = duration(days_total, "%H:%m") days_breaks_as_string = duration(days_breaks, "%H:%m") rows.append([unicode(current_date_as_string), unicode(presence_start_as_string), unicode(presence_end_as_string), unicode(days_breaks_as_string), unicode(days_total_as_string)]) # if we're looking at a sunday, insert two empty lines afterwards. if current_date.isoweekday() == 7: rows.append([]) rows.append([]) # go to the next day. current_date = current_date + one_day # Return CSV to the browser lines = [u",".join(row) for row in rows] response = HttpResponse(u"\n".join(lines), content_type = "text/csv") currentYearAndMonth = datetime(int(year), int(month), 1) csv_filename = "Arbeitszeitstatistik " + djangoDate(currentYearAndMonth, "F") + " " + djangoDate(currentYearAndMonth, "Y") + ".csv" response["Content-Disposition"] = helpers.createContentDispositionAttachmentString(csv_filename, request) return response else: return HttpResponseNotFound("Page not found.")
def monthly_time_statistics_csv(request, year, month): year = int(year) if year else None month = int(month) if month else None if year and month: rows = [] rows.append([ u"#Datum", u"Präsenzzeit Start", u"Präsenzzeit Ende", u"Total Pause", u"Total Präsenzzeit" ]) current_date = date(year, month, 1) one_day = timedelta(days=1) while current_date.month == month: presence_start = get_presence_start(request.user, current_date) presence_end = get_presence_end(request.user, current_date) if presence_start and presence_end: presence_duration = presence_end - presence_start else: presence_duration = timedelta() presence_date = presence_start.replace( hour=0, minute=0, second=0, microsecond=0) if presence_start is not None else None days_total = get_days_total( request.user, presence_date) if presence_date else timedelta() days_breaks = presence_duration - days_total if days_total else timedelta( ) current_date_as_string = djangoDate(current_date, "d.m.Y") presence_start_as_string = djangoDate( presence_start, "H:i") if presence_start else "00:00" presence_end_as_string = djangoDate( presence_end, "H:i") if presence_end else "00:00" days_total_as_string = duration(days_total, "%H:%m") days_breaks_as_string = duration(days_breaks, "%H:%m") rows.append([ unicode(current_date_as_string), unicode(presence_start_as_string), unicode(presence_end_as_string), unicode(days_breaks_as_string), unicode(days_total_as_string) ]) # if we're looking at a sunday, insert two empty lines afterwards. if current_date.isoweekday() == 7: rows.append([]) rows.append([]) # go to the next day. current_date = current_date + one_day # Return CSV to the browser lines = [u",".join(row) for row in rows] response = HttpResponse(u"\n".join(lines), content_type="text/csv") currentYearAndMonth = datetime(int(year), int(month), 1) csv_filename = "Arbeitszeitstatistik " + djangoDate( currentYearAndMonth, "F") + " " + djangoDate( currentYearAndMonth, "Y") + ".csv" response[ "Content-Disposition"] = helpers.createContentDispositionAttachmentString( csv_filename, request) return response else: return HttpResponseNotFound("Page not found.")
def clock(request): # POST request if request.method == "POST": # if we change the comment or create a new entry with the new comment if "comment" in request.POST and not "command" in request.POST: # We're only changing the comment if request.POST["commentmode"] == "changecomment": topTaskEntry = helpers.getCurrentTaskEntry(request.user) if topTaskEntry != None: topTaskEntry.comment = request.POST["comment"] topTaskEntry.save() # we're creating a new entry with the new comment elif request.POST["commentmode"] == "newentrywithcomment": topTaskEntry = helpers.getCurrentTaskEntry(request.user) if topTaskEntry: helpers.pause(request.user) helpers.resume(request.user, topTaskEntry.customer, request.POST["comment"]) # if we pause/resume a task if "command" in request.POST: # If there's a comment, read it from the request if "comment" in request.POST: comment = request.POST["comment"] else: comment = "" # pause command if request.POST["command"] == COMMAND_PAUSE: helpers.pause(request.user) # pause and immediately resume command elif request.POST["command"] == COMMAND_PAUSE_AND_RESUME: helpers.iTookABreak(request.user, int(request.POST["duration"]), comment) # resume command elif request.POST["command"] == COMMAND_RESUME: customer = Customer.objects.get(pk=request.POST["customer"]) if "delay" in request.POST: helpers.resume(request.user, customer, comment, int(request.POST["delay"])) else: helpers.resume(request.user, customer, comment) # replay command (re-use customer and comment of an already # existing entry) elif request.POST["command"] == COMMAND_REPLAY: if "entry" in request.POST: entry_id = int(request.POST["entry"]) entry = Entry.objects.get(id=entry_id) # a little bit of safety if entry.owner == request.user: helpers.resume(request.user, entry.customer, entry.comment) if helpers.isAnyTaskRunning(request.user): state = STATE_RUNNING else: state = STATE_PAUSED customers = Customer.objects.all().order_by("name") entries = Entry.objects.filter(owner=request.user).order_by("-start") currentCustomer = helpers.getCurrentCustomer(request.user) if not currentCustomer: currentCustomer = helpers.getDefaultCustomer() topTaskEntry = helpers.getCurrentTaskEntry(request.user) # calculate presence time presence_start = get_presence_start(request.user) presence_end = get_presence_end(request.user) if presence_start and presence_end: presence_duration = presence_end - presence_start else: presence_duration = timedelta() presence_date = presence_start.replace( hour=0, minute=0, second=0, microsecond=0) if presence_start is not None else None # calculate working time days_total = get_days_total( request.user, presence_date) if presence_date else timedelta() # calculate time spent on breaks days_breaks = presence_duration - days_total if days_total else timedelta() # calculate daily totals if len(entries) == 1: entry = entries[0] if entry.end: daily_total = entry.end - entry.start else: daily_total = datetime.now() - entry.start setattr(entry, "daily_total", daily_total) elif len(entries) > 1: previous_entry = None daily_total = timedelta() for index, entry in enumerate(entries): if previous_entry: if previous_entry.start.day != entry.start.day or previous_entry.start.month != entry.start.month or previous_entry.start.year != entry.start.year: setattr(entries[index - 1], "daily_total", daily_total) daily_total = timedelta() if entry.end: daily_total = daily_total + (entry.end - entry.start) else: daily_total = daily_total + (datetime.now() - entry.start) previous_entry = entry setattr(entries[len(entries) - 1], "daily_total", daily_total) return render( request, "timesheet/clock.html", { "state": state, "customers": customers, "currentCustomer": currentCustomer, "entries": entries, "topTaskEntry": topTaskEntry, "serverTime": datetime.now(), "user": request.user, "presence_date": presence_date, "presence_start": presence_start, "presence_end": presence_end, "presence_duration": presence_duration, "days_total": days_total, "days_breaks": days_breaks })
def clock(request): # POST request if request.method == "POST": # if we change the comment or create a new entry with the new comment if "comment" in request.POST and not "command" in request.POST: # We're only changing the comment if request.POST["commentmode"] == "changecomment": topTaskEntry = helpers.getCurrentTaskEntry(request.user) if topTaskEntry != None: topTaskEntry.comment = request.POST["comment"] topTaskEntry.save() # we're creating a new entry with the new comment elif request.POST["commentmode"] == "newentrywithcomment": topTaskEntry = helpers.getCurrentTaskEntry(request.user) if topTaskEntry: helpers.pause(request.user) helpers.resume(request.user, topTaskEntry.customer, request.POST["comment"]) # if we pause/resume a task if "command" in request.POST: # If there's a comment, read it from the request if "comment" in request.POST: comment = request.POST["comment"] else: comment = "" # pause command if request.POST["command"] == COMMAND_PAUSE: helpers.pause(request.user) # pause and immediately resume command elif request.POST["command"] == COMMAND_PAUSE_AND_RESUME: helpers.iTookABreak(request.user, int(request.POST["duration"]), comment) # resume command elif request.POST["command"] == COMMAND_RESUME: customer = Customer.objects.get(pk = request.POST["customer"]) if "delay" in request.POST: helpers.resume(request.user, customer, comment, int(request.POST["delay"])) else: helpers.resume(request.user, customer, comment) # replay command (re-use customer and comment of an already # existing entry) elif request.POST["command"] == COMMAND_REPLAY: if "entry" in request.POST: entry_id = int(request.POST["entry"]) entry = Entry.objects.get(id = entry_id) # a little bit of safety if entry.owner == request.user: helpers.resume(request.user, entry.customer, entry.comment) if helpers.isAnyTaskRunning(request.user): state = STATE_RUNNING else: state = STATE_PAUSED customers = Customer.objects.all().order_by("name") entries = Entry.objects.filter(owner = request.user).order_by("-start") currentCustomer = helpers.getCurrentCustomer(request.user) if not currentCustomer: currentCustomer = helpers.getDefaultCustomer() topTaskEntry = helpers.getCurrentTaskEntry(request.user) # calculate presence time presence_start = get_presence_start(request.user) presence_end = get_presence_end(request.user) if presence_start and presence_end: presence_duration = presence_end - presence_start else: presence_duration = timedelta() presence_date = presence_start.replace(hour = 0, minute = 0, second = 0, microsecond = 0) if presence_start is not None else None # calculate working time days_total = get_days_total(request.user, presence_date) if presence_date else timedelta() # calculate time spent on breaks days_breaks = presence_duration - days_total if days_total else timedelta() # calculate daily totals if len(entries) == 1: entry = entries[0] if entry.end: daily_total = entry.end - entry.start else: daily_total = datetime.now() - entry.start setattr(entry, "daily_total", daily_total) elif len(entries) > 1: previous_entry = None daily_total = timedelta() for index, entry in enumerate(entries): if previous_entry: if previous_entry.start.day != entry.start.day or previous_entry.start.month != entry.start.month or previous_entry.start.year != entry.start.year: setattr(entries[index - 1], "daily_total", daily_total) daily_total = timedelta() if entry.end: daily_total = daily_total + (entry.end - entry.start) else: daily_total = daily_total + (datetime.now() - entry.start) previous_entry = entry setattr(entries[len(entries) - 1], "daily_total", daily_total) return render(request, "timesheet/clock.html", {"state": state, "customers": customers, "currentCustomer": currentCustomer, "entries": entries, "topTaskEntry": topTaskEntry, "serverTime": datetime.now(), "user": request.user, "presence_date": presence_date, "presence_start": presence_start, "presence_end": presence_end, "presence_duration": presence_duration, "days_total": days_total, "days_breaks": days_breaks})