) root_comment.id = self.next_id root_comment.tree_id = self.next_tree_id self.next_id += 1 self.next_tree_id += 1 tree = self.generate_tree(parent_id=root_comment.id, lft=2, level=root_comment.level) # pylint: disable=no-member root_comment.lft = 1 root_comment.rght = 2 + len(tree) * 2 yield root_comment for comment in tree: comment.content_type = ctype comment.object_id = instance.pk comment.filtered_comment = comment.original_comment[1] comment.updated = comment.submit_date comment.tree_id = root_comment.tree_id yield comment def done(self): for model in self.GENERATE_FOR_MODELS: model_class = apps.get_model(model) ctype = ContentType.objects.get_for_model(model_class) for instance in get_default_manager(model_class).all(): root = Comment.objects.get_or_create_root_comment(ctype, instance.pk)[0] update_comments_header(Comment, instance=root) if self.command is not None and self.command.verbosity > 1: self.command.stdout.write('#', ending='') register = GeneratorRegister() register.register(CommentGenerator(Comment))
# -*- coding: utf-8 -*- from __future__ import unicode_literals from datetime import datetime, timedelta from django.conf import settings from django_sample_generator import GeneratorRegister, ModelGenerator, samples from .models import Section, Topic from accounts.models import User class TopicGenerator(ModelGenerator): section_id = samples.RelationSample(queryset=Section.objects.all().order_by("pk"), random_data=True, only_pk=True, fetch_all=True) title = samples.SentenceSample() original_text = samples.ParagraphSample() created = samples.DateTimeSample(min_date=datetime.now() - timedelta(14), max_date=datetime.now()) author_id = samples.RelationSample(queryset=User.objects.all().order_by("pk"), random_data=True, only_pk=True, fetch_all=True) authors_name = samples.NameSample() def get_object(self): obj = super(TopicGenerator, self).get_object() obj.filtered_text = obj.original_text[1] obj.updated = obj.created obj.title = obj.title[:60] return obj register = GeneratorRegister() register.register(TopicGenerator(Topic, settings.INITIAL_DATA_COUNT['forum_topic']))
def get_object(self): obj = super(ArticleGenerator, self).get_object() obj.title = obj.title[:50] obj.annotation = obj.perex obj.published = True unique_slugify(obj, "slug") return obj class HitCountGenerator(ModelGenerator): hits = samples.NumberSample() object_id = samples.RelationSample( queryset=Article.objects.all().order_by("pk"), random_data=False, only_pk=True, fetch_all=True ) def __init__(self, *args, **kwargs): super(HitCountGenerator, self).__init__(*args, **kwargs) self.content_type = ContentType.objects.get_for_model(Article) def get_object(self): obj = super(HitCountGenerator, self).get_object() obj.content_type = self.content_type return obj register = GeneratorRegister() register.register(CategoryGenerator(Category, settings.INITIAL_DATA_COUNT["article_category"])) register.register(ArticleGenerator(Article, settings.INITIAL_DATA_COUNT["article_article"])) register.register(HitCountGenerator(HitCount, settings.INITIAL_DATA_COUNT["article_article"]))
from django_autoslugfield.utils import unique_slugify from django_sample_generator import GeneratorRegister, ModelGenerator, samples from .models import News, Category from accounts.models import User from django.conf import settings class NewsGenerator(ModelGenerator): title = samples.SentenceSample(unique=True) category_id = samples.RelationSample(queryset=Category.objects.all().order_by("pk"), random_data=True, only_pk=True, fetch_all=True) original_short_text = samples.ParagraphSample() author_id = samples.RelationSample(queryset=User.objects.all().order_by("pk"), random_data=True, only_pk=True, fetch_all=True) authors_name = samples.NameSample() created = samples.DateTimeSample() updated = samples.DateTimeSample() def get_object(self): obj = super(NewsGenerator, self).get_object() obj.title = obj.title[:50] obj.filtered_short_text = obj.original_short_text.field_text obj.original_long_text = obj.original_short_text obj.filtered_long_text = obj.original_long_text.field_text obj.approved = True unique_slugify(obj, 'slug') return obj register = GeneratorRegister() register.register(NewsGenerator(News, settings.INITIAL_DATA_COUNT['news_news']))
updated = samples.DateTimeSample() def get_object(self): obj = super(BlogGenerator, self).get_object() obj.filtered_description = obj.original_description[1] obj.filtered_sidebar = obj.original_sidebar[1] unique_slugify(obj, 'slug') return obj class PostGenerator(ModelGenerator): blog_id = samples.RelationSample(queryset=Blog.objects.all().order_by("pk"), random_data=True, only_pk=True, fetch_all=True) title = samples.SentenceSample(unique=True, max_length=50) original_perex = samples.ParagraphSample(max_length=500) original_content = LongHtmlGenerator() pub_time = samples.DateTimeSample() created = samples.DateTimeSample() updated = samples.DateTimeSample() linux = samples.BooleanSample() def get_object(self): obj = super(PostGenerator, self).get_object() obj.filtered_perex = obj.original_perex[1] obj.filtered_content = obj.original_content[1] unique_slugify(obj, 'slug') return obj register = GeneratorRegister() register.register(BlogGenerator(Blog, settings.INITIAL_DATA_COUNT['accounts_user'])) register.register(PostGenerator(Post, settings.INITIAL_DATA_COUNT['blog_post']))
demo.email = "*****@*****.**" demo.username = "******" demo.is_staff = False demo.is_superuser = False demo.set_password("demo") demo.last_login = last_login self.users.append(demo) kwargs["count"] = len(self.users) super(SuperuserGenerator, self).__init__(*args, **kwargs) def __iter__(self): return iter(self.users) class UserGenerator(ModelGenerator): email = samples.EmailSample(unique=True) def get_object(self): obj = super(UserGenerator, self).get_object() obj.username = obj.email.replace('@', '_').replace('.', '_') obj.is_staff = False obj.is_superuser = False obj.last_login = timezone.now() return obj register = GeneratorRegister() register.register(SuperuserGenerator(User)) register.register(UserGenerator(User, settings.INITIAL_DATA_COUNT['accounts_user'] - 2))