def __init__(self, *args, **kwargs): self._user = kwargs.pop('user') super(AiGoalActivitiesForm, self).__init__(*args, **kwargs) choices = () for row in AiGoals.objects.filter(goal_type=AI_GOAL_TYPE_AICHI_TARGET): choices += ((row.id, 'Goal %02d. - %s' % (row.order, row.name)), ) self.fields['target'].widget = forms.widgets.Select( attrs={'tabindex': 5}, choices=choices ) choices = () for row in AiTreaty.get_all(): choices += ((row.odata_name, row.short_title),) self.fields['odata_name'].widget = forms.widgets.Select( attrs={'tabindex': 6}, choices=choices )
def goal_activity_edit(request, id_target, odata_name): ctx = inject_permissions_into_context(RequestContext(request), request, odata_name) if not ctx['change_aigoalactivities']: return render(request, 'permissions.html', context_instance=RequestContext(request)) target = get_object_or_404(AiGoals, pk=id_target) if not odata_name or len(odata_name) <= 0: messages.error(request, "Please specify instrument's OData identifier") return redirect(reverse('aichi_activities.views.index'), request={}) try: ob = AiGoalActivities.objects.get(target=target, odata_name=odata_name) except: ob = AiGoalActivities(target=target, odata_name=odata_name) ctx['form'] = AiGoalActivitiesForm(user=request.user, instance=ob) ctx['target'] = target ctx['odata_name'] = odata_name ctx['instrument'] = AiTreaty.get_instrument_name(odata_name) if request.method == 'POST': if 'delete' in request.POST.keys(): if ctx['delete_aigoalactivities']: if 'confirmed' in request.POST.keys(): ob.delete() messages.success(request, 'Activity has been successfully removed') return redirect(reverse('aichi_activities.views.index'), request={}) elif request.POST.has_key('cancel'): return render(request, 'aichi_activities/edit.html', context_instance=ctx) else: return render(request, 'aichi_activities/delete.html', context_instance=ctx) else: messages.error(request, 'You do not have sufficient privileges to delete this record') elif 'save' in request.POST.keys(): form = AiGoalActivitiesForm(request.POST, user=request.user, instance=ob) if form.is_valid(): messages.success(request, 'Activity successfully updated') form.save() return render(request, 'aichi_activities/edit.html', context_instance=ctx)