From e47f80802e49e63edc0d1ac5f68942d5385c121f Mon Sep 17 00:00:00 2001 From: james Date: Mon, 11 Jul 2011 14:00:31 -0400 Subject: [PATCH] CC-2524:MusicDir.php: adding error handling - following functions now handle error and return code and error message if there is an error: addDir(), setStorDir(), removeWatchedDir() - fixed airtime-importy.py to work with new MusicDir functions --- .../application/controllers/ApiController.php | 9 ++-- airtime_mvc/application/models/MusicDir.php | 45 ++++++++++++++----- python_apps/airtime-import.py | 12 ++--- python_apps/api_clients/api_client.py | 5 ++- 4 files changed, 48 insertions(+), 23 deletions(-) diff --git a/airtime_mvc/application/controllers/ApiController.php b/airtime_mvc/application/controllers/ApiController.php index 3517be709..08e01c3de 100644 --- a/airtime_mvc/application/controllers/ApiController.php +++ b/airtime_mvc/application/controllers/ApiController.php @@ -569,7 +569,7 @@ class ApiController extends Zend_Controller_Action exit; } - $this->view = MusicDir::addWatchedDir($path); + $this->view->msg = MusicDir::addWatchedDir($path); } public function removeWatchedDirAction() { @@ -586,8 +586,7 @@ class ApiController extends Zend_Controller_Action exit; } - $dir = MusicDir::getDirByPath($path); - $this->view = $dir->remove(); + $this->view->msg = MusicDir::removeWatchedDir($path); } public function setStorageDirAction() { @@ -603,8 +602,8 @@ class ApiController extends Zend_Controller_Action print 'You are not allowed to access this resource.'; exit; } - MusicDir::setStorDir($path); - $this->view = MusicDir::getStorDir()->getDirectory(); + + $this->view->msg = MusicDir::setStorDir($path); } } diff --git a/airtime_mvc/application/models/MusicDir.php b/airtime_mvc/application/models/MusicDir.php index c875f72fc..8ceac2be8 100644 --- a/airtime_mvc/application/models/MusicDir.php +++ b/airtime_mvc/application/models/MusicDir.php @@ -47,12 +47,16 @@ class MusicDir { { $dir = new CcMusicDirs(); $dir->setType($p_type); - $dir->setDirectory($p_path); - $dir->save(); - - $mus_dir = new MusicDir($dir); - - return $mus_dir; + $temp = $dir->setDirectory($p_path); + try{ + $dir->save(); + return array("code"=>0); + } + catch(Exception $e){ + //echo $e->getMessage(); + return array("code"=>1, "error"=>"$p_path is already set as the current storage dir or the watched folders"); + } + } public static function addWatchedDir($p_path) @@ -75,8 +79,13 @@ class MusicDir { ->filterByDirectory($p_path) ->findOne(); - $mus_dir = new MusicDir($dir); - return $mus_dir; + if($dir == NULL){ + return null; + } + else{ + $mus_dir = new MusicDir($dir); + return $mus_dir; + } } public static function getWatchedDirs() @@ -109,8 +118,14 @@ class MusicDir { public static function setStorDir($p_dir) { $dir = self::getStorDir(); - // we need to check if p_dir is in watched list - $dir->setDirectory($p_dir); + // if $p_dir doesn't exist in DB + $exist = $dir->getDirByPath($p_dir); + if($exist == NULL){ + $dir->setDirectory($p_dir); + return array("code"=>0); + }else{ + return array("code"=>1, "error"=>"$p_dir is already set as the current storage dir or the watched folders"); + } } public static function getWatchedDirFromFilepath($p_filepath) @@ -128,6 +143,16 @@ class MusicDir { return null; } + + public static function removeWatchedDir($p_dir){ + $dir = MusicDir::getDirByPath($p_dir); + if($dir == NULL){ + return array("code"=>1,"error"=>"$p_dir doesn't exist in the watched list"); + }else{ + $dir->remove(); + return array("code"=>0); + } + } public static function splitFilePath($p_filepath) { diff --git a/python_apps/airtime-import.py b/python_apps/airtime-import.py index 86c739c2a..3e1ed24ef 100644 --- a/python_apps/airtime-import.py +++ b/python_apps/airtime-import.py @@ -46,10 +46,10 @@ def watch_add(args): if(os.path.isdir(args.path)): res = api_client.add_watched_dir(args.path) # sucess - if(res == '[]'): + if(res['msg']['code'] == 0): print "%s added to watched folder list successfully" % args.path else: - print "Adding %s to watched folder list failed.( path already exist in the list )" % args.path + print "Adding a watched folder failed. : %s" % res['msg']['error'] else: print "Given path is not a directory: %s" % args.path @@ -69,10 +69,10 @@ def watch_remove(args): if(os.path.isdir(args.path)): res = api_client.remove_watched_dir(args.path) # sucess - if(res == '[]'): + if(res['msg']['code'] == 0): print "%s removed from watched folder list successfully" % args.path else: - print "Removing %s from watched folder list failed.( path doesn't exist in the list )" % args.path + print "Removing a watched folder failed. : %s" % res['msg']['error'] else: print "Given path is not a directory: %s" % args.path @@ -80,10 +80,10 @@ def set_stor_dir(args): if(os.path.isdir(args.path)): res = api_client.set_storage_dir(args.path) # sucess - if(res == '[]'): + if(res['msg']['code'] == 0): print "Successfully set storage folder to %s" % args.path else: - print "Setting storage folder to %s failed." % args.path + print "Setting storage folder to failed.: %s" % res['msg']['error'] else: print "Given path is not a directory: %s" % args.path diff --git a/python_apps/api_clients/api_client.py b/python_apps/api_clients/api_client.py index e1e25b085..d6473809b 100644 --- a/python_apps/api_clients/api_client.py +++ b/python_apps/api_clients/api_client.py @@ -464,6 +464,7 @@ class AirTimeApiClient(ApiClientInterface): req = urllib2.Request(url) response = urllib2.urlopen(req).read() + response = json.loads(response) except Exception, e: response = None logger.error("Exception: %s", e) @@ -480,7 +481,7 @@ class AirTimeApiClient(ApiClientInterface): req = urllib2.Request(url) response = urllib2.urlopen(req).read() - #response = json.loads(response) + response = json.loads(response) except Exception, e: response = None logger.error("Exception: %s", e) @@ -497,7 +498,7 @@ class AirTimeApiClient(ApiClientInterface): req = urllib2.Request(url) response = urllib2.urlopen(req).read() - #response = json.loads(response) + response = json.loads(response) except Exception, e: response = None logger.error("Exception: %s", e)