import os

import django
from django.utils import timezone

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
django.setup()
from django.contrib.auth.models import User
from Fantasy.models import Source
from django.core.files import File


def bulk_create():
    username = range(1002, 1030)
    password = ['123456' for i in range(28)]

    for u, p in zip(username, password):
        user = User.objects.create_user(username=u, password=p)
        user.first_name = '用户'
        user.groups.add(1)
        user.is_staff = True
        user.last_login = timezone.now()
        user.save()


def size(path):
    __size = os.path.getsize(path)
    __size = __size / float(1024 * 1024)

    return round(__size, 2)


def bulk_upload(dirpath):
    myfile, __file = None, None

    items = list()
    path_list = list(os.scandir(dirpath))
    for i in path_list:
        if os.path.isfile(i):
            file_size = size(i)
            file_name = os.path.basename(i).split('.')[-2]
            __file = open(i, mode='rb')
            myfile = File(__file, name=os.path.basename(i))

            source = Source(title=file_name, file_size=file_size, file=myfile, pub_date=timezone.now(),
                            category='其他资料')
            items.append(source)

    Source.objects.bulk_create(items)
    myfile.close()
    __file.close()
Logo

开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!

更多推荐