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()
Esempio n. 2
0
    def get(self, request, subscription_id, content_format, **_kwargs):
        send_email = as_bool(request.GET, self.PARAM_SEND, False)

        subscription = DocumentNotificationSubscription.objects.get(pk=subscription_id)

        document_type = subscription.document_type

        document_id = as_int(request.GET, self.PARAM_DOCUMENT, None)
        if document_id:
            document = Document.objects.filter(document_type=document_type, pk=document_id).first()
            if not document:
                return HttpResponseBadRequest('Document with id = {0} not found or has wrong type.'.format(document_id))
        else:
            document = Document.objects.filter(document_type=document_type).first()
            if not document:
                return HttpResponseBadRequest('Document id not provided and '
                                              'there are no example documents of type {0}.'.format(document_type.code))

        document_id = document.pk
        field_handlers = build_field_handlers(document_type, include_suggested_fields=False)
        field_values = get_document_field_values(document_type, document_id, handlers=field_handlers)

        example_changes = dict()
        if subscription.event in {DocumentAssignedEvent.code, DocumentChangedEvent.code} and field_values:
            for h in field_handlers:
                if random.random() > 0.3:
                    continue
                field_type = FIELD_TYPES_REGISTRY.get(h.field_type)  # type: FieldType
                field = DocumentField.objects.filter(code=h.field_code).first()
                if not field:
                    continue
                example_value = field_type.example_python_value(field=field)
                example_changes[h.field_code] = (example_value, field_values.get(h.field_code))

        notification = render_notification(already_sent_user_ids=set(),
                                           subscription=subscription,
                                           document=document,
                                           field_handlers=field_handlers,
                                           field_values=field_values,
                                           changes=example_changes,
                                           changed_by_user=request.user
                                           )
        if not notification:
            return HttpResponse('Notification contains no data.', status=200)

        if content_format == self.FORMAT_HTML:
            content = notification.html
            content_type = 'text/html'
        else:
            content = notification.txt
            content_type = 'text/plain'

        if send_email:
            log = ErrorCollectingLogger()
            notification.send(log=log)
            error = log.get_error()
            if error:
                return HttpResponseServerError(content=error, content_type='application/json')

        return HttpResponse(content=content, content_type=content_type, status=200)
Esempio n. 3
0
 def __init__(self,
              data=None,
              files=None,
              auto_id='id_%s',
              prefix=None,
              initial=None,
              error_class=ErrorList,
              label_suffix=None,
              empty_permitted=False,
              instance=None,
              use_required_attribute=None):
     super().__init__(data, files, auto_id, prefix, initial, error_class,
                      label_suffix, empty_permitted, instance,
                      use_required_attribute)
     eval_locals = IManageConfig.prepare_eval_locals(
         {}, log=ErrorCollectingLogger())
     self.fields[
         'project_resolving_code'].help_text = '''Python code returning correct Project to put the imported 
     document. Executed with Python exec. Should set "result" variable to the value it returns 
     (e.g. contain "result = 123" as its last line). 
     Local context: {0}'''.format(eval_locals)
     self.fields[
         'assignee_resolving_code'].help_text = '''Python code returning correct User to assign the imported 
     document to. Executed with Python exec. Should set "result" variable to the value it returns 
     (e.g. contain "result = 123" as its last line). Local context: {0}'''.format(
             eval_locals)
    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 suggest_field_value(field: DocumentField, document: Document, log: ProcessLogger = None):
    if not log:
        log = ErrorCollectingLogger()
    detected_field_values = detect_and_cache_field_values(log, document, field,
                                                          save=False)  # type: List[DetectedFieldValue]

    field_codes_to_values = merge_detected_field_values_to_python_value(detected_field_values)  # type: Dict[str, Any]

    return field_codes_to_values.get(field.code)
Esempio n. 6
0
    def clean(self):
        super().clean()
        example_doc = {'id': '12345', 'document_number': '67890'}
        eval_locals = IManageConfig.prepare_eval_locals(
            example_doc, log=ErrorCollectingLogger())
        password = self.cleaned_data.get('auth_password')
        if password is False and self.instance and self.instance.pk:
            conf = IManageConfig.objects.filter(
                pk=self.instance.pk).first()  # type: IManageConfig
            if conf:
                self.cleaned_data['auth_password'] = conf.auth_password

        assignee = self.cleaned_data['assignee']
        project = self.cleaned_data['project']
        assignee_code = self.cleaned_data['assignee_resolving_code']
        project_code = self.cleaned_data['project_resolving_code']

        if project and project_code:
            self.add_error(
                'project',
                'Both project and project resolving code specified. '
                'Please use only one of the options.')

        if assignee and assignee_code:
            self.add_error(
                'assignee',
                'Both assignee and assignee resolving code specified.'
                'Please use only one of the options.')

        if assignee_code:
            try:
                test_value = exec_script(
                    'assignee resolving on a test document', assignee_code,
                    eval_locals)
                if test_value is not None and not isinstance(test_value, User):
                    raise RuntimeError(
                        'Assignee resolving script must return either None or a User.'
                    )
            except RuntimeError as err:
                self.add_error('assignee_resolving_code', str(err).split('\n'))

        if project_code:
            try:
                test_value = exec_script('test project resolving',
                                         project_code, eval_locals)
                if not isinstance(test_value, Project):
                    raise RuntimeError(
                        'Project resolving script must return either a Project.'
                    )
            except RuntimeError as err:
                self.add_error('project_resolving_code', str(err).split('\n'))
Esempio n. 7
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