def index_seasons(): """Figure out season for files with timestamps""" FileTimeIndexer.install() filter_code = 'season' FilterHelper.create_filter(filter_code, 'Season') FilterHelper.add_filter_option(filter_code, 0, 'Spring') FilterHelper.add_filter_option(filter_code, 1, 'Summer') FilterHelper.add_filter_option(filter_code, 2, 'Fall') FilterHelper.add_filter_option(filter_code, 3, 'Winter') time_select = FileTimeIndexer.get_time_attr_select() # join on value table to find not yet indexed files FilterHelper.join_filter_values(time_select, filter_code, 'tt.parent') time_select.where('fv.file IS NULL').limit(100) for time in time_select.query().fetchall(): try: month = int(datetime.fromtimestamp(time['value']).strftime('%m')) shifted = (month - 3)%12 # makes march = 0, first month of spring FilterHelper.set_filter_values( time['parent'], filter_code, shifted/3) except ValueError: pass # invalid format
def index_seasons(): """Figure out season for files with timestamps""" FileTimeIndexer.install() filter_code = 'season' FilterHelper.create_filter(filter_code, 'Season') FilterHelper.add_filter_option(filter_code, 0, 'Spring') FilterHelper.add_filter_option(filter_code, 1, 'Summer') FilterHelper.add_filter_option(filter_code, 2, 'Fall') FilterHelper.add_filter_option(filter_code, 3, 'Winter') time_select = FileTimeIndexer.get_time_attr_select() # join on value table to find not yet indexed files FilterHelper.join_filter_values(time_select, filter_code, 'tt.parent') time_select.where('fv.file IS NULL').limit(100) for time in time_select.query().fetchall(): try: month = int( datetime.fromtimestamp(time['value']).strftime('%m')) shifted = (month - 3) % 12 # makes march = 0, first month of spring FilterHelper.set_filter_values(time['parent'], filter_code, shifted / 3) except ValueError: pass # invalid format
def index_time_of_day(): """Figure out time of day for files with timestamps""" FileTimeIndexer.install() filter_code = 'tod' # time-of-day FilterHelper.create_filter(filter_code, 'Time of Day') FilterHelper.add_filter_option(filter_code, 0, 'Morning') FilterHelper.add_filter_option(filter_code, 1, 'Noon') FilterHelper.add_filter_option(filter_code, 2, 'Afternoon') FilterHelper.add_filter_option(filter_code, 3, 'Evening') FilterHelper.add_filter_option(filter_code, 4, 'Night') time_select = FileTimeIndexer.get_time_attr_select() # join on value table to find not yet indexed files FilterHelper.join_filter_values(time_select, filter_code, 'tt.parent') time_select.where('fv.file IS NULL').limit(100) for time in time_select.query().fetchall(): try: hour = int( datetime.fromtimestamp(time['value']).strftime('%H')) tod = 4 # Night if hour >= 5 and hour <= 10: tod = 0 # Morning elif hour >= 11 and hour <= 13: tod = 1 # Noon elif hour >= 14 and hour <= 17: tod = 2 # Afternoon elif hour >= 18 and hour <= 21: tod = 3 # Evening FilterHelper.set_filter_values(time['parent'], filter_code, tod) except ValueError: pass # invalid format
def index_time_of_day(): """Figure out time of day for files with timestamps""" FileTimeIndexer.install() filter_code = 'tod' # time-of-day FilterHelper.create_filter(filter_code, 'Time of Day') FilterHelper.add_filter_option(filter_code, 0, 'Morning') FilterHelper.add_filter_option(filter_code, 1, 'Noon') FilterHelper.add_filter_option(filter_code, 2, 'Afternoon') FilterHelper.add_filter_option(filter_code, 3, 'Evening') FilterHelper.add_filter_option(filter_code, 4, 'Night') time_select = FileTimeIndexer.get_time_attr_select() # join on value table to find not yet indexed files FilterHelper.join_filter_values(time_select, filter_code, 'tt.parent') time_select.where('fv.file IS NULL').limit(100) for time in time_select.query().fetchall(): try: hour = int(datetime.fromtimestamp(time['value']).strftime('%H')) tod = 4 # Night if hour >= 5 and hour <= 10: tod = 0 # Morning elif hour >= 11 and hour <= 13: tod = 1 # Noon elif hour >= 14 and hour <= 17: tod = 2 # Afternoon elif hour >= 18 and hour <= 21: tod = 3 # Evening FilterHelper.set_filter_values( time['parent'], filter_code, tod) except ValueError: pass # invalid format
def test_filter(self): """Test FilterHelper functions""" # create a type filter type_filter = FilterHelper.create_filter('type', 'Type') FilterHelper.add_filter_option(type_filter, 1, 'Text') FilterHelper.add_filter_option(type_filter, 2, 'Audio') FilterHelper.add_filter_option(type_filter, 3, 'Image') # create a size filter size_filter = FilterHelper.create_filter('size', 'Size') FilterHelper.add_filter_option(size_filter, 1, 'Small') FilterHelper.add_filter_option(size_filter, 2, 'Medium') FilterHelper.add_filter_option(size_filter, 3, 'Large') f = FileModel({ 'name': 'textfile', 'type': 'text', 'size': '10' }).save() FilterHelper.set_filter_values(f, 'type', 1) FilterHelper.set_filter_values(f, 'size', 1) f = FileModel({ 'name': 'audiofile', 'type': 'audio', 'size': '100' }).save() FilterHelper.set_filter_values(f, 'type', 2) FilterHelper.set_filter_values(f, 'size', 2) f = FileModel({ 'name': 'hybridfile', 'type': 'hybrid', 'size': '1000' }).save() FilterHelper.set_filter_values(f, 'type', (1, 3)) FilterHelper.set_filter_values(f, 'size', (2, 3)) fileset = FileModel.all() FilterHelper.apply_filter('type', 1, fileset) # fileset should contain 'textfile' and 'hybridfile' self.assertEqual(len(fileset), 2) self.assertIn(fileset[0].name(), ('textfile', 'hybridfile')) self.assertIn(fileset[1].name(), ('textfile', 'hybridfile')) fileset = FileModel.all() FilterHelper.apply_filter('size', 2, fileset) # fileset should contain 'audiofile' and 'hybridfile' self.assertEqual(len(fileset), 2) self.assertIn(fileset[0].name(), ('audiofile', 'hybridfile')) self.assertIn(fileset[1].name(), ('audiofile', 'hybridfile')) fileset = FileModel.all() FilterHelper.apply_filter('type', 1, fileset) FilterHelper.apply_filter('size', 2, fileset) # fileset should only contain 'hybridfile' self.assertEqual(len(fileset), 1) self.assertEqual(fileset[0].name(), 'hybridfile')