def bulk(self, type=None, ids=None, **kwargs): """Perform bulk operations on media items :param type: The type of bulk action to perform (delete) :param ids: A list of IDs. """ if not ids: ids = [] elif not isinstance(ids, list): ids = [ids] if type == 'delete': Category.query.filter(Category.id.in_(ids)).delete(False) DBSession.commit() success = True else: success = False return dict( success = success, ids = ids, parent_options = unicode(category_form.c['parent_id'].display()), )
def save(self, id, email_address, display_name, login_details, delete=None, **kwargs): """Save changes or create a new :class:`~mediadrop.model.auth.User` instance. :param id: User ID. If ``"new"`` a new user is created. :type id: ``int`` or ``"new"`` :returns: Redirect back to :meth:`index` after successful save. """ user = fetch_row(User, id) if delete: all_deletes = dict( comments=Comment.query.filter( Comment.author_name == user.user_name), podcasts=Podcast.query.filter( Podcast.author_name == user.user_name), medias=Media.query.filter(Media.author_name == user.user_name)) for need_delete in all_deletes: for element in all_deletes[need_delete]: DBSession.delete(element) DBSession.delete(user) redirect(action='index', id=None) user.display_name = display_name user.email_address = email_address if login_details['user_name'] != 'anonymous': user.user_name = login_details['user_name'] else: raise webob.exc.HTTPUnauthorized().exception password = login_details['password'] if password is not None and password != '': user.password = password if login_details['groups']: query = DBSession.query(Group).filter( Group.group_id.in_(login_details['groups'])) user.groups = list(query.all()) else: user.groups = [] DBSession.add(user) # Check if we're changing the logged in user's own password if user.id == request.perm.user.id \ and password is not None and password != '': DBSession.commit() # repoze.who sees the Unauthorized response and clears the cookie, # forcing a fresh login with the new password raise webob.exc.HTTPUnauthorized().exception redirect(action='index', id=None)
def _autocommit_commit(req): from mediadrop.model.meta import DBSession try: DBSession.commit() except: _autocommit_rollback(req) raise else: _autocommit_fire_callbacks(req, req.commit_callbacks)
def setUp(self): super(DBTestCase, self).setUp() self.env_dir = self._create_environment_folders() self.pylons_config = setup_environment_and_database(self.env_dir, enabled_plugins=self.enabled_plugins) add_default_data() DBSession.commit() config.push_process_config(self.pylons_config)
def setUp(self): super(DBTestCase, self).setUp() self.env_dir = self._create_environment_folders() self.pylons_config = setup_environment_and_database( self.env_dir, enabled_plugins=self.enabled_plugins) add_default_data() DBSession.commit() config.push_process_config(self.pylons_config)
def save(self, id, slug, title, subtitle, author_name, author_email, description, details, feed, delete=None, **kwargs): """Save changes or create a new :class:`~mediadrop.model.podcasts.Podcast` instance. Form handler the :meth:`edit` action and the :class:`~mediadrop.forms.admin.podcasts.PodcastForm`. Redirects back to :meth:`edit` after successful editing and :meth:`index` after successful deletion. """ podcast = fetch_row(Podcast, id) if delete: DBSession.delete(podcast) DBSession.commit() delete_thumbs(podcast) redirect(action='index', id=None) if not slug: slug = title if slug != podcast.slug: podcast.slug = get_available_slug(Podcast, slug, podcast) podcast.title = title podcast.subtitle = subtitle podcast.author = Author(author_name, author_email) podcast.description = description podcast.copyright = details['copyright'] podcast.category = details['category'] podcast.itunes_url = feed['itunes_url'] podcast.feedburner_url = feed['feedburner_url'] podcast.explicit = { 'yes': True, 'clean': False }.get(details['explicit'], None) if id == 'new': DBSession.add(podcast) DBSession.flush() create_default_thumbs_for(podcast) redirect(action='edit', id=podcast.id)
def save(self, name, email, title, description, tags, uploaded_file, url, publish=False): # Update media object metadata self.author = Author(name, email) self.title = title self.slug = get_available_slug(Media, title) self.description = description self.set_tags(tags) # Give the Media object an ID. DBSession.add(self) DBSession.flush() # Create a MediaFile object, add it to the media_obj, and store the file permanently. media_file = add_new_media_file(self, file=uploaded_file, url=url) # The thumbs may have been created already by add_new_media_file if not has_thumbs(self): create_default_thumbs_for(self) self.update_status() ## Save to database DBSession.add(self) DBSession.commit() ## Optional publish if publish: self.reviewed = True self.publishable = True self.publish_on = datetime.now() self.update_popularity() self.update_status() DBSession.add(self) DBSession.commit() return self
def new(self, **kwargs): metadata = json.load(request.POST.get('metadata').file) file = request.POST.get('file') media_obj = Media().save( name=metadata.get('author_name'),email=metadata.get('author_email'), title=metadata.get('title'), description=metadata.get('description'), tags=metadata.get('tags'), uploaded_file=file, url=metadata.get('url'), publish=metadata.get('publish', False) ) DBSession.add(media_obj) DBSession.commit() return { 'status': 201, 'message': 'Media object created.', 'media_id': media_obj.id }
def save(self, id, email_address, display_name, login_details, delete=None, **kwargs): """Save changes or create a new :class:`~mediadrop.model.auth.User` instance. :param id: User ID. If ``"new"`` a new user is created. :type id: ``int`` or ``"new"`` :returns: Redirect back to :meth:`index` after successful save. """ user = fetch_row(User, id) if delete: DBSession.delete(user) redirect(action='index', id=None) user.display_name = display_name user.email_address = email_address user.user_name = login_details['user_name'] password = login_details['password'] if password is not None and password != '': user.password = password if login_details['groups']: query = DBSession.query(Group).filter(Group.group_id.in_(login_details['groups'])) user.groups = list(query.all()) else: user.groups = [] DBSession.add(user) # Check if we're changing the logged in user's own password if user.id == request.perm.user.id \ and password is not None and password != '': DBSession.commit() # repoze.who sees the Unauthorized response and clears the cookie, # forcing a fresh login with the new password raise webob.exc.HTTPUnauthorized().exception redirect(action='index', id=None)
def save( self, id, slug, title, subtitle, author_name, author_email, description, details, feed, delete=None, **kwargs ): """Save changes or create a new :class:`~mediadrop.model.podcasts.Podcast` instance. Form handler the :meth:`edit` action and the :class:`~mediadrop.forms.admin.podcasts.PodcastForm`. Redirects back to :meth:`edit` after successful editing and :meth:`index` after successful deletion. """ podcast = fetch_row(Podcast, id) if delete: DBSession.delete(podcast) DBSession.commit() delete_thumbs(podcast) redirect(action="index", id=None) if not slug: slug = title if slug != podcast.slug: podcast.slug = get_available_slug(Podcast, slug, podcast) podcast.title = title podcast.subtitle = subtitle podcast.author = Author(author_name, author_email) podcast.description = description podcast.copyright = details["copyright"] podcast.category = details["category"] podcast.itunes_url = feed["itunes_url"] podcast.feedburner_url = feed["feedburner_url"] podcast.explicit = {"yes": True, "clean": False}.get(details["explicit"], None) if id == "new": DBSession.add(podcast) DBSession.flush() create_default_thumbs_for(podcast) redirect(action="edit", id=podcast.id)
def insert_settings(defaults): """Insert the given setting if they don't exist yet. XXX: Does not include any support for MultiSetting. This approach won't work for that. We'll need to use a migration script. :type defaults: list :param defaults: Key and value pairs :rtype: list :returns: Any settings that have just been created. """ inserted = [] try: settings_query = DBSession.query(Setting.key)\ .filter(Setting.key.in_([key for key, value in defaults])) existing_settings = set(x[0] for x in settings_query) except ProgrammingError: # If we are running paster setup-app on a fresh database with a # plugin which tries to use this function every time the # Environment.loaded event fires, the settings table will not # exist and this exception will be thrown, but its safe to ignore. # The settings will be created the next time the event fires, # which will likely be the first time the app server starts up. return inserted for key, value in defaults: if key in existing_settings: continue transaction = DBSession.begin_nested() try: s = Setting(key, value) DBSession.add(s) transaction.commit() inserted.append(s) except IntegrityError: transaction.rollback() if inserted: DBSession.commit() return inserted
def bulk(self, type=None, ids=None, **kwargs): """Perform bulk operations on media items :param type: The type of bulk action to perform (delete) :param ids: A list of IDs. """ if not ids: ids = [] elif not isinstance(ids, list): ids = [ids] if type == 'delete': Category.query.filter(Category.id.in_(ids)).delete(False) DBSession.commit() success = True else: success = False return dict( success=success, ids=ids, parent_options=unicode(category_form.c['parent_id'].display()), )