<commit_before>"""Model managers used by the pages application."""


from __future__ import with_statement

import threading, contextlib
from functools import wraps

from django.db import models


class PublicationManagementError(Exception):
    
    """
    Exception thrown when something goes wrong with publication management.
    """


class PublicationManager(threading.local):
    
    """
    Tracks a thread-local state of whether querysets should be filtered based on
    publication state.
    
    By default, unpublished content will be filtered out.
    """
    
    def __init__(self):
        """Initializes the PublicationManager."""
        super(PublicationManager, self).__init__()
        self._stack = []
        
    def begin(self, select_published):
        """Starts a block using the given publication setting."""
        self._stack.append(select_published)
        
    def select_published_active(self):
        """
        Returns True if querysets should be filtered to exclude unpublished
        content.
        """
        try:
            return self._stack[-1]
        except IndexError:
            return True
        
    def end(self):
        """Ends a block of publication control."""
        try:
            self._stack.pop()
        except IndexError:
            raise PublicationManagementError, "There is no active block of publication management."
        
    @contextlib.contextmanager
    def select_published(self, select_published):
        """Marks a block of publication management."""
        self.begin(select_published)
        try:
            yield
        except:
            raise
        finally:
            self.end()
            
    def getter(self, func):
        """Decorator for getters that perform subqueries on published models."""
        @wraps(func)
        def wrapper(obj, *args, **kwargs):
            with self.select_published(obj._select_published_active):
                return func(obj, *args, **kwargs)
        return wrapper
            
    
# A single, thread-safe publication manager.
publication_manager = PublicationManager()<commit_msg>Turning publication management off by default, since it interferres with management commands.<commit_after>"""Model managers used by the pages application."""


from __future__ import with_statement

import threading, contextlib
from functools import wraps

from django.db import models


class PublicationManagementError(Exception):
    
    """
    Exception thrown when something goes wrong with publication management.
    """


class PublicationManager(threading.local):
    
    """
    Tracks a thread-local state of whether querysets should be filtered based on
    publication state.
    
    By default, unpublished content will be filtered out.
    """
    
    def __init__(self):
        """Initializes the PublicationManager."""
        super(PublicationManager, self).__init__()
        self._stack = []
        
    def begin(self, select_published):
        """Starts a block using the given publication setting."""
        self._stack.append(select_published)
        
    def select_published_active(self):
        """
        Returns True if querysets should be filtered to exclude unpublished
        content.
        """
        try:
            return self._stack[-1]
        except IndexError:
            return False
        
    def end(self):
        """Ends a block of publication control."""
        try:
            self._stack.pop()
        except IndexError:
            raise PublicationManagementError, "There is no active block of publication management."
        
    @contextlib.contextmanager
    def select_published(self, select_published):
        """Marks a block of publication management."""
        self.begin(select_published)
        try:
            yield
        except:
            raise
        finally:
            self.end()
            
    def getter(self, func):
        """Decorator for getters that perform subqueries on published models."""
        @wraps(func)
        def wrapper(obj, *args, **kwargs):
            with self.select_published(obj._select_published_active):
                return func(obj, *args, **kwargs)
        return wrapper
            
    
# A single, thread-safe publication manager.
publication_manager = PublicationManager()
{'fraction_non_alphanumeric': 0.07871116225546605, 'fraction_numerical': 0.00046029919447640965, 'mean_word_length': 2.275056518462698, 'pattern_counts': {'":': 0, '<': 3, '<?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': '6063346', 'n_tokens_mistral': 1048, 'n_tokens_neox': 1032, 'n_words': 346}