def filter_queryset(self, request, queryset, view): model_type = getattr(view, 'model_type', None) if not model_type: return queryset search_terms = self.get_search_terms(request) if not search_terms: return queryset if 'limit' in request.query_params: limit = request.query_params['limit'] search = LilySearch( tenant_id=request.user.tenant_id, model_type=model_type, size=int(limit), ) else: search = LilySearch( tenant_id=request.user.tenant_id, model_type=model_type, ) search.filter_query(' AND '.join(search_terms)) ids = [result['id'] for result in search.do_search(['id'])[0]] return queryset.filter(id__in=ids)
def history(self, request, pk): """ Returns what happend to an email """ email = self.get_object() account_email = email.account.email_address sent_from_account = (account_email == email.sender.email_address) search = LilySearch( request.user.tenant_id, model_type='email_emailmessage', sort='sent_date', ) search.filter_query('thread_id:%s' % email.thread_id) history, total, took = search.do_search([ 'message_id', 'received_by_email', 'received_by_cc_email', 'sender_email', 'sender_name', 'sent_date', ]) try: index = (key for key, item in enumerate(history) if item['message_id'] == email.message_id).next() except StopIteration: logger.exception('No history for message %s\nhistory:\n%s' % (email.id, history)) results = { 'history_size': 0, } return Response(results) messages_after = history[index + 1:] results = { 'history': history, 'history_size': total, } if messages_after: if sent_from_account: next_messages = [item for item in messages_after if item['sender_email'] != account_email] if len(next_messages): next_message = next_messages[0] email_addresses = next_message.get('received_by_email', []) + next_message.get('received_by_cc_email', []) if email_addresses.count(email.account.email_address): results['replied_with'] = next_message else: # If the message was received, we want to know if we did something with it next_messages = [item for item in messages_after if item['sender_email'] == account_email] if len(next_messages): next_message = next_messages[0] email_addresses = next_message.get('received_by_email', []) + next_message.get('received_by_cc_email', []) if email_addresses.count(email.sender.email_address): results['replied_with'] = next_message else: results['forwarded_with'] = next_message return Response(results)
def get_items(self): search = LilySearch( tenant_id=self.request.user.tenant_id, model_type='accounts_account', page=0, size=1000000000, ) if self.request.GET.get('export_filter'): search.query_common_fields(self.request.GET.get('export_filter')) return search.do_search()[0]
def history(self, request, pk): """ Returns what happened to an email; did the user reply or forwarded the email message. """ email = self.get_object() account_email = email.account.email_address sent_from_account = (account_email == email.sender.email_address) # Get the messages in the thread for the current email message. search = LilySearch( request.user.tenant_id, model_type='email_emailmessage', sort='sent_date', size=100 # Paged results, when a thread is containing more that 100 results StopIteration could be thrown. ) search.filter_query('thread_id:%s' % email.thread_id) thread, facets, total, took = search.do_search([ 'message_id', 'received_by_email', 'received_by_cc_email', 'sender_email', 'sender_name', 'sent_date', ]) try: # Get the index of the current email message in the thread. index = (key for key, item in enumerate(thread) if item['message_id'] == email.message_id).next() except StopIteration: logger.exception('Number of messages larger that search page size for message %s.' % email.id) results = {} return Response(results) # Only look at the messages in the thread after the current email message. messages_after = thread[index + 1:] results = {} if not messages_after or sent_from_account: # Current email message is the last in the thread or is send by the user and therefor not a possible reply # or forwarded email message. return Response(results) # The current email message isn't the last in the thread. So look the follow up messages in the thread to # determine what actions occured on the current email message. Current email message is not send from the # account of the user, so it is a received email message. So determine is we replied or forwarded it. # Get all the outgoing follow up messages in the thread. next_messages = [item for item in messages_after if item['sender_email'] == account_email] if len(next_messages): # We only need to look at the first follow up message in the thread. # TODO LILY-2244: improve search filter above so it leads to the only / first follow up message. next_message = next_messages[0] # Get all the mail addresses where the follow up message was sent to. email_addresses = next_message.get('received_by_email', []) + next_message.get('received_by_cc_email', []) if email_addresses.count(email.sender.email_address): # The sender of the current, received email message is one of the reciepents of the follow up message, # so it is a reply message. results['replied_with'] = next_message else: results['forwarded_with'] = next_message return Response(results)
def history(self, request, pk): """ Returns what happend to an email """ email = self.get_object() account_email = email.account.email_address sent_from_account = (account_email == email.sender.email_address) search = LilySearch( request.user.tenant_id, model_type='email_emailmessage', sort='sent_date', ) search.filter_query('thread_id:%s' % email.thread_id) history, total, took = search.do_search([ 'message_id', 'received_by_email', 'received_by_cc_email', 'sender_email', 'sender_name', 'sent_date', ]) try: index = (key for key, item in enumerate(history) if item['message_id'] == email.message_id).next() except StopIteration: logger.exception('No history for message %s\nhistory:\n%s' % (email.id, history)) results = { 'history_size': 0, } return Response(results) messages_after = history[index + 1:] results = { 'history': history, 'history_size': total, } if messages_after: if sent_from_account: next_messages = [ item for item in messages_after if item['sender_email'] != account_email ] if len(next_messages): next_message = next_messages[0] email_addresses = next_message.get( 'received_by_email', []) + next_message.get( 'received_by_cc_email', []) if email_addresses.count(email.account.email_address): results['replied_with'] = next_message else: # If the message was received, we want to know if we did something with it next_messages = [ item for item in messages_after if item['sender_email'] == account_email ] if len(next_messages): next_message = next_messages[0] email_addresses = next_message.get( 'received_by_email', []) + next_message.get( 'received_by_cc_email', []) if email_addresses.count(email.sender.email_address): results['replied_with'] = next_message else: results['forwarded_with'] = next_message return Response(results)
def history(self, request, pk): """ Returns what happened to an email; did the user reply or forwarded the email message. """ email = self.get_object() account_email = email.account.email_address sent_from_account = (account_email == email.sender.email_address) # Get the messages in the thread for the current email message. search = LilySearch( request.user.tenant_id, model_type='email_emailmessage', sort='sent_date', size= 100 # Paged results, when a thread is containing more that 100 results StopIteration could be thrown. ) search.filter_query('thread_id:%s' % email.thread_id) thread, facets, total, took = search.do_search([ 'message_id', 'received_by_email', 'received_by_cc_email', 'sender_email', 'sender_name', 'sent_date', ]) try: # Get the index of the current email message in the thread. index = (key for key, item in enumerate(thread) if item['message_id'] == email.message_id).next() except StopIteration: logger.exception( 'Number of messages larger that search page size for message %s.' % email.id) results = {} return Response(results) # Only look at the messages in the thread after the current email message. messages_after = thread[index + 1:] results = {} if not messages_after or sent_from_account: # Current email message is the last in the thread or is send by the user and therefor not a possible reply # or forwarded email message. return Response(results) # The current email message isn't the last in the thread. So look the follow up messages in the thread to # determine what actions occured on the current email message. Current email message is not send from the # account of the user, so it is a received email message. So determine is we replied or forwarded it. # Get all the outgoing follow up messages in the thread. next_messages = [ item for item in messages_after if item['sender_email'] == account_email ] if len(next_messages): # We only need to look at the first follow up message in the thread. # TODO LILY-2244: improve search filter above so it leads to the only / first follow up message. next_message = next_messages[0] # Get all the mail addresses where the follow up message was sent to. email_addresses = next_message.get('received_by_email', []) + next_message.get( 'received_by_cc_email', []) if email_addresses.count(email.sender.email_address): # The sender of the current, received email message is one of the reciepents of the follow up message, # so it is a reply message. results['replied_with'] = next_message else: results['forwarded_with'] = next_message return Response(results)