<commit_before>from datetime import datetime
from collections import defaultdict
from django.db.models import Manager
from alert.utils import ALERT_TYPES, ALERT_BACKENDS
from django.contrib.auth.models import User


class AlertManager(Manager): pass


class PendingAlertManager(AlertManager):
    
    def get_query_set(self, *args, **kwargs):
        qs = super(PendingAlertManager, self).get_query_set(*args, **kwargs)
        return qs.filter(is_sent=False, failed=False, when__gte=datetime.now())
    
    
    
    
class AlertPrefsManager(Manager):
    
    def get_user_prefs(self, user):
        if not user.is_authenticated():
            return dict(((notice_type.id, backend.id), False)
                            for notice_type in ALERT_TYPES.values()
                            for backend in ALERT_BACKENDS.values()
                        )
        
            
        alert_prefs = self.get_query_set().filter(user=user)
        
        prefs = {}
        for pref in alert_prefs:
            prefs[pref.alert_type, pref.backend] = pref.preference
        
        for notice_type in ALERT_TYPES.values():
            for backend in ALERT_BACKENDS.values():
                if (notice_type.id, backend.id) not in prefs:
                    default_pref = notice_type.get_default(backend.id)
                    prefs[notice_type.id, backend.id] = default_pref
        return prefs
    
                
    def get_recipients_for_notice(self, notice_type, users):
        if isinstance(notice_type, basestring):
            notice_type = ALERT_TYPES[notice_type]
        
        alert_prefs = self.get_query_set().filter(alert_type=notice_type.id).filter(user__in=users)
        
        prefs = {}
        for pref in alert_prefs:
            prefs[pref.user_id, pref.backend] = pref.preference
        
        for user in users:
            for backend in ALERT_BACKENDS.values():
                if (user.id, backend.id) not in prefs:
                    prefs[user.id, backend.id] = notice_type.get_default(backend.id)
        
        return tuple((User.objects.get(id=user_id), ALERT_BACKENDS[backend_id]) for ((user_id, backend_id), pref) in prefs.items() if pref)
    
<commit_msg>fix pending objects manager<commit_after>from datetime import datetime
from collections import defaultdict
from django.db.models import Manager
from alert.utils import ALERT_TYPES, ALERT_BACKENDS
from django.contrib.auth.models import User


class AlertManager(Manager): pass


class PendingAlertManager(AlertManager):
    
    def get_query_set(self, *args, **kwargs):
        qs = super(PendingAlertManager, self).get_query_set(*args, **kwargs)
        return qs.filter(is_sent=False)
    
    
    
    
class AlertPrefsManager(Manager):
    
    def get_user_prefs(self, user):
        if not user.is_authenticated():
            return dict(((notice_type.id, backend.id), False)
                            for notice_type in ALERT_TYPES.values()
                            for backend in ALERT_BACKENDS.values()
                        )
        
            
        alert_prefs = self.get_query_set().filter(user=user)
        
        prefs = {}
        for pref in alert_prefs:
            prefs[pref.alert_type, pref.backend] = pref.preference
        
        for notice_type in ALERT_TYPES.values():
            for backend in ALERT_BACKENDS.values():
                if (notice_type.id, backend.id) not in prefs:
                    default_pref = notice_type.get_default(backend.id)
                    prefs[notice_type.id, backend.id] = default_pref
        return prefs
    
                
    def get_recipients_for_notice(self, notice_type, users):
        if isinstance(notice_type, basestring):
            notice_type = ALERT_TYPES[notice_type]
        
        alert_prefs = self.get_query_set().filter(alert_type=notice_type.id).filter(user__in=users)
        
        prefs = {}
        for pref in alert_prefs:
            prefs[pref.user_id, pref.backend] = pref.preference
        
        for user in users:
            for backend in ALERT_BACKENDS.values():
                if (user.id, backend.id) not in prefs:
                    prefs[user.id, backend.id] = notice_type.get_default(backend.id)
        
        return tuple((User.objects.get(id=user_id), ALERT_BACKENDS[backend_id]) for ((user_id, backend_id), pref) in prefs.items() if pref)
    

{'fraction_non_alphanumeric': 0.08704545454545455, 'fraction_numerical': 0.0, 'mean_word_length': 2.145818441744103, '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': '3900863', 'n_tokens_mistral': 1303, 'n_tokens_neox': 1302, 'n_words': 273}