def __init__(self, entry, *args, **kwargs): super(HoursForm, self).__init__(*args, **kwargs) self.entry = entry for date in utils.get_weekday_dates(self.entry.start, self.entry.end): field_name = date.strftime("d-%Y%m%d") try: hours_ = Hours.objects.get(date=date, entry__user=entry.user) help_text = "Already logged %d hours on this day" % hours_.hours except Hours.DoesNotExist: help_text = "" hours_ = None choices = [] choices.append((settings.WORK_DAY, "Full day (%sh)" % settings.WORK_DAY)) choices.append((settings.WORK_DAY / 2, "Half day (%sh)" % (settings.WORK_DAY / 2))) choices.append((-1, "Birthday")) if hours_: choices.append((0, "0 hrs")) self.fields[field_name] = forms.fields.ChoiceField( tuple(choices), required=True, label=date.strftime(settings.DEFAULT_DATE_FORMAT), widget=forms.widgets.RadioSelect(attrs={"class": "hours"}), help_text=help_text, )
def __init__(self, entry, *args, **kwargs): super(HoursForm, self).__init__(*args, **kwargs) self.entry = entry for date in utils.get_weekday_dates(self.entry.start, self.entry.end): field_name = date.strftime('d-%Y%m%d') try: hours_ = Hours.objects.get(date=date, entry__user=entry.user) help_text = ('Already logged %d hours on this day' % hours_.hours) except Hours.DoesNotExist: help_text = '' hours_ = None choices = [] choices.append( (settings.WORK_DAY, 'Full day (%sh)' % settings.WORK_DAY)) choices.append((settings.WORK_DAY / 2, 'Half day (%sh)' % (settings.WORK_DAY / 2))) choices.append((-1, 'Birthday')) if hours_: choices.append((0, '0 hrs')) self.fields[field_name] = forms.fields.ChoiceField( tuple(choices), required=True, label=date.strftime(settings.DEFAULT_DATE_FORMAT), widget=forms.widgets.RadioSelect(attrs={'class': 'hours'}), help_text=help_text, )
def save_entry_hours(entry, form): assert form.is_valid() total_hours = 0 for date in utils.get_weekday_dates(entry.start, entry.end): hours = int(form.cleaned_data[date.strftime('d-%Y%m%d')]) birthday = False if hours == -1: birthday = True hours = 0 assert hours >= 0 and hours <= settings.WORK_DAY, hours try: hours_ = Hours.objects.get(entry__user=entry.user, date=date) if hours_.hours: # this nullifies the previous entry on this date reverse_entry = Entry.objects.create( user=hours_.entry.user, start=date, end=date, details=hours_.entry.details, total_hours=hours_.hours * -1, ) Hours.objects.create( entry=reverse_entry, hours=hours_.hours * -1, date=date, ) #hours_.hours = hours # nasty stuff! #hours_.birthday = birthday #hours_.save() except Hours.DoesNotExist: # nothing to credit pass Hours.objects.create( entry=entry, hours=hours, date=date, birthday=birthday, ) total_hours += hours #raise NotImplementedError is_edit = entry.total_hours is not None #if entry.total_hours is not None: entry.total_hours = total_hours entry.save() return total_hours, is_edit
def clean(self): cleaned_data = super(HoursForm, self).clean() dates = list(utils.get_weekday_dates(self.entry.start, self.entry.end)) for date in dates: field_name = date.strftime("d-%Y%m%d") try: value = int(cleaned_data[field_name]) if date == dates[0] and not value: _search = dict(start__gte=date, end__gte=date, user=self.entry.user) if not Entry.objects.filter(**_search).exists(): raise forms.ValidationError("First date can't be 0 hours") elif date == dates[-1] and not value: raise forms.ValidationError("Last date can't be 0 hours") except (KeyError, ValueError): # something else is wrong and the clean method shouldn't # was called even though not all fields passed continue return cleaned_data
def clean(self): cleaned_data = super(HoursForm, self).clean() dates = list(utils.get_weekday_dates(self.entry.start, self.entry.end)) for date in dates: field_name = date.strftime('d-%Y%m%d') try: value = int(cleaned_data[field_name]) if date == dates[0] and not value: _search = dict(start__gte=date, end__gte=date, user=self.entry.user) if not Entry.objects.filter(**_search).exists(): raise forms.ValidationError( "First date can't be 0 hours") elif date == dates[-1] and not value: raise forms.ValidationError("Last date can't be 0 hours") except (KeyError, ValueError): # something else is wrong and the clean method shouldn't # was called even though not all fields passed continue return cleaned_data
def hours(request, pk): data = {} entry = get_object_or_404(Entry, pk=pk) if entry.user != request.user: if not (request.user.is_staff or request.user.is_superuser): return http.HttpResponseForbidden('insufficient access') if request.method == 'POST': form = forms.HoursForm(entry, data=request.POST) if form.is_valid(): total_hours, is_edit = save_entry_hours(entry, form) extra_users = request.session.get('notify_extra', '') extra_users = [ x.strip() for x in extra_users.split(';') if x.strip() ] success, email_addresses = send_email_notification( entry, extra_users, is_edit=is_edit, ) assert success #messages.info(request, # '%s hours of PTO logged.' % total_hours #) recently_created = make_entry_title(entry, request.user) cache_key = 'recently_created_%s' % request.user.pk cache.set(cache_key, recently_created, 60) url = reverse('dates.emails_sent', args=[entry.pk]) url += '?' + urlencode({'e': email_addresses}, True) return redirect(url) else: initial = {} for date in utils.get_weekday_dates(entry.start, entry.end): try: #hours_ = Hours.objects.get(entry=entry, date=date) hours_ = Hours.objects.get(date=date, entry__user=entry.user) initial[date.strftime('d-%Y%m%d')] = hours_.hours except Hours.DoesNotExist: initial[date.strftime('d-%Y%m%d')] = settings.WORK_DAY form = forms.HoursForm(entry, initial=initial) data['form'] = form if entry.total_hours: data['total_hours'] = entry.total_hours else: total_hours = 0 for date in utils.get_weekday_dates(entry.start, entry.end): try: hours_ = Hours.objects.get(entry=entry, date=date) total_hours += hours_.hours except Hours.DoesNotExist: total_hours += settings.WORK_DAY data['total_hours'] = total_hours notify = request.session.get('notify_extra', []) data['notify'] = notify return render(request, 'dates/hours.html', data)
def hours(request, pk): data = {} entry = get_object_or_404(Entry, pk=pk) if entry.user != request.user: if not (request.user.is_staff or request.user.is_superuser): return http.HttpResponseForbidden('insufficient access') if request.method == 'POST': form = forms.HoursForm(entry, data=request.POST) if form.is_valid(): total_hours, is_edit = save_entry_hours(entry, form) extra_users = request.session.get('notify_extra', '') extra_users = [x.strip() for x in extra_users.split(';') if x.strip()] success, email_addresses = send_email_notification( entry, extra_users, is_edit=is_edit, ) assert success #messages.info(request, # '%s hours of PTO logged.' % total_hours #) recently_created = make_entry_title(entry, request.user) cache_key = 'recently_created_%s' % request.user.pk cache.set(cache_key, recently_created, 60) url = reverse('dates.emails_sent', args=[entry.pk]) url += '?' + urlencode({'e': email_addresses}, True) return redirect(url) else: initial = {} for date in utils.get_weekday_dates(entry.start, entry.end): try: #hours_ = Hours.objects.get(entry=entry, date=date) hours_ = Hours.objects.get(date=date, entry__user=entry.user) initial[date.strftime('d-%Y%m%d')] = hours_.hours except Hours.DoesNotExist: initial[date.strftime('d-%Y%m%d')] = settings.WORK_DAY form = forms.HoursForm(entry, initial=initial) data['form'] = form if entry.total_hours: data['total_hours'] = entry.total_hours else: total_hours = 0 for date in utils.get_weekday_dates(entry.start, entry.end): try: hours_ = Hours.objects.get(entry=entry, date=date) total_hours += hours_.hours except Hours.DoesNotExist: total_hours += settings.WORK_DAY data['total_hours'] = total_hours notify = request.session.get('notify_extra', []) data['notify'] = notify return render(request, 'dates/hours.html', data)