def test_user_model(self): from motionman.models import User user_orig = User() user_orig.id = 888 user_orig.email = u"*****@*****.**" user_orig.name = u"test1" user_orig.vote_weight = 1 user_orig.created_time = datetime.datetime.now() user_orig.created_user = 3 user_orig.last_modified_time = datetime.datetime.now() user_orig.last_modified_user = 3 user_orig.status = 2 user_new = copy.deepcopy(user_orig) session = DBSession() session.add(user_new) session.flush() transaction.commit() user_load = session.query(User).filter(User.id==888).first() self.assertEqual(user_load.email, user_orig.email) self.assertEqual(user_load.name, user_orig.name) self.assertEqual(user_load.vote_weight, user_orig.vote_weight) self.assertEqual(user_load.created_time, user_orig.created_time) self.assertEqual(user_load.created_user, user_orig.created_user) self.assertEqual(user_load.last_modified_time, user_orig.last_modified_time) self.assertEqual(user_load.last_modified_user, user_orig.last_modified_user) self.assertEqual(user_load.status, user_orig.status) session.delete(user_load) session.flush() transaction.commit()
def test_setting_model(self): from motionman.models import Setting setting_orig = Setting() setting_orig.id = 888 setting_orig.name = u"setting1" setting_orig.value = u"setting value" setting_orig.valuex = u"long setting value" setting_orig.created_time = datetime.datetime.now() setting_orig.created_user = 3 setting_orig.last_modified_time = datetime.datetime.now() setting_orig.last_modified_user = 3 setting_orig.status = 2 setting_new = copy.deepcopy(setting_orig) session = DBSession() session.add(setting_new) session.flush() transaction.commit() setting_load = session.query(Setting).filter(Setting.id==888).first() self.assertEqual(setting_load.name, setting_orig.name) self.assertEqual(setting_load.value, setting_orig.value) self.assertEqual(setting_load.valuex, setting_orig.valuex) self.assertEqual(setting_load.created_time, setting_orig.created_time) self.assertEqual(setting_load.created_user, setting_orig.created_user) self.assertEqual(setting_load.last_modified_time, setting_orig.last_modified_time) self.assertEqual(setting_load.last_modified_user, setting_orig.last_modified_user) self.assertEqual(setting_load.status, setting_orig.status) session.delete(setting_load) session.flush() transaction.commit()
def test_update_user(self): from motionman.services.user import update_user #prepare data session = DBSession() user_1 = copy.deepcopy(self.user_orig) try: session.add(user_1) session.flush() except IntegrityError: transaction.abort() update_user(user_1.id, u'newname', u'newemail', 888) user_load = session.query(User).filter(User.id==user_1.id).first() self.assertEqual(user_load.name, u'newname') self.assertEqual(user_load.email, u'newemail') self.assertEqual(user_load.vote_weight, 888) self.assertEqual(user_load.status, 1) session.delete(user_load) session.flush() transaction.commit()
def test_load_user(self): from motionman.services.user import load_user session = DBSession() user_1 = copy.deepcopy(self.user_orig) user_1.email = u"testemail" user_1.name = u"testname" user_1.vote_weight = 88 try: session.add(user_1) session.flush() except IntegrityError: transaction.abort() user_load = load_user(user_1.id) self.assertEqual(user_load.name, u'testname') self.assertEqual(user_load.email, u'testemail') self.assertEqual(user_load.vote_weight, 88) self.assertEqual(user_load.status, 1) session.delete(user_load) session.flush() transaction.commit()
def get_config_by_name(name): session = DBSession() setting = session.query(Setting).filter(Setting.name==name).first() if setting: return (setting.value, setting.valuex) else: log.error(u"Can't %s from configuration" % name) raise MissConfiguration()
def get_config_by_name(name): session = DBSession() setting = session.query(Setting).filter(Setting.name == name).first() if setting: return (setting.value, setting.valuex) else: log.error(u"Can't %s from configuration" % name) raise MissConfiguration()
def update_setting(name, value, valuex=None): session = DBSession() session.query(Setting).filter(Setting.name == name).update({ Setting.value: value, Setting.valuex: valuex })
def test_get_motion_id_prefix(self): from motionman.services.setting import get_motion_id_prefix, update_setting session = DBSession() setting_load = session.query(Setting).filter( Setting.name == u'motion_id_prefix').first() update_setting(setting_load.id, u"newtestofidprefix") self.assertEqual(get_motion_id_prefix(), u"newtestofidprefix")
def test_get_user_password(self): from motionman.services.setting import get_user_password, update_setting session = DBSession() setting_load = session.query(Setting).filter( Setting.name == u'user_password').first() update_setting(setting_load.id, u"newtestofuserpassword") self.assertEqual(get_user_password(), u"newtestofuserpassword")
def test_get_mail_template(self): from motionman.services.setting import get_mail_template, update_setting session = DBSession() setting_load = session.query(Setting).filter( Setting.name == u'mail_template').first() update_setting(setting_load.id, u"", u"newtestofmailtemplate") self.assertEqual(get_mail_template(), u"newtestofmailtemplate")
def add_message(motion_id, message): session = DBSession() mess = MotionMessage() mess.motion_id = motion_id mess.message = message session.add(mess) session.flush()
def update_user(id, name, email, vote_weight): session = DBSession() session.query(User).filter(User.id == id).update({ User.name: name, User.email: email, User.vote_weight: vote_weight })
def load_motions(offset, limit): session = DBSession() motion_list = session.query(Motion).filter(Motion.status<>0).order_by(desc(Motion.create_time)) if offset: motion_list = motion_list.offset(offset) if limit: motion_list = motion_list.limit(limit) return motion_list.all()
def load_motions(offset, limit): session = DBSession() motion_list = session.query(Motion).filter(Motion.status <> 0).order_by( desc(Motion.create_time)) if offset: motion_list = motion_list.offset(offset) if limit: motion_list = motion_list.limit(limit) return motion_list.all()
def test_get_value_by_name(self): from motionman.services.setting import get_value_by_name, update_setting session = DBSession() setting_load = session.query(Setting).filter( Setting.name == u'motion_id_prefix').first() update_setting(setting_load.id, u"newtestofidprefix", u"newtestvaluexofidprefix") self.assertEqual( get_value_by_name(u'motion_id_prefix')[0], u"newtestofidprefix") self.assertEqual( get_value_by_name(u'motion_id_prefix')[1], u"newtestvaluexofidprefix") setting_load = get_value_by_name(u'srwerslf3sfsofdfs') self.assertEqual(setting_load, None)
def test_get_config_by_name(self): from motionman.services.setting import get_config_by_name, update_setting, MissConfiguration session = DBSession() setting_load = session.query(Setting).filter( Setting.name == u'motion_id_prefix').first() update_setting(setting_load.id, u"newtestofidprefix", u"newtestvaluexofidprefix") self.assertEqual( get_config_by_name(u'motion_id_prefix')[0], u"newtestofidprefix") self.assertEqual( get_config_by_name(u'motion_id_prefix')[1], u"newtestvaluexofidprefix") self.assertRaises(MissConfiguration, get_config_by_name, u"srwerslf3sfsofdfs")
def save_vote_result(motion_id, option_id, chips): session = DBSession() vote_weight = len(chips.split('_')) transaction.begin() try: session.query(MotionParticipant).filter(and_(MotionParticipant.motion_id==motion_id, MotionParticipant.chip_list==chips)) \ .update({MotionParticipant.status:2}) session.query(MotionOption).filter(and_(MotionOption.motion_id==motion_id, MotionOption.id==option_id)) \ .update({MotionOption.result:MotionOption.result+vote_weight}) session.flush() transaction.commit() except Exception: transaction.abort()
def test_update_setting(self): from motionman.services.setting import update_setting session = DBSession() setting_1 = copy.deepcopy(self.setting_orig) try: session.add(setting_1) session.flush() except IntegrityError: transaction.abort() session.refresh(setting_1) update_setting(setting_1.id, u"new value", u"new valuex") setting_load = session.query(Setting).filter( Setting.id == setting_1.id).first() self.assertEqual(setting_load.value, u"new value") self.assertEqual(setting_load.valuex, u"new valuex")
def test_delete_user(self): from motionman.services.user import delete_user from sqlalchemy import and_ #prepare data session = DBSession() user_1 = copy.deepcopy(self.user_orig) try: session.add(user_1) session.flush() except IntegrityError: transaction.abort() delete_user(user_1.id) user_load = session.query(User).filter( and_(User.id == user_1.id, User.status == 1)).first() self.assertEqual(user_load, None) user_load = session.query(User).filter(User.id == user_1.id).first() self.assertEqual(user_load.status, 0)
def test_add_user(self): from motionman.services.user import add_user, DuplicatedName, DuplicatedEmail add_user(u"testname", u"testemail", 88) session = DBSession() user_load = session.query(User).filter( User.name == u'testname').first() self.assertEqual(user_load.name, u'testname') self.assertEqual(user_load.email, u'testemail') self.assertEqual(user_load.vote_weight, 88) self.assertEqual(user_load.status, 1) self.assertRaises(DuplicatedName, add_user, u"testname", u"different email", 88) self.assertRaises(DuplicatedEmail, add_user, u"different name", u"testemail", 88) session.delete(user_load) session.flush() transaction.commit()
def test_add_user(self): from motionman.services.user import add_user, DuplicatedName, DuplicatedEmail add_user(u"testname", u"testemail", 88) session = DBSession() user_load = session.query(User).filter(User.name==u'testname').first() self.assertEqual(user_load.name, u'testname') self.assertEqual(user_load.email, u'testemail') self.assertEqual(user_load.vote_weight, 88) self.assertEqual(user_load.status, 1) self.assertRaises(DuplicatedName, add_user, u"testname", u"different email", 88) self.assertRaises(DuplicatedEmail, add_user, u"different name", u"testemail", 88) session.delete(user_load) session.flush() transaction.commit()
def test_delete_user(self): from motionman.services.user import delete_user from sqlalchemy import and_ #prepare data session = DBSession() user_1 = copy.deepcopy(self.user_orig) try: session.add(user_1) session.flush() except IntegrityError: transaction.abort() delete_user(user_1.id) user_load = session.query(User).filter(and_(User.id==user_1.id, User.status==1)).first() self.assertEqual(user_load, None) user_load = session.query(User).filter(User.id==user_1.id).first() self.assertEqual(user_load.status, 0)
def add_user(name, email, vote_weight): session = DBSession() dup_check = session.query(User).filter( and_(User.status == 1, or_(User.name == name, User.email == email))).all() if len(dup_check) != 0: for i in dup_check: if i.name == name: raise DuplicatedName() if i.email == email: raise DuplicatedEmail() user = User() user.email = email user.name = name user.vote_weight = vote_weight try: session.add(user) session.flush() transaction.commit() except IntegrityError: transaction.abort()
def add_user(name, email, vote_weight): session = DBSession() dup_check = session.query(User).filter(and_(User.status==1,or_(User.name==name, User.email==email))).all() if len(dup_check) != 0: for i in dup_check: if i.name == name: raise DuplicatedName() if i.email == email: raise DuplicatedEmail() user = User() user.email = email user.name = name user.vote_weight = vote_weight try: session.add(user) session.flush() transaction.commit() except IntegrityError: transaction.abort()
def load_user(id): session = DBSession() return session.query(User).filter(User.id==id).first()
def load_user(id): session = DBSession() return session.query(User).filter(User.id == id).first()
def load_users(): session = DBSession() return session.query(User).filter(User.status == 1).all()
def load_motion(motion_id): session = DBSession() return session.query(Motion).filter(Motion.id==motion_id).first()
def add_motion(title, desc, options, users): session = DBSession() all_users = session.query(User).filter(User.status==1).all() transaction.begin() try: motion = Motion() motion.title = title motion.desc = desc session.add(motion) session.flush() for opt in options: option = MotionOption() option.motion_id = motion.id option.value = opt option.result = 0 session.add(option) for user in all_users: if user.id in users: participant = MotionParticipant() participant.motion_id = motion.id participant.user_id = user.id chips = '_'.join([''.join(random.sample(CHIP_PATTERN, 5)) for _ in range(user.vote_weight)]) participant.chip_list = chips session.add(participant) session.flush() transaction.commit() except Exception: transaction.abort()
def get_motion_participant_by_chips(motion_id, chips): session = DBSession() return session.query(MotionParticipant).filter( and_(MotionParticipant.motion_id == motion_id, MotionParticipant.chip_list == chips)).first()
def load_motion(motion_id): session = DBSession() return session.query(Motion).filter(Motion.id == motion_id).first()
def get_value_by_name(name): session = DBSession() setting = session.query(Setting).filter(Setting.name == name).first() if setting: return (setting.value, setting.valuex)
def test_load_users(self): from motionman.services.user import load_users #prepare data session = DBSession() user_1 = copy.deepcopy(self.user_orig) try: session.add(user_1) session.flush() transaction.commit() except IntegrityError: transaction.abort() user_2 = copy.deepcopy(self.user_orig) user_2.name = u"test2" try: session.add(user_2) session.flush() transaction.commit() except IntegrityError: transaction.abort() user_3 = copy.deepcopy(self.user_orig) user_3.name = u"test3" user_3.status = 0 try: session.add(user_3) session.flush() transaction.commit() except IntegrityError: transaction.abort() users = load_users() user1_found = False user2_found = False user3_found = False for user in users: if user.name == u'test1': user1_found = True if user.name == u'test2': user2_found = True if user.name == u'test3': user3_found = True self.assertTrue(user1_found) self.assertTrue(user2_found) self.assertFalse(user3_found)
def update_setting(name, value, valuex = None): session = DBSession() session.query(Setting).filter(Setting.name==name).update({Setting.value:value,Setting.valuex:valuex})
def test_user_model(self): from motionman.models import User user_orig = User() user_orig.id = 888 user_orig.email = u"*****@*****.**" user_orig.name = u"test1" user_orig.vote_weight = 1 user_orig.created_time = datetime.datetime.now() user_orig.created_user = 3 user_orig.last_modified_time = datetime.datetime.now() user_orig.last_modified_user = 3 user_orig.status = 2 user_new = copy.deepcopy(user_orig) session = DBSession() session.add(user_new) session.flush() transaction.commit() user_load = session.query(User).filter(User.id == 888).first() self.assertEqual(user_load.email, user_orig.email) self.assertEqual(user_load.name, user_orig.name) self.assertEqual(user_load.vote_weight, user_orig.vote_weight) self.assertEqual(user_load.created_time, user_orig.created_time) self.assertEqual(user_load.created_user, user_orig.created_user) self.assertEqual(user_load.last_modified_time, user_orig.last_modified_time) self.assertEqual(user_load.last_modified_user, user_orig.last_modified_user) self.assertEqual(user_load.status, user_orig.status) session.delete(user_load) session.flush() transaction.commit()
def delete_user(id): session = DBSession() session.query(User).filter(User.id == id).update({User.status: 0})
def get_motions_count(): session = DBSession() return session.query(Motion).filter(Motion.status<>0).count()
def load_users(): session = DBSession() return session.query(User).filter(User.status==1).all()
def get_motion_participant_by_chips(motion_id, chips): session = DBSession() return session.query(MotionParticipant).filter(and_(MotionParticipant.motion_id==motion_id, MotionParticipant.chip_list==chips)).first()
def get_motions_count(): session = DBSession() return session.query(Motion).filter(Motion.status <> 0).count()
def delete_user(id): session = DBSession() session.query(User).filter(User.id==id).update({User.status:0})
def add_motion(title, desc, options, users): session = DBSession() all_users = session.query(User).filter(User.status == 1).all() transaction.begin() try: motion = Motion() motion.title = title motion.desc = desc session.add(motion) session.flush() for opt in options: option = MotionOption() option.motion_id = motion.id option.value = opt option.result = 0 session.add(option) for user in all_users: if user.id in users: participant = MotionParticipant() participant.motion_id = motion.id participant.user_id = user.id chips = '_'.join([ ''.join(random.sample(CHIP_PATTERN, 5)) for _ in range(user.vote_weight) ]) participant.chip_list = chips session.add(participant) session.flush() transaction.commit() except Exception: transaction.abort()
def update_user(id, name, email, vote_weight): session = DBSession() session.query(User).filter(User.id==id).update({User.name:name, User.email:email, User.vote_weight:vote_weight})
def test_load_settings(self): from motionman.services.setting import load_settings #prepare data session = DBSession() setting_1 = copy.deepcopy(self.setting_orig) try: session.add(setting_1) session.flush() transaction.commit() except IntegrityError: transaction.abort() setting_2 = copy.deepcopy(self.setting_orig) setting_2.name = u"test2" try: session.add(setting_2) session.flush() transaction.commit() except IntegrityError: transaction.abort() setting_3 = copy.deepcopy(self.setting_orig) setting_3.name = u"test3" setting_3.status = 0 try: session.add(setting_3) session.flush() transaction.commit() except IntegrityError: transaction.abort() settings = load_settings() test1_found = False test2_found = False test3_found = False for setting in settings: if setting.name == u'test1': test1_found = True if setting.name == u'test2': test2_found = True if setting.name == u'test3': test3_found = True self.assertTrue(test1_found) self.assertTrue(test2_found) self.assertFalse(test3_found)
def test_setting_model(self): from motionman.models import Setting setting_orig = Setting() setting_orig.id = 888 setting_orig.name = u"setting1" setting_orig.value = u"setting value" setting_orig.valuex = u"long setting value" setting_orig.created_time = datetime.datetime.now() setting_orig.created_user = 3 setting_orig.last_modified_time = datetime.datetime.now() setting_orig.last_modified_user = 3 setting_orig.status = 2 setting_new = copy.deepcopy(setting_orig) session = DBSession() session.add(setting_new) session.flush() transaction.commit() setting_load = session.query(Setting).filter(Setting.id == 888).first() self.assertEqual(setting_load.name, setting_orig.name) self.assertEqual(setting_load.value, setting_orig.value) self.assertEqual(setting_load.valuex, setting_orig.valuex) self.assertEqual(setting_load.created_time, setting_orig.created_time) self.assertEqual(setting_load.created_user, setting_orig.created_user) self.assertEqual(setting_load.last_modified_time, setting_orig.last_modified_time) self.assertEqual(setting_load.last_modified_user, setting_orig.last_modified_user) self.assertEqual(setting_load.status, setting_orig.status) session.delete(setting_load) session.flush() transaction.commit()
def test_update_user(self): from motionman.services.user import update_user #prepare data session = DBSession() user_1 = copy.deepcopy(self.user_orig) try: session.add(user_1) session.flush() except IntegrityError: transaction.abort() update_user(user_1.id, u'newname', u'newemail', 888) user_load = session.query(User).filter(User.id == user_1.id).first() self.assertEqual(user_load.name, u'newname') self.assertEqual(user_load.email, u'newemail') self.assertEqual(user_load.vote_weight, 888) self.assertEqual(user_load.status, 1) session.delete(user_load) session.flush() transaction.commit()
def get_value_by_name(name): session = DBSession() setting = session.query(Setting).filter(Setting.name==name).first() if setting: return (setting.value, setting.valuex)
def init_data(): from motionman.models import Setting session = DBSession() setting_orig = Setting() setting_orig.id = 1 setting_orig.name = u"motion_id_prefix" setting_orig.value = u"mm" setting_orig.valuex = u"" setting_orig.creator = 0 setting_orig.create_time = datetime.datetime.now() setting_orig.last_modifier = 0 setting_orig.last_modify_time = datetime.datetime.now() setting_orig.status = 1 setting_1 = copy.deepcopy(setting_orig) try: session.add(setting_1) session.flush() transaction.commit() except IntegrityError: transaction.abort() setting_2 = copy.deepcopy(setting_orig) setting_2.id = 2 setting_2.name = u"admin_password" setting_2.value = u"123456" try: session.add(setting_2) session.flush() transaction.commit() except IntegrityError: transaction.abort() setting_3 = copy.deepcopy(setting_orig) setting_3.id = 3 setting_3.name = u"user_password" setting_3.value = u"123456" try: session.add(setting_3) session.flush() transaction.commit() except IntegrityError: transaction.abort() setting_4 = copy.deepcopy(setting_orig) setting_4.id = 4 setting_4.name = u"mail_sender" setting_4.value = u"*****@*****.**" try: session.add(setting_4) session.flush() transaction.commit() except IntegrityError: transaction.abort() setting_5 = copy.deepcopy(setting_orig) setting_5.id = 5 setting_5.name = u"mail_smtppassword" setting_5.value = u"123456" try: session.add(setting_5) session.flush() transaction.commit() except IntegrityError: transaction.abort() setting_6 = copy.deepcopy(setting_orig) setting_6.id = 6 setting_6.name = u"mail_template" setting_6.valuex = u"123456" try: session.add(setting_6) session.flush() transaction.commit() except IntegrityError: transaction.abort()
def load_settings(): session = DBSession() return session.query(Setting).filter(Setting.status==1).all()