def list_by_parent(cls, parent_key, request): date_time_to_string = lambda x: x.strftime("%Y-%m-%dT%H:%M:00.000" ) if x else "" task_list = [] task_edge_list = Edge.list(start_node=parent_key, kind='tasks', limit=request.tasks.limit, pageToken=request.tasks.pageToken) for edge in task_edge_list['items']: task = edge.end_node.get() if task is not None: status_color = 'green' status_label = '' if task.due: now = datetime.datetime.now() diff = task.due - now if 0 <= diff.days <= 2: status_color = 'orange' status_label = 'soon: due in ' + str( diff.days) + ' days' elif diff.days < 0: status_color = 'red' status_label = 'overdue' else: status_label = 'due in ' + str(diff.days) + ' days' if task.status == 'closed': status_color = 'white' status_label = 'closed' author_schema = None if task.author: author_schema = AuthorSchema( google_user_id=task.author.google_user_id, display_name=task.author.display_name, google_public_profile_url=task.author. google_public_profile_url, photo=task.author.photo) task_schema = TaskSchema( id=str(task.key.id()), entityKey=task.key.urlsafe(), title=task.title, status=task.status, status_color=status_color, status_label=status_label, created_by=author_schema, completed_by=AuthorSchema(), created_at=date_time_to_string(task.created_at), updated_at=date_time_to_string(task.updated_at)) if task.due: task_schema.due = date_time_to_string(task.due) task_list.append(task_schema) if task_edge_list['next_curs'] and task_edge_list['more']: task_next_curs = task_edge_list['next_curs'].urlsafe() else: task_next_curs = None return TaskListResponse(items=task_list, nextPageToken=task_next_curs)
def get_schema(cls, user_from_email, request): event = cls.get_by_id(int(request.id)) if event is None: raise endpoints.NotFoundException('Event not found.') tag_list = Tag.list_by_parent(parent_key=event.key) about = None edge_list = Edge.list(start_node=event.key, kind='related_to') for edge in edge_list['items']: about_kind = edge.end_node.kind() parent = edge.end_node.get() if parent: if about_kind == 'Contact' or about_kind == 'Lead': about_name = parent.firstname + ' ' + parent.lastname else: about_name = parent.name about = DiscussionAboutSchema( kind=about_kind, id=str(parent.key.id()), name=about_name ) author_schema = None if event.author: author_schema = AuthorSchema( google_user_id=event.author.google_user_id, email=user_from_email.email, display_name=event.author.display_name, google_public_profile_url=event.author.google_public_profile_url, photo=event.author.photo) event_schema = EventSchema( id=str(event.key.id()), entityKey=event.key.urlsafe(), title=event.title, starts_at=event.starts_at.isoformat(), ends_at=event.ends_at.isoformat(), where=event.where, description=event.description, about=about, created_by=author_schema, tags=tag_list, created_at=event.created_at.isoformat(), updated_at=event.updated_at.isoformat(), access=event.access, allday=event.allday, timezone=event.timezone ) return event_schema
def get_schema(cls, user_from_email, request): document = cls.get_by_id(int(request.id)) if document is None: raise endpoints.NotFoundException('Document not found.') tag_list = Tag.list_by_parent(parent_key=document.key) about = None edge_list = Edge.list(start_node=document.key, kind='parents') for edge in edge_list['items']: about_kind = edge.end_node.kind() parent = edge.end_node.get() if parent: if about_kind == 'Contact' or about_kind == 'Lead': about_name = parent.firstname + ' ' + parent.lastname elif about_kind == 'Task' or about_kind == 'Event': about_name = parent.title else: about_name = parent.name about = DiscussionAboutSchema( kind=about_kind, id=str(parent.key.id()), name=about_name ) author_schema = None if document.author: author_schema = AuthorSchema( google_user_id=document.author.google_user_id, display_name=document.author.display_name, google_public_profile_url=document.author.google_public_profile_url, photo=document.author.photo) document_schema = DocumentSchema( id=str(document.key.id()), entityKey=document.key.urlsafe(), title=document.title, resource_id=document.resource_id, alternateLink=document.alternateLink, thumbnailLink=document.thumbnailLink, embedLink=document.embedLink, mimeType=document.mimeType, access=document.access, tags=tag_list, about=about, created_by=author_schema, created_at=document.created_at.strftime("%Y-%m-%dT%H:%M:00.000"), updated_at=document.updated_at.strftime("%Y-%m-%dT%H:%M:00.000") ) return document_schema
def list(cls, user_from_email, request): curs = Cursor(urlsafe=request.pageToken) if request.limit: limit = int(request.limit) else: limit = 10 items = list() date_to_string = lambda x: x.strftime("%Y-%m-%d") if x else "" date_time_to_string = lambda x: x.strftime("%Y-%m-%dT%H:%M:00.000" ) if x else "" you_can_loop = True count = 0 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.' % (order_by, )) if ascending: tasks, next_curs, more = cls.query().filter( cls.organization == user_from_email.organization).order(+attr).fetch_page( limit, start_cursor=curs) else: tasks, next_curs, more = cls.query().filter( cls.organization == user_from_email.organization).order(-attr).fetch_page( limit, start_cursor=curs) else: tasks, next_curs, more = cls.query().filter( cls.organization == user_from_email.organization).fetch_page(limit, start_cursor=curs) for task in tasks: if len(items) < limit: is_filtered = True if task.access == 'private' and task.owner != user_from_email.google_user_id: end_node_set = [user_from_email.key] if not Edge.find(start_node=task.key, kind='permissions', end_node_set=end_node_set, operation='AND'): is_filtered = False if request.status and task.status != request.status and is_filtered: is_filtered = False if request.tags and is_filtered: end_node_set = [ ndb.Key(urlsafe=tag_key) for tag_key in request.tags ] if not Edge.find(start_node=task.key, kind='tags', end_node_set=end_node_set, operation='AND'): is_filtered = False if request.assignee and is_filtered: user_assigned = model.User.get_by_gid(request.assignee) end_node_set = [user_assigned.key] if not Edge.find(start_node=task.key, kind='assignees', end_node_set=end_node_set, operation='AND'): is_filtered = False if request.owner and task.owner != request.owner and is_filtered: is_filtered = False if request.completed_by and task.completed_by != request.completed_by and is_filtered: is_filtered = False if request.about and is_filtered: end_node_set = [ndb.Key(urlsafe=request.about)] if not Edge.find(start_node=task.key, kind='related_to', end_node_set=end_node_set, operation='AND'): is_filtered = False if request.urgent and is_filtered: if task.due is None: is_filtered = False else: now = datetime.datetime.now() diff = task.due - now if diff.days > 2: is_filtered = False if task.status == 'closed': is_filtered = False if is_filtered: count += 1 # list of tags related to this task tag_list = Tag.list_by_parent(parent_key=task.key) about = None edge_list = Edge.list(start_node=task.key, kind='related_to') for edge in edge_list['items']: about_kind = edge.end_node.kind() parent = edge.end_node.get() if parent: if about_kind == 'Contact' or about_kind == 'Lead': about_name = parent.firstname + ' ' + parent.lastname else: about_name = parent.name about = DiscussionAboutSchema( kind=about_kind, id=str(parent.key.id()), name=about_name) # list of tags related to this task edge_list = Edge.list(start_node=task.key, kind='assignees') assignee_list = list() for edge in edge_list['items']: if edge.end_node.get(): assignee_list.append( AuthorSchema( edgeKey=edge.key.urlsafe(), google_user_id=edge.end_node.get( ).google_user_id, google_public_profile_url=edge.end_node .get().google_public_profile_url, photo=edge.end_node.get( ).google_public_profile_photo_url)) status_color = 'green' status_label = '' if task.due: now = datetime.datetime.now() diff = task.due - now if 0 <= diff.days <= 2: status_color = 'orange' status_label = 'soon: due in ' + str( diff.days) + ' days' elif diff.days < 0: status_color = 'red' status_label = 'overdue' else: status_label = 'due in ' + str( diff.days) + ' days' if task.status == 'closed': status_color = 'white' status_label = 'closed' author_schema = None if task.author: author_schema = AuthorSchema( google_user_id=task.author.google_user_id, display_name=task.author.display_name, google_public_profile_url=task.author. google_public_profile_url, photo=task.author.photo) task_schema = TaskSchema( id=str(task.key.id()), entityKey=task.key.urlsafe(), title=task.title, status=task.status, status_color=status_color, status_label=status_label, comments=0, about=about, created_by=author_schema, completed_by=AuthorSchema(), tags=tag_list, assignees=assignee_list, created_at=date_time_to_string(task.created_at), updated_at=date_time_to_string(task.updated_at)) if task.due: task_schema.due = date_to_string(task.due) items.append(task_schema) if len(items) >= limit: you_can_loop = False if next_curs: if count >= limit: next_curs_url_safe = next_curs.urlsafe() else: next_curs_url_safe = curs.urlsafe() if next_curs: curs = next_curs else: you_can_loop = False next_curs_url_safe = None return TaskListResponse(items=items, nextPageToken=next_curs_url_safe)
def get_schema(cls, user_from_email, request): task = cls.get_by_id(int(request.id)) if task is None: raise endpoints.NotFoundException('Task not found.') tag_list = Tag.list_by_parent(parent_key=task.key) about = None edge_list = Edge.list(start_node=task.key, kind='related_to') for edge in edge_list['items']: about_kind = edge.end_node.kind() parent = edge.end_node.get() if parent: if about_kind == 'Contact' or about_kind == 'Lead': if parent.lastname and parent.firstname: about_name = parent.firstname + ' ' + parent.lastname else: if parent.lastname: about_name = parent.lastname else: if parent.firstname: about_name = parent.firstname else: about_name = parent.name about = DiscussionAboutSchema(kind=about_kind, id=str(parent.key.id()), name=about_name) # list of tags related to this task edge_list = Edge.list(start_node=task.key, kind='assignees') assignee_list = list() for edge in edge_list['items']: assignee_list.append( AuthorSchema( edgeKey=edge.key.urlsafe(), google_user_id=edge.end_node.get().google_user_id, display_name=edge.end_node.get().google_display_name, google_public_profile_url=edge.end_node.get( ).google_public_profile_url, photo=edge.end_node.get().google_public_profile_photo_url)) status_color = 'green' status_label = '' if task.due: now = datetime.datetime.now() diff = task.due - now if 0 <= diff.days <= 2: status_color = 'orange' status_label = 'soon: due in ' + str(diff.days) + ' days' elif diff.days < 0: status_color = 'red' status_label = 'overdue' else: status_label = 'due in ' + str(diff.days) + ' days' if task.status == 'closed': status_color = 'white' status_label = 'closed' author_schema = None if task.author: author_schema = AuthorSchema( google_user_id=task.author.google_user_id, display_name=task.author.display_name, google_public_profile_url=task.author. google_public_profile_url, photo=task.author.photo) due_date = None if task.due: due_date = task.due.strftime('%Y-%m-%d') task_schema = TaskSchema(id=str(task.key.id()), entityKey=task.key.urlsafe(), title=task.title, status=task.status, status_color=status_color, status_label=status_label, due=due_date, comments=0, about=about, created_by=author_schema, completed_by=AuthorSchema(), tags=tag_list, assignees=assignee_list, access=task.access, created_at=task.created_at.isoformat(), updated_at=task.updated_at.isoformat()) return task_schema
def list(cls, user_from_email, request): curs = Cursor(urlsafe=request.pageToken) if request.limit: limit = int(request.limit) else: limit = 10 items = [] you_can_loop = True count = 0 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.' % (order_by,)) if ascending: events, next_curs, more = cls.query().filter( cls.organization == user_from_email.organization).order(+attr).fetch_page(limit, start_cursor=curs) else: events, next_curs, more = cls.query().filter( cls.organization == user_from_email.organization).order(-attr).fetch_page(limit, start_cursor=curs) else: events, next_curs, more = cls.query().filter( cls.organization == user_from_email.organization).fetch_page(limit, start_cursor=curs) for event in events: if count <= limit: is_filtered = True if event.access == 'private' and event.owner != user_from_email.google_user_id: end_node_set = [user_from_email.key] if not Edge.find(start_node=event.key, kind='permissions', end_node_set=end_node_set, operation='AND'): is_filtered = False if request.tags and is_filtered: end_node_set = [ndb.Key(urlsafe=tag_key) for tag_key in request.tags] if not Edge.find(start_node=event.key, kind='tags', end_node_set=end_node_set, operation='AND'): is_filtered = False if request.owner and event.owner != request.owner and is_filtered: is_filtered = False if request.about and is_filtered: end_node_set = [ndb.Key(urlsafe=request.about)] if not Edge.find(start_node=event.key, kind='related_to', end_node_set=end_node_set, operation='AND'): is_filtered = False if is_filtered: count += 1 # list of tags related to this event tag_list = Tag.list_by_parent(parent_key=event.key) about = None edge_list = Edge.list(start_node=event.key, kind='related_to') for edge in edge_list['items']: about_kind = edge.end_node.kind() parent = edge.end_node.get() if parent: if about_kind == 'Contact' or about_kind == 'Lead': about_name = parent.firstname + ' ' + parent.lastname else: about_name = parent.name about = DiscussionAboutSchema(kind=about_kind, id=str(parent.key.id()), name=about_name) author_schema = None if event.author: author_schema = AuthorSchema(google_user_id=event.author.google_user_id, display_name=event.author.display_name, google_public_profile_url=event.author.google_public_profile_url, photo=event.author.photo) event_schema = EventSchema( id=str(event.key.id()), entityKey=event.key.urlsafe(), title=event.title, starts_at=event.starts_at.isoformat(), ends_at=event.ends_at.isoformat(), where=event.where, description=event.description, about=about, created_by=author_schema, tags=tag_list, created_at=event.created_at.isoformat(), updated_at=event.updated_at.isoformat() ) items.append(event_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 EventListResponse(items=items, nextPageToken=next_curs_url_safe)