Example #1
0
    def broadcast_message_to_channel(message: ChannelMessage,
                                     target_channel: str) -> None:
        try:
            message_obj = {
                'type': 'send_message',
                'content': jsonpickle.encode(message, unpicklable=False)
            }

            layer = get_channel_layer()
            async_to_sync(layer.group_send)(target_channel, message_obj)
        except Exception:
            type, value, _ = sys.exc_info()
            get_django_logger().error(
                f'Error in broadcast_message_to_channel: {type}, {value}')
Example #2
0
 def decorator(*args, **kwargs):
     try:
         return func(*args, **kwargs)
     except ObjectDoesNotExist as e:
         logger = get_django_logger()
         logger.warning(
             f'{func.__name__} failed because related object was probably deleted, '
             f'original exception is "{e}"')
Example #3
0
def notify_field_annotation_deleted(instance: FieldAnnotation):
    try:
        message = ChannelMessage(
            message_types.CHANNEL_MSG_TYPE_FIELD_ANNOTATION_DELETED, {
                'annotation': _annotation_to_dto(instance),
                'user': _get_user_dto(instance)
            })
        notify_on_document_changes(instance.document.pk, message)
    except ObjectDoesNotExist:
        logger = get_django_logger()
        logger.warning(f'notify_field_annotation_deleted is called for '
                       f'field {instance.field_id}, that was probably deleted')
        return
class DocumentFilesCleaner:
    logger = get_django_logger()

    @staticmethod
    def delete_document_files(paths: List[str],
                              logger: Callable = None) -> None:
        stor = get_file_storage()
        for path in paths:
            try:
                stor.delete_document(path)
            except Exception as e:
                msg = f'Unable to delete file "{path}" in {type(stor).__name__}'
                DocumentFilesCleaner.log_error(msg, e, logger)

    @staticmethod
    def log_error(msg: str, e: Any = None, logger: Callable = None) -> None:
        if logger:
            logger(msg, e)
            return
        task_logger = DocumentFilesCleaner.logger
        task_logger.error(msg, e)
Example #5
0
def _notify_field_value_saved(instance: FieldValue, deleted=False):
    try:
        field_value = {
            'document': instance.document_id,
            'project_id': instance.document.project_id,
            'field__code': instance.field.code,
            'value': instance.value if not deleted else None
        }
    except ObjectDoesNotExist:
        logger = get_django_logger()
        logger.warning(f'_notify_field_value_saved is called for '
                       f'field {instance.field_id}, that was probably deleted')
        return

    annotation_stats = DocumentFieldRepository(
    ).get_annotation_stats_by_field_value(instance)

    message = ChannelMessage(
        message_types.CHANNEL_MSG_TYPE_FIELD_VALUE_SAVED, {
            'field_value': field_value,
            'annotation_stats': annotation_stats,
            'user': _get_user_dto(instance)
        })
    notify_on_document_changes(instance.document.pk, message)
__version__ = "1.8.0"
__maintainer__ = "LexPredict, LLC"
__email__ = "*****@*****.**"


import pickle
from typing import List, Dict
import pandas as pd
from django.utils.timezone import now

from apps.common.models import ObjectStorage
from apps.common.singleton import Singleton
from apps.task.utils.logger import get_django_logger


logger = get_django_logger()


class LocatingPerformanceRecord:
    def __init__(self, locator: str = '', duration: float = 0.0,
                 text_unit_id: int = 0, text_length: int = 0,
                 text_hash: int = 0):
        self.locator = locator
        self.duration = duration
        self.text_unit_id = text_unit_id
        self.text_length = text_length
        self.text_hash = text_hash

    @classmethod
    def get_text_hash(cls, text: str) -> int:
        return hash(text or '')
 def get_logger() -> Logger:
     return get_django_logger()
Example #8
0
 def __init__(self, *args, **kwargs):
     super().__init__(*args, **kwargs)
     self.logger = get_django_logger()
     self.ws = Websockets()
Example #9
0
 def _notify_upload_started(cls, session: UploadSession) -> None:
     try:
         session.notify_upload_started()
     except Exception as exc:
         get_django_logger().exception(exc)
 def __init__(self, *args, **kwargs):
     super().__init__(*args, **kwargs)
     self.groups_by_channel = {}  # type: Dict[str, Set[str]]
     self.logger = get_django_logger()