<commit_before># -*- coding: utf-8 -*-
from datetime import datetime, timedelta
from django.contrib.auth.models import User
from django.utils.translation import ugettext_lazy as _

from qsstats import QuerySetStats
from admin_tools.dashboard import modules


class RegistrationChart(modules.DashboardModule):
    """
    Dashboard module with user registration charts.

    With default values it is suited best for 2-column dashboard layouts.
    """
    title = _('Registration chart')
    template = 'admin_user_stats/modules/chart.html'
    chart_size = "580x100"
    days = None
    interval = 'days'

    def is_empty(self):
        return False

    def __init__(self, *args, **kwargs):
        super(RegistrationChart, self).__init__(*args, **kwargs)

        if self.days is None:
            self.days = {'days': 30, 'weeks': 30*7, 'months': 30*12}[self.interval]

        self.data = self.get_registrations(self.interval, self.days)
        self.prepare_template_data(self.data)

    def get_caption(self, dt):
        return {
            'days': dt.day,
            'months': dt.strftime("%b"),
            'weeks': dt.strftime('%W'),
        }[self.interval]

    # @cached(60*5)
    def get_registrations(self, interval, days):
        """ Returns an array with new users count per interval """
        stats = QuerySetStats(User.objects.filter(is_active=True), 'date_joined')
        today = datetime.today()
        begin = today - timedelta(days=days-1)
        return stats.time_series(begin, today+timedelta(days=1), interval)

    def prepare_template_data(self, data):
        """ Prepares data for template (it is passed as module attributes) """
        self.captions = [self.get_caption(t[0]) for t in data]
        self.values = [t[1] for t in data]
        self.max_value = max(self.values)

def get_registration_charts():
    """ Returns 3 basic chart modules (per-day, per-week and per-month) """
    return [
        RegistrationChart(_('By Day'), interval='days'),
        RegistrationChart(_('By Week'), interval='weeks'),
        RegistrationChart(_('By Month'), interval='months'),
    ]

class RegistrationCharts(modules.Group):
    """ Group module with 3 default registration charts """
    title = _('New Users')

    def __init__(self, *args, **kwargs):
        kwargs.setdefault('children', get_registration_charts())
        super(RegistrationCharts, self).__init__(*args, **kwargs)
<commit_msg>Make datetime in the module timezone aware for django 1.5<commit_after># -*- coding: utf-8 -*-
from datetime import timedelta
from django.contrib.auth.models import User
from django.utils.translation import ugettext_lazy as _
try:
    from django.utils.timezone import now
except ImportError:
    from datetime import datetime
    now = datetime.now

from qsstats import QuerySetStats
from admin_tools.dashboard import modules


class RegistrationChart(modules.DashboardModule):
    """
    Dashboard module with user registration charts.

    With default values it is suited best for 2-column dashboard layouts.
    """
    title = _('Registration chart')
    template = 'admin_user_stats/modules/chart.html'
    chart_size = "580x100"
    days = None
    interval = 'days'

    def is_empty(self):
        return False

    def __init__(self, *args, **kwargs):
        super(RegistrationChart, self).__init__(*args, **kwargs)

        if self.days is None:
            self.days = {'days': 30, 'weeks': 30*7, 'months': 30*12}[self.interval]

        self.data = self.get_registrations(self.interval, self.days)
        self.prepare_template_data(self.data)

    def get_caption(self, dt):
        return {
            'days': dt.day,
            'months': dt.strftime("%b"),
            'weeks': dt.strftime('%W'),
        }[self.interval]

    # @cached(60*5)
    def get_registrations(self, interval, days):
        """ Returns an array with new users count per interval """
        stats = QuerySetStats(User.objects.filter(is_active=True), 'date_joined')
        today = now()
        begin = today - timedelta(days=days-1)
        return stats.time_series(begin, today+timedelta(days=1), interval)

    def prepare_template_data(self, data):
        """ Prepares data for template (it is passed as module attributes) """
        self.captions = [self.get_caption(t[0]) for t in data]
        self.values = [t[1] for t in data]
        self.max_value = max(self.values)

def get_registration_charts():
    """ Returns 3 basic chart modules (per-day, per-week and per-month) """
    return [
        RegistrationChart(_('By Day'), interval='days'),
        RegistrationChart(_('By Week'), interval='weeks'),
        RegistrationChart(_('By Month'), interval='months'),
    ]

class RegistrationCharts(modules.Group):
    """ Group module with 3 default registration charts """
    title = _('New Users')

    def __init__(self, *args, **kwargs):
        kwargs.setdefault('children', get_registration_charts())
        super(RegistrationCharts, self).__init__(*args, **kwargs)

{'fraction_non_alphanumeric': 0.12372982665869695, 'fraction_numerical': 0.010759115361625823, 'mean_word_length': 3.1763727121464225, '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': '1850460', 'n_tokens_mistral': 1517, 'n_tokens_neox': 1424, 'n_words': 395}