def usuario_novo(request): ''' @usuario_novo: Metodo de criação de um novo Usuario ''' form = UsuarioForm() if request.method == 'POST': form = UsuarioForm(request.POST) if form.is_valid(): usuario = form.save(commit=False) usuario.save() # Criando o Hashcode do Usuario usuario.hashcode = hashlib.md5(str(usuario.id)).hexdigest() usuario.password_textplan= usuario.password # Password criptografado usuario.set_password(usuario.password) #adicionando usuario a staff para permitir upload de fotos no ckeditor usuario.is_staff = True grupo = Group.objects.get(id=request.POST['grupo']) usuario.groups.add(grupo) usuario.save() return HttpResponseRedirect( r('usuarios:usuarios')) else: return render(request,'usuario_novo.html',{'form': form,'status':'Novo'}) else: return render(request,'usuario_novo.html',{'form': form,'status':'Novo'})
def nuevoU(request): if request.method == "POST": form = UsuarioForm(request.POST) print request.POST if form.is_valid(): user = form.save() user.save() return redirect('usuarios.views.listarU') else: form = UsuarioForm return render(request,'usuarios/usuario_nuevo.html',{'form':form, 'etiqueta':'Nuevo'})
def editarU(request, pk): user = get_object_or_404(Usuario, pk = pk) if request.method == "POST": form = UsuarioForm(request.POST, instance = user) print request.POST if form.is_valid(): user = form.save() user.save() return redirect('listarU') else: form = UsuarioForm (instance=user) return render(request,'usuarios/usuario_nuevo.html',{'form':form, 'etiqueta':'Actualizar'})
def signup(request): if request.method == 'POST': form = UsuarioForm(request.POST,request.FILES) if form.is_valid(): user = form.save() user.set_password(user.password) user.groups.add(4) user.save() else: pass return render_to_response('usuarios/templates/signup.html',locals(),context_instance=RequestContext(request))
def atualizar_user(request, id): user = get_object_or_404(Usuarios, pk=id) empresa = get_object_or_404(Usuarios, pk=user.empresa_id) form = UsuarioForm(request.POST or None, request.FILES or None, instance=user) if form.is_valid(): form.save() return redirect('listar_user', empresa_id=empresa.id) return render(request, 'usuario_form.html', { 'form': form, 'empresa': empresa })
def criar_user(request, id): empresa = get_object_or_404(Empresa, pk=id) form = UsuarioForm(request.POST or None, request.FILES or None, initial={'empresa': id}) if form.is_valid(): form.save() return redirect('listar_user', empresa_id=id) return render(request, 'usuario_form.html', { 'form': form, 'empresa': empresa })
def get_context_data(self, **kwargs): context = super(FacilitatorView, self).get_context_data(**kwargs) usuarios = Usuario.objects.filter(role=2) | Usuario.objects.filter( role=3) context['usuarios'] = usuarios context['formulario'] = UsuarioForm(instance=None) return context
def save_edit_administrator(request): if request.method == 'POST': admin = Usuario.objects.get(pk=request.POST.get('id_admin')) form = UsuarioForm(request.POST, request.FILES, instance=admin) print(form.is_valid()) if form.is_valid(): admin = form.save(role=admin.role) print(admin) return HttpResponse(json.dumps({ 'usuario': admin.usuario, 'nombre': admin.nombre, 'apellido': admin.apellido, 'mail': admin.mail }), content_type='application/json') else: return HttpResponse(0)
def edit_administrator(request): if request.method == 'POST': id_admin = request.POST.get('id_admin') admin = Usuario.objects.get(pk=id_admin) form = UsuarioForm(instance=admin) return render(request, 'editar_usuario.html', { 'formulario': form, 'id_admin': id_admin }) else: mensaje = "cero" return HttpResponse(mensaje, content_type='application/json')
def registro(request): registrado = False if request.method == 'POST': usuario_form = UsuarioForm(data=request.POST) perfil_form = PerfilUsuarioForm(data=request.POST) if usuario_form.is_valid() and perfil_form.is_valid(): usuario = usuario_form.save() usuario.set_password(usuario.password) usuario.save() perfil = perfil_form.save(commit=False) perfil.usuario = usuario if 'perfil_pic' in request.FILES: perfil.perfil_pic = request.FILES['perfil_pic'] perfil.save() registrado = True else: print(usuario_form.errors, perfil_form.errors) else: usuario_form = UsuarioForm() perfil_form = PerfilUsuarioForm() return render( request, 'usuarios/registro.html', { 'usuario_form': usuario_form, 'perfil_form': perfil_form, 'registrado': registrado })
def new(request): form = UsuarioForm(request.POST) if (form.is_valid()): login = form.cleaned_data['login'] senha = form.cleaned_data['senha'] telefone = form.cleaned_data['telefone'] endereco = form.cleaned_data['endereco'] tipo = form.cleaned_data['tipo'] new_user = User.objects.create_user(login, password=senha) new_user.save() new_profile = UserProfile(user=new_user, telefone=telefone, endereco=endereco, tipo=tipo) new_profile.save() return HttpResponseRedirect('/home/') else: form = UsuarioForm() context_dict = {'form': form} return render(request, 'usuario/new.html', context=context_dict)
def editarUsuario(request, id): usuario = get_object_or_404(Usuarios, pk=id) if request.method == 'POST': formaUsuario = UsuarioForm(request.POST, instance=usuario) if formaUsuario.is_valid(): formaUsuario.save() return redirect('usuarios') else: formaUsuario = UsuarioForm(instance=usuario) return render(request, 'usuarios/editarUsuario.html', { 'formaUsuario': formaUsuario, 'usuario': usuario })
def save_administrator(request): if request.POST: if request.POST.get('id_admin'): admin = Usuario.objects.get(pk=request.POST.get('id_admin')) form = UsuarioForm(request.POST, request.FILES, instance=admin) else: form = UsuarioForm(request.POST, request.FILES) print(request.FILES) print(form) if form.is_valid(): print(form.cleaned_data['pic_profile']) if 'pic_profile' in request.FILES: filename = request.FILES['pic_profile'] print(filename) usuario = form.save() # No se guarda la imagen todavia # usuario.pic_profile = form.cleaned_data['pic_profile'] # usuario.save() return HttpResponse(1) else: print(form.errors) return HttpResponse(0)
def edit(request): if request.method != 'POST': #should display the form form = UsuarioForm(instance=request.user) else: #should process the form if request.POST['old_password'] == '': #there's no password to change POSTcopy = request.POST.copy() del POSTcopy['password'] del POSTcopy['confirm_password'] POSTcopy['username'] = request.user.username form = UsuarioForm(POSTcopy,request.FILES,instance=request.user) if form.is_valid(): form.save() return HttpResponseRedirect('/usuarios/edit/finish') else: print [x for x in form.errors.items()] elif request.user.check_password(request.POST['old_password']): #the old password matches with the actual password request.POST['username'] = request.user.username form = UsuarioForm(request.POST,request.FILES,instance=request.user) if (request.POST['password'] == request.POST['confirm_password'] and request.POST['password'] != ''): if form.is_valid(): form.save() request.user.set_password(request.POST['password']) print request.POST['password'] request.user.save() return HttpResponseRedirect('/usuarios/edit/finish') else: #the new passwords doesn't match form.errors['senha'] = u"<ul class=\"errorlist\"><li>As novas senhas digitadas são diferentes ou vazias.</li></ul>" else: #the old password is wrong #request.user.set_password('1234') #request.user.save() request.POST['username'] = request.user.username form = UsuarioForm(request.POST,request.FILES,instance=request.user) if form.is_valid(): pass form.errors['senha'] = u"<ul class=\"errorlist\"><li>A senha atual digitada é inválida.</li></ul>" #form displaying fo everythin form1 = [] form2 = [] for f in form.visible_fields()[1:13]: form1.append(f) for f in form.visible_fields()[13:]: form2.append(f) return render_to_response('usuarios/templates/edit.html',locals(),context_instance=RequestContext(request))