def test_is_tracking_active_record(self): record = TimeRecord(project=self.subject, user=self.user) record.start_time = datetime.now(tz=timezone.utc) - timedelta(hours=1) record.save() self.subject.timerecord_set.add(record) self.subject.save() self.assertIs(True, self.subject.is_tracking(self.user))
def test_last_updated_only_start(self): record = TimeRecord(project=self.subject, user=self.user) record.start_time = datetime.now(tz=timezone.utc) - timedelta(hours=1) record.save() self.subject.timerecord_set.add(record) self.subject.save() self.assertEqual('Never updated', self.subject.last_updated())
def test_round_time(self): actual = datetime(day=10, month=2, year=2019, hour=10, minute=21) self.assertEqual( datetime(day=10, month=2, year=2019, hour=10, minute=30), TimeRecord.round_time(actual, timedelta(minutes=30))) self.assertEqual( datetime(day=10, month=2, year=2019, hour=10, minute=15), TimeRecord.round_time(actual, timedelta(minutes=15))) self.assertEqual( datetime(day=10, month=2, year=2019, hour=10, minute=20), TimeRecord.round_time(actual, timedelta(minutes=10))) self.assertEqual( datetime(day=10, month=2, year=2019, hour=10, minute=20), TimeRecord.round_time(actual, timedelta(minutes=5))) actual = datetime(day=10, month=2, year=2019, hour=10, minute=53) self.assertEqual( datetime(day=10, month=2, year=2019, hour=11, minute=00), TimeRecord.round_time(actual, timedelta(minutes=30))) self.assertEqual( datetime(day=10, month=2, year=2019, hour=11, minute=00), TimeRecord.round_time(actual, timedelta(minutes=15))) self.assertEqual( datetime(day=10, month=2, year=2019, hour=10, minute=50), TimeRecord.round_time(actual, timedelta(minutes=10))) self.assertEqual( datetime(day=10, month=2, year=2019, hour=10, minute=55), TimeRecord.round_time(actual, timedelta(minutes=5)))
def start(request, organization, project_id): project = get_object_or_404(Project, id=project_id) setting, _ = Setting.objects.get_or_create(user=request.user) if not project.is_member(request.user): return HttpResponseForbidden() if request.user.is_tracking() and not setting.allow_parallel_tracking: for entry in request.user.get_tracking_records(): entry.end_time = datetime.now().replace(second=0, microsecond=0) entry.save() entry = TimeRecord(project_id=project_id, user=request.user) entry.start_time = datetime.now().replace(second=0, microsecond=0) entry.save() target = request.GET.get('from', 'tracker:project/timetable') return redirect(target, organization, project_id)
def test_last_updated(self): record = TimeRecord(project=self.subject, user=self.user) record.start_time = datetime.now(tz=timezone.utc) - timedelta(hours=1) record.end_time = datetime.now(tz=timezone.utc) record.save() self.subject.timerecord_set.add(record) self.subject.save() self.assertEqual('Last updated a moment ago', self.subject.last_updated())
def timetable(request, organization, project_id): organization = get_object_or_404(Organization, name=organization) project = get_object_or_404(Project, id=project_id) setting, _ = Setting.objects.get_or_create(user=request.user) timezone = pytz.timezone(str(setting.timezone)) if not project.is_member(request.user): return HttpResponseForbidden() tl_activate(setting.locale) tz_activate(timezone) time_records = TimeRecordTable(project.timerecord_set.all(), request=request) request.session['timetable.sort'] = request.GET.get( 'sort') or request.session.get('timetable.sort') time_records.order_by = request.session.get( 'timetable.sort') or '-end_time' RequestConfig(request, paginate={'per_page': 15}).configure(time_records) current_time = TimeRecord.round_time( datetime.now(timezone), timedelta(minutes=setting.timestamp_rounding)) form_add_record = TimeRecordForm( initial={"start_time": current_time.strftime('%Y-%m-%dT%H:%M')}) form_edit_record = TimeRecordForm( initial={"end_time": current_time.strftime('%Y-%m-%dT%H:%M')}) step_in_seconds = setting.timestamp_rounding * 60 form_add_record.fields['start_time'].widget.attrs.update( step=step_in_seconds) form_add_record.fields['end_time'].widget.attrs.update( step=step_in_seconds) form_edit_record.fields['start_time'].widget.attrs.update( step=step_in_seconds) form_edit_record.fields['end_time'].widget.attrs.update( step=step_in_seconds) context = dict(organization=organization, project=project, time_records=time_records, form_add_record=form_add_record, form_edit_record=form_edit_record) return render(request, 'tracker/timetable.html', context)
def stop(request, organization, project_id): project = get_object_or_404(Project, id=project_id) if not project.is_member(request.user): return HttpResponseForbidden() setting, _ = Setting.objects.get_or_create(user=request.user) current_time = TimeRecord.round_time( datetime.now().replace(second=0, microsecond=0), timedelta(minutes=setting.timestamp_rounding)) entry = get_object_or_404(TimeRecord, user=request.user, project_id=project_id, end_time=None) entry.end_time = current_time entry.save() target = request.GET.get('from', 'tracker:project/timetable') return redirect(target, organization, project_id)
def split(request, organization, project_id, record_id): entry = get_object_or_404(TimeRecord, id=record_id) if not entry.user == request.user: return HttpResponseForbidden() entry2 = TimeRecord(user=entry.user, project=entry.project) entry2.end_time = entry.end_time duration = (entry.end_time - entry.start_time) // 2 entry.end_time = entry.end_time - duration entry.end_time.replace(second=0) entry2.start_time = entry.end_time entry.save() entry2.save() return redirect('tracker:project/timetable', organization=organization, project_id=project_id)
def create(request, organization, project_id): organization = get_object_or_404(Organization, name=organization) project = get_object_or_404(Project, id=project_id) setting, _ = Setting.objects.get_or_create(user=request.user) timezone = pytz.timezone(str(setting.timezone)) if not project.is_member(request.user): return HttpResponseForbidden() entry = TimeRecord(project=project, user=request.user) form = TimeRecordForm(request.POST) start_time = datetime.strptime(form.data['start_time'], "%Y-%m-%dT%H:%M") entry.start_time = timezone.localize(start_time, is_dst=None) if form.data['end_time']: end_time = datetime.strptime(form.data['end_time'], "%Y-%m-%dT%H:%M") entry.end_time = timezone.localize(end_time, is_dst=None) entry.save() return redirect('tracker:project/timetable', organization=organization, project_id=project_id)
def test_duration(self): subject = TimeRecord() subject.start_time = datetime(2000, 1, 1, 8, 0, 0) subject.end_time = datetime(2000, 1, 1, 9, 0, 0) self.assertEqual(timedelta(hours=1), subject.duration())
def test_duration_incomplete(self): subject = TimeRecord() subject.start_time = datetime(2000, 1, 1, 8, 0, 0) self.assertEqual(timedelta(0, 0), subject.duration())