def save_document(provider, title, href, images, embed, desc, duration, views, categories): from documents.models import Document, Category, Image try: doc = Document.objects.get(href=href) except Document.DoesNotExist: doc = Document() doc.provider = provider doc.title = title doc.href = href doc.embed = embed doc.desc = desc doc.duration = duration locale.setlocale(locale.LC_ALL, 'en_US.UTF-8') doc.views = locale.atoi(views) doc.save() if doc.images.count() == 0: for image_url in images: response = requests.get(image_url) if response.status_code == 200: image = Image() image.document = doc image.url = image_url image.save() for category in categories: cat = Category.objects.get_or_create(title=category) doc.categories.add(cat[0])
def handle_noargs(self, **options): doc_dir = os.path.join(os.path.dirname(__file__), '..', '..', 'fixtures') doctypes = DocumentType.objects.all() programs = Program.objects.all() users = IntranetUser.objects.all() for i in range(1000): for f in os.listdir(doc_dir): doc = Document() doc.title = random.random() doc.file = File(open(os.path.join(doc_dir, f))) doc.notes = random.random() doc.document_type = random.choice(doctypes) doc.save() doc.programs = random.sample(programs, 2) doc.authors = random.sample(users, 1)