def insert(cls, user_from_email, request): parent_key = ndb.Key(urlsafe=request.about) note_author = Userinfo() note_author.display_name = user_from_email.google_display_name note_author.photo = user_from_email.google_public_profile_photo_url note = Note(owner=user_from_email.google_user_id, organization=user_from_email.organization, author=note_author, title=request.title, content=request.content) entityKey_async = note.put_async() entityKey = entityKey_async.get_result() note.put_index() Edge.insert(start_node=parent_key, end_node=entityKey, kind='topics', inverse_edge='parents') author_shema = AuthorSchema( google_user_id=note.owner, display_name=note_author.display_name, google_public_profile_url=note_author.google_public_profile_url, photo=note_author.display_name, edgeKey="", email=note_author.email) note_schema = NoteSchema(id=str(note.key.id()), entityKey=note.key.urlsafe(), title=note.title, content=note.content, created_by=author_shema) return note_schema
def update_status(cls, user_from_email, request): case_key = ndb.Key(urlsafe=request.entityKey) status_key = ndb.Key(urlsafe=request.status) # insert edges Edge.insert(start_node=case_key, end_node=status_key, kind='status', inverse_edge='related_cases')
def update_status(cls, user_from_email, request): case_key = ndb.Key(urlsafe=request.entityKey) status_key = ndb.Key(urlsafe=request.status) # insert edges Edge.insert(start_node=case_key, end_node=status_key, kind='status', inverse_edge='related_cases')
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 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 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(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 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 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(
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 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
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'