def get(self): claims = get_jwt_claims() if not claims['is_admin']: return {'message': 'User not authorized to view'} try: # data = _organization_parser.parse_args() data = request.args organization_name = data.get("organization_name") organization_desc = data.get("organization_desc") organization_id = data.get("organization_id") # isactive = data.get("isactive") if organization_id: organizations = OrganizationModel.find_by_organization_id( organization_id) elif organization_name: organizations = OrganizationModel.find_by_organization_name( organization_name) elif organization_desc: organizations = OrganizationModel.find_by_organization_desc( organization_desc) else: organizations = OrganizationModel.find_all() except: organizations = OrganizationModel.find_all() finally: if len(organizations) > 0: resp = [] for organization in organizations: resp.append(organization.json()) return resp, 200 return {'message': 'Organization not found'}, 404
def json(self): return {"email": self.email, # "status": self.status, \ "first_name": self.first_name, "last_name": self.last_name, \ "gender": self.gender, "mobile": self.mobile, \ "organization_id": self.organization_id, "organization_name": OrganizationModel.find_by_organization_id( self.organization_id).organization_name if OrganizationModel.find_by_organization_id( self.organization_id) else None, "user_role_id": self.user_role_id, "user_role_name": UserRoleModel.find_by_role_id(self.user_role_id).role_name if UserRoleModel.find_by_role_id(self.user_role_id) else None, \ "image": self.image, "type": self.type, \ "authtoken": self.authtoken, "teacher_code": self.teacher_code, \ "bloodgroup": self.bloodgroup}
def get(cls, uuid: str): org = OrganizationModel.find_by_uuid(uuid) if not org: return {"message": "Organization not found."}, 404 return organization_schema.dump(org)
def find_by_id(cls, _id, user): from models.organization import OrganizationModel record = cls.query.filter_by(id=_id).first() if record: if OrganizationModel.find_by_id(record.organization_id, user): return record
def delete(cls, uuid: str): org = OrganizationModel.find_by_uuid(uuid) if not org: return {"message": "Organization not found."}, 404 org.delete_from_db() return {"message": "Organization deleted!."}, 200
def fetch_user_role_org(user: UserModel): try: user_role = UserRoleModel.find_by_role_id(user.user_role_id).role_name user_organization = \ OrganizationModel.find_by_organization_id(user.organization_id).organization_name except: user_role = None user_organization = None finally: return {'user_role': user_role, 'user_organization': user_organization}
def post(cls): organization = organization_schema.load(request.get_json()) if OrganizationModel.find_by_name(organization.name): return { "message": "A organization with that name already exists." }, 400 organization.save_to_db() return {"message": "Organization created successfully."}, 201
def put(cls, uuid: str): org_request = organization_schema.load(request.get_json()) org = OrganizationModel.find_by_uuid(uuid) if not org: return {"message": "Organization not found."}, 404 org.name = org_request.name org_request.save_to_db() return organization_schema.dump(org_request), 200
def post(cls, org: str, user: str): organization = OrganizationModel.find_by_uuid(org) member = UserModel.find_by_uuid(user) if not organization: return {"message": "Organization not found."}, 404 if not member: return {"message": "User not found."}, 404 organization.users.append(member) organization.save_to_db() return user
def delete(self): claims = get_jwt_claims() if not claims['is_super_admin']: return {'message': 'Super Admin privilege required.'}, 412 data = _organization_parser.parse_args() try: organization_name = data["organization_name"] except Exception as e: return { "message": f"Organization name missing. {organization_name}" } organization = OrganizationModel.find_by_organization_name( organization_name)[0] if organization: organization.delete_from_db() return {'message': 'Organization deleted.'}, 200 return {'message': 'Organization not found.'}, 404
def get(cls): # users = 0 # organizations = 0 # posts = 0 # user = get_current_user() # if user.role == "admin": # print("***************") # print(claims.organizations[0].id) users = UserModel.count() organizations = OrganizationModel.count() posts = PostModel.count() return { "users": users, "organizations": organizations, "posts": posts, }
def set_attribute(self, payload): cols = [ 'status', 'first_name', 'last_name', 'gender', 'password', 'mobile', 'designation', 'image', 'type', 'authtoken', 'teacher_code', 'bloodgroup', 'user_role_id', 'organization_id' ] for col in cols: if col in payload.keys(): setattr(self, col, payload[col]) if payload.get('user_role_name'): user_role = UserRoleModel.find_by_role_name( payload['user_role_name']) self.user_role_id = user_role.id if payload.get('organization_name'): organization = OrganizationModel.find_by_organization_name( payload['organization_name']) self.organization_id = organization.id if 'password' in payload.keys(): self.password = md5(str( payload['password']).encode('UTF-8')).hexdigest()
def put(self): claims = get_jwt_claims() if not claims['is_super_admin']: return {'message': 'Super Admin privilege required.'}, 412 data = _organization_parser.parse_args() for key in data.keys(): if str(data[key]).lower() in ('none', 'null', ''): data[key] = None try: organization_name = data["organization_name"] except Exception as e: return {"message": f"Organization name required. {repr(e)}"} organization = OrganizationModel.find_by_organization_name( organization_name)[0] if organization: organization.set_attribute(data) else: organization = OrganizationModel(**data) organization.save_to_db() return organization.json(), 201
def post(self): claims = get_jwt_claims() if not claims['is_super_admin']: return {'message': 'Super Admin privilege required.'}, 412 data = _organization_parser.parse_args() try: organization_name = data["organization_name"] except Exception as e: return {'message': f"Organization Name missing. {repr(e)}"} if OrganizationModel.find_by_organization_name(organization_name): return { 'message': f"An Organization with name '{organization_name}' already exists." }, 400 organization = OrganizationModel(**data) try: organization.save_to_db() except Exception as e: return { "message": f"An error occurred inserting the organization. Error: {repr(e)}" }, 500 return organization.json(), 201
def get(self): return [organization.json() for organization in OrganizationModel.objects()]
organization = {} organization['name'] = fake.company() organization['owner'] = fake.name() organization['address'] = fake.address() organization['about'] = fake.text() organization['followers'] = [] organization['followers'] = [ fake.name() for _ in range(random.randint(1, 5)) ] organization['image_url'] = fake.image_url() organization['organization_type'] = organization_type[random.randint( 0, len(organization_type) - 1)] # append to list organization_model = OrganizationModel(**organization) organizations.append(organization_model) # initialize list events = [] event_type = [ 'tech', 'science', 'finance', 'artisan skills', 'health', 'education', 'art', 'other' ] # generate event for _ in range(50): # generate event event = {} event['name'] = fake.sentence()
def parent_id_exists(path_id): """Check if organization with id=path_id exists in the DB""" if not OrganizationModel.find_by_id(path_id): raise ValidationError('Organization with id={} does not exist.'.format(path_id))
def find_by_any(cls, **kwargs): cols = [ 'id', 'email', 'status', 'first_name', 'last_name', 'gender', 'mobile', 'designation', 'type', 'teacher_code', 'bloodgroup', 'organization_id', 'user_role_id', 'organization_name', 'user_role_name' ] filter_str = 'cls.query' if 'id' in kwargs.keys(): filter_str = filter_str + '.filter_by(_id="' + str( kwargs['id']) + '")' if 'email' in kwargs.keys(): filter_str = filter_str + '.filter_by(email="' + str( kwargs['email']) + '")' if 'status' in kwargs.keys(): filter_str = filter_str + '.filter_by(status="' + str( kwargs['status']) + '")' if 'first_name' in kwargs.keys(): filter_str = filter_str + '.filter_by(first_name="' + str( kwargs['first_name']) + '")' if 'last_name' in kwargs.keys(): filter_str = filter_str + '.filter_by(last_name="' + str( kwargs['last_name']) + '")' if 'gender' in kwargs.keys(): filter_str = filter_str + '.filter_by(gender="' + str( kwargs['gender']) + '")' if 'mobile' in kwargs.keys(): filter_str = filter_str + '.filter_by(mobile="' + str( kwargs['mobile']) + '")' if 'designation' in kwargs.keys(): filter_str = filter_str + '.filter_by(designation="' + str( kwargs['designation']) + '")' if 'type' in kwargs.keys(): filter_str = filter_str + '.filter_by(type="' + str( kwargs['type']) + '")' if 'teacher_code' in kwargs.keys(): filter_str = filter_str + '.filter_by(teacher_code="' + str( kwargs['teacher_code']) + '")' if 'bloodgroup' in kwargs.keys(): filter_str = filter_str + '.filter_by(bloodgroup="' + str( kwargs['bloodgroup']) + '")' if 'organization_id' in kwargs.keys(): filter_str = filter_str + '.filter_by(organization_id="' + str( kwargs['organization_id']) + '")' if 'user_role_id' in kwargs.keys(): filter_str = filter_str + '.filter_by(user_role_id="' + str( kwargs['user_role_id']) + '")' if 'user_role_name' in kwargs.keys(): user_role = UserRoleModel.find_by_role_name( kwargs['user_role_name']) if user_role: user_role_id = user_role[0].id filter_str = filter_str + '.filter_by(user_role_id="' + str( user_role_id) + '")' if 'organization_name' in kwargs.keys(): organization = OrganizationModel.find_by_organization_name( kwargs['organization_name']) if organization: organization_id = organization[0].id filter_str = filter_str + '.filter_by(organization_id="'\ + str(organization_id) + '")' filter_str = filter_str + '.all()' return eval(filter_str)
def get(cls): return { "organizations": organization_list_schema.dump(OrganizationModel.find_all()) }
def get(self, name): org = OrganizationModel.find_by_name(name) if org: return org.json() return {'message': 'Organization not found'}, 404