Exemple #1
0
    def create_users(self):
        self.user = User(email='*****@*****.**', is_staff=True)
        self.user.set_password('password')
        self.user.save()

        self.user_list = []
        for i in range(1, self.conf['USERS']+1):
            user = User(email='testuser{}@example.com'.format(i))
            user.set_password('password')
            user.save()
            self.user_list.append(user)

        return self.user
Exemple #2
0
class DataCreator:

    def __init__(self, conf=DEFAULT_CONF):
        self.conf = conf
        self.start = None
        self.end = None

    def datacreator_start(self):
        print('----------------------')
        print('datacreator.py started')
        self.start = datetime.datetime.now()

    def datacreator_end(self):
        self.end = datetime.datetime.now()
        print('datacreator.py finished in %s' % (self.end-self.start))
        print('-----------------------')

    def timed(func):
        """
        use @timed to decorate a function that will print out the time it took
        for this function to run.
        """

        def inner(*args, **kwargs):
            start = datetime.datetime.now()
            result = func(*args, **kwargs)
            finish = datetime.datetime.now()
            print('\t{} - {}'.format(func.__name__, finish-start))
            return result
        return inner

    #---------------------------------------------------------------------------

    @timed
    def create_users(self):
        self.user = User(email='*****@*****.**', is_staff=True)
        self.user.set_password('password')
        self.user.save()

        self.user_list = []
        for i in range(1, self.conf['USERS']+1):
            user = User(email='testuser{}@example.com'.format(i))
            user.set_password('password')
            user.save()
            self.user_list.append(user)

        return self.user

    @timed
    def create_labels(self):
        self.label_list = []
        def create_label_list(parent, label_list):
            if not label_list: return
            for i, (label, children) in enumerate(label_list):
                label = Label.objects.create(name=label, parent=parent, pinned=(not parent), order=i)
                self.label_list.append(label)
                if len(self.label_list) < self.conf['LABELS']:
                    create_label_list(label, children)
        create_label_list(None, LABELS)

    @timed
    def create_videos(self):
        label_list = Label.objects.get(name='Anime').children.all()
        video_dict = defaultdict(list)

        for i in range(self.conf['VIDEOS']):
            label = random.choice(label_list)
            episode = len(video_dict[label.name]) + 1
            video = Video.objects.create(
                name='{} {:02d}'.format(label.name, episode),
                label=label,
            )
            video_dict[label.name].append(video)

    #---------------------------------------------------------------------------

    def run(self):
        self.datacreator_start()

        self.create_users()
        self.create_labels()
        self.create_videos()

        self.datacreator_end()