def attachments(request): #, DBSession, commit """ """ attachments_data = [ ('test/preview1.3gp' , 'preview' ), ('test/preview2.flv' , 'preview' ), ('test/preview3.mp4' , 'preview' ), ('test/image1.jpg' , 'thumbnail'), ('test/image2.jpg' , 'thumbnail'), ('test/image3.png' , 'thumbnail'), ('test/processed.mpg', 'video' ), ('test/subtitles.ssa', 'subtitle' ), ] attachments = [] for attachment_location, attachment_type in attachments_data: attachment = Attachment() attachment.location = attachment_location attachment.type = attachment_type DBSession.add(attachment) attachments.append(attachment) def finalizer(): pass #for attachment in attachments: # DBSession.delete(attachment) #commit() request.addfinalizer(finalizer) commit() return attachments
def _add_attachments(self, track, processed_files): track.attachments.clear() for processde_file in processed_files.values(): assert processde_file.attachment_type in ATTACHMENT_TYPES attachment = Attachment() attachment.type = processde_file.attachment_type attachment.location = processde_file.relative track.attachments.append(attachment)
def _add_attachments(self, track, processed_files): track.attachments.clear() for attachment_type in processed_files.keys() & set(_attachment_types.enums): for processed_file in processed_files[attachment_type]: attachment = Attachment() attachment.type = attachment_type attachment.location = processed_file.relative track.attachments.append(attachment)
def track_import_post(request): existing_track_ids = _existing_tracks_dict().keys() for track_dict in _get_json_request(request): if track_dict['id'] in existing_track_ids: log.warning( 'Exists: {source_filename} - {id}'.format(**track_dict)) continue log.info('Import: {source_filename} - {id}'.format(**track_dict)) track = Track() track.id = track_dict['id'] track.source_filename = track_dict['source_filename'] track.duration = track_dict['duration'] track.lyrics = track_dict['lyrics'] # Attachments for attachment_dict in track_dict['attachments']: assert attachment_dict['type'] in ATTACHMENT_TYPES attachment = Attachment() attachment.type = attachment_dict['type'] attachment.location = attachment_dict['location'] track.attachments.append(attachment) # Tags for tag_string in track_dict['tags']: tag = get_tag(tag_string, create_if_missing=True) if tag: track.tags.append(tag) elif tag_string: log.warning('null tag %s', tag_string) for duplicate_tag in (tag for tag in track.tags if track.tags.count(tag) > 1): log.warning('Unneeded duplicate tag found %s in %s', duplicate_tag, track.source_filename) track.tags.remove(duplicate_tag) DBSession.add(track) commit() request.registry.settings['karakara.tracks.version'] += 1 return action_ok()
def track_import_post(request): existing_track_ids = _existing_tracks_dict().keys() for track_dict in _get_json_request(request): if track_dict['id'] in existing_track_ids: log.warning('Exists: {source_filename} - {id}'.format(**track_dict)) continue log.info('Import: {source_filename} - {id}'.format(**track_dict)) track = Track() track.id = track_dict['id'] track.source_filename = track_dict['source_filename'] track.duration = track_dict['duration'] track.lyrics = track_dict['lyrics'] # Attachments for attachment_dict in track_dict['attachments']: assert attachment_dict['type'] in ATTACHMENT_TYPES attachment = Attachment() attachment.type = attachment_dict['type'] attachment.location = attachment_dict['location'] track.attachments.append(attachment) # Tags for tag_string in track_dict['tags']: tag = get_tag(tag_string, create_if_missing=True) if tag: track.tags.append(tag) elif tag_string: log.warning('null tag %s', tag_string) for duplicate_tag in (tag for tag in track.tags if track.tags.count(tag) > 1): log.warning('Unneeded duplicate tag found %s in %s', duplicate_tag, track.source_filename) track.tags.remove(duplicate_tag) DBSession.add(track) commit() request.registry.settings['karakara.tracks.version'] += 1 return action_ok()
def random_tracks(request, DBSession, commit, num_tracks=800): log.info('Generating {0} random tracks'.format(num_tracks)) # Attachment generation ---------------------------------------------------- attachments_meta = [ ('preview1.3gp' , 'preview' ), ('preview2.flv' , 'preview' ), ('image1.jpg' , 'thumbnail'), ('image2.jpg' , 'thumbnail'), ('image3.png' , 'thumbnail'), ('processed.mpg', 'video' ), ('subtitles.ssa', 'subtitle' ), ] attachments = [] for location, type in attachments_meta: attachment = Attachment() attachment.location = location attachment.type = type DBSession.add(attachment) attachments.append(attachment) commit() # Random Tag generation ---------------------------------------------------- parent_tag = get_tag('from') for series_num in range(10): DBSession.add(Tag('Series %s'%random_string(1), parent_tag)) commit() # -------------------------------------------------------------------------- tags = DBSession.query(Tag).options(joinedload(Tag.parent)).all() alias_parent_tag = aliased(Tag) tags_category = DBSession.query(Tag).join(alias_parent_tag, Tag.parent).filter(alias_parent_tag.name=='category').all() tags_from = DBSession.query(Tag).join(alias_parent_tag, Tag.parent).filter(alias_parent_tag.name=='from').all() def get_random_tags(num_tags=None): random_tags = [] if not num_tags: num_tags = random.randint(1,5) for tag_num in range(num_tags): random_tags.append(tags[random.randint(0,len(tags)-1)]) # if we have 'from' tags and NO category tag, then add a random category from_tags = [t for t in random_tags if t.parent and t.parent.name=='from'] category_tags = [t for t in random_tags if t.parent and t.parent.name=='category'] if from_tags and not category_tags: random_tags.append(random.choice(tags_category)) if category_tags and not from_tags: random_tags.append(random.choice(tags_from)) # only have 1 'from' tag for tag in from_tags[1:]: random_tags.remove(tag) # only have one category tag for tag in category_tags[1:]: random_tags.remove(tag) return random_tags def get_random_description(words=None, word_size=None): if not words: words = random.randint(4,24) if not word_size: word_size = random.randint(2,16) return " ".join([random_string(word_size) for word in range(words)]) # Track generation --------------------------------------------------------- tag_titles = [get_tag('Test Track {0}'.format(i), 'title', create_if_missing=True) for i in range(num_tracks)] #commit() for track_number in range(num_tracks): track = Track() track.id = "track_%d" % track_number #track.description = get_random_description() track.duration = random.randint(60,360) track.tags = list(set(get_random_tags())) + [tag_titles[track_number]] track.attachments = attachments DBSession.add(track) commit()
def create_attachment(attachment_description): attachment = Attachment() attachment.location = attachment_description.location attachment.type = attachment_description.type DBSession.add(attachment) return attachment
def init_random_data(num_tracks=100): # Attachment generation ---------------------------------------------------- attachments_meta = [ ('preview1.3gp' , 'preview' ), ('preview2.flv' , 'preview' ), ('image1.jpg' , 'image' ), ('image2.jpg' , 'image' ), ('image3.png' , 'image' ), ('processed.mpg', 'processed'), ('subtitles.ssa', 'subtitle' ), ] attachments = [] for location, type in attachments_meta: attachment = Attachment() attachment.location = location attachment.type = type DBSession.add(attachment) attachments.append(attachment) # Random Tag generation ---------------------------------------------------- parent_tag = get_tag('from') for series_num in range(10): DBSession.add(Tag('Series %s'%random_string(1), parent_tag)) commit() # -------------------------------------------------------------------------- tags = DBSession.query(Tag).all() def get_random_tags(num_tags=None): random_tags = [] if not num_tags: num_tags = random.randint(1,5) for tag_num in range(num_tags): random_tags.append(tags[random.randint(0,len(tags)-1)]) return random_tags def get_random_description(words=None, word_size=None): if not words: words = random.randint(4,24) if not word_size: word_size = random.randint(2,16) return " ".join([random_string(word_size) for word in range(words)]) # Track generation --------------------------------------------------------- for track_number in range(num_tracks): track = Track() track.id = "track_%d" % track_number track.title = "Test Track %s" % track_number track.description = get_random_description() track.duration = random.randint(60,360) track.tags = list(set(get_random_tags())) track.attachments = attachments DBSession.add(track) commit()
def random_tracks(request, DBSession, commit, num_tracks=800): """ Generate 100's of random tracks """ log.info('Generating {0} random tracks'.format(num_tracks)) # Attachment generation ---------------------------------------------------- attachments_meta = [ ('preview1.3gp', 'preview'), ('preview2.flv', 'preview'), ('image1.jpg', 'image'), ('image2.jpg', 'image'), ('image3.png', 'image'), ('processed.mpg', 'video'), ('subtitles.srt', 'srt'), ] attachments = [] for location, type in attachments_meta: attachment = Attachment() attachment.location = location attachment.type = type DBSession.add(attachment) attachments.append(attachment) commit() # Random Tag generation ---------------------------------------------------- parent_tag = get_tag('from') for series_num in range(10): DBSession.add(Tag('Series %s' % random_string(1), parent_tag)) commit() # -------------------------------------------------------------------------- tags = DBSession.query(Tag).options(joinedload(Tag.parent)).all() alias_parent_tag = aliased(Tag) tags_category = DBSession.query(Tag).join( alias_parent_tag, Tag.parent).filter(alias_parent_tag.name == 'category').all() tags_from = DBSession.query(Tag).join( alias_parent_tag, Tag.parent).filter(alias_parent_tag.name == 'from').all() def get_random_tags(num_tags=None): random_tags = [] if not num_tags: num_tags = random.randint(1, 5) for tag_num in range(num_tags): random_tags.append(tags[random.randint(0, len(tags) - 1)]) # if we have 'from' tags and NO category tag, then add a random category from_tags = [ t for t in random_tags if t.parent and t.parent.name == 'from' ] category_tags = [ t for t in random_tags if t.parent and t.parent.name == 'category' ] if from_tags and not category_tags: random_tags.append(random.choice(tags_category)) if category_tags and not from_tags: random_tags.append(random.choice(tags_from)) # only have 1 'from' tag for tag in from_tags[1:]: random_tags.remove(tag) # only have one category tag for tag in category_tags[1:]: random_tags.remove(tag) return random_tags def get_random_description(words=None, word_size=None): if not words: words = random.randint(4, 24) if not word_size: word_size = random.randint(2, 16) return " ".join([random_string(word_size) for word in range(words)]) # Track generation --------------------------------------------------------- tag_titles = [ get_tag('Test Track {0}'.format(i), 'title', create_if_missing=True) for i in range(num_tracks) ] #commit() for track_number in range(num_tracks): track = Track() track.id = "track_%d" % track_number #track.description = get_random_description() track.duration = random.randint(60, 360) track.tags = list(set(get_random_tags())) + [tag_titles[track_number]] track.attachments = attachments DBSession.add(track) commit()