def fetch_and_create_tags(tag_names): """Return a list of Tag instances that match the given names. Tag names that don't yet exist are created automatically and returned alongside the results that did already exist. If you try to create a new tag that would have the same slug as an already existing tag, the existing tag is used instead. :param tag_names: The display :attr:`Tag.name` :type tag_names: list :returns: A list of :class:`Tag` instances. :rtype: :class:`TagList` instance """ lower_names = [name.lower() for name in tag_names] slugs = [slugify(name) for name in lower_names] # Grab all the tags that exist already, whether its the name or slug # that matches. Slugs can be changed by the tag settings UI so we can't # rely on each tag name evaluating to the same slug every time. results = Tag.query.filter( sql.or_(func.lower(Tag.name).in_(lower_names), Tag.slug.in_(slugs))).all() # Filter out any tag names that already exist (case insensitive), and # any tag names evaluate to slugs that already exist. for tag in results: # Remove the match from our three lists until its completely gone while True: try: try: index = slugs.index(tag.slug) except ValueError: index = lower_names.index(tag.name.lower()) tag_names.pop(index) lower_names.pop(index) slugs.pop(index) except ValueError: break # Any remaining tag names need to be created. if tag_names: # We may still have multiple tag names which evaluate to the same slug. # Load it into a dict so that duplicates are overwritten. uniques = dict((slug, name) for slug, name in izip(slugs, tag_names)) # Do a bulk insert to create the tag rows. new_tags = [{'name': n, 'slug': s} for s, n in uniques.iteritems()] DBSession.execute(tags.insert(), new_tags) DBSession.flush() # Query for our newly created rows and append them to our result set. results += Tag.query.filter(Tag.slug.in_(uniques.keys())).all() return results
def cleanup_players_table(enabled=False): """ Ensure that all available players are added to the database and that players are prioritized in incrementally increasing order. :param enabled: Should the default players be enabled upon creation? :type enabled: bool """ from mediadrop.lib.players import (BlipTVFlashPlayer, DailyMotionEmbedPlayer, GoogleVideoFlashPlayer, JWPlayer, VimeoUniversalEmbedPlayer, YoutubePlayer) # When adding players, prefer them in the following order: default_players = [ JWPlayer, YoutubePlayer, VimeoUniversalEmbedPlayer, GoogleVideoFlashPlayer, BlipTVFlashPlayer, DailyMotionEmbedPlayer, ] unordered_players = [p for p in AbstractPlayer if p not in default_players] all_players = default_players + unordered_players # fetch the players that are already in the database s = players.select().order_by('priority') existing_players_query = DBSession.execute(s) existing_player_rows = [p for p in existing_players_query] existing_player_names = [p['name'] for p in existing_player_rows] # Ensure all priorities are monotonically increasing from 1..n priority = 0 for player_row in existing_player_rows: priority += 1 if player_row['priority'] != priority: u = players.update()\ .where(players.c.id == player_row['id'])\ .values(priority=priority) DBSession.execute(u) # Ensure that all available players are in the database for player_cls in all_players: if player_cls.name not in existing_player_names: enable_player = enabled and player_cls in default_players priority += 1 DBSession.execute(players.insert().values( name=player_cls.name, enabled=enable_player, data=player_cls.default_data, priority=priority, ))
def fetch_and_create_tags(tag_names): """Return a list of Tag instances that match the given names. Tag names that don't yet exist are created automatically and returned alongside the results that did already exist. If you try to create a new tag that would have the same slug as an already existing tag, the existing tag is used instead. :param tag_names: The display :attr:`Tag.name` :type tag_names: list :returns: A list of :class:`Tag` instances. :rtype: :class:`TagList` instance """ lower_names = [name.lower() for name in tag_names] slugs = [slugify(name) for name in lower_names] # Grab all the tags that exist already, whether its the name or slug # that matches. Slugs can be changed by the tag settings UI so we can't # rely on each tag name evaluating to the same slug every time. results = Tag.query.filter(sql.or_(func.lower(Tag.name).in_(lower_names), Tag.slug.in_(slugs))).all() # Filter out any tag names that already exist (case insensitive), and # any tag names evaluate to slugs that already exist. for tag in results: # Remove the match from our three lists until its completely gone while True: try: try: index = slugs.index(tag.slug) except ValueError: index = lower_names.index(tag.name.lower()) tag_names.pop(index) lower_names.pop(index) slugs.pop(index) except ValueError: break # Any remaining tag names need to be created. if tag_names: # We may still have multiple tag names which evaluate to the same slug. # Load it into a dict so that duplicates are overwritten. uniques = dict((slug, name) for slug, name in izip(slugs, tag_names)) # Do a bulk insert to create the tag rows. new_tags = [{'name': n, 'slug': s} for s, n in uniques.iteritems()] DBSession.execute(tags.insert(), new_tags) DBSession.flush() # Query for our newly created rows and append them to our result set. results += Tag.query.filter(Tag.slug.in_(uniques.keys())).all() return results
def fetch_enabled_players(): """Return player classes and their data dicts in ascending priority. Warnings are logged any time a row is found that does not match up to one of the classes that are currently registered. A warning will also be raised if there are no players configured/enabled. :rtype: list of tuples :returns: :class:`~mediadrop.lib.players.AbstractPlayer` subclasses and the configured data associated with them. """ player_classes = dict((p.name, p) for p in AbstractPlayer) query = sql.select((players.c.name, players.c.data))\ .where(players.c.enabled == True)\ .order_by(players.c.priority.asc(), players.c.id.desc()) query_data = DBSession.execute(query).fetchall() while query_data: try: return [(player_classes[name], data) for name, data in query_data] except KeyError: log.warn('Player name %r exists in the database but has not ' 'been registered.' % name) query_data.remove((name, data)) log.warn('No registered players are configured in your database.') return []
def increment_views(self): """Increment the number of views in the database. We avoid concurrency issues by incrementing JUST the views and not allowing modified_on to be updated automatically. """ if self.id is None: self.views += 1 return self.views DBSession.execute(media.update()\ .values(views=media.c.views + 1)\ .where(media.c.id == self.id)) # Increment the views by one for the rest of the request, # but don't allow the ORM to increment the views too. attributes.set_committed_value(self, 'views', self.views + 1) return self.views