def pupil_edit(request, mode, id = 0): ''' Работа с учениками ''' render = {} if request.method == 'GET': if mode == 'edit': pupil = get_object_or_404(Pupil, id = id) render['groups'] = [PupilConnectionForm(sbj, pupil, prefix = sbj.id) for sbj in pupil.grade.get_subjects() if sbj.groups] render['form'] = PupilForm(instance = pupil) elif mode == 'delete': try: Pupil.objects.get(id = id).delete() return HttpResponseRedirect('/curatorship/pupil/') except Exception: return HttpResponseRedirect('/curatorship/pupil/') else: render['form'] = PupilForm() render['groups'] = [PupilConnectionForm(sbj, prefix = sbj.id) for sbj in request.user.grade.get_subjects() if sbj.groups] if request.method == 'POST': if mode == 'edit': pupil = get_object_or_404(Pupil, id = id) form = PupilForm(request.POST, instance = pupil) render['groups'] = [PupilConnectionForm(sbj, pupil, data = request.POST, prefix = sbj.id) for sbj in pupil.grade.get_subjects() if sbj.groups] form_factory_valid = all([f.is_valid() for f in render['groups']]) if form.is_valid() and form_factory_valid: form.save() for f in render['groups']: f.save() return HttpResponseRedirect('/curatorship/pupil/') else: render['form'] = form else: form = PupilForm(request.POST) render['groups'] = [PupilConnectionForm(sbj, data = request.POST, prefix = sbj.id) for sbj in request.user.grade.get_subjects() if sbj.groups] form_factory_valid = all([f.is_valid() for f in render['groups']]) if form.is_valid() and form_factory_valid: obj = form.save(commit = False) obj.school = request.user.school obj.grade = request.user.grade obj.save() for f in render['groups']: f.save(pupil = obj) return HttpResponseRedirect('/curatorship/pupil/') else: render['form'] = form return render_to_response( '~curatorship/pupil.html', render, context_instance=RequestContext(request))
render = {} if request.method == 'GET': if mode == 'edit': render['form'] = PupilForm(instance = get_object_or_404(Pupil, id = id)) elif mode == 'delete': try: Pupil.objects.get(id = id).delete() return HttpResponseRedirect('/curatorship/pupil/') except Exception, (error, ): return HttpResponseRedirect('/curatorship/pupil/') else: render['form'] = PupilForm() return render_to_response('curatorship/pupil.html', render, context_instance = RequestContext(request)) if request.method == 'POST': if mode == 'edit': form = PupilForm(request.POST, instance = get_object_or_404(Pupil, id = id)) if form.is_valid(): form.save() return HttpResponseRedirect('/curatorship/pupil/') else: render['form'] = form return render_to_response('curatorship/pupil.html', render, context_instance = RequestContext(request)) else: form = PupilForm(request.POST) if form.is_valid(): obj = form.save(commit = False) obj.school = request.user.school obj.grade = request.user.grade obj.save() return HttpResponseRedirect('/curatorship/pupil/') else:
def pupil_edit(request, mode, id=0): ''' Работа с учениками ''' render = {} if request.method == 'GET': if mode == 'edit': pupil = get_object_or_404(Pupil, id=id) render['groups'] = [ PupilConnectionForm(sbj, pupil, prefix=sbj.id) for sbj in pupil.grade.get_subjects() if sbj.groups ] render['form'] = PupilForm(instance=pupil) elif mode == 'delete': try: Pupil.objects.get(id=id).delete() return HttpResponseRedirect('/curatorship/pupil/') except Exception: return HttpResponseRedirect('/curatorship/pupil/') else: render['form'] = PupilForm() render['groups'] = [ PupilConnectionForm(sbj, prefix=sbj.id) for sbj in request.user.grade.get_subjects() if sbj.groups ] if request.method == 'POST': if mode == 'edit': pupil = get_object_or_404(Pupil, id=id) form = PupilForm(request.POST, instance=pupil) render['groups'] = [] for sbj in pupil.grade.get_subjects(): if sbj.groups: render['groups'].append( PupilConnectionForm( sbj, pupil, data=request.POST, prefix=sbj.id, instance=PupilConnection.objects.get_or_create( subject=sbj, pupil=pupil)[0])) form_factory_valid = all([f.is_valid() for f in render['groups']]) if form.is_valid() and form_factory_valid: form.save() for f in render['groups']: f.save() return HttpResponseRedirect('/curatorship/pupil/') else: render['form'] = form else: form = PupilForm(request.POST) render['groups'] = [ PupilConnectionForm(sbj, data=request.POST, prefix=sbj.id) for sbj in request.user.grade.get_subjects() if sbj.groups ] form_factory_valid = all([f.is_valid() for f in render['groups']]) if form.is_valid() and form_factory_valid: obj = form.save(commit=False) obj.school = request.user.school obj.grade = request.user.grade obj.save() for f in render['groups']: f.save(pupil=obj) return HttpResponseRedirect('/curatorship/pupil/') else: render['form'] = form return render_to_response('~curatorship/pupil.html', render, context_instance=RequestContext(request))