def timeoff_book(request, start_date=None): """ Books timeoff for the individual. It creates a entry in BookTimeOff and starts the Queue for approval. .. note:: Create ApprovalQueue process, by sending it a signal. Args: start_date (date): optional. defaults to start of the month """ if request.method == 'GET': return 'book-timeoff', timeoff_bookings(request, start_date) else: booktimeoff_form = BookTimeOffForm(request.POST) if booktimeoff_form.is_valid(): booktimeoff = booktimeoff_form.save() workflow.signals.post_attach_queue_save_event.send(sender=BookTimeOff, instance=booktimeoff, is_draft=False) #check if we have validators #TODO: have all forms extend RuleEnabledForm that on save can actually run the ruleSet. rule_set = RuleSet.objects.for_invoker_model(booktimeoff) if rule_set: validated_instance = GenericAspect.validate(rule_set, booktimeoff) if validated_instance.has_errors: raise TypeError('ruleset errors encountered') #TODO: circulate back to main html return 'book-timeoff', {}
def weeksnapshot_post_final_status_update(sender, **kwargs): """ Called when weeksnapshot is updated with final status. If banking, it updates the timeoff policy associated with the user's overtime policy. If not banking, ??? Lost in thin air Arguments: sender: expected to be `weeksnapshot` """ if kwargs['status'] == ApproverQueue.approved_status(): weeksnapshot = kwargs['instance'] #check if user has overtime policy set user_overtime_policy = UserOverTimePolicy.objects.get(user_profile=weeksnapshot.user.get_profile()) if user_overtime_policy: timesheet_type = ContentType.objects.get_for_model(Timesheet) week_type = ContentType.objects.get_for_model(WeekSnapshot) #get the ruleset and run thru the validation conditions = user_overtime_policy.overtime_policy.overtime_policy_conditions.all() overtime_hours = 0 banked_hours = 0 for condition in conditions: if condition.ruleset.content_type == timesheet_type: for timesheet in weeksnapshot.timesheets: if not timesheet.is_timeoff: validated_instance = GenericAspect.validate((condition.ruleset,), timesheet) overtime_hours, banked_hours = tally(validated_instance, condition, overtime_hours, banked_hours) elif condition.ruleset.content_type == week_type: validated_instance = GenericAspect.validate((condition.ruleset,), weeksnapshot) overtime_hours, banked_hours = tally(validated_instance, condition, overtime_hours, banked_hours) #update the user's timeoff policy linked to the overtime. only update if banked if banked_hours: user_overtime_policy.bank_user_timeoff_policy.time_remaining += banked_hours user_overtime_policy.bank_user_timeoff_policy.save()
def weekly_view(request, year=None, week=None): """ Current timesheet pulls the current week from the database. if one doesn't exists, it creates in-mem object. If Post, saves it to the database """ #TODO: have submit thru JSON year = year or date.today().year week = week or date.isocalendar()[1] if request.method == 'GET': user_projects = request.user.get_profile().projects start_week, end_week = determine_period(monday_of_week(int(week), int(year))) extra_form = 1 week_snapshot, timesheets = WeekSnapshot.objects.in_period(year, week, request.user) if not week_snapshot: week_snapshot = WeekSnapshot(user=request.user, year=year, week=week, start_week=start_week, end_week=end_week) else: extra_form = 0 week_snapshot_form = WeekSnapshotForm(prefix="week_snapshot", instance=week_snapshot) TimesheetFormSet = modelformset_factory(Timesheet, can_delete=True, extra=extra_form, form=TimesheetForm) timesheet_form_set = TimesheetFormSet(queryset=timesheets) return 'timesheet', {'projects': user_projects, 'year': int(year), 'week': int(week), 'timesheet_form_set': timesheet_form_set, \ 'week_snapshot': week_snapshot, 'week_snapshot_form': week_snapshot_form} else: is_draft = request.POST['is_draft'] == 'true' status = ApproverQueue.draft_status if is_draft else ApproverQueue.in_queue_status() week_snapshot = WeekSnapshot.objects.get_or_none(year=year, week=week, user=request.user) week_snapshot_form = WeekSnapshotForm(request.POST, prefix="week_snapshot", instance=week_snapshot) if week_snapshot_form.is_valid(): week_snapshot = week_snapshot_form.save(request.user) TimesheetFormSet = modelformset_factory(Timesheet, can_delete=True) timesheet_form_set = TimesheetFormSet(request.POST) #Pull rulesets for weeksnapshot rulesets = RuleSet.objects.for_invoker_model(WeekSnapshot) timesheet_rulesets = rulesets.filter(content_type=ContentType.objects.get_for_model(Timesheet)) week_rulesets = rulesets.filter(content_type=ContentType.objects.get_for_model(WeekSnapshot)) #TODO: serve the errors through JSON errors if timesheet_form_set.is_valid(): timsheets = timesheet_form_set.save() week_snapshot.timesheets.clear() #TODO: should batch all of the errors into one and send them back for timesheet in timsheets: if timesheet_rulesets: validated_instance = GenericAspect.validate(timesheet_rulesets, timesheet) if validated_instance.has_errors: raise TypeError('ruleset errors encountered') week_snapshot.timesheets.add(timesheet) week_snapshot.save() else: raise TypeError('validation errors encountered') #check if we have validators if week_rulesets: validated_instance = GenericAspect.validate(week_rulesets, week_snapshot) if validated_instance.has_errors: raise TypeError('ruleset errors encountered') #add new status to the weeksnapshot post_status_update(week_snapshot, status) #send signal since everything is done correctly workflow.signals.post_attach_queue_save_event.send(sender=WeekSnapshot, instance=week_snapshot, is_draft=is_draft) return 'home-r', {}