def view(request, pk): position = Position.goof(pk=pk) opbases = position.opbases() compensation = Compensation.goon(position=position) if compensation: payscales = compensation.payscaleyear_set.all() status = Status.goon(position=position) if status: assign_bases = status.assign_bases.all() choice_bases = status.choice_bases.all() layoff_bases = status.layoff_bases.all() for opbase in opbases: opbase.fill_in_status(status) #watchers = position.watchers.count() if request.user in position.watchers.all(): already_watching = True kmz_url = reverse("kml-position", kwargs={"pk": position.pk}) return locals()
def edit(request, pk): from compensation.forms import PayscaleFormset, CompensationForm position = Position.goof(pk=pk) operation = Operation.goon(positions=position) if operation: opbases = operation.opbase_set.all() else: opbases = [] ####################################################################### status = Status.goon(position=position) if not status: status = Status(position=position) compensation = Compensation.goon(position=position) if not compensation: compensation = Compensation(position=position) ####################################################################### if request.method == "POST": newPOST = request.POST.copy() newPOST.update({"position": position.pk}) field_bases = rearrange_fields(newPOST, opbases) status_form = StatusForm(newPOST, instance=status) pos_form = PositionForm(newPOST, instance=position) comp_form = CompensationForm(newPOST, instance=compensation) payscale_formset = PayscaleFormset(newPOST, instance=compensation) if status_form.is_valid() and pos_form.is_valid() and comp_form.is_valid() and payscale_formset.is_valid(): if pos_form.has_changed(): pos_form.save() if comp_form.has_changed(): comp_form.save() #if payscale_formset.has_changed(): payscale_formset.save() ####### if not status_form.instance.pk: instance = status_form.save() else: instance = status_form.save(commit=False) if list(instance.layoff_bases.all()) != field_bases["layoff"] or \ list(instance.assign_bases.all()) != field_bases["assign"] or \ list(instance.choice_bases.all()) != field_bases["choice"] and \ status_form.has_changed() or not status_form.instance.pk: #only if the status has changed or its the first edit, otherwise dont run because we dont want the 'last modified' value to change status_form.save() instance.layoff_bases = field_bases["layoff"] instance.assign_bases = field_bases["assign"] instance.choice_bases = field_bases["choice"] instance.save() #assert False ####### return HttpResponseRedirect( position.get_absolute_url() ) else: payscale_formset = PayscaleFormset(instance=compensation) status_form = StatusForm(instance=status) mark_opbases(opbases, status) pos_form = PositionForm(instance=position) comp_form = CompensationForm(instance=compensation) return {"payscale_formset": payscale_formset, "comp_form": comp_form, "opbases": opbases, "status_form": status_form, "position": position, "operation": operation, "pos_form": pos_form, "last_modified": status.last_modified}