def test_break_start(self): event = Event(event='IN', user=self.user, time=timezone.now()) event.save() Shift(start=event, user=self.user).save() response = self.client.post('/shifts', {'event': 'BST'}) self.assertEqual(response.status_code, 200) self.assertIsNotNone(response.context['last_shift']) self.assertIsNotNone(response.context['last_event']) self.assertEqual(response.context['last_event'].event, 'BST')
def do(self): now = datetime.datetime.now() users = User.objects.exclude(profile__auto_clock_out__isnull = False, profile__auto_clock_out__gte = now.time()) for user in users: clock_out_event = Event( time = datetime.datetime.combine(now.date, user.profile.auto_clock_out) event = "OUT" user = user ) clock_out_event.save() last_shift = Shifts.objects.filter(user = user, end = None) last_shift.end = clock_out_event last_shift.save()
def handle(self, *args, **options): now = datetime.datetime.now() users = User.objects.exclude(profile__auto_clock_out__isnull=True) users = users.filter(profile__auto_clock_out__lte=now.time()) for user in users: clock_out_event = Event( time=datetime.datetime.combine(now.date, user.profile.auto_clock_out), event="OUT", user=user, ) clock_out_event.save() last_shift = Shifts.objects.filter(user=user, end=None) last_shift.end = clock_out_event last_shift.save()
def test_second_clock_in(self): start = Event(event='IN', user=self.user, time=timezone.now()) start.save() shift = Shift(start=start, user=self.user) shift.save() end = Event(event='OUT', user=self.user, time=timezone.now()) end.save() shift.end = end shift.save() response = self.client.post('/shifts', {'event': 'IN'}) self.assertEqual(response.status_code, 200) self.assertIsNotNone(response.context['last_shift']) self.assertIsNotNone(response.context['last_event']) self.assertNotEqual(response.context['last_shift'], shift) self.assertIsInstance(response.context['last_shift'], Shift) self.assertEqual(response.context['last_event'].event, 'IN')
def test_post_task(self): event = Event(event='IN', user=self.user, time=timezone.now()) event.save() Shift(start=event, user=self.user).save() response = self.client.post('/shifts', { 'event': 'task', 'desc': 'Test' }) self.assertEqual(response.status_code, 200) self.assertIsNotNone(response.context['last_shift']) self.assertIn('tasks_completed', response.context) self.assertEqual(response.context['tasks_completed'], ['Test']) response = self.client.post('/shifts', { 'event': 'task', 'desc': 'Test2' }) self.assertEqual(response.status_code, 200) self.assertIsNotNone(response.context['last_shift']) self.assertIn('tasks_completed', response.context) self.assertEqual(response.context['tasks_completed'], ['Test', 'Test2'])
def handle(self, *args, **options): try: if(settings.DISABLE_CLOCK_CHECK == False): raise CommandError('DISABLE_CLOCK_CHECK must be true for this command to work properly!') except: raise CommandError('DISABLE_CLOCK_CHECK must be true for this command to work properly!') start_month, start_day, start_year = options['start_day'][0].split('/') start_hour, start_minute = options['start_time'][0].split(':') start = datetime.datetime( year = int(start_year), month = int(start_month), day = int(start_day), hour = int(start_hour), minute = int(start_minute)) start = timezone.make_aware(start) user = None try: user = User.objects.get(username = options['username'][0]) except User.DoesNotExist: raise CommandError('User, %s, does not exist.'% options['username'][0]) self.stdout.write("Checking for conflicting events...") end = start + datetime.timedelta(hours = options['num_hours'][0]) if len(Event.objects.filter(user = user, time__gte = start, time__lte = end )) != 0: raise CommandError('User has event between the start and end of pesudo shift.') start_event = Event(time = start, event = 'IN', user = user) start_event.save() end_event = Event(time = end, event = 'OUT', user = user) end_event.save() shift = Shift(user = user, start = start_event, end = end_event, tasks_completed = '`This shift was manully added via the command line`') shift.save()
def shifts(request): last_shift = Shift.objects.filter(user=request.user).last() last_event = Event.objects.filter(user=request.user).last() if request.method == 'POST': event = request.POST.get("event") newtime = request.POST.get("newtime") if event == "task" and last_shift != None: tasks = last_shift.tasks_completed task = bleach.clean(request.POST['desc']).replace('`', '\'') if tasks == "": last_shift.tasks_completed = '`%s`' % task else: last_shift.tasks_completed = '%s,`%s`' % (tasks, task) last_shift.save() elif event == "update" and newtime != None: newtime_dt = datetime.datetime.strptime(newtime, '%Y-%m-%dT%H:%M:%S') other_events = Event.objects.filter(user=request.user, time__gt=newtime_dt) if len(other_events) > 1: messages.error( request, "Updated time cannot be earlier than any previous event.") elif newtime_dt > datetime.datetime.now(): messages.error(request, "Updated time cannot be in the future.") else: last_event.time = newtime last_event.save() messages.info(request, "Last event time modified.") elif event in Event.REQUIRED_EVENT.keys(): last_event = Event(time=timezone.now(), event=event, user=request.user) last_event.save() if event == "IN": last_shift = Shift(user=request.user, start=last_event, tasks_completed="") elif event == "OUT": last_shift.end = last_event last_shift.save() context = { "last_shift": last_shift, "last_event": last_event, "lastest_events": Event.objects.filter(user=request.user), "event_choices": Event.EVENTS, "possible_events": Event.REQUIRED_EVENT[last_event.event if last_event != None else None] } if last_shift != None: context['tasks_completed'] = last_shift.tasks_completed[1:-1].split( '`,`') return render(request, 'shifts.html', context)