<commit_before># -*- coding: utf-8 -*-

import random
from easy_thumbnails.files import get_thumbnailer
import reversion
import six
import string
import os
import unittest

from datetime import datetime
from random import randint

from django.conf import settings
from django.contrib.auth.models import User
from django.core.files import File as DjangoFile
from django.core.urlresolvers import reverse
from django.db import transaction
from django.test import TestCase, TransactionTestCase
from django.utils.translation import activate, override

from cms import api
from cms.utils import get_cms_setting
from filer.models.imagemodels import Image
from parler.tests.utils import override_parler_settings
from parler.utils.conf import add_default_language_settings
from parler.utils.context import switch_language


from aldryn_categories.models import Category
from aldryn_categories.tests import CategoryTestCaseMixin

from aldryn_people.models import Person


from aldryn_newsblog.models import Article, NewsBlogConfig
from aldryn_newsblog.versioning import create_revision_with_placeholders

from . import TESTS_STATIC_ROOT

FEATURED_IMAGE_PATH = os.path.join(TESTS_STATIC_ROOT, 'featured_image.jpg')


def rand_str(prefix=u'', length=23, chars=string.ascii_letters):
    return prefix + u''.join(random.choice(chars) for _ in range(length))


class NewsBlogTestsMixin(CategoryTestCaseMixin):

    @staticmethod
    def create_user():
        return User.objects.create(username=rand_str(), first_name=rand_str(),
                                   last_name=rand_str())

    def create_person(self):
        return Person.objects.create(user=self.create_user(), slug=rand_str())

    def create_article(self, content=None, **kwargs):
        try:
            author = kwargs['author']
        except KeyError:
            author = self.create_person()

        fields = {
            'title': rand_str(),
            'slug': rand_str(),
            'author': author,
            'owner': author.user,
            'namespace': self.ns_newsblog,
            'publishing_date': datetime.now()
        }

        fields.update(kwargs)

        article = Article.objects.create(**fields)

        if content:
            api.add_plugin(article.content, 'TextPlugin',
                           self.language, body=content)

        return article

    def setup_categories(self):
        """Sets-up i18n categories (self.category_root, self.category1 and
        self.category2) for use in tests"""
        if not self.language:
            self.language = settings.LANGUAGES[0][0]

        categories = []
        # Set the default language, create the objects
        with override(self.language):
            code = "{0}-".format(self.language)
            self.category_root = Category.add_root(
                name=rand_str(prefix=code, length=8))
            categories.append(self.category_root)
            self.category1 = self.category_root.add_child(
                name=rand_str(prefix=code, length=8))
            categories.append(self.category1)
            self.category2 = self.category_root.add_child(
                name=rand_str(prefix=code, length=8))
            categories.append(self.category2)

        # We should reload category_root, since we modified its children.
        self.category_root = self.reload(self.category_root)

        # Setup the other language(s) translations for the categories
        for language, _ in settings.LANGUAGES[1:]:
            for category in categories:
                with switch_language(category, language):
                    code = "{0}-".format(language)
                    category.name = rand_str(prefix=code, length=8)
                    category.save()

    def setUp(self):
        self.template = get_cms_setting('TEMPLATES')[0][0]
        self.language = settings.LANGUAGES[0][0]
        self.ns_newsblog = NewsBlogConfig.objects.create(namespace='NBNS')
        self.page = api.create_page(
            'page', self.template, self.language, published=True,
            apphook='NewsBlogApp',
            apphook_namespace=self.ns_newsblog.namespace)
        self.page.publish(self.language)
        self.placeholder = self.page.placeholders.all()[0]

        self.setup_categories()

        self.tag_name1 = rand_str()
        self.tag_name2 = rand_str()

        for language, _ in settings.LANGUAGES[1:]:
            api.create_title(language, 'page', self.page)
            self.page.publish(language)


