def testCodeRequired(self): "Test that a proper failure occur when a collection code isn't provided" c = MediaCollection() try: c.set_revision(self.olivier) except RequiredFieldError, e: self.assertEquals(e.field.name, 'code')
def wav_import(self): from telemeta.models import MediaItem, MediaCollection collections = [] for collection in self.collections: collection_dir = self.source_dir + os.sep + collection collection_files = os.listdir(collection_dir) if not '/.' in collection_dir and self.pattern in collection_dir: collection_name = collection.split(os.sep)[-1] collections.append(collection_name) c = MediaCollection.objects.filter(code=collection_name) if not c and collection + '.csv' in collection_files: msg = collection + ' collection NON présente dans la base de données, SORTIE ' self.logger.error(collection, msg) sys.exit(msg) elif not c: msg = 'collection NON présente dans la base de données, CREATION ' self.logger.info(collection, msg) c = MediaCollection(code=collection_name) c.save() c.set_revision(self.user) else: msg = 'collection présente dans la base de données, SELECTION' self.logger.info(collection, msg) for collection in collections: collection_dir = self.source_dir + os.sep + collection collection_name = collection collection_files = os.listdir(collection_dir) msg = '************************ ' + collection + ' ******************************' self.logger.info(collection, msg[:70]) overwrite = True csv_file = '' rows = {} if collection + '.csv' in collection_files: csv_file = self.source_dir + os.sep + collection + os.sep + collection + '.csv' csv_data = csv.reader(open(csv_file), delimiter=';') for row in csv_data: rows[row[1].strip()] = row[0].strip() msg = collection + ' import du fichier CSV de la collection' self.logger.info(collection, msg[:70]) else: msg = collection + ' pas de fichier CSV dans la collection' self.logger.info(collection, msg[:70]) c = MediaCollection.objects.filter(code=collection_name) if not c: c = MediaCollection(code=collection_name) c.save() msg = ' collection NON présente dans la BDD, CREATION ' self.logger.info(c.code, msg) else: c = c[0] msg = ' id = '+str(c.id) self.logger.info(c.code, msg) audio_files = [] for file in collection_files: ext = ['WAV', 'wav'] if file.split('.')[-1] in ext: audio_files.append(file) audio_files.sort() nb_items = c.items.count() counter = 0 for file in audio_files: code = file.split('.')[0] wav_file = self.source_dir + os.sep + collection + os.sep + file if len(audio_files) <= nb_items: items = MediaItem.objects.filter(code=code) old_ref = '' if code in rows and not items: old_ref = rows[code] items = MediaItem.objects.filter(old_code=old_ref) if items: item = items[0] msg = code + ' : ' + item.old_code + ' : Cas 1 ou 2 : id = ' + str(item.id) self.logger.info('item', msg) item.code = code item.save() else: item = MediaItem(code=code, collection=c) msg = code + ' : ' + old_ref + ' : Cas 1 ou 2 : item NON présent dans la base de données, CREATION' self.logger.info('item', msg) self.write_file(item, wav_file, overwrite) elif nb_items == 1 and len(audio_files) > 1: if counter == 0: msg = code + ' : Cas 3a : item n°01 présent dans la base de données, PASSE' self.logger.info('item', msg) else: item = MediaItem(code=code, collection=c) msg = code + ' : Cas 3a : item NON présent dans la base de données, CREATION' self.logger.info('item', msg) self.write_file(item, wav_file, overwrite) elif nb_items > 1 and nb_items < len(audio_files): msg = code + ' : Cas 3b : nb items < nb de fichiers audio, PAS de creation' self.logger.info('item', msg) counter += 1 msg = 'Liste des URLs des collections importées :' self.logger.info('INFO', msg) for collection in collections: msg = 'http://'+self.domain+'/collections/'+collection self.logger.info(collection, msg)
def setUp(self): "Create a test database based on objects created in Django" # self.david = User.objects.create(username="******", level="user") # self.olivier = User.objects.create(username="******", level="admin") self.david = User.objects.create_user(username="******") self.olivier = User.objects.create_user(username="******") self.country = LocationType.objects.create(code="country", name="Country") self.continent = LocationType.objects.create(code="continent", name="Continent") self.city = LocationType.objects.create(code="city", name="City") self.paris = Location.objects.create(name="Paris", type=Location.OTHER_TYPE, complete_type=self.city) self.france = Location.objects.create(name="France", type=Location.COUNTRY, complete_type=self.country) self.europe = Location.objects.create(name="Europe", type=Location.CONTINENT, complete_type=self.continent) self.belgique = Location.objects.create(name="Belgique", type=Location.COUNTRY, complete_type=self.country) self.europe.add_child(self.france) self.france.add_child(self.paris) self.europe.add_child(self.belgique) self.a = EthnicGroup.objects.create(value="a") self.b = EthnicGroup.objects.create(value="b") self.c = EthnicGroup.objects.create(value="c") self.d = EthnicGroup.objects.create(value="d") self.persepolis = MediaCollection(id=1, code="CNRSMH_E_1970_001_002", reference="A1", title="persepolis", creator="Abraham LINCOLN", collector="Friedrich HEINZ", year_published=2009, is_published=True, recorded_from_year=1970, recorded_to_year=1980) self.persepolis.set_revision(self.david) self.volonte = MediaCollection(id=2, reference="A2", code="CNRSMH_I_1960_001", title="Volonté de puissance", creator="Friedrich NIETZSCHE", collector="Jean AMORA", year_published=1999, recorded_from_year=1960, recorded_to_year=2000) self.volonte.set_revision(self.olivier) self.nicolas = MediaCollection(id=3, reference="A3", code="CNRSMH_I_1967_123", title="petit nicolas", creator="Georgette McKenic", collector="Paul MAILLE", year_published=1999, recorded_from_year=1967, recorded_to_year=1968) self.nicolas.set_revision(self.olivier) self.item_1 = MediaItem(id=1, collection=self.persepolis, code="CNRSMH_E_1970_001_002_44", recorded_from_date="1971-01-12", recorded_to_date="1971-02-24", location=self.paris, ethnic_group=self.a, title="item 1", author="Mickael SHEPHERD", collector="Charles PREMIER", comment="comment 1") self.item_1.set_revision(self.david) self.item_2 = MediaItem(id=2, collection=self.volonte, code="CNRSMH_I_1960_001_129", recorded_from_date="1981-01-12", recorded_to_date="1991-02-24", location=self.france, ethnic_group=self.a, title="item 2", author="Rick ROLL", comment="comment 2") self.item_2.set_revision(self.david) self.item_3 = MediaItem(id=3, collection=self.nicolas, code="CNRSMH_I_1967_123_456_01", recorded_from_date="1968-01-12", recorded_to_date="1968-02-24", location=self.belgique, ethnic_group=self.b, title="item 3", author="John SMITH", collector="Paul CARLOS", comment="comment 3", ) self.item_3.set_revision(self.olivier) self.item_4 = MediaItem(id=4, collection=self.persepolis, code="CNRSMH_E_1970_001_002_22_33", recorded_from_date="1972-01-12", recorded_to_date="1972-02-24", location=self.europe, ethnic_group=self.a, title="item 4", alt_title="I4", author="Keanu REAVES", collector="Christina BARCELONA", comment="comment 4") self.item_4.set_revision(self.olivier) self.item_5 = MediaItem(id=5, collection=self.volonte,code="CNRSMH_I_1960_001_789_85_22", approx_duration="00:05:00", recorded_from_date="1978-01-12", recorded_to_date="1978-02-24", location=self.belgique, ethnic_group=self.a, title="item 5", alt_title="I5", author="Simon PAUL", collector="Javier BARDEM", comment="comment 5") self.item_5.set_revision(self.olivier) self.item_6 = MediaItem(id=6, collection=self.persepolis, code="CNRSMH_E_1970_001_002_90", recorded_from_date="1968-01-12", recorded_to_date="1968-02-11", location=self.france, ethnic_group=self.b, title="item 6", author="Paul ANDERSON", collector="Jim CARLSON", comment="comment 10000") self.item_6.set_revision(self.david) self.collections = MediaCollection.objects.all() self.items = MediaItem.objects.all()
class CollectionItemTestCase(unittest.TestCase): pytestmark = pytest.mark.django_db def setUp(self): "Create a test database based on objects created in Django" # self.david = User.objects.create(username="******", level="user") # self.olivier = User.objects.create(username="******", level="admin") self.david = User.objects.create_user(username="******") self.olivier = User.objects.create_user(username="******") self.country = LocationType.objects.create(code="country", name="Country") self.continent = LocationType.objects.create(code="continent", name="Continent") self.city = LocationType.objects.create(code="city", name="City") self.paris = Location.objects.create(name="Paris", type=Location.OTHER_TYPE, complete_type=self.city) self.france = Location.objects.create(name="France", type=Location.COUNTRY, complete_type=self.country) self.europe = Location.objects.create(name="Europe", type=Location.CONTINENT, complete_type=self.continent) self.belgique = Location.objects.create(name="Belgique", type=Location.COUNTRY, complete_type=self.country) self.europe.add_child(self.france) self.france.add_child(self.paris) self.europe.add_child(self.belgique) self.a = EthnicGroup.objects.create(value="a") self.b = EthnicGroup.objects.create(value="b") self.c = EthnicGroup.objects.create(value="c") self.d = EthnicGroup.objects.create(value="d") self.persepolis = MediaCollection(id=1, code="CNRSMH_E_1970_001_002", reference="A1", title="persepolis", creator="Abraham LINCOLN", collector="Friedrich HEINZ", year_published=2009, is_published=True, recorded_from_year=1970, recorded_to_year=1980) self.persepolis.set_revision(self.david) self.volonte = MediaCollection(id=2, reference="A2", code="CNRSMH_I_1960_001", title="Volonté de puissance", creator="Friedrich NIETZSCHE", collector="Jean AMORA", year_published=1999, recorded_from_year=1960, recorded_to_year=2000) self.volonte.set_revision(self.olivier) self.nicolas = MediaCollection(id=3, reference="A3", code="CNRSMH_I_1967_123", title="petit nicolas", creator="Georgette McKenic", collector="Paul MAILLE", year_published=1999, recorded_from_year=1967, recorded_to_year=1968) self.nicolas.set_revision(self.olivier) self.item_1 = MediaItem(id=1, collection=self.persepolis, code="CNRSMH_E_1970_001_002_44", recorded_from_date="1971-01-12", recorded_to_date="1971-02-24", location=self.paris, ethnic_group=self.a, title="item 1", author="Mickael SHEPHERD", collector="Charles PREMIER", comment="comment 1") self.item_1.set_revision(self.david) self.item_2 = MediaItem(id=2, collection=self.volonte, code="CNRSMH_I_1960_001_129", recorded_from_date="1981-01-12", recorded_to_date="1991-02-24", location=self.france, ethnic_group=self.a, title="item 2", author="Rick ROLL", comment="comment 2") self.item_2.set_revision(self.david) self.item_3 = MediaItem(id=3, collection=self.nicolas, code="CNRSMH_I_1967_123_456_01", recorded_from_date="1968-01-12", recorded_to_date="1968-02-24", location=self.belgique, ethnic_group=self.b, title="item 3", author="John SMITH", collector="Paul CARLOS", comment="comment 3", ) self.item_3.set_revision(self.olivier) self.item_4 = MediaItem(id=4, collection=self.persepolis, code="CNRSMH_E_1970_001_002_22_33", recorded_from_date="1972-01-12", recorded_to_date="1972-02-24", location=self.europe, ethnic_group=self.a, title="item 4", alt_title="I4", author="Keanu REAVES", collector="Christina BARCELONA", comment="comment 4") self.item_4.set_revision(self.olivier) self.item_5 = MediaItem(id=5, collection=self.volonte,code="CNRSMH_I_1960_001_789_85_22", approx_duration="00:05:00", recorded_from_date="1978-01-12", recorded_to_date="1978-02-24", location=self.belgique, ethnic_group=self.a, title="item 5", alt_title="I5", author="Simon PAUL", collector="Javier BARDEM", comment="comment 5") self.item_5.set_revision(self.olivier) self.item_6 = MediaItem(id=6, collection=self.persepolis, code="CNRSMH_E_1970_001_002_90", recorded_from_date="1968-01-12", recorded_to_date="1968-02-11", location=self.france, ethnic_group=self.b, title="item 6", author="Paul ANDERSON", collector="Jim CARLSON", comment="comment 10000") self.item_6.set_revision(self.david) self.collections = MediaCollection.objects.all() self.items = MediaItem.objects.all() def tearDown(self): User.objects.all().delete() LocationType.objects.all().delete() Location.objects.all().delete() EthnicGroup.objects.all().delete() MediaCollection.objects.all().delete() MediaItem.objects.all().delete() def testQuickSearchOnCollections(self): "Test quick_search property of MediaCollection class" result = self.collections.quick_search("persepolis") self.assertEquals(len(result), 1) self.assertEquals(result[0], self.persepolis) self.assertEquals(self.collections.quick_search("nietzsche")[0], self.volonte) result = self.collections.quick_search("nicolas") self.assertEquals(result[0], self.nicolas) def testQuickSearchOnItems(self): "Test quick_search property of MediaItem class" result = self.items.quick_search("item").order_by("title") self.assertEquals(result[0], self.item_1) self.assertEquals(result[1], self.item_2) self.assertEquals(result[2], self.item_3) self.assertEquals(result[3], self.item_4) self.assertEquals(result[4], self.item_5) self.assertEquals(result[5], self.item_6) def testWordSearch(self): "Test quick_search property of MediaCollection class, specificly quick_search on collection title" result = self.collections.quick_search("volonté puissance") self.assertEquals(result[0], self.volonte) result = self.collections.quick_search("puissance volonté") self.assertEquals(result[0], self.volonte) result = self.collections.quick_search("volonte puissance") self.assertEquals(result[0], self.volonte) result = self.collections.quick_search("puissance volonte") self.assertEquals(result[0], self.volonte) def testLocationSearch(self): "Test by_country and by_continent properties of MediaCollection class" self.assertEquals(self.collections.by_location(self.france)[0], self.persepolis) self.assertEquals(self.collections.by_location(self.europe)[0], self.persepolis) self.assertEquals(self.collections.by_location(self.belgique).order_by("title")[0], self.nicolas) self.assertEquals(self.collections.by_location(self.belgique).order_by("title")[1], self.volonte) def testRecordingYear(self): "Test by_recording_year property of MediaCollection class" self.assertEquals(self.collections.by_recording_year(1970, 1980)[0], self.persepolis) result = self.collections.by_recording_year(1975).order_by("title") self.assertEquals(result[0], self.persepolis) self.assertEquals(result[1], self.volonte) def testPublishYearOnCollection(self): "Test by_publish_year property of MediaCollection class" result=self.collections.by_publish_year(1999).order_by("title") self.assertEquals(result[0], self.nicolas) self.assertEquals(result[1], self.volonte) def testEthnicGroup(self): "Test by_ethnic_group property of MediaCollection class" result=self.collections.by_ethnic_group(self.a).order_by("title") self.assertEquals(result[0], self.persepolis) self.assertEquals(result[1], self.volonte) def testRecordingDate(self): "Test by_recording_date property of MediaItem class" result = self.items.by_recording_date("1968-01-01", "1972-12-12").order_by("title") self.assertEquals(result[0], self.item_1) self.assertEquals(result[1], self.item_3) self.assertEquals(result[2], self.item_4) self.assertEquals(result[3], self.item_6) result = self.items.by_recording_date("1968-02-06").order_by("title") self.assertEquals(result[0], self.item_3) self.assertEquals(result[1], self.item_6) def testTitle(self): "Test by_title property of MediaItem class" result = self.items.by_title("item").order_by("title") self.assertEquals(result[0], self.item_1) self.assertEquals(result[1], self.item_2) self.assertEquals(result[2], self.item_3) self.assertEquals(result[3], self.item_4) self.assertEquals(result[4], self.item_5) self.assertEquals(result[5], self.item_6) self.item_2.title = '' self.item_2.save() self.item_5.title = '' self.item_5.save() result = self.items.by_title("volonté").order_by("title") self.assertEquals(result[0], self.item_2) self.assertEquals(result[1], self.item_5) result = self.items.by_title("puissance volonté").order_by("title") self.assertEquals(result[0], self.item_2) self.assertEquals(result[1], self.item_5) def testPublishYearOnItem(self): "Test by_publish_year property of MediaItem class" result = self.items.by_publish_year(1999).order_by("title") self.assertEquals(result[0], self.item_2) self.assertEquals(result[1], self.item_3) self.assertEquals(result[2], self.item_5) def testWordSearchCore(self): "Test word_search property of CoreQuerySet class" self.assertEquals(self.collections.word_search("title", "volonté")[0], self.volonte) self.assertEquals(self.collections.word_search("code", "CNRSMH_E_1970_001_002")[0], self.persepolis) self.assertEquals(self.items.word_search("code", "CNRSMH_E_1970_001_002_44")[0], self.item_1) result = self.items.word_search("comment", "comment").order_by("title") self.assertEquals(result[0], self.item_1) self.assertEquals(result[1], self.item_2) self.assertEquals(result[2], self.item_3) self.assertEquals(result[3], self.item_4) self.assertEquals(result[4], self.item_5) self.assertEquals(result[5], self.item_6) def testByChangeTimeOnCollection(self): "Test by_change_time property of MediaCollection class" now = datetime.now() print self.collections.all() print MediaCollection.objects.all() result = self.collections.by_change_time(now - timedelta(hours=1), now).order_by("title") self.assertEquals(result[0], self.persepolis) def testByChangeTimeOnItem(self): "Test by_change_time property of MediaItem class" now = datetime.now() result = self.items.by_change_time(now - timedelta(hours=1), now).order_by("title") self.assertEquals(result[0], self.item_1) def testWithoutCollection(self): "Test without_collection property of MediaItem class" self.assertEquals(self.items.without_collection().count(), 0) def testCodeRequired(self): "Test that a proper failure occur when a collection code isn't provided" c = MediaCollection() try: c.set_revision(self.olivier) except RequiredFieldError, e: self.assertEquals(e.field.name, 'code') else:
def setUp(self): "Create a test database based on objects created in Django" # self.david = User.objects.create(username="******", level="user") # self.olivier = User.objects.create(username="******", level="admin") self.david = User.objects.create_user(username="******") self.olivier = User.objects.create_user(username="******") self.country = LocationType.objects.create(code="country", name="Country") self.continent = LocationType.objects.create(code="continent", name="Continent") self.city = LocationType.objects.create(code="city", name="City") self.paris = Location.objects.create(name="Paris", type=Location.OTHER_TYPE, complete_type=self.city) self.france = Location.objects.create(name="France", type=Location.COUNTRY, complete_type=self.country) self.europe = Location.objects.create(name="Europe", type=Location.CONTINENT, complete_type=self.continent) self.belgique = Location.objects.create(name="Belgique", type=Location.COUNTRY, complete_type=self.country) self.europe.add_child(self.france) self.france.add_child(self.paris) self.europe.add_child(self.belgique) self.a = EthnicGroup.objects.create(value="a") self.b = EthnicGroup.objects.create(value="b") self.c = EthnicGroup.objects.create(value="c") self.d = EthnicGroup.objects.create(value="d") self.persepolis = MediaCollection(id=1, code="CNRSMH_E_1970_001_002", reference="A1", title="persepolis", creator="Abraham LINCOLN", collector="Friedrich HEINZ", year_published=2009, is_published=True, recorded_from_year=1970, recorded_to_year=1980) self.persepolis.set_revision(self.david) self.volonte = MediaCollection(id=2, reference="A2", code="CNRSMH_I_1960_001", title="Volonté de puissance", creator="Friedrich NIETZSCHE", collector="Jean AMORA", year_published=1999, recorded_from_year=1960, recorded_to_year=2000) self.volonte.set_revision(self.olivier) self.nicolas = MediaCollection(id=3, reference="A3", code="CNRSMH_I_1967_123", title="petit nicolas", creator="Georgette McKenic", collector="Paul MAILLE", year_published=1999, recorded_from_year=1967, recorded_to_year=1968) self.nicolas.set_revision(self.olivier) self.item_1 = MediaItem(id=1, collection=self.persepolis, code="CNRSMH_E_1970_001_002_44", recorded_from_date="1971-01-12", recorded_to_date="1971-02-24", location=self.paris, ethnic_group=self.a, title="item 1", author="Mickael SHEPHERD", collector="Charles PREMIER", comment="comment 1") self.item_1.set_revision(self.david) self.item_2 = MediaItem(id=2, collection=self.volonte, code="CNRSMH_I_1960_001_129", recorded_from_date="1981-01-12", recorded_to_date="1991-02-24", location=self.france, ethnic_group=self.a, title="item 2", author="Rick ROLL", comment="comment 2") self.item_2.set_revision(self.david) self.item_3 = MediaItem( id=3, collection=self.nicolas, code="CNRSMH_I_1967_123_456_01", recorded_from_date="1968-01-12", recorded_to_date="1968-02-24", location=self.belgique, ethnic_group=self.b, title="item 3", author="John SMITH", collector="Paul CARLOS", comment="comment 3", ) self.item_3.set_revision(self.olivier) self.item_4 = MediaItem(id=4, collection=self.persepolis, code="CNRSMH_E_1970_001_002_22_33", recorded_from_date="1972-01-12", recorded_to_date="1972-02-24", location=self.europe, ethnic_group=self.a, title="item 4", alt_title="I4", author="Keanu REAVES", collector="Christina BARCELONA", comment="comment 4") self.item_4.set_revision(self.olivier) self.item_5 = MediaItem(id=5, collection=self.volonte, code="CNRSMH_I_1960_001_789_85_22", approx_duration="00:05:00", recorded_from_date="1978-01-12", recorded_to_date="1978-02-24", location=self.belgique, ethnic_group=self.a, title="item 5", alt_title="I5", author="Simon PAUL", collector="Javier BARDEM", comment="comment 5") self.item_5.set_revision(self.olivier) self.item_6 = MediaItem(id=6, collection=self.persepolis, code="CNRSMH_E_1970_001_002_90", recorded_from_date="1968-01-12", recorded_to_date="1968-02-11", location=self.france, ethnic_group=self.b, title="item 6", author="Paul ANDERSON", collector="Jim CARLSON", comment="comment 10000") self.item_6.set_revision(self.david) self.collections = MediaCollection.objects.all() self.items = MediaItem.objects.all()
class CollectionItemTestCase(unittest.TestCase): pytestmark = pytest.mark.django_db def setUp(self): "Create a test database based on objects created in Django" # self.david = User.objects.create(username="******", level="user") # self.olivier = User.objects.create(username="******", level="admin") self.david = User.objects.create_user(username="******") self.olivier = User.objects.create_user(username="******") self.country = LocationType.objects.create(code="country", name="Country") self.continent = LocationType.objects.create(code="continent", name="Continent") self.city = LocationType.objects.create(code="city", name="City") self.paris = Location.objects.create(name="Paris", type=Location.OTHER_TYPE, complete_type=self.city) self.france = Location.objects.create(name="France", type=Location.COUNTRY, complete_type=self.country) self.europe = Location.objects.create(name="Europe", type=Location.CONTINENT, complete_type=self.continent) self.belgique = Location.objects.create(name="Belgique", type=Location.COUNTRY, complete_type=self.country) self.europe.add_child(self.france) self.france.add_child(self.paris) self.europe.add_child(self.belgique) self.a = EthnicGroup.objects.create(value="a") self.b = EthnicGroup.objects.create(value="b") self.c = EthnicGroup.objects.create(value="c") self.d = EthnicGroup.objects.create(value="d") self.persepolis = MediaCollection(id=1, code="CNRSMH_E_1970_001_002", reference="A1", title="persepolis", creator="Abraham LINCOLN", collector="Friedrich HEINZ", year_published=2009, is_published=True, recorded_from_year=1970, recorded_to_year=1980) self.persepolis.set_revision(self.david) self.volonte = MediaCollection(id=2, reference="A2", code="CNRSMH_I_1960_001", title="Volonté de puissance", creator="Friedrich NIETZSCHE", collector="Jean AMORA", year_published=1999, recorded_from_year=1960, recorded_to_year=2000) self.volonte.set_revision(self.olivier) self.nicolas = MediaCollection(id=3, reference="A3", code="CNRSMH_I_1967_123", title="petit nicolas", creator="Georgette McKenic", collector="Paul MAILLE", year_published=1999, recorded_from_year=1967, recorded_to_year=1968) self.nicolas.set_revision(self.olivier) self.item_1 = MediaItem(id=1, collection=self.persepolis, code="CNRSMH_E_1970_001_002_44", recorded_from_date="1971-01-12", recorded_to_date="1971-02-24", location=self.paris, ethnic_group=self.a, title="item 1", author="Mickael SHEPHERD", collector="Charles PREMIER", comment="comment 1") self.item_1.set_revision(self.david) self.item_2 = MediaItem(id=2, collection=self.volonte, code="CNRSMH_I_1960_001_129", recorded_from_date="1981-01-12", recorded_to_date="1991-02-24", location=self.france, ethnic_group=self.a, title="item 2", author="Rick ROLL", comment="comment 2") self.item_2.set_revision(self.david) self.item_3 = MediaItem( id=3, collection=self.nicolas, code="CNRSMH_I_1967_123_456_01", recorded_from_date="1968-01-12", recorded_to_date="1968-02-24", location=self.belgique, ethnic_group=self.b, title="item 3", author="John SMITH", collector="Paul CARLOS", comment="comment 3", ) self.item_3.set_revision(self.olivier) self.item_4 = MediaItem(id=4, collection=self.persepolis, code="CNRSMH_E_1970_001_002_22_33", recorded_from_date="1972-01-12", recorded_to_date="1972-02-24", location=self.europe, ethnic_group=self.a, title="item 4", alt_title="I4", author="Keanu REAVES", collector="Christina BARCELONA", comment="comment 4") self.item_4.set_revision(self.olivier) self.item_5 = MediaItem(id=5, collection=self.volonte, code="CNRSMH_I_1960_001_789_85_22", approx_duration="00:05:00", recorded_from_date="1978-01-12", recorded_to_date="1978-02-24", location=self.belgique, ethnic_group=self.a, title="item 5", alt_title="I5", author="Simon PAUL", collector="Javier BARDEM", comment="comment 5") self.item_5.set_revision(self.olivier) self.item_6 = MediaItem(id=6, collection=self.persepolis, code="CNRSMH_E_1970_001_002_90", recorded_from_date="1968-01-12", recorded_to_date="1968-02-11", location=self.france, ethnic_group=self.b, title="item 6", author="Paul ANDERSON", collector="Jim CARLSON", comment="comment 10000") self.item_6.set_revision(self.david) self.collections = MediaCollection.objects.all() self.items = MediaItem.objects.all() def tearDown(self): User.objects.all().delete() LocationType.objects.all().delete() Location.objects.all().delete() EthnicGroup.objects.all().delete() MediaCollection.objects.all().delete() MediaItem.objects.all().delete() def testQuickSearchOnCollections(self): "Test quick_search property of MediaCollection class" result = self.collections.quick_search("persepolis") self.assertEquals(len(result), 1) self.assertEquals(result[0], self.persepolis) self.assertEquals( self.collections.quick_search("nietzsche")[0], self.volonte) result = self.collections.quick_search("nicolas") self.assertEquals(result[0], self.nicolas) def testQuickSearchOnItems(self): "Test quick_search property of MediaItem class" result = self.items.quick_search("item").order_by("title") self.assertEquals(result[0], self.item_1) self.assertEquals(result[1], self.item_2) self.assertEquals(result[2], self.item_3) self.assertEquals(result[3], self.item_4) self.assertEquals(result[4], self.item_5) self.assertEquals(result[5], self.item_6) def testWordSearch(self): "Test quick_search property of MediaCollection class, specificly quick_search on collection title" result = self.collections.quick_search("volonté puissance") self.assertEquals(result[0], self.volonte) result = self.collections.quick_search("puissance volonté") self.assertEquals(result[0], self.volonte) result = self.collections.quick_search("volonte puissance") self.assertEquals(result[0], self.volonte) result = self.collections.quick_search("puissance volonte") self.assertEquals(result[0], self.volonte) def testLocationSearch(self): "Test by_country and by_continent properties of MediaCollection class" self.assertEquals( self.collections.by_location(self.france)[0], self.persepolis) self.assertEquals( self.collections.by_location(self.europe)[0], self.persepolis) self.assertEquals( self.collections.by_location(self.belgique).order_by("title")[0], self.nicolas) self.assertEquals( self.collections.by_location(self.belgique).order_by("title")[1], self.volonte) def testRecordingYear(self): "Test by_recording_year property of MediaCollection class" self.assertEquals( self.collections.by_recording_year(1970, 1980)[0], self.persepolis) result = self.collections.by_recording_year(1975).order_by("title") self.assertEquals(result[0], self.persepolis) self.assertEquals(result[1], self.volonte) def testPublishYearOnCollection(self): "Test by_publish_year property of MediaCollection class" result = self.collections.by_publish_year(1999).order_by("title") self.assertEquals(result[0], self.nicolas) self.assertEquals(result[1], self.volonte) def testEthnicGroup(self): "Test by_ethnic_group property of MediaCollection class" result = self.collections.by_ethnic_group(self.a).order_by("title") self.assertEquals(result[0], self.persepolis) self.assertEquals(result[1], self.volonte) def testRecordingDate(self): "Test by_recording_date property of MediaItem class" result = self.items.by_recording_date("1968-01-01", "1972-12-12").order_by("title") self.assertEquals(result[0], self.item_1) self.assertEquals(result[1], self.item_3) self.assertEquals(result[2], self.item_4) self.assertEquals(result[3], self.item_6) result = self.items.by_recording_date("1968-02-06").order_by("title") self.assertEquals(result[0], self.item_3) self.assertEquals(result[1], self.item_6) def testTitle(self): "Test by_title property of MediaItem class" result = self.items.by_title("item").order_by("title") self.assertEquals(result[0], self.item_1) self.assertEquals(result[1], self.item_2) self.assertEquals(result[2], self.item_3) self.assertEquals(result[3], self.item_4) self.assertEquals(result[4], self.item_5) self.assertEquals(result[5], self.item_6) self.item_2.title = '' self.item_2.save() self.item_5.title = '' self.item_5.save() result = self.items.by_title("volonté").order_by("title") self.assertEquals(result[0], self.item_2) self.assertEquals(result[1], self.item_5) result = self.items.by_title("puissance volonté").order_by("title") self.assertEquals(result[0], self.item_2) self.assertEquals(result[1], self.item_5) def testPublishYearOnItem(self): "Test by_publish_year property of MediaItem class" result = self.items.by_publish_year(1999).order_by("title") self.assertEquals(result[0], self.item_2) self.assertEquals(result[1], self.item_3) self.assertEquals(result[2], self.item_5) def testWordSearchCore(self): "Test word_search property of CoreQuerySet class" self.assertEquals( self.collections.word_search("title", "volonté")[0], self.volonte) self.assertEquals( self.collections.word_search("code", "CNRSMH_E_1970_001_002")[0], self.persepolis) self.assertEquals( self.items.word_search("code", "CNRSMH_E_1970_001_002_44")[0], self.item_1) result = self.items.word_search("comment", "comment").order_by("title") self.assertEquals(result[0], self.item_1) self.assertEquals(result[1], self.item_2) self.assertEquals(result[2], self.item_3) self.assertEquals(result[3], self.item_4) self.assertEquals(result[4], self.item_5) self.assertEquals(result[5], self.item_6) def testByChangeTimeOnCollection(self): "Test by_change_time property of MediaCollection class" now = datetime.now() print self.collections.all() print MediaCollection.objects.all() result = self.collections.by_change_time(now - timedelta(hours=1), now).order_by("title") self.assertEquals(result[0], self.persepolis) def testByChangeTimeOnItem(self): "Test by_change_time property of MediaItem class" now = datetime.now() result = self.items.by_change_time(now - timedelta(hours=1), now).order_by("title") self.assertEquals(result[0], self.item_1) def testWithoutCollection(self): "Test without_collection property of MediaItem class" self.assertEquals(self.items.without_collection().count(), 0) def testCodeRequired(self): "Test that a proper failure occur when a collection code isn't provided" c = MediaCollection() try: c.set_revision(self.olivier) except RequiredFieldError, e: self.assertEquals(e.field.name, 'code') else:
def wav_import(self): from telemeta.models import MediaItem, MediaCollection collections = [] for collection in self.collections: collection_dir = self.source_dir + os.sep + collection collection_files = os.listdir(collection_dir) if not '/.' in collection_dir and self.pattern in collection_dir: collection_name = collection.split(os.sep)[-1] collections.append(collection_name) c = MediaCollection.objects.filter(code=collection_name) if not c and collection + '.csv' in collection_files: msg = collection + ' collection NON présente dans la base de données, SORTIE ' self.logger.error(collection, msg) sys.exit(msg) elif not c: msg = 'collection NON présente dans la base de données, CREATION ' self.logger.info(collection, msg) c = MediaCollection(code=collection_name, title=collection_name) c.save() c.set_revision(self.user) else: msg = 'collection présente dans la base de données, SELECTION' self.logger.info(collection, msg) for collection in collections: collection_dir = self.source_dir + os.sep + collection collection_name = collection collection_files = os.listdir(collection_dir) msg = '************************ ' + collection + ' ******************************' self.logger.info(collection, msg[:70]) overwrite = True csv_file = '' rows = {} if collection + '.csv' in collection_files: csv_file = self.source_dir + os.sep + collection + os.sep + collection + '.csv' csv_data = csv.reader(open(csv_file), delimiter=';') for row in csv_data: rows[row[1].strip()] = row[0].strip() msg = collection + ' import du fichier CSV de la collection' self.logger.info(collection, msg[:70]) else: msg = collection + ' pas de fichier CSV dans la collection' self.logger.info(collection, msg[:70]) c = MediaCollection.objects.filter(code=collection_name) if not c: c = MediaCollection(code=collection_name) c.save() msg = ' collection NON présente dans la BDD, CREATION ' self.logger.info(c.code, msg) else: c = c[0] msg = ' id = ' + str(c.id) self.logger.info(c.code, msg) audio_files = [] for file in collection_files: ext = ['WAV', 'wav'] if file.split('.')[-1] in ext and file[0] != '.': audio_files.append(file) audio_files.sort() nb_items = c.items.count() counter = 0 for file in audio_files: code = file.split('.')[0] wav_file = self.source_dir + os.sep + collection + os.sep + file if len(audio_files) <= nb_items: items = MediaItem.objects.filter(code=code) old_ref = '' if code in rows and not items: old_ref = rows[code] items = MediaItem.objects.filter(old_code=old_ref) if items: item = items[0] msg = code + ' : ' + item.old_code + ' : Cas 1 ou 2 : id = ' + str( item.id) self.logger.info('item', msg) item.code = code item.save() else: item = MediaItem(code=code, collection=c) msg = code + ' : ' + old_ref + ' : Cas 1 ou 2 : item NON présent dans la base de données, CREATION' self.logger.info('item', msg) self.write_file(item, wav_file, overwrite) elif nb_items == 1 and len(audio_files) > 1: if counter == 0: msg = code + ' : Cas 3a : item n°01 présent dans la base de données, PASSE' self.logger.info('item', msg) else: item = MediaItem(code=code, collection=c) msg = code + ' : Cas 3a : item NON présent dans la base de données, CREATION' self.logger.info('item', msg) self.write_file(item, wav_file, overwrite) elif nb_items > 1 and nb_items < len(audio_files): msg = code + ' : Cas 3b : nb items < nb de fichiers audio, PAS de creation' self.logger.info('item', msg) counter += 1 msg = 'Liste des URLs des collections importées :' self.logger.info('INFO', msg) for collection in collections: msg = 'http://' + self.domain + '/archives/collections/' + collection self.logger.info(collection, msg)