def save(self, id, slug, title, author_name, author_email, description, notes, details, podcast, tags, categories, delete=None, **kwargs): """Save changes or create a new :class:`~mediacore.model.media.Media` instance. Form handler the :meth:`edit` action and the :class:`~mediacore.forms.admin.media.MediaForm`. Redirects back to :meth:`edit` after successful editing and :meth:`index` after successful deletion. """ media = fetch_row(Media, id) if delete: file_paths = helpers.thumb_paths(media) for f in media.files: file_paths.append(f.file_path) # Remove the file from the session so that SQLAlchemy doesn't # try to issue an UPDATE to set the MediaFile.media_id to None. # The database ON DELETE CASCADE handles everything for us. DBSession.expunge(f) DBSession.delete(media) transaction.commit() helpers.delete_files(file_paths, 'media') redirect(action='index', id=None) media.slug = get_available_slug(Media, slug, media) media.title = title media.author = Author(author_name, author_email) media.description = description media.notes = notes media.duration = details['duration'] # validator converts hh:mm:ss to secs media.podcast_id = podcast media.set_tags(tags) media.set_categories(categories) media.update_status() DBSession.add(media) DBSession.flush() if id == 'new': helpers.create_default_thumbs_for(media) redirect(action='edit', id=media.id)
def _save_media_obj(self, name, email, title, description, tags, file, url): # create our media object as a status-less placeholder initially media_obj = Media() media_obj.author = Author(name, email) media_obj.title = title media_obj.slug = get_available_slug(Media, title) media_obj.description = description media_obj.notes = fetch_setting('wording_additional_notes') media_obj.set_tags(tags) # Create a media object, add it to the media_obj, and store the file permanently. if file is not None: media_file = _add_new_media_file(media_obj, file.filename, file.file) else: media_file = MediaFile() url = unicode(url) embed = parse_embed_url(url) if embed: media_file.type = embed['type'] media_file.container = embed['container'] media_file.embed = embed['id'] media_file.display_name = '%s ID: %s' % \ (embed['container'].capitalize(), media_file.embed) else: # Check for types we can play ourselves ext = os.path.splitext(url)[1].lower()[1:] container = guess_container_format(ext) if container in accepted_extensions(): media_file.type = guess_media_type(container) media_file.container = container media_file.url = url media_file.display_name = os.path.basename(url) else: # Trigger a validation error on the whole form. raise formencode.Invalid('Please specify a URL or upload a file below.', None, None) media_obj.files.append(media_file) # Add the final changes. media_obj.update_status() DBSession.add(media_obj) DBSession.flush() create_default_thumbs_for(media_obj) return media_obj
def _save_media_obj(self, name, email, title, description, tags, file, url): # create our media object as a status-less placeholder initially media_obj = Media() media_obj.author = Author(name, email) media_obj.title = title media_obj.slug = get_available_slug(Media, title) media_obj.description = description media_obj.notes = fetch_setting('wording_additional_notes') media_obj.set_tags(tags) # Create a media object, add it to the media_obj, and store the file permanently. if file is not None: media_file = _add_new_media_file(media_obj, file.filename, file.file) else: # FIXME: For some reason the media.type isn't ever set to video # during this request. On subsequent requests, when # media_obj.update_type() is called, it is set properly. # This isn't too serious an issue right now because # it is called the first time a moderator goes to review # the new media_obj. media_file = MediaFile() url = unicode(url) for type, info in external_embedded_containers.iteritems(): match = info['pattern'].match(url) if match: media_file.type = guess_media_type(type) media_file.container = type media_file.embed = match.group('id') media_file.display_name = type.capitalize() + ' ID: ' + media_file.embed break else: # Trigger a validation error on the whole form. raise formencode.Invalid('Please specify a URL or upload a file below.', None, None) media_obj.files.append(media_file) # Add the final changes. media_obj.update_type() media_obj.update_status() DBSession.add(media_obj) DBSession.flush() create_default_thumbs_for(media_obj) return media_obj
def save(self, id, slug, title, subtitle, author_name, author_email, description, details, delete=None, **kwargs): """Save changes or create a new :class:`~mediacore.model.podcasts.Podcast` instance. Form handler the :meth:`edit` action and the :class:`~mediacore.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: file_paths = helpers.thumb_paths(podcast) DBSession.delete(podcast) transaction.commit() helpers.delete_files(file_paths, 'podcasts') redirect(action='index', id=None) 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 = details['itunes_url'] podcast.feedburner_url = details['feedburner_url'] podcast.explicit = {'yes': True, 'clean': False}.get(details['explicit'], None) DBSession.add(podcast) DBSession.flush() if id == 'new': helpers.create_default_thumbs_for(podcast) redirect(action='edit', id=podcast.id)
media_file.url = url media_file.display_name = os.path.basename(url) data['success'] = True else: data['message'] = 'Unsupported URL' else: data['message'] = 'No action to perform.' if data['success']: media.files.append(media_file) media.update_status() DBSession.add(media) DBSession.flush() if id == 'new': helpers.create_default_thumbs_for(media) # Render some widgets so the XHTML can be injected into the page edit_form_xhtml = unicode(edit_file_form.display( action=url_for(action='edit_file', id=media.id), file=media_file)) status_form_xhtml = unicode(update_status_form.display( action=url_for(action='update_status', id=media.id), media=media)) data.update(dict( media_id = media.id, file_id = media_file.id, file_type = media_file.type, edit_form = edit_form_xhtml, status_form = status_form_xhtml,
def add_file(self, id, file=None, url=None, **kwargs): """Save action for the :class:`~mediacore.forms.admin.media.AddFileForm`. Creates a new :class:`~mediacore.model.media.MediaFile` from the uploaded file or the local or remote URL. :param id: Media ID. If ``"new"`` a new Media stub is created with :func:`~mediacore.model.media.create_media_stub`. :type id: :class:`int` or ``"new"`` :param file: The uploaded file :type file: :class:`cgi.FieldStorage` or ``None`` :param url: A URL to a recognizable audio or video file :type url: :class:`unicode` or ``None`` :rtype: JSON dict :returns: success bool message Error message, if unsuccessful media_id The :attr:`~mediacore.model.media.Media.id` which is important if new media has just been created. file_id The :attr:`~mediacore.model.media.MediaFile.id` for the newly created file. edit_form The rendered XHTML :class:`~mediacore.forms.admin.media.EditFileForm` for this file. status_form The rendered XHTML :class:`~mediacore.forms.admin.media.UpdateStatusForm` """ if id == 'new': media = create_media_stub() else: media = fetch_row(Media, id) data = dict(success=False) if file is not None: # Create a media object, add it to the video, and store the file permanently. media_file = _add_new_media_file(media, file.filename, file.file) data['success'] = True elif url: media_file = MediaFile() # Parse the URL checking for known embeddables like YouTube for type, info in external_embedded_containers.iteritems(): match = info['pattern'].match(url) if match: media_file.type = guess_media_type(type) media_file.container = type media_file.embed = match.group('id') media_file.display_name = type.capitalize() + ' ID: ' + media_file.embed data['success'] = True break else: # Check for types we can play ourselves try: ext = os.path.splitext(url)[1].lower()[1:] container = guess_container_format(ext) except KeyError: container = None for conts in playable_containers.itervalues(): if container in conts: media_file.type = guess_media_type(container) media_file.container = container media_file.url = url media_file.display_name = os.path.basename(url) data['success'] = True break else: data['message'] = 'Unsupported URL' else: data['message'] = 'No action to perform.' if data['success']: media.files.append(media_file) media.update_type() media.update_status() DBSession.add(media) DBSession.flush() if id == 'new': helpers.create_default_thumbs_for(media) # Render some widgets so the XHTML can be injected into the page edit_form_xhtml = unicode(edit_file_form.display( action=url_for(action='edit_file', id=media.id), file=media_file)) status_form_xhtml = unicode(update_status_form.display( action=url_for(action='update_status', id=media.id), media=media)) data.update(dict( media_id = media.id, file_id = media_file.id, file_type = media_file.type, edit_form = edit_form_xhtml, status_form = status_form_xhtml, )) return data
DBSession.add(f) continue f.display_name = f.url f.file_name = f.url f.url = None DBSession.add(f) commit_session() helpers.config = conf helpers.config['thumb_sizes'] = { # the dimensions (in pixels) to scale thumbnails 'media': { 's': (128, 72), 'm': (160, 90), 'l': (560, 315), }, 'podcasts': { 's': (128, 128), 'm': (160, 160), 'l': (600, 600), }, } for collection in (Media.query, Podcast.query): for item in collection: if not helpers.thumb_path(item, 'm', exists=True): helpers.create_default_thumbs_for(item) print 'Default thumbs created for', item