def callback(user): roles = yield Role.query(ancestor=user.key).fetch_async() userXcompany = UserXCompany.query(UserXCompany.user==user.key).get() if(userXcompany is not None): company = Company.query(Company.key == userXcompany.company).get() if company is not None: user._company = company.to_dict() user._roles = roles raise ndb.Return(user)
def save(entity): from services import UserService, CompanyService if entity.user is None: raise ValueError("User is required") if entity.company is None: raise ValueError("Company is required") user = UserService.UserInstance.get(entity.user.urlsafe()) if user is None: raise ValueError("User not found") company = CompanyService.CompanyInstance.get(entity.company.urlsafe()) if company is None: raise ValueError("Company not found") if entity.key is None: #It validates if association already exists v = UserXCompany.get_by_company_and_user(entity.company, entity.user) if v is not None: raise ValueError("Association already exists") entity = UserXCompany.save(entity) else: current = UserXCompany.get(entity.key.urlsafe()) if current is not None: current.user = entity.user current.company = entity.company entity = UserXCompany.save(entity) else: raise ValueError("UserXCompany does not exists") return entity
def remove_all_companies_from_user(user): return UserXCompany.remove_all_companies_from_user(user)
def get_by_company_and_user(company, user): return UserXCompany.get_by_company_and_user(company, user)
def get_all(page, page_size): return UserXCompany.get_all(page, page_size)
def get(id): return UserXCompany.get(id)
def delete(id): entity = UserXCompany.get(id) if (entity is None): raise ValueError("UserXCompany does not exists") else: entity.delete()
def get(self, request): '''Authorization''' if config.VALID_AUTHENTICATION == True: user = endpoints.get_current_user() if user is None: raise endpoints.UnauthorizedException try: from models import User from models import Role from models import UserXCompany from models import Company #Pagination page = request.page page_size = request.page_size #Filters filters = {} filters["activated"] = request.activated email = request.email roles_joined = request.roles company = request.company_key @ndb.tasklet def callback(user): roles = yield Role.query(ancestor=user.key).fetch_async() userXcompany = UserXCompany.query(UserXCompany.user==user.key).get() if(userXcompany is not None): company = Company.query(Company.key == userXcompany.company).get() if company is not None: user._company = company.to_dict() user._roles = roles raise ndb.Return(user) qo = ndb.QueryOptions(limit=100) all_users = [] total = 0 qry = User.query() '''Role''' if roles_joined: all_users = User.get_by_roles(roles_joined.upper()) keys = [] for user in all_users: keys.append(user.key) if not keys: qry = qry.filter(User.email == None) else: qry = qry.filter(User.key.IN(keys)) '''Email''' if email: qry = qry.filter(User.email==email) '''Company''' if company: users_by_company = UserXCompany.get_all_by_company(company) users_ids = [] for x in users_by_company: users_ids.append(x.user) if len(users_ids) > 0: qry = qry.filter(User.key.IN(users_ids)) else: qry = qry.filter(User.email == None) '''Active''' if (filters["activated"] and str(filters["activated"]) == "all"): pass elif filters["activated"] and (json.loads(filters["activated"]) == True or json.loads(filters["activated"]) == False): qry = qry.filter(User.activated == json.loads(filters["activated"])) else: qry = qry.filter(User.activated == True) if (not page or not page_size): all_users = qry.order(-User.created_at).fetch() total = len(all_users) else: offset = int(page_size) * (int(page) - 1) limit = int(page_size) all_users = qry.order(-User.created_at).fetch(offset=offset, limit=limit) total = qry.count() response = { "total": total, "records": json.loads(json.dumps([u.to_dict() for u in all_users])) } return Response( status=SUCCESS, response=response ) except Exception, e: errors = str(e) return Response(status=FAIL, response=errors)