def _populate_database(): get_user_model().objects.create_user(username='******', password='******') get_user_model().objects.create_superuser(username='******', password='******') image_name = 'test-image.jpg' image_bytes = None with open(CURRENT_DIR + '/' + image_name, 'rb') as image: image_bytes = image.read() resource = Resource( external_id='recpbs29kfas9i', url='https://test.test/resource.pdf' ) resource.image.save(image_name, ContentFile(image_bytes), save=True) resource.save() for external_id in ('recZxlcM61qaDoOkc', 'recYK5ljTyL3b18J3', 'recvSDrARAcmKogbD'): practice = Practice( external_id=external_id, main_resource=resource, ) practice.image.save(image_name, ContentFile(image_bytes), save=True) practice.save() for category_id in ('rec82929kfas9i', 'rec0098afaooka', 'recppasf09aii'): category = Category( external_id=category_id, practice_external_ids=['recZxlcM61qaDoOkc'] ) category.image.save(image_name, ContentFile(image_bytes), save=True) category.save() category.practices.add(Practice.objects.filter(external_id='recZxlcM61qaDoOkc').first())
return ret # If the workflow does not exist, we inform the user except ObjectDoesNotExist: return "Workflow with slug '" + workflowslug + "' does not exist" # Get or create categories category 1 and category 2 for i in range(1, 3): print "Creating category " + str(i) + "..." try: # Try to find the category c = Category.objects.get(name="category " + str(i)) except ObjectDoesNotExist: # If it does not exist, we create it c = Category(name="category " + str(i)) c.save() # Get or create workflows with names workflow xy, x in {1,2} and y in {1,2,3} and relate them to category x for i in range(1, 4): for j in range(1, 3): print "Creating workflow " + str(j) + str(i) + "..." try: # Try to find the workflow w = Workflow.objects.get(name="workflow " + str(j) + str(i)) # Relate with category j w.category.add(Category.objects.get(name="category " + str(j))) except ObjectDoesNotExist: # If the workflow does not exist, we create it w = Workflow(name="workflow " + str(j) + str(i), json=Command.getJson(populate)) w.save()
def addCategory(self, baseName): # create categories self.createObjects(baseName, 5) for item in self.data[baseName]: object = Category(name=item, tooltip="tooltip_" + item) object.save()