def update_or_create_document(yaml_obj): """ Submit an object read from our YAML files and it will update it in the database, creating it if it doesn't already exist. Returns the database object, and a boolean that is true if a new object was created. """ # Check if the table already exists in the datastore obj = Document.get_by_key_name(yaml_obj.get('slug')) # Update the obj if it exists if obj: # Loop through the keys and update the object one by one. for key in yaml_obj.keys(): # With some special casing for projects... if key == 'project_slug': proj = Project.get_by_key_name(yaml_obj.get('project_slug')) obj.project = proj # ...and for tags. elif key == 'tags': obj.tags = get_tag_keys(yaml_obj.get("tags")) else: setattr(obj, key, yaml_obj.get(key)) # Save it out obj.put() created = False # Create it if it doesn't else: # If it has tags.... if yaml_obj.has_key('tags'): # Convert to database keys tags = get_tag_keys(yaml_obj.pop("tags")) # Load the data obj = Document(key_name=yaml_obj.get('slug'), **yaml_obj) # Set the tags obj.tags = tags # Otherwise.... else: # Update the basic values obj = Document(key_name=yaml_obj.get('slug'), **yaml_obj) # And clear out the tag data obj.tags = [] obj.similar_documents = [] # Connected it to a project, if it exists if yaml_obj.has_key('project_slug'): proj = Project.get_by_key_name(yaml_obj.get('project_slug')) obj.project = proj # Save it out obj.put() created = True # Update the similarity lists of documents with the same tags taskqueue.add( url='/_/document/update-similar/', params=dict(key=obj.key()), method='GET' ) # Pass it out return obj, created
def get_object(self, bits): if len(bits) != 1: raise ObjectDoesNotExist obj = Project.get_by_key_name(bits[0]) if obj: return obj else: raise ObjectDoesNotExist
def setUp(self): self.project = Project(**{'name': 'Customer A'}) self.project.save() self.folder = Folder(**{'name': 'Images', 'project': self.project}) self.folder.save() files = ['2210571.jpg', 'images.docx'] for file in files: path = 'tests/files/%s' % file f = open(path, 'rb') document = Document() document.folder = self.folder document.name = file document.file.save(name=document.name, content=File(f)) document.save() document.save_thumbnail() f.close()
def update_or_create_project(yaml_obj): """ Submit an object read from our YAML files and it will update it in the database, creating it if it doesn't already exist. Returns the database object, and a boolean that is true if a new object was created. """ obj = Project.get_by_key_name(yaml_obj.get('slug')) # Update the obj if it exists if obj: for key in yaml_obj.keys(): setattr(obj, key, yaml_obj.get(key)) obj.put() return obj, False # Create it if it doesn't else: obj = Project(key_name=yaml_obj.get('slug'), **yaml_obj) obj.put() return obj, True
def handle(self, *args, **options): # Make sure they provided a table name try: key_name = args[0] except: raise CommandError("You must provide the key_name of a project as the first argument.") # Login self.authorize(options) # Drop it, if it exists. obj = Project.get_by_key_name(key_name) if obj: print "Deleting %s" % obj obj.delete() else: print "'%s' does not exist" % key_name
def handle(self, *args, **options): # Make sure they provided a table name try: key_name = args[0] except: raise CommandError( "You must provide the key_name of a project as the first argument." ) # Login self.authorize(options) # Drop it, if it exists. obj = Project.get_by_key_name(key_name) if obj: print "Deleting %s" % obj obj.delete() else: print "'%s' does not exist" % key_name
class DeleteTests(TransactionTestCase): def setUp(self): self.project = Project(**{'name': 'Customer A'}) self.project.save() self.folder = Folder(**{'name': 'Images', 'project': self.project}) self.folder.save() files = ['2210571.jpg', 'images.docx'] for file in files: path = 'tests/files/%s' % file f = open(path, 'rb') document = Document() document.folder = self.folder document.name = file document.file.save(name=document.name, content=File(f)) document.save() document.save_thumbnail() f.close() def tearDown(self): Document.objects.all().delete() Folder.objects.all().delete() Project.objects.all().delete() for item in os.listdir(settings.MEDIA_ROOT): shutil.rmtree(os.path.join(settings.MEDIA_ROOT, item)) def test_delete_project(self): document = Document.objects.all().first() base_upload_to = document.base_upload_to() path = os.path.join(settings.MEDIA_ROOT, base_upload_to) parent_base_upload_to = Document.parent_base_upload_to(document.folder) self.assertTrue(os.path.isdir(path)) self.assertTrue(os.path.isdir(os.path.join(path, 'filemanager'))) self.assertTrue( os.path.isdir(os.path.join(path, 'filemanager_thumbnails'))) Project.objects.all().delete() self.assertFalse(os.path.isdir(path)) path = os.path.join(settings.MEDIA_ROOT, base_upload_to) self.assertFalse(os.path.isdir(parent_base_upload_to)) def test_delete_folder(self): document = Document.objects.all().first() base_upload_to = document.base_upload_to() path = os.path.join(settings.MEDIA_ROOT, base_upload_to) parent_base_upload_to = Document.parent_base_upload_to(document.folder) self.assertTrue(os.path.isdir(path)) self.assertTrue(os.path.isdir(os.path.join(path, 'filemanager'))) self.assertTrue( os.path.isdir(os.path.join(path, 'filemanager_thumbnails'))) Folder.objects.all().delete() self.assertFalse(os.path.isdir(path)) path = os.path.join(settings.MEDIA_ROOT, base_upload_to) self.assertFalse(os.path.isdir(parent_base_upload_to)) def test_delete_documents(self): files = [] documents = Document.objects.all() for document in documents: files.append(document.file.path) if document.thumbnail: files.append(document.thumbnail.path) self.assertTrue(len(files) == 3) for file in files: self.assertTrue(os.path.isfile(file)) Document.objects.all().delete() for file in files: self.assertFalse(os.path.isfile(file))