# -*- coding: utf-8 -*- from datetime import timedelta from optparse import make_option from random import randint from city.models import City from conference.models import Conference from country.models import Country from django.contrib.contenttypes.models import ContentType from django.core.cache import cache from django.core.management.base import BaseCommand from django.db.models.fields import related from django.utils import timezone from exposition.models import Exposition from stats_collector.models import ObjectStats, SectionStats from stats_collector.utils import unique_items_iterator from theme.models import Tag, Theme today = timezone.now().date() class Command(BaseCommand): def handle(self, *args, **options): self.create_section_test_data() self.create_objects_test_data() def create_section_test_data(self): total_objects = [Conference.objects.all()[:30], Exposition.objects.all()[:30]] bulk_objects = [] for day in xrange(92): date = today - timedelta(days=day) for model, field in [(City, 'city'), (Country, 'country'), (Theme, 'theme'), (Tag, 'tag')]: for _objects in total_objects: obj = _objects[randint(0, 29)] field_value = getattr(obj, field) if isinstance(obj._meta.get_field_by_name(field)[0], related.ManyToManyField): _field_value = field_value.all() if len(_field_value) == 0: continue field_value = _field_value[randint(0, len(_field_value) - 1)] bulk_objects.append( SectionStats( section=obj.__class__.__name__.lower()[:4], kind=field, content_object=field_value, created_at=date, value=randint(11, 898), ) ) bulk_objects.append( SectionStats( section=obj.__class__.__name__.lower()[:4], kind=field, created_at=date, value=randint(11, 898), ) ) bulk_objects.append( SectionStats( section=obj.__class__.__name__.lower()[:4], created_at=date, value=randint(11, 898), ) ) SectionStats.objects.bulk_create(bulk_objects) def create_objects_test_data(self): total_objects = { 'conference': Conference.objects.all()[:30], 'exposition': Exposition.objects.all()[:30] } bulk_objects = [] for day in xrange(92): date = today - timedelta(days=day) # for model, field in [(City, 'city'), (Country, 'country'), (Theme, 'theme'), (Tag, 'tag')]: for attr, _objects in total_objects.items(): for obj in _objects: bulk_objects.append( ObjectStats( created_at=date, value=randint(11, 898), **{attr: obj} ) ) ObjectStats.objects.bulk_create(bulk_objects)