def get_queryset(self): queryset = self.get_base_queryset(self.request) before = self.request.query_params.get('before', None) if before: try: before = json_date_to_datetime(before) queryset = queryset.filter(modified_on__lte=before) except Exception: # pragma: needs cover queryset = queryset.filter(pk=-1) after = self.request.query_params.get('after', None) if after: try: after = json_date_to_datetime(after) queryset = queryset.filter(modified_on__gte=after) except Exception: # pragma: needs cover queryset = queryset.filter(pk=-1) phones = splitting_getlist(self.request, 'phone') # deprecated, use urns if phones: queryset = queryset.filter(urns__path__in=phones, urns__scheme=TEL_SCHEME) urns = self.request.query_params.getlist('urns', None) if urns: queryset = queryset.filter(urns__identity__in=urns) groups = self.request.query_params.getlist( 'group', None) # deprecated, use group_uuids if groups: queryset = queryset.filter( all_groups__name__in=groups, all_groups__group_type=ContactGroup.TYPE_USER_DEFINED) group_uuids = self.request.query_params.getlist('group_uuids', None) if group_uuids: queryset = queryset.filter( all_groups__uuid__in=group_uuids, all_groups__group_type=ContactGroup.TYPE_USER_DEFINED) uuids = self.request.query_params.getlist('uuid', None) if uuids: queryset = queryset.filter(uuid__in=uuids) # can't prefetch a custom manager directly, so here we prefetch user groups as new attribute user_groups_prefetch = Prefetch( 'all_groups', queryset=ContactGroup.user_groups.all(), to_attr='prefetched_user_groups') return queryset.select_related('org').prefetch_related( user_groups_prefetch).order_by('-modified_on', 'pk')
def handle_event_task(): """ Priority queue task that handles both event fires (when fired) and new incoming messages that need to be handled. Currently three types of events may be "popped" from our queue: msg - Which contains the id of the Msg to be processed fire - Which contains the id of the EventFire that needs to be fired timeout - Which contains a run that timed out and needs to be resumed """ # pop off the next task org_id, event_task = start_task(HANDLE_EVENT_TASK) # it is possible we have no message to send, if so, just return if not event_task: # pragma: needs cover return try: if event_task['type'] == MSG_EVENT: process_message_task(event_task) elif event_task['type'] == FIRE_EVENT: fire_ids = event_task.get('fires') if 'fires' in event_task else [ event_task.get('id') ] process_fire_events(fire_ids) elif event_task['type'] == TIMEOUT_EVENT: timeout_on = json_date_to_datetime(event_task['timeout_on']) process_run_timeout(event_task['run'], timeout_on) else: # pragma: needs cover raise Exception("Unexpected event type: %s" % event_task) finally: complete_task(HANDLE_EVENT_TASK, org_id)
def get_queryset(self): queryset = self.model.objects.filter( org=self.request.user.get_org(), is_active=True).order_by('-created_on') uuids = self.request.query_params.getlist('uuid', None) if uuids: queryset = queryset.filter(uuid__in=uuids) ids = self.request.query_params.getlist('flow', None) # deprecated, use uuid if ids: queryset = queryset.filter(pk__in=ids) before = self.request.query_params.get('before', None) if before: # pragma: needs cover try: before = json_date_to_datetime(before) queryset = queryset.filter(created_on__lte=before) except Exception: queryset = queryset.filter(pk=-1) after = self.request.query_params.get('after', None) if after: # pragma: needs cover try: after = json_date_to_datetime(after) queryset = queryset.filter(created_on__gte=after) except Exception: queryset = queryset.filter(pk=-1) label = self.request.query_params.getlist('label', None) if label: queryset = queryset.filter(labels__name__in=label) archived = self.request.query_params.get('archived', None) if archived is not None: queryset = queryset.filter(is_archived=str_to_bool(archived)) flow_type = self.request.query_params.getlist('type', None) if flow_type: # pragma: needs cover queryset = queryset.filter(flow_type__in=flow_type) return queryset.prefetch_related('labels')
def filter_before_after(self, queryset, field): """ Filters the queryset by the before/after params if are provided """ before = self.request.query_params.get('before') if before: try: before = json_date_to_datetime(before) queryset = queryset.filter(**{field + '__lte': before}) except Exception: queryset = queryset.filter(pk=-1) after = self.request.query_params.get('after') if after: try: after = json_date_to_datetime(after) queryset = queryset.filter(**{field + '__gte': after}) except Exception: queryset = queryset.filter(pk=-1) return queryset
def handle_event_task(): """ Priority queue task that handles both event fires (when fired) and new incoming messages that need to be handled. Currently three types of events may be "popped" from our queue: msg - Which contains the id of the Msg to be processed fire - Which contains the id of the EventFire that needs to be fired timeout - Which contains a run that timed out and needs to be resumed """ from temba.campaigns.models import EventFire r = get_redis_connection() # pop off the next task org_id, event_task = start_task(HANDLE_EVENT_TASK) # it is possible we have no message to send, if so, just return if not event_task: # pragma: needs cover return try: if event_task['type'] == MSG_EVENT: process_message_task(event_task['id'], event_task.get('from_mage', False), event_task.get('new_contact', False)) elif event_task['type'] == FIRE_EVENT: # use a lock to make sure we don't do two at once somehow key = 'fire_campaign_%s' % event_task['id'] if not r.get(key): with r.lock(key, timeout=120): event = EventFire.objects.filter(pk=event_task['id'], fired=None)\ .select_related('event', 'event__campaign', 'event__campaign__org').first() if event: print "E[%09d] Firing for org: %s" % ( event.id, event.event.campaign.org.name) start = time.time() event.fire() print "E[%09d] %08.3f s" % (event.id, time.time() - start) elif event_task['type'] == TIMEOUT_EVENT: timeout_on = json_date_to_datetime(event_task['timeout_on']) process_run_timeout(event_task['run'], timeout_on) else: # pragma: needs cover raise Exception("Unexpected event type: %s" % event_task) finally: complete_task(HANDLE_EVENT_TASK, org_id)
def handle_event_task(): """ Priority queue task that handles both event fires (when fired) and new incoming messages that need to be handled. Currently two types of events may be "popped" from our queue: msg - Which contains the id of the Msg to be processed fire - Which contains the id of the EventFire that needs to be fired timeout - Which contains a run that timed out and needs to be resumed """ from temba.campaigns.models import EventFire r = get_redis_connection() # pop off the next task event_task = pop_task(HANDLE_EVENT_TASK) # it is possible we have no message to send, if so, just return if not event_task: return if event_task['type'] == MSG_EVENT: process_message_task(event_task['id'], event_task.get('from_mage', False), event_task.get('new_contact', False)) elif event_task['type'] == FIRE_EVENT: # use a lock to make sure we don't do two at once somehow key = 'fire_campaign_%s' % event_task['id'] if not r.get(key): with r.lock(key, timeout=120): event = EventFire.objects.filter(pk=event_task['id'], fired=None)\ .select_related('event', 'event__campaign', 'event__campaign__org').first() if event: print "E[%09d] Firing for org: %s" % (event.id, event.event.campaign.org.name) start = time.time() event.fire() print "E[%09d] %08.3f s" % (event.id, time.time() - start) elif event_task['type'] == TIMEOUT_EVENT: timeout_on = json_date_to_datetime(event_task['timeout_on']) process_run_timeout(event_task['run'], timeout_on) else: raise Exception("Unexpected event type: %s" % event_task)