def get(self, request, config_id, content_format, **kwargs): dst_user_id_or_name = request.GET[self.PARAM_DST_USER] send_email = as_bool(request.GET, self.PARAM_SEND, False) emulate_no_docs = as_bool(request.GET, self.PARAM_EMULATE_NO_DOCS, False) config = DocumentDigestConfig.objects.get(pk=config_id) if dst_user_id_or_name: try: dst_user = User.objects.get(pk=int(dst_user_id_or_name)) except ValueError: dst_user = User.objects.get(username=str(dst_user_id_or_name)) else: dst_user = request.user run_date = datetime.now(tz=dst_user.timezone or get_localzone()) try: digest = render_digest(config=config, dst_user=dst_user, run_date=run_date, emulate_no_docs=emulate_no_docs) except Exception as e: return HttpResponse(render_error( 'Exception caught while trying to render digest', e), status=500, content_type='text/plain') if not digest: return HttpResponse('Notification contains no data.', status=200) if content_format == self.FORMAT_HTML: content = digest.html content_type = 'text/html' else: content = digest.txt content_type = 'text/plain' if send_email: log = ErrorCollectingLogger() digest.send(log) log.raise_if_error() return HttpResponse(content=content, content_type=content_type, status=200)
def process(self, **kwargs): if self.PARAM_CONFIG in kwargs: config_id = kwargs[self.PARAM_CONFIG]['pk'] else: config_id = kwargs[self.PARAM_CONFIG_ID] if self.PARAM_USER in kwargs: user_ids = {kwargs[self.PARAM_USER]['pk']} else: user_ids = kwargs.get(self.PARAM_USER_IDS) run_date = kwargs.get(self.PARAM_RUN_DATE) run_date_specified = run_date is not None if isinstance(run_date, str): run_date = parse(run_date) run_date = run_date or datetime.datetime.now(tz=tzlocal.get_localzone()) run_even_if_not_enabled = bool(kwargs.get(self.PARAM_RUN_EVEN_IF_NOT_ENABLED)) config = DocumentDigestConfig.objects \ .filter(pk=config_id).select_related('for_role', 'for_user').first() # type: DocumentDigestConfig if not config: self.log_error('{1} not found: #{0}'.format(config_id, DocumentDigestConfig.__name__)) return if not config.enabled and not run_even_if_not_enabled: self.log_info('{1} #{0} is disabled.'.format(config_id, DocumentDigestConfig.__name__)) return tz_msg = ' at timezone {0}'.format(run_date.tzname()) if run_date_specified else '' self.log_info('Rendering and sending {what} #{pk} ({doc_filter}) for date "{run_date}" to {n} users{tz_msg}' .format(what=DocumentDigestConfig.__name__, pk=config.pk, doc_filter=config.documents_filter, n=len(user_ids), run_date=run_date, tz_msg=tz_msg)) if user_ids: users_qr = User.objects.filter(pk__in=user_ids) elif config.for_role_id is not None: users_qr = User.objects.filter(role_id=config.for_role_id) elif config.for_user_id is not None: users_qr = User.objects.get(pk=config.for_user_id) else: self.log_error('{what} #{config_id} specifies neither for_user nor for_role.' .format(what=DocumentDigestConfig.__name__, config_id=config.pk)) return log = CeleryTaskLogger(self) for user in users_qr: # type: User if config.for_user_id != user.id and (config.for_role_id is None or config.for_role_id != user.role_id): self.log_error('{what} #{what_id} is not applicable for user {user_name} (#{user_id})' .format(what=DocumentDigestConfig.__name__, what_id=config.pk, user_name=user.get_full_name(), user_id=user.pk)) continue try: digest = render_digest(config=config, dst_user=user, run_date=run_date) if digest: digest.send(log) except Exception as e: self.log_error(f'Unable to send {RenderedDigest}.\n' f'Config: #{config.pk}\n' f'Dst user: {user.get_full_name()} #{user.pk}\n' f'Run date: {run_date}', exc_info=e)