def usuariosAddView(request): if request.user.profile.rol == 1: EstuFormSet = modelformset_factory(Estudiantes, form=EstudiantesForm) if request.method == 'POST': request.POST = request.POST.copy() request.POST['email'] = request.POST['username'] logging.debug(request.POST['email'] ) logging.debug(request.POST['username']) ucform = usercreateForm(request.POST) pform = profileForm(request.POST) formset = EstuFormSet(request.POST) if pform.is_valid() and ucform.is_valid() and formset.is_valid(): payload = ucform.save() # guardamos el perfil del usuario rpform = pform.save(commit=False) rpform.user_id = payload.id rpform.rol = 3 # id rol acudiente rpform.save() # guardamos los estudiantes instances = formset.save(commit=False) for instance in instances: instance.acudiente_id = rpform.id instance.save() return HttpResponseRedirect(reverse("usuariosVerView")) else: ucform = usercreateForm() pform = profileForm() formset = EstuFormSet(queryset=Estudiantes.objects.none()) return render(request, "usuariosadd.html", { "pform": pform, "ucform": ucform, "formset": formset }) raise Http404
def docentesAddView(request): if request.user.profile.rol == 1: if request.method == 'POST': ucform = usercreateForm(request.POST) pform = profileForm(request.POST) dcform = docentesCreateForm(request.POST) if pform.is_valid() and ucform.is_valid() and dcform.is_valid(): payload = ucform.save() # guardamos el perfil del usuario rpform = pform.save(commit=False) rpform.user_id = payload.id rpform.rol = 2 # id rol docente rpform.save() # guardamos al docente y su grupo rdcform = dcform.save(commit=False) rdcform.user_id = payload.id rdcform.save() dcform.save_m2m() # guardamos los estudiantes return HttpResponseRedirect(reverse("docentesVerView")) else: ucform = usercreateForm() pform = profileForm() dcform = docentesCreateForm() return render(request, "docentesadd.html", { "ucform": ucform, "pform": pform, "dcform": dcform }) raise Http404
def usuariosEditView(request, idpro): if request.user.profile.rol == 1: pro = get_object_or_404(Profile, id=idpro) EstuFormSet = modelformset_factory(Estudiantes, form=EstudiantesForm, formset=BaseFormSetCustom, can_delete=True, extra=1 if pro.estudiantes_set.count() == 0 else 0) if request.method == 'POST': ucform = userchangeForm(data=request.POST, instance=pro.user) pform = profileForm(data=request.POST, instance=pro) formset = EstuFormSet(queryset=Estudiantes.objects.filter(acudiente=pro.id), data=request.POST) if ucform.is_valid(): ucform.save() if pform.is_valid(): rpform = pform.save() print(ucform.errors) for form in formset: if form.is_valid(): rform = form.save(commit=False) rform.acudiente_id = pro.id rform.save() form.save_m2m() # borramos los estudiantes selecionados if formset.is_valid(): formset.save(commit=False) for obj in formset.deleted_objects: obj.delete() return HttpResponseRedirect(reverse("usuariosVerView")) else: formset = EstuFormSet(queryset=Estudiantes.objects.filter(acudiente=pro.id)) ucform = userchangeForm(instance=pro.user) pform = profileForm(instance=pro) return render(request, "usuariosadd.html", {"ucform": ucform, "pform": pform, "formset": formset, "modo": "edit"}) raise Http404
def docentesEditView(request, doce): if request.user.profile.rol == 1: ds = get_object_or_404(Docentes, id=doce) if request.method == 'POST': ucform = userchangeForm(data=request.POST, instance=ds.user) pform = profileForm(request.POST, request.FILES, instance=ds.user.profile) dcform = docentesCreateForm(data=request.POST, instance=ds) if ucform.is_valid(): payload = ucform.save() if pform.is_valid(): # guardamos el perfil del usuario rpform = pform.save(commit=False) rpform.user_id = ds.user.id rpform.rol = 2 # id rol docente rpform.save() if dcform.is_valid(): # guardamos al docente y los grupos rdcform = dcform.save(commit=False) rdcform.user_id = ds.user.id rdcform.save() dcform.save_m2m() return HttpResponseRedirect(reverse("docentesVerView")) else: ucform = userchangeForm(instance=ds.user) pform = profileForm(instance=ds.user.profile) dcform = docentesCreateForm(instance=ds) return render(request, "docentesadd.html", { "ucform": ucform, "pform": pform, "dcform": dcform, "modo": "edit" }) raise Http404
def apiChangeUserProfile(request): ucform = userChangeForm(data=request.GET, instance=request.resource_owner) pform = profileForm(data=request.GET, files=request.FILES, instance=request.resource_owner.profile) if ucform.is_valid(): ucform.save() if pform.is_valid(): ucform.save() return JsonResponse({ "status": "OK", "results": { "user": ucform.errors, "profile": pform.errors } })