def actualizar_registro(cls, documento, nombres, apellidos, usuario, clave, role, rutas): existe_documento = cls.existe_documento(documento) if existe_documento and usuario: u = User.query(User.documento == documento).get() u.nombres = nombres u.apellidos = apellidos if clave: u.clave_hash = User.make_pw_hash(usuario, clave) elif u.usuario != usuario and not cls.existe_usuario(usuario) and clave: u.clave_hash = User.make_pw_hash(usuario, clave) u.usuario = usuario u.role = role u.put() rutasEliminar = cls.query_all_rutas(u.key) if rutasEliminar: for r in rutasEliminar: if r.usuario.get().usuario == u.usuario: r.key.delete() if rutas: for rut in rutas: ruta_key = ndb.Key(urlsafe=rut) rta = ruta_key.get() nueva_ruta = UserRuta(ruta=rta.key, usuario=u.key) nueva_ruta.put() return ["alert-success", "La actualizaciĆ³n ha sido exitoso"] else: return ["alert-danger", "No se pudo completar la actualizaciĆ³n"]
def existe_usuario(cls, usuario): if usuario: if User.query(User.usuario == usuario).fetch(): return True else: return False else: return False
def existe_documento(cls, documento): if documento: if User.query(User.documento == documento).fetch(): return True else: return False else: return False
def initialise_db(): # Create an ancestor for all of our Posts to ensure consistency when manipulating Posts blog_instance = Blog.query().get() if not blog_instance: blog_instance = Blog() blog_instance.put() # Create Users if not User.query().fetch(): User( username='******', password='******' # TODO - obfuscate this ).put()
def authenticate(request, username=None, password=None): """ Authenticate User, create Session token and return it's value in a Cookie :param username [STRING] :param password [PASSWORD] :return: 200 or 403 """ user = User.query(User.username == username, User.password == password).get() if user: response = render_to_response(ADMIN_PAGE, {}) response.set_cookie(AUTH_COOKIE_KEY, log_user_in(), max_age=SESSION_EXPIRY) return response else: return HttpResponseForbidden()
def has_children(cls, key): key = ndb.Key(urlsafe=key) kind = key.kind() children = 0 if kind == 'Ciudad': children += Ruta.query(Ruta.ciudad == key).count() elif kind == 'Ruta': children += Abono.query(Abono.ruta == key).count() children += Cliente.query(Cliente.ruta == key).count() children += Credito.query(Credito.ruta == key).count() children += Transaccion.query(Transaccion.ruta == key).count() children += User.query(User.ruta == key).count() elif kind == 'Cliente': pass elif kind == 'Credito': pass return True if children > 0 else False
def query_all(cls, admin=False): if admin: usuarios = User.query().fetch() else: usuarios = User.query(User.activo == True).fetch() return usuarios