factories.py 1.08 KB
Newer Older
luxcem's avatar
luxcem committed
1
import datetime
2
from random import choice
luxcem's avatar
luxcem committed
3
4

import pytz
5
import factory
6
from factory.fuzzy import FuzzyDateTime, FuzzyChoice
7

8
from .models import Article, STATUS_CHOICES
9
10
11
12
13


class ArticleFactory(factory.django.DjangoModelFactory):
    class Meta:
        model = Article
luxcem's avatar
luxcem committed
14

15
    url = factory.Faker("url")
16
    lang = "EN"
luxcem's avatar
luxcem committed
17

18
    title = factory.Faker("sentence", nb_words=4)
luxcem's avatar
luxcem committed
19
    website = factory.Sequence(lambda n: "Website {}".format(n))
20
    extracts = factory.Faker("text")
luxcem's avatar
luxcem committed
21
22
23
24
25
26
27
28

    created_at = FuzzyDateTime(
        datetime.datetime(2014, 1, 1, tzinfo=pytz.UTC))
    updated_at = FuzzyDateTime(
        datetime.datetime(2014, 1, 1, tzinfo=pytz.UTC))
    published_at = FuzzyDateTime(
        datetime.datetime(2014, 1, 1, tzinfo=pytz.UTC))

29
    status = FuzzyChoice([s[0] for s in STATUS_CHOICES])
30
31
32
    archive = choice([True, False])
    quote = choice([True, False])
    speak = choice([True, False])
33
34
35
36
37
38
39
40
41

    @factory.post_generation
    def tags(self, create, extracted, **kwargs):
        if not create:
            return

        if extracted:
            for tag in extracted:
                self.tags.add(tag)