Add example content with BaseCommand

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
from django.core.management.base import BaseCommand
from content.models import Content
import sys
import random

class Command(BaseCommand):

    def _get_string(self, prompt, reader_func=raw_input, required=True):
        """Helper method to get a non-empty string.
        """
        string = ''
        while not string:
            string = reader_func(prompt + ': ')
            if not required:
                break
        return string

    def handle(self, **kwargs):
        n = self._get_string('Number')
        # random_number = random.randint(0,400)
        # n = random_number
        words = open('/usr/share/dict/words').readlines()

        title= []
        for i in range(2):
            title.append(words[random.randrange(0, len(words))][:-1])

        if title is not None:
            title = ' '.join(title)

        paragraph = []
        for i in range(int(n)):
            paragraph.append(words[random.randrange(0, len(words))][:-1])

        if paragraph is not None:
            paragraph = ' '.join(paragraph)

        content = Content(title=title, paragraph=paragraph)
        content.save()

        print 'Content successfully added'

More like this

  1. Add example contact with BaseCommand by magik_cypress 9 months, 1 week ago
  2. Simple snippet to generate whole project models uml diagram in YUML.me format by nikolajusk 2 years, 7 months ago
  3. extras.py for management commands by dnordberg 5 years, 9 months ago
  4. Custom Command for Rebuilding Permissions and ContentTypes by cronosa 2 years, 2 months ago
  5. Improved command to generate UML diagrams of whole project or specified apps by nikolajusk 2 years, 7 months ago

Comments

(Forgotten your password?)