def insert_info_node(cls, parent_key, request): try: from crm.endpoints_helper import EndpointsHelper node = Node(kind=request.kind) node_values = [] for record in request.fields: if record.value: if len(record.value) > 500: prop = ndb.TextProperty(record.field, indexed=False) prop._code_name = record.field node._properties[record.field] = prop prop._set_value(node, record.value) else: setattr( node, record.field.encode('ascii', 'ignore').decode('ascii'), record.value.encode('ascii', 'ignore').decode('ascii')) node_values.append(record.value) if record.property_type: setattr(node, 'property_type', record.property_type) entityKey_async = node.put_async() entityKey = entityKey_async.get_result() Edge.insert(start_node=parent_key, end_node=entityKey, kind='infos', inverse_edge='parents') indexed_edge = '_' + request.kind + ' ' + " ".join( node_values).encode('ascii', 'ignore').decode('ascii') EndpointsHelper.update_edge_indexes(parent_key=parent_key, kind='infos', indexed_edge=indexed_edge) except: print 'error in insert infonode'
def patch(cls, user_from_email, request): case = cls.get_by_id(int(request.id)) if case is None: raise endpoints.NotFoundException('Case not found.') if (case.owner != user_from_email.google_user_id) and not user_from_email.is_admin: raise endpoints.ForbiddenException('you are not the owner') EndpointsHelper.share_related_documents_after_patch( user_from_email, case, request ) properties = ['owner', 'name', 'access', 'status', 'type_case', 'priority', 'description', 'case_origin'] for p in properties: if hasattr(request, p): if (eval('case.' + p) != eval('request.' + p)) \ and (eval('request.' + p) and not (p in ['put', 'set_perm', 'put_index'])): exec ('case.' + p + '= request.' + p) if request.closed_date: closed_date = datetime.datetime.strptime( request.closed_date, "%Y-%m-%dT%H:%M:00.000000" ) case.closed_date = closed_date case_key_async = case.put_async() data = EndpointsHelper.get_data_from_index(str(case.key.id())) case.put_index(data) get_schema_request = CaseGetRequest(id=int(request.id)) return cls.get_schema(user_from_email, get_schema_request)
def attach_files(cls, user_from_email, request): items = request.items author = Userinfo() author.google_user_id = user_from_email.google_user_id author.display_name = user_from_email.google_display_name author.photo = user_from_email.google_public_profile_photo_url if request.access: access = request.access else: access = 'public' items_attached = [] for item in items: document = cls( title=item.title, resource_id=item.id, mimeType=item.mimeType, embedLink=item.embedLink, owner=user_from_email.google_user_id, organization=user_from_email.organization, author=author, access=access, comments=0 ) document_key = document.put_async() document_key_async = document_key.get_result() if request.parent: parent_key = ndb.Key(urlsafe=request.parent) taskqueue.add( url='/workers/syncdocumentwithteam', queue_name='iogrow-low', params={ 'user_email': user_from_email.email, 'doc_id': str(document_key_async.id()), 'parent_key_str': request.parent } ) # insert edges Edge.insert(start_node=parent_key, end_node=document_key_async, kind='documents', inverse_edge='parents') EndpointsHelper.update_edge_indexes( parent_key=document_key_async, kind='documents', indexed_edge=str(parent_key.id()) ) else: data = {'id': document_key_async.id()} document.put_index(data) file_attached = iomessages.FileAttachedSchema( id=str(document_key_async.id()), name=item.title, embedLink=document.embedLink, ) items_attached.append(file_attached) return iomessages.FilesAttachedResponse(items=items_attached)
def insert(cls, user_from_email, request): # prepare google drive service credentials = user_from_email.google_credentials http = httplib2.Http() service = build('drive', 'v2', http=http) credentials.authorize(http) # prepare params to insert params = { 'title': request.title, 'mimeType': request.mimeType } # execute files.insert and get resource_id created_document = service.files().insert(body=params).execute() author = Userinfo() author.google_user_id = user_from_email.google_user_id author.display_name = user_from_email.google_display_name author.photo = user_from_email.google_public_profile_photo_url document = cls( owner=user_from_email.google_user_id, organization=user_from_email.organization, access=request.access, title=request.title, mimeType=request.mimeType, resource_id=created_document['id'], embedLink=created_document['embedLink'], author=author ) document_key = document.put_async() document_key_async = document_key.get_result() if request.parent: parent_key = ndb.Key(urlsafe=request.parent) taskqueue.add( url='/workers/syncdocumentwithteam', queue_name='iogrow-low', params={ 'user_email': user_from_email.email, 'doc_id': str(document_key_async.id()), 'parent_key_str': request.parent } ) # insert edges Edge.insert(start_node=parent_key, end_node=document_key_async, kind='documents', inverse_edge='parents') EndpointsHelper.update_edge_indexes( parent_key=document_key_async, kind='documents', indexed_edge=str(parent_key.id()) ) else: data = {'id': document_key_async.id()} document.put_index(data) return DocumentSchema(id=str(document_key_async.id()), embedLink=document.embedLink)
def delete_all_cascade(cls, start_node): from crm.endpoints_helper import EndpointsHelper EndpointsHelper.delete_document_from_index(start_node.id()) start_node_kind = start_node.kind() edges = cls.query(cls.start_node == start_node).fetch() for edge in edges: # check if we should delete subGraph or not if start_node_kind in DELETED_ON_CASCADE.keys(): if edge.kind in DELETED_ON_CASCADE[start_node_kind]: cls.delete_all_cascade(start_node=edge.end_node) cls.delete(edge.key) start_node.delete()
def attach_keyword(cls, user_from_email, request): start_node = ndb.Key(urlsafe=request.parent) end_node = ndb.Key(urlsafe=request.keyword_key) edge_key = Edge.insert(start_node=start_node, end_node=end_node, kind='keywords', inverse_edge='keywordged_on') edge = edge_key.get() if end_node.get().locality != 'Blog': EndpointsHelper.update_edge_indexes(parent_key=start_node, kind='keywords', indexed_edge=str( end_node.id())) return KeywordSchema(edgeKey=edge.key.urlsafe(), id=str(edge.end_node.id()), entityKey=edge.end_node.urlsafe(), word=edge.end_node.get().word, color=edge.end_node.get().color)
def attach_tag(cls, user_from_email, request): start_node = ndb.Key(urlsafe=request.parent) end_node = ndb.Key(urlsafe=request.tag_key) edge_key = Edge.insert(start_node=start_node, end_node=end_node, kind='tags', inverse_edge='tagged_on') edge = edge_key.get() if end_node.get().about_kind != 'Blog': EndpointsHelper.update_edge_indexes(parent_key=start_node, kind='tags', indexed_edge=str( end_node.id())) return TagSchema(edgeKey=edge.key.urlsafe(), id=str(edge.end_node.id()), entityKey=edge.end_node.urlsafe(), name=edge.end_node.get().name, color=edge.end_node.get().color)
def insert_info_node(cls, parent_key, request): try: from crm.endpoints_helper import EndpointsHelper node = Node(kind=request.kind) node_values = [] for record in request.fields: if record.value: if len(record.value) > 500: prop = ndb.TextProperty(record.field, indexed=False) prop._code_name = record.field node._properties[record.field] = prop prop._set_value(node, record.value) else: setattr( node, record.field.encode('ascii', 'ignore').decode('ascii'), record.value.encode('ascii', 'ignore').decode('ascii') ) node_values.append(record.value) if record.property_type: setattr( node, 'property_type', record.property_type ) entityKey_async = node.put_async() entityKey = entityKey_async.get_result() Edge.insert( start_node=parent_key, end_node=entityKey, kind='infos', inverse_edge='parents' ) indexed_edge = '_' + request.kind + ' ' + " ".join(node_values).encode('ascii', 'ignore').decode('ascii') EndpointsHelper.update_edge_indexes( parent_key=parent_key, kind='infos', indexed_edge=indexed_edge ) except: print 'error in insert infonode'
def insert(cls, user_from_email, request): user_from_email = EndpointsHelper.require_iogrow_user() tag = cls(owner=user_from_email.google_user_id, organization=user_from_email.organization, name=request.name, color=request.color, about_kind=request.about_kind) tag_async = tag.put_async() tag_key = tag_async.get_result() return TagSchema(id=str(tag_key.id()), entityKey=tag_key.urlsafe(), name=tag.name, color=tag.color, about_kind=tag.about_kind)
def patch(cls, user_from_email, request): pipeline = cls.get_by_id(int(request.id)) if pipeline is None: raise endpoints.NotFoundException('Pipeline not found.') properties = ['owner', 'name', 'access', 'description'] for p in properties: if hasattr(request, p): if (eval('pipeline.' + p) != eval('request.' + p)) \ and (eval('request.' + p) and not (p in ['put', 'set_perm', 'put_index'])): exec('pipeline.' + p + '= request.' + p) pipeline_key_async = pipeline.put_async() data = EndpointsHelper.get_data_from_index(str(pipeline.key.id())) pipeline.put_index(data) get_schema_request = PipelineGetRequest(id=int(request.id)) return cls.get_schema(user_from_email, get_schema_request)
def attach_tag(cls, user_from_email, request): start_node = ndb.Key(urlsafe=request.parent) end_node = ndb.Key(urlsafe=request.tag_key) edge_key = Edge.insert( start_node=start_node, end_node=end_node, kind='tags', inverse_edge='tagged_on' ) edge = edge_key.get() if end_node.get().about_kind != 'Blog': EndpointsHelper.update_edge_indexes( parent_key=start_node, kind='tags', indexed_edge=str(end_node.id()) ) return TagSchema( edgeKey=edge.key.urlsafe(), id=str(edge.end_node.id()), entityKey=edge.end_node.urlsafe(), name=edge.end_node.get().name, color=edge.end_node.get().color )
def attach_keyword(cls, user_from_email, request): start_node = ndb.Key(urlsafe=request.parent) end_node = ndb.Key(urlsafe=request.keyword_key) edge_key = Edge.insert( start_node=start_node, end_node=end_node, kind='keywords', inverse_edge='keywordged_on' ) edge = edge_key.get() if end_node.get().locality != 'Blog': EndpointsHelper.update_edge_indexes( parent_key=start_node, kind='keywords', indexed_edge=str(end_node.id()) ) return KeywordSchema( edgeKey=edge.key.urlsafe(), id=str(edge.end_node.id()), entityKey=edge.end_node.urlsafe(), word=edge.end_node.get().word, color=edge.end_node.get().color )
def patch(cls, user_from_email, request): pipeline = cls.get_by_id(int(request.id)) if pipeline is None: raise endpoints.NotFoundException('Pipeline not found.') properties = ['owner', 'name', 'access', 'description'] for p in properties: if hasattr(request, p): if (eval('pipeline.' + p) != eval('request.' + p)) \ and (eval('request.' + p) and not (p in ['put', 'set_perm', 'put_index'])): exec ('pipeline.' + p + '= request.' + p) pipeline_key_async = pipeline.put_async() data = EndpointsHelper.get_data_from_index(str(pipeline.key.id())) pipeline.put_index(data) get_schema_request = PipelineGetRequest(id=int(request.id)) return cls.get_schema(user_from_email, get_schema_request)
def insert(cls, user_from_email, request): user_from_email = EndpointsHelper.require_iogrow_user() tag = cls( owner=user_from_email.google_user_id, organization=user_from_email.organization, name=request.name, color=request.color, about_kind=request.about_kind ) tag_async = tag.put_async() tag_key = tag_async.get_result() return TagSchema( id=str(tag_key.id()), entityKey=tag_key.urlsafe(), name=tag.name, color=tag.color, about_kind=tag.about_kind )
def patch(cls, user_from_email, request): task = cls.get_by_id(int(request.id)) task_id = int(request.id) edges = Edge.query().filter(Edge.kind == "assignees", Edge.start_node == task.key) if task is None: raise endpoints.NotFoundException('Task not found.') if (task.owner != user_from_email.google_user_id) and not user_from_email.is_admin: raise endpoints.ForbiddenException('you are not the owner') if request.title: task.title = request.title if request.access: task.access = request.access if request.status: task.status = request.status if task.status == 'closed': task.completed_by = user_from_email.google_user_id body = '<p>#closed, view details on ioGrow: <a href="http://app.iogrow.com/#/tasks/show/' + str( task.key.id()) + '">' body += task.title body += '</a></p>' created_by = model.User.get_by_gid(task.owner) to = None if created_by: to = created_by.email edge_list = Edge.list(start_node=task.key, kind='assignees') assignee_list = list() cc = None for edge in edge_list['items']: assignee_list.append(edge.end_node.get().email) cc = ",".join(assignee_list) taskqueue.add( url='/workers/send_email_notification', queue_name='iogrow-low', params={ 'user_email': user_from_email.email, 'to': to, 'cc': cc, 'subject': '[task]: ' + task.title, 'body': body } ) if request.due and task.due is None: task.due = datetime.datetime.strptime(request.due, "%Y-%m-%dT%H:%M:00.000000") if edges: for edge in edges: assigned_to = edge.end_node.get() taskqueue.add( url='/workers/syncassignedtask', queue_name='iogrow-low-task', params={ 'email': assigned_to.email, 'task_key': task_id, 'assigned_to': edge.end_node.get() } ) taskqueue.add( url='/workers/synctask', queue_name='iogrow-low-task', params={ 'email': user_from_email.email, 'starts_at': request.due, 'ends_at': request.due, 'summary': task.title, 'task_id': task_id } ) else: taskqueue.add( url='/workers/synctask', queue_name='iogrow-low-task', params={ 'email': user_from_email.email, 'starts_at': request.due, 'ends_at': request.due, 'summary': task.title, 'task_id': task_id } ) elif request.due and task.due is not None: task.due = datetime.datetime.strptime(request.due, "%Y-%m-%dT%H:%M:00.000000") if edges: for edge in edges: assigned_to = edge.end_node.get() taskqueue.add( url='/workers/syncassignedpatchtask', queue_name='iogrow-low-task', params={ 'email': assigned_to.email, 'task_key': task_id, 'assigned_to': edge.end_node.get() } ) taskqueue.add( url='/workers/syncpatchtask', queue_name='iogrow-low-task', params={ 'email': user_from_email.email, 'starts_at': request.due, 'ends_at': request.due, 'summary': task.title, 'task_google_id': task.task_google_id } ) else: taskqueue.add( url='/workers/syncpatchtask', queue_name='iogrow-low-task', params={ 'email': user_from_email.email, 'starts_at': request.due, 'ends_at': request.due, 'summary': task.title, 'task_google_id': task.task_google_id } ) task_key = task.put_async() task_key_async = task_key.get_result() EndpointsHelper.update_edge_indexes( parent_key=task_key_async, kind=None, indexed_edge=None ) return cls.get_schema( user_from_email=user_from_email, request=request )
def insert(cls, user_from_email, request): if request.status: status = request.status else: status = 'open' if request.access: access = request.access else: access = 'public' author = Userinfo() author.google_user_id = user_from_email.google_user_id author.display_name = user_from_email.google_display_name author.photo = user_from_email.google_public_profile_photo_url task = Task(title=request.title, status=status, owner=user_from_email.google_user_id, organization=user_from_email.organization, access=access, author=author) if request.due: task.due = datetime.datetime.strptime( request.due, "%Y-%m-%dT%H:%M:00.000000" ) task_key = task.put_async() if request.due: taskqueue.add( url='/workers/synctask', queue_name='iogrow-low-task', params={ 'email': user_from_email.email, 'starts_at': request.due, 'ends_at': request.due, 'summary': request.title, 'task_id': task_key.get_result().id() } ) if request.reminder: pass task_key_async = task_key.get_result() if request.parent: # insert edges parent_key = ndb.Key(urlsafe=request.parent) Edge.insert(start_node=parent_key, end_node=task_key_async, kind='tasks', inverse_edge='related_to') EndpointsHelper.update_edge_indexes( parent_key=task_key_async, kind='tasks', indexed_edge=str(parent_key.id()) ) else: data = {'id': task_key_async.id()} task.put_index(data) if request.assignees: # insert edges for assignee in request.assignees: assignee_key = ndb.Key(urlsafe=assignee.entityKey) assignee_user = assignee_key.get() assignee_email = assignee_user.email # add a task queue to send notification email to assignee body = '<p>view details on ioGrow: <a href="http://app.iogrow.com/#/tasks/show/' + str( task_key_async.id()) + '">' body += request.title body += '</a></p>' taskqueue.add( url='/workers/send_email_notification', queue_name='iogrow-low', params={ 'user_email': user_from_email.email, 'to': assignee_email, 'subject': '[task]: ' + request.title, 'body': body } ) Edge.insert(start_node=task_key_async, end_node=assignee_key, kind='assignees', inverse_edge='assigned_to') if request.tags: # insert edges for tag in request.tags: Edge.insert(start_node=task_key_async, end_node=ndb.Key(urlsafe=tag.entityKey), kind='tags', inverse_edge='tagged_on') task_schema = TaskSchema( id=str(task_key_async.id()), entityKey=task_key_async.urlsafe(), title=task.title, status=task.status, access=task.access ) if task.due: task_schema.due = task.due.isoformat() return task_schema
def patch(cls, user_from_email, request): task = cls.get_by_id(int(request.id)) task_id = int(request.id) edges = Edge.query().filter(Edge.kind == "assignees", Edge.start_node == task.key) if task is None: raise endpoints.NotFoundException('Task not found.') if (task.owner != user_from_email.google_user_id ) and not user_from_email.is_admin: raise endpoints.ForbiddenException('you are not the owner') if request.title: task.title = request.title if request.access: task.access = request.access if request.status: task.status = request.status if task.status == 'closed': task.completed_by = user_from_email.google_user_id body = '<p>#closed, view details on ioGrow: <a href="http://app.iogrow.com/#/tasks/show/' + str( task.key.id()) + '">' body += task.title body += '</a></p>' created_by = model.User.get_by_gid(task.owner) to = None if created_by: to = created_by.email edge_list = Edge.list(start_node=task.key, kind='assignees') assignee_list = list() cc = None for edge in edge_list['items']: assignee_list.append(edge.end_node.get().email) cc = ",".join(assignee_list) taskqueue.add(url='/workers/send_email_notification', queue_name='iogrow-low', params={ 'user_email': user_from_email.email, 'to': to, 'cc': cc, 'subject': '[task]: ' + task.title, 'body': body }) if request.due and task.due is None: task.due = datetime.datetime.strptime(request.due, "%Y-%m-%dT%H:%M:00.000000") if edges: for edge in edges: assigned_to = edge.end_node.get() taskqueue.add(url='/workers/syncassignedtask', queue_name='iogrow-low-task', params={ 'email': assigned_to.email, 'task_key': task_id, 'assigned_to': edge.end_node.get() }) taskqueue.add(url='/workers/synctask', queue_name='iogrow-low-task', params={ 'email': user_from_email.email, 'starts_at': request.due, 'ends_at': request.due, 'summary': task.title, 'task_id': task_id }) else: taskqueue.add(url='/workers/synctask', queue_name='iogrow-low-task', params={ 'email': user_from_email.email, 'starts_at': request.due, 'ends_at': request.due, 'summary': task.title, 'task_id': task_id }) elif request.due and task.due is not None: task.due = datetime.datetime.strptime(request.due, "%Y-%m-%dT%H:%M:00.000000") if edges: for edge in edges: assigned_to = edge.end_node.get() taskqueue.add(url='/workers/syncassignedpatchtask', queue_name='iogrow-low-task', params={ 'email': assigned_to.email, 'task_key': task_id, 'assigned_to': edge.end_node.get() }) taskqueue.add(url='/workers/syncpatchtask', queue_name='iogrow-low-task', params={ 'email': user_from_email.email, 'starts_at': request.due, 'ends_at': request.due, 'summary': task.title, 'task_google_id': task.task_google_id }) else: taskqueue.add(url='/workers/syncpatchtask', queue_name='iogrow-low-task', params={ 'email': user_from_email.email, 'starts_at': request.due, 'ends_at': request.due, 'summary': task.title, 'task_google_id': task.task_google_id }) task_key = task.put_async() task_key_async = task_key.get_result() EndpointsHelper.update_edge_indexes(parent_key=task_key_async, kind=None, indexed_edge=None) return cls.get_schema(user_from_email=user_from_email, request=request)
def insert(cls, user_from_email, request): if request.status: status = request.status else: status = 'open' if request.access: access = request.access else: access = 'public' author = Userinfo() author.google_user_id = user_from_email.google_user_id author.display_name = user_from_email.google_display_name author.photo = user_from_email.google_public_profile_photo_url task = Task(title=request.title, status=status, owner=user_from_email.google_user_id, organization=user_from_email.organization, access=access, author=author) if request.due: task.due = datetime.datetime.strptime(request.due, "%Y-%m-%dT%H:%M:00.000000") task_key = task.put_async() if request.due: taskqueue.add(url='/workers/synctask', queue_name='iogrow-low-task', params={ 'email': user_from_email.email, 'starts_at': request.due, 'ends_at': request.due, 'summary': request.title, 'task_id': task_key.get_result().id() }) if request.reminder: pass task_key_async = task_key.get_result() if request.parent: # insert edges parent_key = ndb.Key(urlsafe=request.parent) Edge.insert(start_node=parent_key, end_node=task_key_async, kind='tasks', inverse_edge='related_to') EndpointsHelper.update_edge_indexes(parent_key=task_key_async, kind='tasks', indexed_edge=str( parent_key.id())) else: data = {'id': task_key_async.id()} task.put_index(data) if request.assignees: # insert edges for assignee in request.assignees: assignee_key = ndb.Key(urlsafe=assignee.entityKey) assignee_user = assignee_key.get() assignee_email = assignee_user.email # add a task queue to send notification email to assignee body = '<p>view details on ioGrow: <a href="http://app.iogrow.com/#/tasks/show/' + str( task_key_async.id()) + '">' body += request.title body += '</a></p>' taskqueue.add(url='/workers/send_email_notification', queue_name='iogrow-low', params={ 'user_email': user_from_email.email, 'to': assignee_email, 'subject': '[task]: ' + request.title, 'body': body }) Edge.insert(start_node=task_key_async, end_node=assignee_key, kind='assignees', inverse_edge='assigned_to') if request.tags: # insert edges for tag in request.tags: Edge.insert(start_node=task_key_async, end_node=ndb.Key(urlsafe=tag.entityKey), kind='tags', inverse_edge='tagged_on') task_schema = TaskSchema(id=str(task_key_async.id()), entityKey=task_key_async.urlsafe(), title=task.title, status=task.status, access=task.access) if task.due: task_schema.due = task.due.isoformat() return task_schema
except: from crm.iomodels.accounts import Account account_key = Account.get_key_by_name( user_from_email=user_from_email, name=request.account) if account_key: account = account_key.get() else: is_new_account = True account = Account( name=request.account, owner=user_from_email.google_user_id, organization=user_from_email.organization, access=request.access) account_key_async = account.put_async() account_key = account_key_async.get_result() data = EndpointsHelper.get_data_from_index( str(account.key.id())) account.put_index(data) if account: # insert edges Edge.insert(start_node=account.key, end_node=case_key_async, kind='cases', inverse_edge='parents') EndpointsHelper.update_edge_indexes(parent_key=case_key_async, kind='cases', indexed_edge=str( account.key.id())) indexed = True contact = None if request.contact:
def receive(self, mail_message): sender_id = mail_message.to.split("@")[0] try: bcc = mail_message.bcc.split("@")[0] except: bcc = 'undefined' user_id = 'undefined' if sender_id.isdigit(): user_id = sender_id if bcc.isdigit(): user_id = bcc user = User.get_by_gid(user_id) if user: sender_email = re.findall(r'[\w\.-]+@[\w\.-]+', mail_message.sender) if user.email == sender_email[0]: print 'authorized' html_bodies = mail_message.bodies('text/html') email_body = '' additional_emails = ' ' for content_type, body in html_bodies: decoded_html = smart_str(body.decode()) email_body += decoded_html additional_emails += ' ' + smart_str(mail_message.to) try: additional_emails += ' ' + smart_str(mail_message.bcc) except: pass try: additional_emails += ' ' + smart_str(mail_message.cc) except: pass re_emails = re.findall(r'[\w\.-]+@[\w\.-]+', email_body + additional_emails) emails = list(set(re_emails)) print 'emails' print emails for email in emails: generic_prop = ndb.GenericProperty() generic_prop._name = 'email' nodes = Node.query(generic_prop == email).fetch() if len(nodes) > 0: for node in nodes: parents_edge_list = Edge.list(start_node=node.key, kind='parents') for edge in parents_edge_list['items']: parent_key = edge.end_node # insert a note related to the parent node note_author = Userinfo() note_author.display_name = user.google_display_name note_author.photo = user.google_public_profile_photo_url note = Note(owner=user.google_user_id, organization=user.organization, author=note_author, title=mail_message.subject, content=email_body) entityKey_async = note.put_async() entityKey = entityKey_async.get_result() Edge.insert(start_node=parent_key, end_node=entityKey, kind='topics', inverse_edge='parents') EndpointsHelper.update_edge_indexes( parent_key=entityKey, kind='topics', indexed_edge=str(parent_key.id())) else: pass # We should create a lead related to this email # and attach this email with this lead else: print 'not authorized' else: print 'user doesnt exist'
def insert(cls, user_from_email, request): author = Userinfo() author.google_user_id = user_from_email.google_user_id author.display_name = user_from_email.google_display_name author.photo = user_from_email.google_public_profile_photo_url event = cls( owner=user_from_email.google_user_id, organization=user_from_email.organization, access=request.access, title=request.title, starts_at=datetime.datetime.strptime(request.starts_at, "%Y-%m-%dT%H:%M:00.000000"), ends_at=datetime.datetime.strptime(request.ends_at, "%Y-%m-%dT%H:%M:00.000000"), where=request.where, description=request.description, author=author, allday=request.allday, timezone=request.timezone ) event_key = event.put_async() attendees = [] if request.invites: attendees = request.invites taskqueue.add( url='/workers/syncevent', queue_name='iogrow-low-event', params={ 'email': user_from_email.email, 'starts_at': request.starts_at, 'ends_at': request.ends_at, 'summary': request.title, 'event_id': event_key.get_result().id(), 'attendees': attendees, 'guest_modify': request.guest_modify, 'guest_invite': request.guest_invite, 'guest_list': request.guest_list, 'description': request.description, 'reminder': request.reminder, 'method': request.method, 'timezone': request.timezone, 'where': request.where } ) event_key_async = event_key.get_result() if request.parent: parent_key = ndb.Key(urlsafe=request.parent) # insert edges Edge.insert(start_node=parent_key, end_node=event_key_async, kind='events', inverse_edge='parents') EndpointsHelper.update_edge_indexes( parent_key=event_key_async, kind='events', indexed_edge=str(parent_key.id()) ) else: data = {'id': event_key_async.id()} event.put_index(data) event_schema = EventSchema( id=str(event_key_async.id()), entityKey=event_key_async.urlsafe(), title=event.title, starts_at=event.starts_at.isoformat(), ends_at=event.ends_at.isoformat(), where=event.where, created_at=event.created_at.isoformat(), updated_at=event.updated_at.isoformat(), access=event.access, timezone=event.timezone, allday=event.allday ) return event_schema
def receive(self, mail_message): sender_id = mail_message.to.split("@")[0] try: bcc = mail_message.bcc.split("@")[0] except: bcc = 'undefined' user_id = 'undefined' if sender_id.isdigit(): user_id=sender_id if bcc.isdigit(): user_id=bcc user = User.get_by_gid(user_id) if user: sender_email=re.findall(r'[\w\.-]+@[\w\.-]+', mail_message.sender) if user.email==sender_email[0]: print 'authorized' html_bodies = mail_message.bodies('text/html') email_body = '' additional_emails = ' ' for content_type, body in html_bodies: decoded_html = smart_str(body.decode()) email_body+=decoded_html additional_emails+=' ' + smart_str(mail_message.to) try: additional_emails+=' ' + smart_str(mail_message.bcc) except: pass try: additional_emails+=' ' + smart_str(mail_message.cc) except: pass re_emails = re.findall(r'[\w\.-]+@[\w\.-]+', email_body + additional_emails) emails = list(set(re_emails)) print 'emails' print emails for email in emails: generic_prop = ndb.GenericProperty() generic_prop._name = 'email' nodes = Node.query(generic_prop==email).fetch() if len(nodes)>0: for node in nodes: parents_edge_list = Edge.list( start_node = node.key, kind = 'parents' ) for edge in parents_edge_list['items']: parent_key = edge.end_node # insert a note related to the parent node note_author = Userinfo() note_author.display_name = user.google_display_name note_author.photo = user.google_public_profile_photo_url note = Note( owner = user.google_user_id, organization = user.organization, author = note_author, title = mail_message.subject, content = email_body ) entityKey_async = note.put_async() entityKey = entityKey_async.get_result() Edge.insert( start_node = parent_key, end_node = entityKey, kind = 'topics', inverse_edge = 'parents' ) EndpointsHelper.update_edge_indexes( parent_key=entityKey, kind='topics', indexed_edge=str(parent_key.id()) ) else: pass # We should create a lead related to this email # and attach this email with this lead else: print 'not authorized' else: print 'user doesnt exist'
def insert(cls, user_from_email, request): case = cls( owner=user_from_email.google_user_id, organization=user_from_email.organization, access=request.access, name=request.name, priority=request.priority, description=request.description ) case_key = case.put_async() case_key_async = case_key.get_result() indexed = False status_key = None if request.status: status_key = ndb.Key(urlsafe=request.status) # insert edges Edge.insert(start_node=case_key_async, end_node=status_key, kind='status', inverse_edge='related_cases') account = None if request.account: try: account_key = ndb.Key(urlsafe=request.account) account = account_key.get() except: from crm.iomodels.accounts import Account account_key = Account.get_key_by_name( user_from_email=user_from_email, name=request.account ) if account_key: account = account_key.get() else: is_new_account = True account = Account( name=request.account, owner=user_from_email.google_user_id, organization=user_from_email.organization, access=request.access ) account_key_async = account.put_async() account_key = account_key_async.get_result() data = EndpointsHelper.get_data_from_index(str(account.key.id())) account.put_index(data) if account: # insert edges Edge.insert(start_node=account.key, end_node=case_key_async, kind='cases', inverse_edge='parents' ) EndpointsHelper.update_edge_indexes( parent_key=case_key_async, kind='cases', indexed_edge=str(account.key.id()) ) indexed = True contact = None if request.contact: try: contact_key = ndb.Key(urlsafe=request.contact) contact = contact_key.get() except: from crm.iomodels.contacts import Contact contact_key = Contact.get_key_by_name( user_from_email=user_from_email, name=request.contact ) if contact_key: contact = contact_key.get() else: firstname = request.contact.split()[0] lastname = " ".join(request.contact.split()[1:]) contact = Contact( firstname=firstname, lastname=lastname, owner=user_from_email.google_user_id, organization=user_from_email.organization, access=request.access ) contact_key_async = contact.put_async() contact_key = contact_key_async.get_result() if account: data = EndpointsHelper.get_data_from_index(str(contact.key.id())) contact.put_index(data) Edge.insert(start_node=account.key, end_node=contact.key, kind='contacts', inverse_edge='parents') EndpointsHelper.update_edge_indexes( parent_key=contact.key, kind='contacts', indexed_edge=str(account.key.id()) ) if contact: # insert edges Edge.insert(start_node=contact.key, end_node=case_key_async, kind='cases', inverse_edge='parents') EndpointsHelper.update_edge_indexes( parent_key=case_key_async, kind='cases', indexed_edge=str(contact.key.id()) ) for infonode in request.infonodes: Node.insert_info_node( case_key_async, iomessages.InfoNodeRequestSchema( kind=infonode.kind, fields=infonode.fields ) ) if not indexed: data = {'id': case_key_async.id()} case.put_index(data) current_status_schema = None if status_key: if status_key.get(): current_status_schema = CaseStatusSchema( name=status_key.get().status ) case_schema = CaseSchema( id=str(case_key_async.id()), entityKey=case_key_async.urlsafe(), name=case.name, current_status=current_status_schema, priority=case.priority, 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") ) return case_schema