<s> import httplib <EOL> import functools <EOL> from modularodm . exceptions import NoResultsFound <EOL> from modularodm . storage . base import KeyExistsException <EOL> from framework . auth . decorators import must_be_signed <EOL> from framework . exceptions import HTTPError <EOL> from website . models import User <EOL> from website . models import Node <EOL> from website . files import models <EOL> from website . files import exceptions <EOL> from website . project . decorators import ( <EOL> must_not_be_registration , must_have_addon , <EOL> ) <EOL> def handle_odm_errors ( func ) : <EOL> @ functools . wraps ( func ) <EOL> def wrapped ( * args , ** kwargs ) : <EOL> try : <EOL> return func ( * args , ** kwargs ) <EOL> except NoResultsFound : <EOL> raise HTTPError ( httplib . NOT_FOUND ) <EOL> except KeyExistsException : <EOL> raise HTTPError ( httplib . CONFLICT ) <EOL> except exceptions . VersionNotFoundError : <EOL> raise HTTPError ( httplib . NOT_FOUND ) <EOL> return wrapped <EOL> def autoload_filenode ( must_be = None , default_root = False ) : <EOL> """<STR_LIT>""" <EOL> def _autoload_filenode ( func ) : <EOL> @ handle_odm_errors <EOL> @ must_have_addon ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> @ functools . wraps ( func ) <EOL> def wrapped ( * args , ** kwargs ) : <EOL> node = kwargs [ '<STR_LIT>' ] <EOL> if '<STR_LIT>' not in kwargs and default_root : <EOL> file_node = kwargs [ '<STR_LIT>' ] . get_root ( ) <EOL> else : <EOL> file_node = models . OsfStorageFileNode . get ( kwargs . get ( '<STR_LIT>' ) , node ) <EOL> if must_be and file_node . kind != must_be : <EOL> raise HTTPError ( httplib . BAD_REQUEST , data = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' . format ( must_be , file_node . kind ) <EOL> } ) <EOL> kwargs [ '<STR_LIT>' ] = file_node <EOL> return func ( * args , ** kwargs ) <EOL> return wrapped <EOL> return _autoload_filenode <EOL> def waterbutler_opt_hook ( func ) : <EOL> @ must_be_signed <EOL> @ handle_odm_errors <EOL> @ must_not_be_registration <EOL> @ must_have_addon ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> @ functools . wraps ( func ) <EOL> def wrapped ( payload , * args , ** kwargs ) : <EOL> try : <EOL> user = User . load ( payload [ '<STR_LIT:user>' ] ) <EOL> dest_node = Node . load ( payload [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) <EOL> source = models . OsfStorageFileNode . get ( payload [ '<STR_LIT:source>' ] , kwargs [ '<STR_LIT>' ] ) <EOL> dest_parent = models . OsfStorageFolder . get ( payload [ '<STR_LIT>' ] [ '<STR_LIT>' ] , dest_node ) <EOL> kwargs . update ( { <EOL> '<STR_LIT:user>' : user , <EOL> '<STR_LIT:source>' : source , <EOL> '<STR_LIT>' : dest_parent , <EOL> '<STR_LIT:name>' : payload [ '<STR_LIT>' ] [ '<STR_LIT:name>' ] , <EOL> } ) <EOL> except KeyError : <EOL> raise HTTPError ( httplib . BAD_REQUEST ) <EOL> return func ( * args , ** kwargs ) <EOL> return wrapped </s>
