Skip to content

Commit f8a1fcd

Browse files
committed
Moved fixture creation scripts to manage commands.
1 parent 2551493 commit f8a1fcd

File tree

9 files changed

+48
-34
lines changed

9 files changed

+48
-34
lines changed

Makefile

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ create_database:
77
./manage.py createsuperuser --username=root [email protected] --noinput
88

99
make_fixtures:
10-
DJANGO_SETTINGS_MODULE='example.settings' ./scripts/create_users.py
11-
DJANGO_SETTINGS_MODULE='example.settings' ./scripts/create_posts.py
12-
DJANGO_SETTINGS_MODULE='example.settings' ./scripts/create_photos.py
10+
./manage.py create_users
11+
./manage.py create_posts
12+
./manage.py create_photos
1313

1414
all: clean create_database make_fixtures

example/api/management/__init__.py

Whitespace-only changes.

example/api/management/commands/__init__.py

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import os
2+
import os.path
3+
4+
from django.core.management.base import BaseCommand, CommandError
5+
from django.conf import settings
6+
7+
from example.api.models import Photo, Post
8+
9+
10+
class Command(BaseCommand):
11+
sample_dir = 'samples'
12+
13+
def handle(self, *args, **options):
14+
sample_images = [os.path.join(self.sample_dir, fn) for fn in os.listdir(os.path.join(settings.MEDIA_ROOT, self.sample_dir))]
15+
16+
posts = Post.objects.all()
17+
18+
for i, image in enumerate(sample_images):
19+
Photo.objects.create(post=posts[i % posts.count()], image=image)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from django.core.management.base import BaseCommand, CommandError
2+
from django.conf import settings
3+
4+
from example.api.models import Post, User
5+
6+
bodies = ['This is a great post', 'Another thing I wanted to share']
7+
8+
9+
class Command(BaseCommand):
10+
def handle(self, *args, **options):
11+
users = User.objects.all()
12+
13+
for i, body in enumerate(bodies):
14+
Post.objects.create(author=users[i % users.count()], title="Title #{}".format(i + 1), body=body)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from django.core.management.base import BaseCommand, CommandError
2+
from django.conf import settings
3+
4+
from example.api.models import User
5+
6+
7+
class Command(BaseCommand):
8+
def handle(self, *args, **options):
9+
users = ['Bob', 'Sally', 'Joe', 'Rachel']
10+
for user in users:
11+
username = user.lower()
12+
User.objects.create(username=username, email="{}@example.com".format(username), first_name=user)

scripts/create_photos.py

-14
This file was deleted.

scripts/create_posts.py

-9
This file was deleted.

scripts/create_users.py

-8
This file was deleted.

0 commit comments

Comments
 (0)