class TestAldrynNewsBlog(NewsBlogTestsMixin, TransactionTestCase):

    def test_create_post(self):
        article = self.create_article()
        response = self.client.get(article.get_absolute_url())
        self.assertContains(response, article.title)

    def test_delete_post(self):
        article = self.create_article()
        article_pk = article.pk
        article_url = article.get_absolute_url()
        response = self.client.get(article_url)
        self.assertContains(response, article.title)
        Article.objects.get(pk=article_pk).delete()
        response = self.client.get(article_url)
        self.assertEqual(response.status_code, 404)

    def test_articles_list(self):
        articles = [self.create_article() for _ in range(10)]
        response = self.client.get(<commit_msg>Tests: create root page and separate page for blog app<commit_after># -*- coding: utf-8 -*-

import random
from easy_thumbnails.files import get_thumbnailer
import reversion
import six
import string
import os
import unittest

from datetime import datetime
from random import randint

from django.conf import settings
from django.contrib.auth.models import User
from django.core.files import File as DjangoFile
from django.core.urlresolvers import reverse
from django.db import transaction
from django.test import TestCase, TransactionTestCase
from django.utils.translation import activate, override

from cms import api
from cms.utils import get_cms_setting
from filer.models.imagemodels import Image
from parler.tests.utils import override_parler_settings
from parler.utils.conf import add_default_language_settings
from parler.utils.context import switch_language


from aldryn_categories.models import Category
from aldryn_categories.tests import CategoryTestCaseMixin

from aldryn_people.models import Person


from aldryn_newsblog.models import Article, NewsBlogConfig
from aldryn_newsblog.versioning import create_revision_with_placeholders

from . import TESTS_STATIC_ROOT

FEATURED_IMAGE_PATH = os.path.join(TESTS_STATIC_ROOT, 'featured_image.jpg')


def rand_str(prefix=u'', length=23, chars=string.ascii_letters):
    return prefix + u''.join(random.choice(chars) for _ in range(length))


class NewsBlogTestsMixin(CategoryTestCaseMixin):

    @staticmethod
    def create_user():
        return User.objects.create(username=rand_str(), first_name=rand_str(),
                                   last_name=rand_str())

    def create_person(self):
        return Person.objects.create(user=self.create_user(), slug=rand_str())

    def create_article(self, content=None, **kwargs):
        try:
            author = kwargs['author']
        except KeyError:
            author = self.create_person()

        fields = {
            'title': rand_str(),
            'slug': rand_str(),
            'author': author,
            'owner': author.user,
            'namespace': self.ns_newsblog,
            'publishing_date': datetime.now()
        }

        fields.update(kwargs)

        article = Article.objects.create(**fields)

        if content:
            api.add_plugin(article.content, 'TextPlugin',
                           self.language, body=content)

        return article

    def setup_categories(self):
        """Sets-up i18n categories (self.category_root, self.category1 and
        self.category2) for use in tests"""
        if not self.language:
            self.language = settings.LANGUAGES[0][0]

        categories = []
        # Set the default language, create the objects
        with override(self.language):
            code = "{0}-".format(self.language)
            self.category_root = Category.add_root(
                name=rand_str(prefix=code, length=8))
            categories.append(self.category_root)
            self.category1 = self.category_root.add_child(
                name=rand_str(prefix=code, length=8))
            categories.append(self.category1)
            self.category2 = self.category_root.add_child(
                name=rand_str(prefix=code, length=8))
            categories.append(self.category2)

        # We should reload category_root, since we modified its children.
        self.category_root = self.reload(self.category_root)

        # Setup the other language(s) translations for the categories
        for language, _ in settings.LANGUAGES[1:]:
            for category in categories:
                with switch_language(category, language):
                    code = "{0}-".format(language)
                    category.name = rand_str(prefix=code, length=8)
                    category.save()

    def setUp(self):
        self.template = get_cms_setting('TEMPLATES')[0][0]
        self.language = settings.LANGUAGES[0][0]
        self.root_page = api.create_page(
            'root page', self.template, self.language, published=True)
        self.ns_newsblog = NewsBlogConfig.objects.create(namespace='NBNS')
        self.page = api.create_page(
            'page', self.template, self.language, published=True,
            parent=self.root_page,
            apphook='NewsBlogApp',
            apphook_namespace=self.ns_newsblog.namespace)
        self.placeholder = self.page.placeholders.all()[0]

        self.setup_categories()

        self.tag_name1 = rand_str()
        self.tag_name2 = rand_str()

        for page in self.root_page, self.page:
            for language, _ in settings.LANGUAGES[1:]:
                api.create_title(language, page.get_slug(), page)
                page.publish(language)


class TestAldrynNewsBlog(NewsBlogTestsMixin, TransactionTestCase):

    def test_create_post(self):
        article = self.create_article()
        response = self.client.get(article.get_absolute_url())
        self.assertContains(response, article.title)

    def test_delete_post(self):
        article = self.create_article()
        article_pk = article.pk
        article_url = article.get_absolute_url()
        response = self.client.get(article_url)
        self.assertContains(response, article.title)
        Article.objects.get(pk=article_pk).delete()
        response = self.client.get(article_url)
        self.assertEqual(response.status_code, 404)

    def test_articles_list(self):
        articles = [self.create_article() for _ in range(10)]
        response = self.client.get(
{'fraction_non_alphanumeric': 0.09266802443991853, 'fraction_numerical': 0.006109979633401222, 'mean_word_length': 2.811926605504587, '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': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6430011', 'n_tokens_mistral': 3069, 'n_tokens_neox': 2928, 'n_words': 658}