def hours_json(request): if not request.user.is_authenticated(): # XXX improve this return {'error': 'Not logged in'} entry_id = request.GET.get('entry') if not entry_id: return {'error': 'No entry pre-loaded'} entry = get_object_or_404(Entry, pk=entry_id) if entry.user != request.user: return http.HttpResponseForbidden("Not your entry") days = [] for date in get_weekday_dates(entry.start, entry.end): key = date.strftime('d-%Y%m%d') try: hours_ = Hours.objects.get( date=date, entry__user=entry.user, hours__gt=0, ) #initial[date.strftime('d-%Y%m%d')] = hours_.hours value = hours_.hours except Hours.DoesNotExist: #initial[date.strftime('d-%Y%m%d')] = settings.WORK_DAY value = settings.WORK_DAY days.append({ 'key': key, 'value': value, 'full_day': date.strftime(settings.DEFAULT_DATE_FORMAT) }) return days
def _create_hours(self, total, start, end): r = {} #print ((total / 8), (end - start).days) if total / 8 == (end - start).days: # easy d = start while d < end: r[d] = 8 d += datetime.timedelta(days=1) else: # this is only going to work if the total is a multiple of 4 if int(total) % 4: return r total = int(total) #print range(total / 4) #print "total", total #print "start", start #print "end", end # spread em' dates = list(get_weekday_dates(start, end)) for d in dates: r[d] = 0 i = 0 while total > 0: d = dates[i % len(dates)] r[d] += 4 total -= 4 i += 1 print r #raise NotImplementedError return r
def hours_json(request): if not request.user.is_authenticated(): # XXX improve this return {'error': 'Not logged in'} entry_id = request.GET.get('entry') if not entry_id: return {'error': 'No entry pre-loaded'} entry = get_object_or_404(Entry, pk=entry_id) if entry.user != request.user: return http.HttpResponseForbidden("Not your entry") days = [] for date in get_weekday_dates(entry.start, entry.end): key = date.strftime('d-%Y%m%d') try: hours_ = Hours.objects.get( date=date, entry__user=entry.user, hours__gt=0, ) #initial[date.strftime('d-%Y%m%d')] = hours_.hours value = hours_.hours except Hours.DoesNotExist: #initial[date.strftime('d-%Y%m%d')] = settings.WORK_DAY value = settings.WORK_DAY days.append({'key': key, 'value': value, 'full_day': date.strftime(settings.DEFAULT_DATE_FORMAT)}) return days
def test_get_weekday_dates(self): from dates.utils import get_weekday_dates d1 = datetime.date(2018, 1, 1) # a Monday d2 = datetime.date(2018, 1, 9) # next Tuesday dates = list(get_weekday_dates(d1, d2)) eq_(dates[0].strftime('%A'), 'Monday') eq_(dates[1].strftime('%A'), 'Tuesday') eq_(dates[2].strftime('%A'), 'Wednesday') eq_(dates[3].strftime('%A'), 'Thursday') eq_(dates[4].strftime('%A'), 'Friday') eq_(dates[5].strftime('%A'), 'Monday') eq_(dates[6].strftime('%A'), 'Tuesday')