def list_by_parent(cls, user_from_email, parent_key, request): case_list = [] you_can_loop = True count = 0 limit = int(request.cases.limit) case_next_curs = request.cases.pageToken while you_can_loop: edge_limit = int(request.cases.limit) - count if edge_limit > 0: case_edge_list = Edge.list(start_node=parent_key, kind='cases', limit=edge_limit, pageToken=case_next_curs) for edge in case_edge_list['items']: case = edge.end_node.get() if Node.check_permission(user_from_email, case): count += 1 tag_list = Tag.list_by_parent(parent_key=case.key) case_status_edges = Edge.list(start_node=case.key, kind='status', limit=1) current_status_schema = None if len(case_status_edges['items']) > 0: current_status = case_status_edges['items'][ 0].end_node.get() current_status_schema = CaseStatusSchema( name=current_status.status, status_changed_at=case_status_edges['items'] [0].created_at.isoformat()) owner = model.User.get_by_gid(case.owner) owner_schema = None if owner: owner_schema = iomessages.UserSchema( id=str(owner.id), email=owner.email, google_display_name=owner.google_display_name, google_public_profile_photo_url=owner. google_public_profile_photo_url, google_public_profile_url=owner. google_public_profile_url, google_user_id=owner.google_user_id) case_list.append( CaseSchema(id=str(case.key.id()), entityKey=case.key.urlsafe(), name=case.name, current_status=current_status_schema, priority=case.priority, tags=tag_list, owner=owner_schema, access=case.access, created_at=case.created_at.strftime( "%Y-%m-%dT%H:%M:00.000"), updated_at=case.updated_at.strftime( "%Y-%m-%dT%H:%M:00.000"))) if case_edge_list['next_curs'] and case_edge_list['more']: case_next_curs = case_edge_list['next_curs'].urlsafe() else: you_can_loop = False case_next_curs = None
def get_schema(cls, user_from_email, request): pipeline = cls.get_by_id(int(request.id)) if pipeline is None: raise endpoints.NotFoundException('pipeline not found.') if Node.check_permission(user_from_email, pipeline): owner = model.User.get_by_gid(pipeline.owner) owner_schema = iomessages.UserSchema( id=str(owner.id), email=owner.email, google_display_name=owner.google_display_name, google_public_profile_photo_url=owner. google_public_profile_photo_url, google_public_profile_url=owner.google_public_profile_url, google_user_id=owner.google_user_id) tab = [] opportunitystages = Opportunitystage.query( Opportunitystage.pipeline == pipeline.key).fetch() for os in opportunitystages: tab.append( OpportunitystageSchema( entityKey=os.entityKey, name=os.name, probability=os.probability, amount_opportunity=os.amount_opportunity, nbr_opportunity=os.nbr_opportunity, stage_number=os.stage_number, pipeline=pipeline.key.urlsafe(), )) pipeline_schema = PipelineSchema( id=str(pipeline.key.id()), entityKey=pipeline.key.urlsafe(), name=pipeline.name, # infonodes = infonodes, description=pipeline.description, created_at=pipeline.created_at.strftime( "%Y-%m-%dT%H:%M:00.000"), updated_at=pipeline.updated_at.strftime( "%Y-%m-%dT%H:%M:00.000"), owner=owner_schema, stages=tab) return pipeline_schema else: raise endpoints.NotFoundException('Permission denied')
class Case(EndpointsModel): _message_fields_schema = ('id', 'entityKey', 'owner', 'folder', 'access', 'collaborators_list', 'collaborators_ids', 'name', 'status', 'type_case', 'priority', 'account', 'account_name', 'contact', 'contact_name', 'created_at', 'updated_at', 'type_case', 'description', 'case_origin', 'closed_date') # Sharing fields owner = ndb.StringProperty() collaborators_list = ndb.StructuredProperty(model.Userinfo, repeated=True) collaborators_ids = ndb.StringProperty(repeated=True) organization = ndb.KeyProperty() folder = ndb.StringProperty() account = ndb.KeyProperty() account_name = ndb.StringProperty() contact = ndb.KeyProperty() contact_name = ndb.StringProperty() name = ndb.StringProperty() status = ndb.StringProperty() type_case = ndb.StringProperty() industry = ndb.StringProperty() priority = ndb.IntegerProperty() created_at = ndb.DateTimeProperty(auto_now_add=True) updated_at = ndb.DateTimeProperty(auto_now=True) created_by = ndb.KeyProperty() description = ndb.StringProperty() case_origin = ndb.StringProperty() closed_date = ndb.DateTimeProperty() # public or private access = ndb.StringProperty() def put(self, **kwargs): ndb.Model.put(self, **kwargs) try: self.put_index() except: print 'error on saving document index' def set_perm(self): about_item = str(self.key.id()) perm = model.Permission(about_kind='Case', about_item=about_item, type='user', role='owner', value=self.owner) perm.put() def put_index(self, data=None): """ index the element at each""" empty_string = lambda x: x if x else "" collaborators = " ".join(self.collaborators_ids) organization = str(self.organization.id()) title_autocomplete = ','.join( tokenize_autocomplete(self.name + ' ' + empty_string(self.account_name) + ' ' + empty_string(self.contact_name))) if data: search_key = ['infos', 'cases', 'tags', 'collaborators'] for key in search_key: if key not in data.keys(): data[key] = "" my_document = search.Document( doc_id=str(data['id']), fields=[ search.TextField(name=u'type', value=u'Case'), search.TextField(name='organization', value=empty_string(organization)), search.TextField(name='access', value=empty_string(self.access)), search.TextField(name='owner', value=empty_string(self.owner)), search.TextField(name='collaborators', value=data['collaborators']), search.TextField(name='title', value=empty_string(self.name)), search.TextField(name='account_name', value=empty_string(self.account_name)), search.TextField(name='contact_name', value=empty_string(self.contact_name)), search.TextField(name='status', value=empty_string(self.status)), search.NumberField(name='priority', value=int(self.priority or 1)), search.DateField(name='created_at', value=self.created_at), search.DateField(name='updated_at', value=self.updated_at), search.TextField(name='infos', value=data['infos']), search.TextField(name='tags', value=data['tags']), search.TextField(name='cases', value=data['cases']), search.TextField(name='title_autocomplete', value=empty_string(title_autocomplete)), search.TextField(name='type_case', value=empty_string(self.type_case)) ]) else: my_document = search.Document( doc_id=str(self.key.id()), fields=[ search.TextField(name=u'type', value=u'Case'), search.TextField(name='organization', value=empty_string(organization)), search.TextField(name='access', value=empty_string(self.access)), search.TextField(name='owner', value=empty_string(self.owner)), search.TextField(name='collaborators', value=collaborators), search.TextField(name='title', value=empty_string(self.name)), search.TextField(name='account_name', value=empty_string(self.account_name)), search.TextField(name='contact_name', value=empty_string(self.contact_name)), search.TextField(name='status', value=empty_string(self.status)), search.NumberField(name='priority', value=int(self.priority)), search.DateField(name='created_at', value=self.created_at), search.DateField(name='updated_at', value=self.updated_at), search.TextField(name='title_autocomplete', value=empty_string(title_autocomplete)), search.TextField(name='type_case', value=empty_string(self.type_case)) ]) my_index = search.Index(name="GlobalIndex") my_index.put(my_document) @classmethod def get_schema(cls, user_from_email, request): case = cls.get_by_id(int(request.id)) if case is None: raise endpoints.NotFoundException('Opportunity not found.') if Node.check_permission(user_from_email, case): parents_edge_list = Edge.list(start_node=case.key, kind='parents') account_schema = None contact_schema = None for parent in parents_edge_list['items']: if parent.end_node.kind() == 'Account': account = parent.end_node.get() infonodes = Node.list_info_nodes(account.key, request) info_nodes_structured = Node.to_structured_data(infonodes) emails = EmailListSchema() if 'emails' in info_nodes_structured.keys(): emails = info_nodes_structured['emails'] addresses = AddressListSchema() if 'addresses' in info_nodes_structured.keys(): addresses = info_nodes_structured['addresses'] phones = PhoneListSchema() if 'phones' in info_nodes_structured.keys(): phones = info_nodes_structured['phones'] social_links = SocialLinkListSchema() if 'sociallinks' in info_nodes_structured.keys(): social_links = info_nodes_structured['sociallinks'] websites = [] if 'websites' in info_nodes_structured.keys(): sites = info_nodes_structured['websites'] for site in sites: websites.append(site['url']) account_schema = AccountSchema( id=str(account.key.id()), entityKey=account.key.urlsafe(), name=account.name, account_type=account.account_type, industry=account.industry, tagline=account.tagline, introduction=account.introduction, access=account.access, folder=account.folder, logo_img_id=account.logo_img_id, logo_img_url=account.logo_img_url, firstname=account.firstname, lastname=account.lastname, personal_account=account.personal_account, emails=emails, addresses=addresses, phones=phones, websites=websites, sociallinks=social_links) elif parent.end_node.kind() == 'Contact': contact = parent.end_node.get() infonodes = Node.list_info_nodes(contact.key, request) info_nodes_structured = Node.to_structured_data(infonodes) emails = EmailListSchema() if 'emails' in info_nodes_structured.keys(): emails = info_nodes_structured['emails'] addresses = AddressListSchema() if 'addresses' in info_nodes_structured.keys(): addresses = info_nodes_structured['addresses'] phones = PhoneListSchema() if 'phones' in info_nodes_structured.keys(): phones = info_nodes_structured['phones'] social_links = SocialLinkListSchema() if 'sociallinks' in info_nodes_structured.keys(): social_links = info_nodes_structured['sociallinks'] websites = [] if 'websites' in info_nodes_structured.keys(): sites = info_nodes_structured['websites'] for site in sites: websites.append(site['url']) contact_schema = iomessages.ContactSchema( id=str(contact.key.id()), entityKey=contact.key.urlsafe(), firstname=contact.firstname, lastname=contact.lastname, title=contact.title, profile_img_id=contact.profile_img_id, profile_img_url=contact.profile_img_url, emails=emails, addresses=addresses, phones=phones, websites=websites, sociallinks=social_links, ) tag_list = Tag.list_by_parent(parent_key=case.key) # list of infonodes infonodes = Node.list_info_nodes(parent_key=case.key, request=request) # list of topics related to this account topics = None if request.topics: topics = Note.list_by_parent(parent_key=case.key, request=request) tasks = None if request.tasks: tasks = Task.list_by_parent(parent_key=case.key, request=request) events = None if request.events: events = Event.list_by_parent(parent_key=case.key, request=request) documents = None if request.documents: documents = Document.list_by_parent(parent_key=case.key, request=request) case_status_edges = Edge.list(start_node=case.key, kind='status', limit=1) current_status_schema = None if len(case_status_edges['items']) > 0: current_status = case_status_edges['items'][0].end_node.get() current_status_schema = CaseStatusSchema( name=current_status.status, status_changed_at=case_status_edges['items'] [0].created_at.isoformat()) closed_date = None if case.closed_date: closed_date = case.closed_date.strftime( "%Y-%m-%dT%H:%M:00.000") owner = model.User.get_by_gid(case.owner) owner_schema = None if owner: owner_schema = iomessages.UserSchema( id=str(owner.id), email=owner.email, google_display_name=owner.google_display_name, google_public_profile_photo_url=owner. google_public_profile_photo_url, google_public_profile_url=owner.google_public_profile_url, google_user_id=owner.google_user_id) case_schema = CaseSchema( id=str(case.key.id()), entityKey=case.key.urlsafe(), name=case.name, folder=case.folder, current_status=current_status_schema, priority=case.priority, tags=tag_list, topics=topics, tasks=tasks, events=events, documents=documents, infonodes=infonodes, access=case.access, description=case.description, case_origin=case.case_origin, closed_date=closed_date, type_case=case.type_case, account=account_schema, contact=contact_schema, created_at=case.created_at.strftime("%Y-%m-%dT%H:%M:00.000"), updated_at=case.updated_at.strftime("%Y-%m-%dT%H:%M:00.000"), owner=owner_schema) return case_schema
current_status_schema = None if len(case_status_edges['items']) > 0: current_status = case_status_edges['items'][ 0].end_node.get() current_status_schema = CaseStatusSchema( name=current_status.status, status_changed_at=case_status_edges['items'] [0].created_at.isoformat()) owner = model.User.get_by_gid(case.owner) owner_schema = None if owner: owner_schema = iomessages.UserSchema( id=str(owner.id), email=owner.email, google_display_name=owner.google_display_name, google_public_profile_photo_url=owner. google_public_profile_photo_url, google_public_profile_url=owner. google_public_profile_url, google_user_id=owner.google_user_id) case_schema = CaseSchema( id=str(case.key.id()), entityKey=case.key.urlsafe(), name=case.name, current_status=current_status_schema, priority=case.priority, owner=owner_schema, access=case.access, tags=tag_list, created_at=case.created_at.strftime( "%Y-%m-%dT%H:%M:00.000"),
def list(cls, user_from_email, request): curs = Cursor(urlsafe=request.pageToken) if request.limit: limit = int(request.limit) else: limit = 1000 items = list() # while you_can_loop: if request.order: ascending = True if request.order.startswith('-'): order_by = request.order[1:] ascending = False else: order_by = request.order attr = cls._properties.get(order_by) if attr is None: raise AttributeError('Order attribute %s not defined.' % (attr_name, )) if ascending: pipelines, next_curs, more = cls.query().filter( cls.organization == user_from_email.organization).order( +attr).fetch_page(limit, start_cursor=curs) else: pipelines, next_curs, more = cls.query().filter( cls.organization == user_from_email.organization).order( -attr).fetch_page(limit, start_cursor=curs) else: pipelines, next_curs, more = cls.query().filter( cls.organization == user_from_email.organization).fetch_page( limit, start_cursor=curs) for pipeline in pipelines: owner = model.User.get_by_gid(pipeline.owner) owner_schema = iomessages.UserSchema( id=str(owner.id), email=owner.email, google_display_name=owner.google_display_name, google_public_profile_photo_url=owner. google_public_profile_photo_url, google_public_profile_url=owner.google_public_profile_url, google_user_id=owner.google_user_id) pipeline_schema = PipelineSchema( id=str(pipeline.key.id()), entityKey=pipeline.key.urlsafe(), name=pipeline.name, owner=owner_schema, created_at=pipeline.created_at.strftime( "%Y-%m-%dT%H:%M:00.000"), updated_at=pipeline.updated_at.strftime( "%Y-%m-%dT%H:%M:00.000")) items.append(pipeline_schema) # if (count == limit): # you_can_loop = False # if more and next_curs: # curs = next_curs # else: # you_can_loop = False if next_curs and more: next_curs_url_safe = next_curs.urlsafe() else: next_curs_url_safe = None return PipelineListResponse(items=items, nextPageToken=next_curs_url_safe)