<commit_before>from django.conf import settings
from persistent_messages.models import Message
from persistent_messages.constants import PERSISTENT_MESSAGE_LEVELS
from django.contrib.auth.models import AnonymousUser
from django.contrib.messages.storage.fallback import FallbackStorage
from django.db.models import Q
import datetime

def get_user(request):
    if hasattr(request, 'user') and request.user.__class__ != AnonymousUser:
        return request.user
    else:
        return AnonymousUser()

"""
Messages need a primary key when being displayed so that they can be closed/marked as read by the user.
Hence, they need to be stored when being added. You can disable this, but then you'll only be able to 
close a message when it is displayed for the second time. 
"""
STORE_WHEN_ADDING = getattr(settings, 'MESSAGES_STORE_WHEN_ADDING', True)

#@TODO USE FALLBACK 
class PersistentMessageStorage(FallbackStorage):
    def __init__(self, *args, **kwargs):
        super(PersistentMessageStorage, self).__init__(*args, **kwargs)
        self.non_persistent_messages = []
        self.is_anonymous = not get_user(self.request).is_authenticated()

    def _message_queryset(self, exclude_unread=True):
        qs = Message.objects.filter(user=get_user(self.request)).filter(
            Q(expires=None) | Q(expires__gt=datetime.datetime.now()))
        if exclude_unread:
            qs = qs.exclude(read=True)
        return qs

    def _get(self, *args, **kwargs):
        """
        Retrieves a list of stored messages. Returns a tuple of the messages
        and a flag indicating whether or not all the messages originally
        intended to be stored in this storage were, in fact, stored and
        retrieved; e.g., ``(messages, all_retrieved)``.
        """
        if not get_user(self.request).is_authenticated():
            return super(PersistentMessageStorage, self)._get(*args, **kwargs)
        messages = []
        for message in self._message_queryset():
            if not message.is_persistent():
                self.non_persistent_messages.append(message)
            messages.append(message)
        return (messages, True)

    def get_persistent(self):
        return self._message_queryset(exclude_unread=False).filter(level__in=PERSISTENT_MESSAGE_LEVELS)

    def get_persistent_unread(self):
        return self._message_queryset(exclude_unread=True).filter(level__in=PERSISTENT_MESSAGE_LEVELS)

    def count_unread(self):
        return self._message_queryset(exclude_unread=True).count()

    def count_persistent_unread(self):
        return self.get_persistent_unread().count()

    def _delete_non_persistent(self):
        for message in self.non_persistent_messages:
            message.delete()
        self.non_persistent_messages = []
        self.non_persistent_messages = []

    def __iter__(self):
        if not get_user(self.request).is_authenticated():
            return super(PersistentMessageStorage, self).__iter__()
        self.used = True
        messages = []
        messages.extend(self._loaded_messages)
        if self._queued_messages:
            messages.extend(self._queued_messages)
        return iter(messages)

    def _prepare_messages(self, messages):
        if not get_user(self.request).is_authenticated():
            return super(PersistentMessageStorage, self)._prepare_messages(messages)
        """
        Obsolete method since model takes care of this.
        """
        pass

    def _store(self, messages, response, *args, **kwargs):
        """
        Stores a list of messages, returning a list of any messages which could
        not be stored.

        If STORE_WHEN_ADDING is True, messages are already stored at this time and won't be
        saved again.
        """

        messages = [message for message in messages if not message.is_sticky()]

        if not get_user(self.request).is_authenticated():
            return super(PersistentMessageStorage, self)._store(messages, response, *args, **kwargs)
        for message in messages:
            if not self.used or message.is_persistent():
                if not message.pk:
                    message.save()
        return []

    def update(self, response):
        if not get_user(self.request).is_authenticated():
            return super(PersistentMessageStorage, self).update(response)
        """
        Deletes all non-persistent, read messages. Saves all unstored messages.
        """
        if self.used:
            self._delete_non_persistent()
        return super(PersistentMessageStorage, self).update(response)

    def add(self, level, message, extra_tags='', subject='', user=None, from_user=None, expires=None,
            close_timeout=None):
        """
        Queues a message to be stored.

        The message is only queued if it contained something and its level is
        not less than the recording level (``self.level``).
        """
        to_user = user or get_user(self.request)
        if not to_user.is_authenticated() or Message(
            level=level).is_sticky():# si es sticky también dejar que lo procese la session
            if Message(level=level).is_persistent():
                raise NotImplementedError('Persistent message levels cannot be used for anonymous users.')
            else:
                return super(PersistentMessageStorage, self).add(level, message, extra_tags)
        if not message:
            return
            # Check that the message level is not less than the recording level.
        level = int(level)
        if level < self.level:
            return
            # Add the message.
        message = Message(user=to_user, level=level, message=message, extra_tags=extra_tags, subject=subject,
            from_user=from_user, expires=expires, close_timeout=close_timeout)
        # Messages need a primary key when being displayed so that they can be closed/marked as read by the user.
        # Hence, save it now instead of adding it to queue:
        if STORE_WHEN_ADDING:
            message.save()
        else:
            self.added_new = True
            self._queued_messages.append(message)<commit_msg>Fix some problems with the characters in python<commit_after>from django.conf import settings
