def _fire_documents_changed(self, doc_qr, user: User):
     log = ErrorCollectingLogger()
     signals.fire_documents_changed(self.__class__,
                                    log,
                                    doc_qr,
                                    changed_by_user=user)
     log.raise_if_error()
    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)
Example #3
0
def suggest_field_value(doc: Document, field: DocumentField) -> Any:
    log = ErrorCollectingLogger()
    field_value_dto = detect_field_value(log, doc=doc, field=field, save=False)
    log.raise_if_error()
    return field_value_dto.field_value if field_value_dto else None