def cadastro(request): #servidor = Tbservidor.objects.all() regional = Regional.objects.all().order_by('nome') grupo = AuthGroup.objects.all()#.filter( regional__id = AuthUser.objects.get( pk = request.user.id ).regional.id ).order_by('name') result = {} for obj in grupo: result.setdefault(obj.name, False) result = sorted(result.items()) ativo = False if request.POST.get('is_active',False): ativo = True if request.method == "POST": if validacao(request, 'cadastro'): usuario = AuthUser( regional = Regional.objects.get( pk = request.POST['regional'] ), password = make_password(request.POST['password']), first_name = request.POST['first_name'], last_name = request.POST['last_name'], email = request.POST['email'], username = request.POST['username'], is_superuser = False, is_staff = True, is_active = ativo, last_login = datetime.datetime.now(), date_joined = datetime.datetime.now() ) usuario.save() for obj in grupo: if request.POST.get(obj.name, False): #verificar se esse grupo ja esta ligado ao usuario # inserir ao authusergroups ug = AuthUserGroups( user = AuthUser.objects.get( pk = usuario.id ), group = AuthGroup.objects.get( pk = obj.id ) ) ug.save() return HttpResponseRedirect("/core/usuario/consulta/") return render_to_response('core/usuario/cadastro.html',{'regional':regional,'result':result,'grupo':grupo}, context_instance = RequestContext(request))
def edicao(request, id): regional = Regional.objects.all().order_by('nome') grupo = AuthGroup.objects.all()#.filter( regional__id = AuthUser.objects.get( pk = request.user.id ).regional.id ).order_by('name') userGrupo = AuthUserGroups.objects.all().filter( user = id ) result = {} for obj in grupo: achou = False for obj2 in userGrupo: if obj.id == obj2.group.id: result.setdefault(obj.name,True) achou = True break if not achou: result.setdefault(obj.name, False) result = sorted(result.items()) user_obj = get_object_or_404(AuthUser, id=id) if request.method == "POST": if not request.user.has_perm('sicop.usuario_edicao'): return HttpResponseRedirect('/excecoes/permissao_negada/') # verificando os grupos do usuario for obj in grupo: if request.POST.get(obj.name, False): #verificar se esse grupo ja esta ligado ao usuario res = AuthUserGroups.objects.all().filter( user = id, group = obj.id ) if not res: # inserir ao authusergroups ug = AuthUserGroups( user = AuthUser.objects.get( pk = id ), group = AuthGroup.objects.get( pk = obj.id ) ) ug.save() #print obj.name + ' nao esta ligado a este usuario' else: #verificar se esse grupo foi desligado do usuario res = AuthUserGroups.objects.all().filter( user = id, group = obj.id ) if res: # excluir do authusergroups for aug in res: aug.delete() #print obj.name + ' desmarcou deste usuario' if validacao(request, 'edicao'): ativo = False if request.POST.get('is_active',False): ativo = True # tratar o campo senha senha_digitada = request.POST['password'] senha_atual = user_obj.password if len(senha_digitada) > 2: senha_atual = make_password( senha_digitada ) usuario = AuthUser( id = user_obj.id, regional = Regional.objects.get( pk = request.POST['regional'] ), password = senha_atual, first_name = request.POST['first_name'], last_name = request.POST['last_name'], email = request.POST['email'], username = request.POST['username'], is_superuser = user_obj.is_superuser, is_staff = user_obj.is_staff, is_active = ativo, last_login = user_obj.last_login, date_joined = user_obj.date_joined ) usuario.save() return HttpResponseRedirect("/core/usuario/consulta/") return render_to_response('core/usuario/edicao.html', {'result':result,'grupo':grupo,'usergrupo':userGrupo,'user_obj':user_obj,'regional':regional}, context_instance = RequestContext(request))
def edicao_usuario_logado(request, id): if str(request.user.id) == str(id): grupo = AuthGroup.objects.all() #servidor = Tbservidor.objects.all() userGrupo = AuthUserGroups.objects.all().filter( user = id ) result = {} for obj in grupo: achou = False for obj2 in userGrupo: if obj.id == obj2.group.id: result.setdefault(obj.name,True) achou = True break if not achou: result.setdefault(obj.name, False) result = sorted(result.items()) ativo = False if request.POST.get('is_active',False): ativo = True user_obj = get_object_or_404(AuthUser, id=id) if request.method == "POST": if request.user.has_perm('usuario_grupo_edicao'): # verificando os grupos do usuario for obj in grupo: if request.POST.get(obj.name, False): #verificar se esse grupo ja esta ligado ao usuario res = AuthUserGroups.objects.all().filter( user = id, group = obj.id ) if not res: # inserir ao authusergroups ug = AuthUserGroups( user = AuthUser.objects.get( pk = id ), group = AuthGroup.objects.get( pk = obj.id ) ) ug.save() #print obj.name + ' nao esta ligado a este usuario' else: #verificar se esse grupo foi desligado do usuario res = AuthUserGroups.objects.all().filter( user = id, group = obj.id ) if res: # excluir do authusergroups for aug in res: aug.delete() #print obj.name + ' desmarcou deste usuario' if validacao(request, 'edicao'): # tratar o campo senha senha_digitada = request.POST['password'] senha_atual = user_obj.password if len(senha_digitada) > 2: senha_atual = hashlib.md5( senha_digitada ).hexdigest() usuario = AuthUser( id = user_obj.id, password = senha_atual, first_name = request.POST['first_name'], last_name = request.POST['last_name'], email = request.POST['email'], username = request.POST['username'], is_superuser = user_obj.is_superuser, is_staff = user_obj.is_staff, is_active = ativo, last_login = user_obj.last_login, date_joined = user_obj.date_joined ) usuario.save() return HttpResponseRedirect("/usuario/edicao/usuario/"+str(id)+"/") return render_to_response('core/admin/usuario/edicao.html', {'result':result,'grupo':grupo,'usergrupo':userGrupo,'user_obj':user_obj}, context_instance = RequestContext(request)) else: return HttpResponseRedirect("/usuario/edicao/"+str(id)+"/")