from persistent_messages.models import Message
from persistent_messages.constants import PERSISTENT_MESSAGE_LEVELS
from django.contrib.auth.models import AnonymousUser
from django.contrib.messages.storage.fallback import FallbackStorage
from django.db.models import Q
import datetime

def get_user(request):
    if hasattr(request, 'user') and request.user.__class__ != AnonymousUser:
        return request.user
    else:
        return AnonymousUser()

"""
Messages need a primary key when being displayed so that they can be closed/marked as read by the user.
Hence, they need to be stored when being added. You can disable this, but then you'll only be able to 
close a message when it is displayed for the second time. 
"""
STORE_WHEN_ADDING = getattr(settings, 'MESSAGES_STORE_WHEN_ADDING', True)

#@TODO USE FALLBACK 
class PersistentMessageStorage(FallbackStorage):
    def __init__(self, *args, **kwargs):
        super(PersistentMessageStorage, self).__init__(*args, **kwargs)
        self.non_persistent_messages = []
        self.is_anonymous = not get_user(self.request).is_authenticated()

    def _message_queryset(self, exclude_unread=True):
        qs = Message.objects.filter(user=get_user(self.request)).filter(
            Q(expires=None) | Q(expires__gt=datetime.datetime.now()))
        if exclude_unread:
            qs = qs.exclude(read=True)
        return qs

    def _get(self, *args, **kwargs):
        """
        Retrieves a list of stored messages. Returns a tuple of the messages
        and a flag indicating whether or not all the messages originally
        intended to be stored in this storage were, in fact, stored and
        retrieved; e.g., ``(messages, all_retrieved)``.
        """
        if not get_user(self.request).is_authenticated():
            return super(PersistentMessageStorage, self)._get(*args, **kwargs)
        messages = []
        for message in self._message_queryset():
            if not message.is_persistent():
                self.non_persistent_messages.append(message)
            messages.append(message)
        return (messages, True)

    def get_persistent(self):
        return self._message_queryset(exclude_unread=False).filter(level__in=PERSISTENT_MESSAGE_LEVELS)

    def get_persistent_unread(self):
        return self._message_queryset(exclude_unread=True).filter(level__in=PERSISTENT_MESSAGE_LEVELS)

    def count_unread(self):
        return self._message_queryset(exclude_unread=True).count()

    def count_persistent_unread(self):
        return self.get_persistent_unread().count()

    def _delete_non_persistent(self):
        for message in self.non_persistent_messages:
            message.delete()
        self.non_persistent_messages = []
        self.non_persistent_messages = []

    def __iter__(self):
        if not get_user(self.request).is_authenticated():
            return super(PersistentMessageStorage, self).__iter__()
        self.used = True
        messages = []
        messages.extend(self._loaded_messages)
        if self._queued_messages:
            messages.extend(self._queued_messages)
        return iter(messages)

    def _prepare_messages(self, messages):
        if not get_user(self.request).is_authenticated():
            return super(PersistentMessageStorage, self)._prepare_messages(messages)
        """
        Obsolete method since model takes care of this.
        """
        pass

    def _store(self, messages, response, *args, **kwargs):
        """
        Stores a list of messages, returning a list of any messages which could
        not be stored.

        If STORE_WHEN_ADDING is True, messages are already stored at this time and won't be
        saved again.
        """

        messages = [message for message in messages if not message.is_sticky()]

        if not get_user(self.request).is_authenticated():
            return super(PersistentMessageStorage, self)._store(messages, response, *args, **kwargs)
        for message in messages:
            if not self.used or message.is_persistent():
                if not message.pk:
                    message.save()
        return []

    def update(self, response):
        if not get_user(self.request).is_authenticated():
            return super(PersistentMessageStorage, self).update(response)
        """
        Deletes all non-persistent, read messages. Saves all unstored messages.
        """
        if self.used:
            self._delete_non_persistent()
        return super(PersistentMessageStorage, self).update(response)

    def add(self, level, message, extra_tags='', subject='', user=None, from_user=None, expires=None,
            close_timeout=None):
        """
        Queues a message to be stored.

        The message is only queued if it contained something and its level is
        not less than the recording level (``self.level``).
        """
        to_user = user or get_user(self.request)
        if not to_user.is_authenticated() or Message(level=level).is_sticky():
            if Message(level=level).is_persistent():
                raise NotImplementedError('Persistent message levels cannot be used for anonymous users.')
            else:
                #if user is not logged or message is sticky
                return super(PersistentMessageStorage, self).add(level, message, extra_tags)
        if not message:
            return
            # Check that the message level is not less than the recording level.
        level = int(level)
        if level < self.level:
            return
            # Add the message.
        message = Message(user=to_user, level=level, message=message, extra_tags=extra_tags, subject=subject,
            from_user=from_user, expires=expires, close_timeout=close_timeout)
        # Messages need a primary key when being displayed so that they can be closed/marked as read by the user.
        # Hence, save it now instead of adding it to queue:
        if STORE_WHEN_ADDING:
            message.save()
        else:
            self.added_new = True
            self._queued_messages.append(message)
{'fraction_non_alphanumeric': 0.08385370205173952, 'fraction_numerical': 0.0, 'mean_word_length': 2.773561811505508, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5977363', 'n_tokens_mistral': 3368, 'n_tokens_neox': 3271, 'n_words': 972}