<commit_before># -*- coding: utf-8 -*-
"""managers.py: Django app_metrics"""

from __future__ import unicode_literals

import logging
from django.contrib.auth.models import User
from django.db import models
from django.db.models import Q
from apps.relationship.models import Relationship

__author__ = '<NAME>'
__date__ = '4/3/13 5:26 AM'
__copyright__ = 'Copyright 2012 Pivotal Energy Solutions. All rights reserved.'
__credits__ = ['<NAME>', ]

log = logging.getLogger(__name__)


class MetricManager(models.Manager):

    def filter_by_company(self, company, **kwargs):
        """A way to trim down the list of objects by company"""
        from apps.company.models import Company
        assert isinstance(company, Company), "Need a Company"
        return self.filter(metric__company=company, **kwargs)

    def filter_by_user(self, user, **kwargs):
        """A way to trim down the list of objects by user"""
        assert isinstance(user, User), "Need a User"
        if user.is_superuser:
            return self.filter(**kwargs)
        kwargs['company'] = user.profile.company
        return self.filter_by_company(**kwargs)


    def filter_certifications_by_user(self, user, **kwargs):
        """A way to trim down the list of objects by company"""
        assert isinstance(user, User), "Need a User"

        name = kwargs.pop("name", None)
        names = [name] if name else []
        from apps.eep_program.models import EEPProgram
        if not len(names):
            names = EEPProgram.objects.filter_by_user(user).values_list('name', flat=True)
            names = ["EEP {} Certifications".format(name) for name in names]

        if user.is_superuser:
            return self.filter(metric__name__in=names, **kwargs)
        company = user.company
        if company.company_type in ["rater", "hvac", "qa", "provider"]:
            return self.filter(metric__company=company, metric__name__in=names, **kwargs)

        # Everyone else should only see data for companies in which we have mutual relationships.
        comps = Relationship.objects.get_reversed_companies(company, ids_only=True)
        # Who do I have a relationship with
        rels = company.relationships.get_companies(ids_only=True)
        # The intersection of these is what matters..
        ints = list(set(rels).intersection(set(comps)))
        data = self.filter(Q(metric__company=company) | Q(metric__company__in=ints),
                           metric__name__in=names, **kwargs)
        return data<commit_msg>Patch for Django 1.5 custom User models<commit_after># -*- coding: utf-8 -*-
"""managers.py: Django app_metrics"""

from __future__ import unicode_literals

import logging
from django.contrib.auth.models import User
from django.db import models
from django.db.models import Q
from apps.relationship.models import Relationship

__author__ = '<NAME>'
__date__ = '4/3/13 5:26 AM'
__copyright__ = 'Copyright 2012 Pivotal Energy Solutions. All rights reserved.'
__credits__ = ['<NAME>', ]

log = logging.getLogger(__name__)


class MetricManager(models.Manager):

    def filter_by_company(self, company, **kwargs):
        """A way to trim down the list of objects by company"""
        from apps.company.models import Company
        assert isinstance(company, Company), "Need a Company"
        return self.filter(metric__company=company, **kwargs)

    def filter_by_user(self, user, **kwargs):
        """A way to trim down the list of objects by user"""
        if user.is_superuser:
            return self.filter(**kwargs)
        kwargs['company'] = user.profile.company
        return self.filter_by_company(**kwargs)


    def filter_certifications_by_user(self, user, **kwargs):
        """A way to trim down the list of objects by company"""

        name = kwargs.pop("name", None)
        names = [name] if name else []
        from apps.eep_program.models import EEPProgram
        if not len(names):
            names = EEPProgram.objects.filter_by_user(user).values_list('name', flat=True)
            names = ["EEP {} Certifications".format(name) for name in names]

        if user.is_superuser:
            return self.filter(metric__name__in=names, **kwargs)
        company = user.company
        if company.company_type in ["rater", "hvac", "qa", "provider"]:
            return self.filter(metric__company=company, metric__name__in=names, **kwargs)

        # Everyone else should only see data for companies in which we have mutual relationships.
        comps = Relationship.objects.get_reversed_companies(company, ids_only=True)
        # Who do I have a relationship with
        rels = company.relationships.get_companies(ids_only=True)
        # The intersection of these is what matters..
        ints = list(set(rels).intersection(set(comps)))
        data = self.filter(Q(metric__company=company) | Q(metric__company__in=ints),
                           metric__name__in=names, **kwargs)
        return data
{'fraction_non_alphanumeric': 0.1043601130399677, 'fraction_numerical': 0.005248284214775939, 'mean_word_length': 3.170875420875421, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '2722922', 'n_tokens_mistral': 1419, 'n_tokens_neox': 1356, 'n_words': 417}