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. :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 = Media() user = request.environ['repoze.who.identity']['user'] media.author = Author(user.display_name, user.email_address) # Create a temp stub until we can set it to something meaningful timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S') media.title = u'Temporary stub %s' % timestamp media.slug = get_available_slug(Media, '_stub_' + timestamp) DBSession.add(media) DBSession.flush() else: media = fetch_row(Media, id) try: media_file = add_new_media_file(media, file, url) except Invalid, e: DBSession.rollback() data = dict( success = False, message = e.message, )
def __call__(self, environ, start_response): """Commit or rollback the DBSession for every request. Your controller may override this method and have it call :meth:`BareBonesController.__call__` directly to avoid this transaction management. """ try: app_iter = BareBonesController.__call__(self, environ, start_response) except: # An unexpected error has occurred that the WebError will catch DBSession.rollback() raise else: # webob.exc.HTTPException's are caught and turned into a regular # responses in WSGIController._inspect_call. Veto error responses: if 200 <= response.status_int < 400: DBSession.commit() else: DBSession.rollback() return app_iter
def save_engine_params(self, engine, panda, s3, cloudfront, profiles, **kwargs): """Map validated field values to engine data. Since form widgets may be nested or named differently than the keys in the :attr:`mediacore.lib.storage.StorageEngine._data` dict, it is necessary to manually map field values to the data dictionary. :type engine: :class:`mediacore.lib.storage.StorageEngine` subclass :param engine: An instance of the storage engine implementation. :param \*\*kwargs: Validated and filtered form values. :raises formencode.Invalid: If some post-validation error is detected in the user input. This will trigger the same error handling behaviour as with the @validate decorator. """ # The panda client library expects strings. for key in panda: if panda[key] is None: panda[key] = u'' StorageForm.save_engine_params(self, engine, **kwargs) engine._data[PANDA_CLOUD_ID] = panda['cloud_id'] engine._data[PANDA_ACCESS_KEY] = panda['access_key'] engine._data[PANDA_SECRET_KEY] = panda['secret_key'] engine._data[PANDA_API_HOST] = panda['api_host'] engine._data[PANDA_PROFILES] = profiles engine._data[S3_BUCKET_NAME] = s3['bucket_name'] engine._data[CLOUDFRONT_STREAMING_URI] = cloudfront['streaming_uri'] engine._data[CLOUDFRONT_DOWNLOAD_URI] = cloudfront['download_uri'] engine.panda_helper.cache.clear() try: engine.panda_helper().client.get_cloud() except PandaException, e: DBSession.rollback() # TODO: Display this error to the user. raise Invalid(str(e), None, None)
def _autocommit_rollback(req): from mediacore.model.meta import DBSession DBSession.rollback() _autocommit_fire_callbacks(req, req.rollback_callbacks)
def _autocommit_rollback(req): DBSession.rollback() _autocommit_fire_callbacks(req, req.rollback_callbacks)