def list_users(): try: access_token = request.args.get('accessToken') helper = DatabaseHelper() return helper.list_users(access_token) except Exception as e: return "API Error\n" + e.message
def delete_project(id): try: access_token = request.args.get('accessToken') helper = DatabaseHelper() return helper.delete_project(id, access_token) except Exception as e: return "API Error\n" + e.message
def login(): try: email = request.data.get("email") password = request.data.get("password") helper = DatabaseHelper() return helper.login(email, password) except Exception as e: return "API Error\n" + e.message
def register(): try: email = request.data.get("email") password = request.data.get("password") firstname = request.data.get("firstname") lastname = request.data.get("lastname") helper = DatabaseHelper() return helper.register(email, password, firstname, lastname) except Exception as e: return "API Error\n" + e.message
def create_project(): try: owner_id = request.data.get("owner_id") name = request.data.get("name") description = request.data.get("description") due_date = request.data.get("due_date") helper = DatabaseHelper() return helper.create_project(owner_id, name, description, due_date) except Exception as e: return "API Error\n" + e.message
def test_saveMember(self): self.helper = DatabaseHelper() self.config = self.helper.getConfig() self.helper.start_tests() self.helper.setup_clean_db() self.helper.execute_instruction(""" insert into projects (projectname, startdate, enddate, description, currency) values ('test', '2012-06-04', '2013-07-03', 'a simple test', 'GBP')""") self.helper.execute_instruction(""" insert into projects (projectname, startdate, enddate, description, currency) values ('test 2', '2012-06-04', '2013-07-03', 'another simple test', 'GBP')""") with session_scope() as session: self.assertEqual(session.query(Household).count(), 0) house1 = Household(hhid=40, householdname='Test', pid=2, dateofcollection=datetime.date(2012,6,4)) session.add(house1) add_house_hold_member_logic = AddHouseHoldMemberLogic(40, 2) assert add_house_hold_member_logic.saveMember('Male', "10", "2004", False, "", "", "") r = AddHouseHoldMemberLogic(40, 2, 'm10') m = r.getExistingMember() self.assertEqual(m.headofhousehold, 'No') self.assertEqual(m.sex, 'Male') assert r.saveMember('Female', "12", "2002", False, "", "", "") m2 = r.getExistingMember() self.assertEqual(m2.personid, 'f12') self.assertEqual(m2.headofhousehold, 'No') self.helper.end_tests()
def setUp(self): self.helper = DatabaseHelper() self.config = self.helper.getConfig() self.helper.start_tests() self.helper.setup_clean_db() self.helper.execute_instruction(""" insert into projects (projectname, startdate, enddate, description, currency) values ('test', '2012-06-04', '2013-07-03', 'a simple test', 'GBP')""") self.helper.execute_instruction(""" insert into projects (projectname, startdate, enddate, description, currency) values ('test 2', '2012-06-04', '2013-07-03', 'another simple test', 'GBP')""") with session_scope() as session: self.assertEqual(session.query(Household).count(), 0) q = household.search(session, 1, '', '') self.assertEqual(q.count(), 0) house1 = Household(hhid=40, householdname='Test', pid=2, dateofcollection=datetime.date(2012,6,4)) house2 = Household(hhid=55, householdname='A Test 2', pid=2, dateofcollection=datetime.date(2012,6,4)) house3 = Household(hhid=42, householdname='Test 3', pid=3, dateofcollection=datetime.date(2012,6,4)) session.add(house1) session.add(house2) session.add(house3) household.addMember(session, house2, 'm4', '2010', 'No', 'Male', '', '', '', '') c = Householdcharacteristic(characteristic='Test', charvalue='a', hhid=42, pid=3) session.add(c)
def __init__(self, config): self.config = config self.cm = CameraManager() self.builder = gtk.Builder() self.builder.add_from_file('win_main.xml') self.dbh = DatabaseHelper() self._init_controls() self._connect_events()
class TestSQLAlchemy(unittest.TestCase): """ In order to use these tests you need to have a config file named test_openihm.cfg in the directory you are running the tests from (i.e. the src directory). [database] superuser = root superuser_password = s00pers3cur3 database = test_openihm user = openihm password = ihm2012 This should contain database credentials for a database that exists in mysql for testing. This database will be toyed around with a lot. Obviously avoid pointing it at a database you care about... """ def setUp(self): self.helper = DatabaseHelper() self.config = self.helper.getConfig() self.helper.start_tests() def tearDown(self): # drop the database self.helper.end_tests() def test_connection_string(self): self.helper.setup_clean_db() cs = self.helper.real_config.sqlalchemy_connection_string() engine = create_engine(cs, echo=True) Session = sessionmaker(bind=engine) session = Session() self.assertEqual(session.query(SetupFoodsCrops).count(), 0) crop = SetupFoodsCrops('test', 'category', 10, 'kg') session.add(crop) self.assertEqual(session.query(SetupFoodsCrops).count(), 1) session.commit() def test_alchemy_wrapper(self): self.helper.setup_clean_db() with session_scope() as session: self.assertEqual(session.query(SetupFoodsCrops).count(), 0)
def __init__(self): self.ah_obj = AtlasHelper() self.awshelper_obj = aws_helper.AwsHelper() self.module = "chef_module" self.db_obj = DatabaseHelper() self.environment_groups = self.ah_obj.get_atlas_config_data("global_config_data", "environment_groups") self.memcache_var = memcache.Client([self.ah_obj.get_atlas_config_data("global_config_data", 'memcache_server_location') ], debug=0) self.environment_subnets_details = self.awshelper_obj.get_environment_subnets_details() try: base_path = self.ah_obj.get_atlas_config_data("chef_module", 'chef-base-path') self.api = chef.autoconfigure(base_path) except Exception as exp_object: exc_type, exc_obj, exc_tb = sys.exc_info() self.ah_obj.print_exception("chef_helper.py", "__init__()", exp_object, exc_type, exc_obj, exc_tb)
class MainWindow: controls = [ 'win_main', 'btn_config_events', 'btn_recognize_face', 'lst_events', 'cmb_cameras', 'btn_view_camera', 'txt_port', 'btn_start_server', 'lst_cameras', 'tr_vw_col_date', 'tr_vw_col_event', 'tr_vw_events' ] def __init__(self, config): self.config = config self.cm = CameraManager() self.builder = gtk.Builder() self.builder.add_from_file('win_main.xml') self.dbh = DatabaseHelper() self._init_controls() self._connect_events() def _init_controls(self): for c in self.controls: self.__dict__[c] = self.builder.get_object(c) cell = gtk.CellRendererText() self.cmb_cameras.pack_start(cell, True) self.cmb_cameras.add_attribute(cell, "text", 0) self._fill_cameras() cell = gtk.CellRendererText() self.tr_vw_col_date.pack_start(cell, True) self.tr_vw_col_date.add_attribute(cell, 'text', 0) cell = gtk.CellRendererText() self.tr_vw_col_event.pack_start(cell, True) self.tr_vw_col_event.add_attribute(cell, 'text', 1) self._fill_events() def _fill_cameras(self): self.cmb_cameras.get_model().clear() for cam in self.cm.get_cameras(): self.cmb_cameras.append_text(cam.device_path) self.cmb_cameras.set_active(0) def _connect_events(self): self.win_main.connect('delete_event', self._win_main_delete_event) self.btn_view_camera.connect('clicked', self._view_camera) self.btn_start_server.connect('clicked', self._start_server) def _fill_events(self, *args): self.lst_events.clear() for a in self.dbh.get_activities(): row = time.strftime('%d/%m/%Y', time.localtime(a[1])), a[2] self.lst_events.append(row) def _start_server(self, *args): port = self.txt_port.get_text() print 'Servidor iniciado en puerto', port proc = Process(target = MainWindow._start_monitor_server, args = (port,)) proc.start() def _win_main_delete_event(self, *args): gtk.main_quit() def _view_camera(self, *args): device = self.cmb_cameras.get_active_text() proc = Process(target = MainWindow._start_camera_monitor, args = (device,) ) proc.start() @staticmethod def _start_monitor_server(port): os.popen(' '.join(('python', 'monitor_server.py', '-p', port))) @staticmethod def _start_camera_monitor(device): os.popen(' '.join(('python', 'camera_manager.py', '-d', device, '-f'))) def main(self): self.win_main.show_all() gtk.main()
import paho.mqtt.client as mqtt #import the client1 from database_helper import DatabaseHelper import paho.mqtt.client as mqtt import re username = "******" password = "******" database_helper = DatabaseHelper("employeerecords.db") def start_record(args): return database_helper.start_record(*args) def stop_record(args): return database_helper.stop_record() def delete_employee(args): return database_helper.delete_employee(args) def show_list(args): return database_helper.show_list(*args) def update_employee(args): return database_helper.update_employee(*args)
from flask import Flask from database_helper import DatabaseHelper app = Flask(__name__) db_instance = DatabaseHelper() @app.route('/api/tasks', methods=["GET"]) def get(): db_instance.get_a_todo() @app.route('/api/tasks/bystatus/<status>', methods=["GET"]) def get_by_status(status): db_instance.get_a_todo_by_status(status) @app.route('/api/tasks/byid/<id>', methods=["GET"]) def get_a_todo_by_id(id): db_instance.get_a_todo_by_id(id) @app.route('/api/tasks/delete/<id>', methods=["DELETE"]) def delete(id): db_instance.delete_a_to_do(id) @app.route('/api/tasks/post', methods=["POST"]) def post(): db_instance.add_a_todo()
class TestModelHouseHold(unittest.TestCase): """ In order to use these tests you need to have a config file named test_openihm.cfg in the directory you are running the tests from (i.e. the src directory). [database] superuser = root superuser_password = s00pers3cur3 database = test_openihm user = openihm password = ihm2012 This should contain database credentials for a database that exists in mysql for testing. This database will be toyed around with a lot. Obviously avoid pointing it at a database you care about... """ def setUp(self): self.helper = DatabaseHelper() self.config = self.helper.getConfig() self.helper.start_tests() self.helper.setup_clean_db() self.helper.execute_instruction(""" insert into projects (projectname, startdate, enddate, description, currency) values ('test', '2012-06-04', '2013-07-03', 'a simple test', 'GBP')""") self.helper.execute_instruction(""" insert into projects (projectname, startdate, enddate, description, currency) values ('test 2', '2012-06-04', '2013-07-03', 'another simple test', 'GBP')""") with session_scope() as session: self.assertEqual(session.query(Household).count(), 0) q = household.search(session, 1, '', '') self.assertEqual(q.count(), 0) house1 = Household(hhid=40, householdname='Test', pid=2, dateofcollection=datetime.date(2012,6,4)) house2 = Household(hhid=55, householdname='A Test 2', pid=2, dateofcollection=datetime.date(2012,6,4)) house3 = Household(hhid=42, householdname='Test 3', pid=3, dateofcollection=datetime.date(2012,6,4)) session.add(house1) session.add(house2) session.add(house3) c = Householdcharacteristic(characteristic='Test', charvalue='a', hhid=42, pid=3) session.add(c) def tearDown(self): # drop the database self.helper.end_tests() def test_household_search(self): # a) no effective paramaters with session_scope() as session: q = household.search(session, 1, '', '') self.assertEqual(q.count(), 0) q = household.search(session, 2, '', '') self.assertEqual(q.count(), 2) q = household.search(session, 3, '', '') self.assertEqual(q.count(), 1) # g) ensure different projects don't clash - gets tested implicitly def test_number(self): # b) just number with session_scope() as session: q = household.search(session, 2, name='', number='55') self.assertEqual(q.count(), 1) q = household.search(session, 2, '', '33') self.assertEqual(q.count(), 0) def test_name(self): # c) just name with session_scope() as session: q = household.search(session, 3, 'Test 3', '') self.assertEqual(q.count(), 1) def test_both(self): # d) both with session_scope() as session: q = household.search(session, 2, 'Test', '40') self.assertEqual(q.count(), 2) q2 = household.search(session, 2, 'A Test', '55') self.assertEqual(q2.count(), 1) q2 = household.search(session, 2, 'A Test 2', '40') self.assertEqual(q2.count(), 2) q2 = household.search(session, 2, 'Not', '55') self.assertEqual(q2.count(), 1) def test_like(self): # e) like works with session_scope() as session: q = household.search(session, 2, 'Test', '') self.assertEqual(q.count(), 2) q2 = household.search(session, 2, 'A test', '') self.assertEqual(q2.count(), 1) q2 = household.search(session, 2, 'A Test 2', '') self.assertEqual(q2.count(), 1) q2 = household.search(session, 2, 'Not', '') self.assertEqual(q2.count(), 0) def test_results(self): # f) count and results work as expected. with session_scope() as session: q = household.search(session, 2, '', '') self.assertEqual(q.count(), 2) l = [ (h.hhid, h.householdname) for h in q ] self.assertEqual(l, [(40, 'Test'), (55, 'A Test 2')]) def test_remove(self): with session_scope() as session: household.remove_house(session, 2, [55]) q = household.search(session, 2, 'Test', '') self.assertEqual(q.count(), 1) def test_remove_project_scope(self): # ensure we don't accidentally delete houses from # different projects. with session_scope() as session: household.remove_house(session, 2, [55, 40, 42]) q = household.search(session, 2, '', '') self.assertEqual(q.count(), 0) q = household.search(session, 3, '', '') self.assertEqual(q.count(), 1) def test_add_member(self): with session_scope() as session: house = Household(hhid=40, pid=2) household.addMember(session, house, 'm4', '2010', 'No', 'Male', '', '', '', '') # ensure that it deals with twins okay. household.addMember(session, house, 'm4', '2010', 'No', 'Male', '', '', '', '') # FIXME: ought to deal with removing and adding members okay. def test_eager_loading(self): with session_scope() as session: p = session.query(Project).options(joinedload('houses')).filter(Project.projectname == 'test').one() self.assertEqual(len(p.houses), 2)
# Opciones de la linea de comandos parser = OptionParser() parser.add_option('-d', '--device', dest='device', default=None, help='Ruta de dispositivo correspondiente a una camara web') parser.add_option('-f', '--face', dest='face', default=False, action='store_true', help='Indica si se detectaran imagenes en la camara') options = parser.parse_args()[0] if options.device: manager = CameraManager() camera = manager.get_camera(options.device) dbh = DatabaseHelper() camera.init() keep_running = True def mouse_callback(event, x, y, flags, params): global keep_running if event == highgui.CV_EVENT_LBUTTONDOWN: keep_running = False wname = 'Monitor de Camara[ {0} ]'.format(options.device) highgui.cvNamedWindow(wname, 1) highgui.cvSetMouseCallback(wname, mouse_callback, ())
class TestDatabaseInitialiser(unittest.TestCase): """ In order to use these tests you need to have a config file named test_openihm.cfg in the directory you are running the tests from (i.e. the src directory). [database] superuser = root superuser_password = s00pers3cur3 database = test_openihm user = openihm password = ihm2012 This should contain database credentials for a database that exists in mysql for testing. This database will be toyed around with a lot. Obviously avoid pointing it at a database you care about... """ def setUp(self): self.helper = DatabaseHelper() self.config = self.helper.getConfig() self.helper.start_tests() def tearDown(self): # drop the database self.helper.end_tests() def test___init__(self): database_initialiser = DatabaseInitialiser(self.config) # FIXME: this test is probably bogus # we're really just checking we manage to create the # object okay. expected = "latest update on 2012-05-16" self.assertEqual(expected, database_initialiser.latestupdatestring) def test_createDatabase(self): database_initialiser = DatabaseInitialiser(self.config) self.helper.setup_db_file('openihmdb_mysql.sql') assert database_initialiser.createDatabase() @unittest.expectedFailure def test_cropsExist(self): database_initialiser = DatabaseInitialiser(self.config) self.helper.setup_clean_db() self.helper.setup_db_file('openihmdb_mysql_fix59.sql') # FIXME: push some data into the relevant table. #self._execute_instruction('insert into setup_foods_crops values (%s)') assert database_initialiser.cropsExist() def test_databaseUpToDate(self): database_initialiser = DatabaseInitialiser(self.config) self.helper.setup_clean_db() assert database_initialiser.databaseUpToDate() def test_databaseUpToDate_needupdate(self): database_initialiser = DatabaseInitialiser(self.config) self.helper.setup_clean_db() self.make_db_outofdate() assert not database_initialiser.databaseUpToDate() def test_databaseUpToDate_fresh_db(self): database_initialiser = DatabaseInitialiser(self.config) self.helper.setup_clean_db() self.helper.execute_instruction('delete from dbupdate') assert database_initialiser.databaseUpToDate() def make_db_outofdate(self): updatestr = "latest update on %s" % (date.min.isoformat()) self.helper.execute_instruction( "update dbupdate set lastupdate = %s", [updatestr]) def test_initialiseDB(self): database_initialiser = DatabaseInitialiser(self.config) self.helper.setup_clean_db() self.assertEqual({ 'mysqlstarted': True, 'dbinstalled': True, 'dbuptodate': True}, database_initialiser.initialiseDB()) def test_insertStartupCrops(self): database_initialiser = DatabaseInitialiser(self.config) self.helper.setup_clean_db() self.helper.setup_db_file('openihmdb_mysql_fix59.sql') assert database_initialiser.insertStartupCrops() def test_setupStartupCrops(self): database_initialiser = DatabaseInitialiser(self.config) self.helper.setup_clean_db() self.helper.setup_db_file('openihmdb_mysql_fix59.sql') assert database_initialiser.setupStartupCrops() def test_updateDatabase(self): # NOTE: it might be worth adding a test that compares the new # database to the updated database to check they match. database_initialiser = DatabaseInitialiser(self.config) self.helper.setup_clean_db() # this isn't much of a check... # our coverage isn't very good. assert database_initialiser.updateDatabase() def test_updateDatabase_needs_update(self): database_initialiser = DatabaseInitialiser(self.config) self.helper.setup_clean_db() self.make_db_outofdate() self.helper.setup_db_file('openihmdb_mysql_update.sql') assert database_initialiser.updateDatabase()
def main(): controller = Controller(View(), Model()) controller.start() DatabaseHelper.close()
class TestDatabase(unittest.TestCase): """ In order to use these tests you need to have a config file named test_openihm.cfg in the directory you are running the tests from (i.e. the src directory). [database] superuser = root superuser_password = s00pers3cur3 database = test_openihm user = openihm password = ihm2012 This should contain database credentials for a database that exists in mysql for testing. This database will be toyed around with a lot. Obviously avoid pointing it at a database you care about... """ def setUp(self): self.helper = DatabaseHelper() self.config = self.helper.getConfig() self.helper.start_tests() def tearDown(self): # drop the database self.helper.end_tests() def test___init__(self): database = Database() self.assertEqual(self.config.dbinfo(), database.config) def test_close(self): database = Database() database.open() database.close() @unittest.expectedFailure def test_databaseExists(self): database = Database() assert database.databaseExists() @unittest.expectedFailure def test_databaseServerRunning(self): database = Database() assert database.databaseServerRunning() def test_execDefinitionQuery(self): self.helper.setup_clean_db() database = Database() database.open() database.execDefinitionQuery('create table simples (test int)') database.close() # and just to prove it's there to put something into. database.open() database.execUpdateQuery('insert into simples values (3)') database.close() def test_execSelectQuery(self): self.helper.setup_clean_db() self.helper.execute_instruction(""" insert into projects (projectname, startdate, enddate, description, currency) values ('test', 2012-06-04, 2013-07-03, 'a simple test', 'GBP')""") database = Database() query = 'select * from projects' database.open() self.assertEqual([(2, u'test', None, None, u'a simple test', u'GBP')], database.execSelectQuery(query)) database.close() def test_execUpdateQuery(self): self.helper.setup_clean_db() database = Database() database.open() database.execUpdateQuery(""" insert into projects (projectname, startdate, enddate, description, currency) values ('test', 2012-06-04, 2013-07-03, 'a simple test', 'GBP')""") query = 'select * from projects' # FIXME: the None's look hinky. self.assertEqual([(2, u'test', None, None, u'a simple test', u'GBP')], database.execSelectQuery(query)) database.close() def test_open(self): database = Database() database.open()
def setUp(self): self.helper = DatabaseHelper() self.config = self.helper.getConfig() self.helper.start_tests()
from config_reader import ConfigReader from database_helper import DatabaseHelper from json import loads from flask import Flask, jsonify, request app = Flask('CountriesREST') cr = ConfigReader() host, dbname, user, password = cr.get_database_config() db = DatabaseHelper(host, dbname, user, password) @app.route('/countries', methods=['GET']) def get_countries(): result = db.execute_select('SELECT id, name, continent FROM public.countries') return db.transform_dataset_into_json(result) @app.route('/countries/<id>', methods=['GET']) def get_country(id): script = 'SELECT id, name, continent FROM public.countries WHERE id = {}'.format(id) result = db.execute_select(script) return db.transform_row_into_json(result) @app.route('/countries/<id>', methods=['DELETE']) def delete_country(id): script = 'DELETE FROM public.countries WHERE id = {}'.format(id) db.execute_script(script) message = 'Country {} was deleted!'.format(id) dic = {'message': message} return jsonify(dic)
class DBInterface(object): def __init__(self, model): self.database = DatabaseHelper() self.model = model def __get_filters(self, query, columns, values): if not isinstance(columns, list) and not isinstance(values, list): return query.filter(columns == values) else: assert (isinstance(columns, list) and isinstance(values, list)) assert (columns.__len__() == values.__len__()) for (column, value) in zip(columns, values): query = query.filter(column == value) return query def __get_update_query(self, query, columns, values): if not isinstance(columns, list) and not isinstance(values, list): return query.update({columns: values}) else: assert (isinstance(columns, list) and isinstance(values, list)) assert (columns.__len__() == values.__len__()) index = 0 obj = {} for column in columns: obj[column] = values[index] index += 1 query = query.update(obj) return query def select_all(self): session = self.database.get_session() result = session.query(self.model).all() return result def select(self, columns, values): session = self.database.get_session() query = session.query(self.model) return self.__get_filters(query, columns, values).all() def delete(self, columns, values): session = self.database.get_session() result = self.select(columns, values) assert result if isinstance(result, list): # Many objects for obj in result: try: session.delete(obj) session.commit() return True except NoResultFound: print("Object NOT FOUND") else: # One object try: session.delete(result) session.commit() return True except NoResultFound: session.rollback() print("Object NOT FOUND") def update(self, columnsFind, valuesFind, columnsNew, valuesNew): session = self.database.get_session() query = session.query(self.model) query = self.__get_filters(query, columnsFind, valuesFind) assert query query = self.__get_update_query(query, columnsNew, valuesNew) if not query: print("Object NOT FOUND") else: v = View() v.print_and_getch("PRESS ANY KEY TO COMMIT") session.commit() return True def insert(self, obj): session = self.database.get_session() try: session.add(obj) session.commit() return True except BaseException as exception: session.rollback() print(exception)
def setUp(self): self.helper = DatabaseHelper() self.helper.start_tests()
class TestMySQLMixin(unittest.TestCase): """ In order to use these tests you need to have a config file named test_openihm.cfg in the directory you are running the tests from (i.e. the src directory). [database] superuser = root superuser_password = s00pers3cur3 database = test_openihm user = openihm password = ihm2012 This should contain database credentials for a database that exists in mysql for testing. This database will be toyed around with a lot. Obviously avoid pointing it at a database you care about... """ def setUp(self): self.helper = DatabaseHelper() self.helper.start_tests() def tearDown(self): # drop the database self.helper.end_tests() def test_executeMultipleResultsQueries(self): self.helper.setup_clean_db() mixin = MySQLMixin() queries = [ """ insert into projects (projectname, description, currency) values ('test', 'a simple test', 'GBP')""", """ insert into projects (projectname, description, currency) values ('test2', 'simple test', 'AUS')""", ] mixin.executeMultipleUpdateQueries(queries) exp = [ [(u'test', None, None, u'a simple test', u'GBP')], [(u'test2', None, None, u'simple test', u'AUS')] ] s_queries = [ """ select projectname, startdate, enddate, description, currency from projects limit 1 """, """ select projectname, startdate, enddate, description, currency from projects limit 1 offset 1 """, ] self.assertEqual(exp, mixin.executeMultipleResultsQueries(s_queries)) def test_executeMultipleUpdateQueries(self): self.helper.setup_clean_db() my_sql_mixin = MySQLMixin() queries = [ """ insert into projects (projectname, description, currency) values ('test', 'a simple test', 'GBP')""", """ insert into projects (projectname, description, currency) values ('test2', 'simple test', 'AUS')""", ] my_sql_mixin.executeMultipleUpdateQueries(queries) expected = [ (u'test', None, None, u'a simple test', u'GBP'), (u'test2', None, None, u'simple test', u'AUS') ] query = """ select projectname, startdate, enddate, description, currency from projects """ self.assertEqual(expected, my_sql_mixin.executeResultsQuery(query)) def test_executeResultsQuery(self): self.helper.setup_clean_db() my_sql_mixin = MySQLMixin() my_sql_mixin.executeUpdateQuery(""" insert into projects (projectname, description, currency) values ('test', 'a simple test', 'GBP')""") expected = [(2, u'test', None, None, u'a simple test', u'GBP')] query = 'select * from projects' self.assertEqual(expected, my_sql_mixin.executeResultsQuery(query)) def test_executeUpdateQueryWithParams(self): self.helper.setup_clean_db() my_sql_mixin = MySQLMixin() p = ['test3', '2012-06-04', 'description', 'LIR'] my_sql_mixin.executeUpdateQuery(""" insert into projects (projectname, startdate, enddate, description, currency) values (%s, %s, '2013-07-03', %s, %s)""", p) expected = [(2, u'test3', datetime.date(2012, 6, 4), datetime.date(2013, 7, 3), u'description', u'LIR')] query = 'select * from projects' self.assertEqual(expected, my_sql_mixin.executeResultsQuery(query)) def test_executeUpdateQuery(self): self.helper.setup_clean_db() my_sql_mixin = MySQLMixin() my_sql_mixin.executeUpdateQuery(""" insert into projects (projectname, description, currency) values ('test', 'a simple test', 'GBP')""") expected = [(2, u'test', None, None, u'a simple test', u'GBP')] query = 'select * from projects' self.assertEqual(expected, my_sql_mixin.executeResultsQuery(query))
def __init__(self, model): self.database = DatabaseHelper() self.model = model
class ChefHelper: def __init__(self): self.ah_obj = AtlasHelper() self.awshelper_obj = aws_helper.AwsHelper() self.module = "chef_module" self.db_obj = DatabaseHelper() self.environment_groups = self.ah_obj.get_atlas_config_data("global_config_data", "environment_groups") self.memcache_var = memcache.Client([self.ah_obj.get_atlas_config_data("global_config_data", 'memcache_server_location') ], debug=0) self.environment_subnets_details = self.awshelper_obj.get_environment_subnets_details() try: base_path = self.ah_obj.get_atlas_config_data("chef_module", 'chef-base-path') self.api = chef.autoconfigure(base_path) except Exception as exp_object: exc_type, exc_obj, exc_tb = sys.exc_info() self.ah_obj.print_exception("chef_helper.py", "__init__()", exp_object, exc_type, exc_obj, exc_tb) def get_databag_list(self, databag=''): #returns a list of all available data bags on the chef_server data_bag_list = [] try: data_bags = DataBag(databag,self.api) for items in data_bags.list(self.api): data_bag_list.append(items) if not data_bag_list: raise Exception, "No Data bags items found" else: return data_bag_list except Exception as exp_object: exc_type, exc_obj, exc_tb = sys.exc_info() self.ah_obj.print_exception("chef_helper.py", "get_databag_list()", exp_object, exc_type, exc_obj, exc_tb) return [] def cache_databag_attributes_foritem(self, databag, item): """ Fetch databag attributes from chef server using databag name and item name and cache it locally. """ try: databag_attributes_foritem = self.get_databag_attribute_foritem(databag, item) self.memcache_var.set("cpdeployment_databag_attrs", databag_attributes_foritem,600) if databag_attributes_foritem is None: raise Exception("Databag attributes cannot be obtained from Chef server. Please make sure data is obtained and populate the cache !!!") if databag_attributes_foritem is not None: self.memcache_var.set("global_cpdeployment_databag_attributes", databag_attributes_foritem ,86400) self.memcache_var.disconnect_all() except Exception as exp_object: exc_type, exc_obj, exc_tb = sys.exc_info() self.ah_obj.print_exception("chef_helper.py", "cache_databag_attributes_foritem()", exp_object, exc_type, exc_obj, exc_tb) self.memcache_var.disconnect_all() return def get_chefdbag_attributes_foritem(self, databag, item): """ Check in short term cache if not fetch results from global cache """ db_attribute_dict = self.memcache_var.get('cpdeployment_databag_attrs') if db_attribute_dict is None: db_attribute_dict = self.memcache_var.get('global_cpdeployment_databag_attrs') if db_attribute_dict is not None: self.memcache_var.set("cpdeployment_databag_attrs", db_attribute_dict, 600) self.memcache_var.disconnect_all() with threading.Lock(): thread = threading.Thread(target=self.cache_databag_attributes_foritem, args=(databag, item)) thread.start() return db_attribute_dict def get_databag_attribute_foritem(self, databag, item): try: data_bag = DataBag(databag,self.api) data_bag_item = data_bag[item] chef_databags = self.ah_obj.get_atlas_config_data("chef_module", 'chef_databags')[0] chef_databags_info = self.ah_obj.get_atlas_config_data("chef_module", 'chef_databags')[1] key_list = [] for index in chef_databags: if databag in chef_databags_info.keys(): for item_index in chef_databags_info[databag]['items'].keys(): if item == item_index: key_list = chef_databags_info[databag]['items'][item]['keys'] data_bag_attr = {} data_bag_attr.fromkeys(data_bag_item.keys(), None) for d_item_key, d_item_values in data_bag_item.iteritems(): if type(d_item_values)== unicode: if d_item_key in key_list: data_bag_attr[d_item_key] = {} data_bag_attr[d_item_key] = d_item_values break; elif type(d_item_values) == dict: data_bag_attr[d_item_key] = {} for key in key_list: data_bag_attr[d_item_key][key] = self.ah_obj.get_nested_attribute_values(d_item_values, key) return data_bag_attr except Exception as exp_object: exc_type, exc_obj, exc_tb = sys.exc_info() self.ah_obj.print_exception("chef_helper.py", "get_databag_attributes()", exp_object, exc_type, exc_obj, exc_tb) return [] def cache_chef_node_attributes(self): """ Fetch node attributes from chef server and cache it locally. """ try: chef_node_attributes_dict = self.get_node_attrs_from_chef() if chef_node_attributes_dict is None: raise Exception("Chef node attributes not available from the Chef server. Please make sure the data is available and populate the cache.") if chef_node_attributes_dict is not None: self.memcache_var.set("global_chef_node_attributes_cache", chef_node_attributes_dict,86400) self.memcache_var.disconnect_all() self.memcache_var.set("chef_node_attr_caches", chef_node_attributes_dict,2*60*60) self.memcache_var.disconnect_all() except Exception as exp_object: exc_type, exc_obj, exc_tb = sys.exc_info() self.ah_obj.print_exception("chef_helper.py", "cache_chef_node_attributes()", exp_object, exc_type, exc_obj, exc_tb) self.memcache_var.disconnect_all() return def get_node_attributes(self): node_attribute_dict = self.memcache_var.get('chef_node_attr_caches') if node_attribute_dict is None: node_attribute_dict = self.memcache_var.get('global_chef_node_attributes_cache') self.memcache_var.disconnect_all() if node_attribute_dict is not None: self.memcache_var.set('chef_node_attr_caches', node_attribute_dict, 600) with threading.Lock(): thread = threading.Thread(target=self.cache_chef_node_attributes) thread.start() return node_attribute_dict def get_node_attrs_from_chef(self): try: env_subnets_dict = {} node_attribute_dict = {} for organization in self.awshelper_obj.get_organizations(): node_attribute_dict = defaultdict(dict) node_list = Node.list(self.api) for environment in self.awshelper_obj.get_environments(organization): for region in self.awshelper_obj.get_regions(): vpc_list = self.awshelper_obj.get_vpc_in_region(region) if vpc_list: for vpc in self.awshelper_obj.get_vpc_in_region(region): env_subnets_dict = self.awshelper_obj.get_env_subnets(organization, region, vpc) for node in node_list: node_obj = Node(node, api=self.api) node_split = self.ah_obj.split_string(node, ["."]) if node_split is None or len(node_split)<=1: pass else: node_subnet = self.ah_obj.split_string(node, ['.'])[1] for key_tuple, environment in env_subnets_dict.iteritems(): if node_subnet in key_tuple: environment = env_subnets_dict[key_tuple] attribute_list = node_obj.attributes if 'ec2' in attribute_list: if 'instance_id' in node_obj.attributes.get_dotted('ec2'): instance_id = node_obj.attributes.get_dotted('ec2.instance_id') node_attribute_dict[instance_id]['node'] = node if 'os' in attribute_list: node_attribute_dict[instance_id]['os']=node_obj['os'] if 'os_version' in attribute_list: node_attribute_dict[instance_id]['os_version'] = node_obj['os_version'] if 'platform' in attribute_list: node_attribute_dict[instance_id]['platform'] = node_obj['platform'] if 'platform_version' in attribute_list: node_attribute_dict[instance_id]['platform_version'] = node_obj['platform_version'] if 'uptime' in attribute_list: node_attribute_dict[instance_id]['uptime'] = node_obj['uptime'] if 'idletime' in attribute_list: node_attribute_dict[instance_id]['idletime'] = node_obj['idletime'] return dict(node_attribute_dict) except Exception as exp_object: exc_type, exc_obj, exc_tb = sys.exc_info() self.ah_obj.print_exception("chef_helper.py", "get_node_attrs_from_chef1()", exp_object, exc_type, exc_obj, exc_tb) return {} def map_node_databag_attributes(self, node_attributes, databag_attributes, environment): tabs_info_dict = {} for instance_id, details in node_attributes.iteritems(): tabs_info_dict[instance_id] = {'chef_information':{}} tabs_info_dict[instance_id]['chef_information'] = details for node_attr, node_attr_value in details.iteritems(): tabs_info_dict[instance_id]['chef_information'][node_attr]=node_attr_value if databag_attributes.has_key(details['node']): for key, value in databag_attributes.iteritems(): if details['node'] == key: for attribute, attribute_value in value.iteritems(): tabs_info_dict[instance_id]['chef_information'][attribute] = attribute_value return tabs_info_dict def get_values_for_attribute(self, environment, region, vpc, subnet, stack, attribute, details): attribute_value_dict = {} if attribute == "owner": ownership_records = self.db_obj.get_stack_ownership_details(environment, region, vpc, subnet, stack) if ownership_records is not None: attribute_value_dict["owner"] = ownership_records.owner attribute_value_dict["start_time"] = int(ownership_records.start_time.strftime("%s")) * 1000 else: attribute_value_dict["owner"] = "none" if attribute == "dbhost" or attribute == "email_override" or attribute == "branch": attribute_record = self.db_obj.get_stack_attribute_value(environment, region, vpc, subnet, stack, attribute) if attribute_record is not None: attribute_value_dict[attribute] = attribute_record.attribute_value else: custportal_dbag_attrs = self.memcache_var.get('cpdeployment_databag_attrs'); if custportal_dbag_attrs is not None: for keys, values in custportal_dbag_attrs.iteritems(): custportal_dbag_attrs_subnet = keys.split(".")[1] if custportal_dbag_attrs_subnet == subnet and stack in details['stack']: if attribute == "dbhost": if values['AWS_DB_HOST']: attribute_value_dict["dbhost"] = values['AWS_DB_HOST'] else: attribute_value_dict["dbhost"] = "none" if attribute == "email_override": if values['EMAIL_OVERRIDE'] and values['EMAIL_OVERRIDE'] is not None: attribute_value_dict["email_override"] = values['EMAIL_OVERRIDE'] if values['EMAIL_OVERRIDE'] == "none": pass if attribute == "branch": if values["branch"]: attribute_value_dict["branch"] = values['branch'] else: "none" return attribute_value_dict def stack_attribute_values(self, request, environment, region_vpc_dict): """ Get attributes and values for each stack. """ stack_attribute_dict = collections.defaultdict(dict) awsmodule_obj = AwsModule(request, environment) (stack_attr_list, stack_attr_details) = stack_attributes = self.ah_obj.get_atlas_config_data(self.module, 'stack_attributes') application_subnets = awsmodule_obj.get_information(environment, application_subnets='true') apps_in_environment = awsmodule_obj.get_information(environment, apps_in_environment='true') if application_subnets is not None and apps_in_environment is not None: for region, vpc_list in region_vpc_dict.iteritems(): stack_attribute_dict[region] = {} if vpc_list is not None: for vpc in vpc_list: stack_attribute_dict[region][vpc] = {} for subnet in application_subnets: stack_attribute_dict[region][vpc][subnet] = {} for stack in apps_in_environment: stack_attribute_dict[region][vpc][subnet][stack] = {} for attribute in stack_attr_list: details = stack_attr_details[attribute] stack_attribute_dict[region][vpc][subnet][stack].update(self.get_values_for_attribute(environment, region, vpc, subnet, stack, attribute, details)) return dict(stack_attribute_dict) def get_stack_attribute_values(self, request, environment, region_vpc_dict): """ Get stack attribute values for environment or environment groups. """ stack_attribute_dict = {} stack_attribute_dict = self.stack_attribute_values(request, environment, region_vpc_dict) return stack_attribute_dict def get_stack_attributes(self, environment): """ Get stack attributes from config file. """ stack_attribute_list = [] stack_attributes_dict = self.ah_obj.get_atlas_config_data('chef_module', 'stack_attributes')[1] for attribute, details in stack_attributes_dict.iteritems(): stack_attribute_list.append((attribute, details['editable'])) return(stack_attribute_list, stack_attributes_dict)