def all(self): messagers = [] for id, data in self._db.items(): m = Message(data['name'], data['message']) m.id = str(id) messagers.append(m) messagers.sort(key=lambda x: x.id) return messagers
def test_search_message(self): messages = [self.messages[0]] with self.subTest('Message by content'): # Предполагается, что найдет только одно сообщение. Так подогнаны данные в тестах found, total = Message.search(messages[0].content, 1, 100) self.assertEqual(total, 1) with self.subTest('Message not found'): found, total = Message.search('I really dont exist', 1, 100) self.assertEqual(total, 0)
def test_mess(self): test_user = User.query.filter_by(userName='******').first() test_activity = Activity.query.filter_by(AID='998').first() test_ua = UserActivity(user=test_user, applyTime='2017-04-09', content="test ua", activity=test_activity, type='applied') test_mess = Message(user=test_user, activity=test_activity, content="test message", time='2017-04-09', isRead=False) db.session.add_all([test_mess, test_ua]) db.session.commit() mess_info = {"token": test_user.generate_auth_token(expiration=3600 * 24 * 30)} response = current_app.test_client().post( url_for('api.change_message_isread', id=test_mess.id), headers={'Accept': 'application/json', 'Content-Type': 'application/json'}, data=json.dumps(mess_info)) self.assertEqual(200, response.status_code) response = current_app.test_client().post( url_for('api.delete_message', id=test_mess.id), headers={'Accept': 'application/json', 'Content-Type': 'application/json'}, data=json.dumps(mess_info)) self.assertIsNone(Message.query.filter_by(content="test message").first()) self.assertEqual(200, response.status_code) Message.query.filter_by(content="test message").delete() UserActivity.query.filter_by(content="test ua").delete() db.session.commit()
def setUp(self): self.app = create_app('testing') self.app.testing = True self.app_context = self.app.app_context() self.app_context.push() db.create_all() new_user = User(userName='******', email='test@user', phone='123456') new_team = Team(teamName='testTeam', email='test@team', password='******') new_activity = Activity(AID='998', team=new_team, starttime='2017-04-09 15:25', endtime='2017-04-09 15:30', location="testLocation", title="testActivity", content="testContent", managePerson="testManager", managePhone=110, manageEmail='test@activity') new_message = Message(user=new_user, activity=new_activity, content='hao', time='2017-04-09 15:30', isRead=False) db.session.add_all([new_user, new_team, new_activity, new_message]) db.session.commit()
def fill_messages(chats): messages = [ Message(chat=chat, user=chat.users[0], content='Test message') for chat in chats ] db.session.add_all(messages) db.session.commit() return messages
def insert_reply_db(self): message = Message( ToUserName=self.__dict['ToUserName'], FromUserName=self.__dict['FromUserName'], MsgType=self.MsgType, MsgId=self.MsgId, Content= self.__dict['Content'] ) db.session.add(message) db.session.commit()
def insert_text_db(self,xmlData): message = Message( ToUserName = self.ToUserName, FromUserName = self.FromUserName, MsgType = self.MsgType, MsgId = self.MsgId, Content = self.Content ) db.session.add(message) db.session.commit()
def create(self): title = self.get_argument('title', '') content = self.get_argument('content', '') user_list = self.get_argument('id', '') type = self.get_argument('type', 'normal') cur_time = int(time.time()) if not content: self.finish({'code': 1, 'msg': u'content不能为空'}) return message = Message(create=cur_time, title=title, content=content, type=type) self.session.add(message) self.session.flush() data = [] if user_list: user_list = [ '%s' % x.strip() for x in filter(None, user_list.split(',')) ] for user_id in user_list: user_message = UserMessage(message=message.id, update=cur_time, user=user_id, type=type) self.session.add(user_message) self.session.commit() data.append(user_message.dict()) self.finish({'code': 0, 'data': data}) else: q = self.session.query(User.id).all() for i in q: user_message = UserMessage(message=message.id, update=cur_time, user=i[0], type=type) self.session.add(user_message) self.session.commit() data.append(user_message.dict()) self.finish({'code': 0, 'data': data})
def do(self): self.last = self.get_last() obj_list = self.session.query(Warn).filter( Warn.create > self.last).order_by(-Warn.create).all() for warn in obj_list: #先查对应设备的id及人 if not warn.cpu_id: logger.error('no cpu_id, warn id is {}'.format(warn.id)) continue device_id = self.session.query( Device.id).filter_by(cpu_id=warn.cpu_id).scalar() if not device_id: logger.error( 'no device map, warn id is {}, cpu_id is {}'.format( warn.id, warn.cpu_id)) continue user_list = self.session.query( UserDevice.user).filter_by(device=device_id).all() user_list = [x[0] for x in user_list] if not user_list: corp_list = self.session.query( CorpDevice.corp).filter_by(device=device_id) user_list = self.session.query(CorpUser.user).filter( CorpUser.corp.in_(corp_list), CorpUser.role == 'admin').all() user_list = [x[0] for x in user_list] if not user_list: logger.error( 'no user map, warn id is {}, cpu_id is {}'.format( warn.id, warn.cpu_id)) continue user_mobile_map = self.session.query(User.id, User.mobile).filter( User.id.in_(user_list)).all() user_mobile_map = dict(user_mobile_map) user_regist_id = self.session.query(User.rid).filter( User.id.in_(user_list)).all() user_regist_id = [x[0] for x in user_regist_id] user_regist_id = [x for x in user_regist_id if x] message = Message(type=Message.TYPE_WARN, create=warn.create, title=u'报警消息', content=warn.content) push = Push(create=warn.create, title=u'报警消息', device=device_id, content=warn.content, target=','.join(str(x) for x in user_regist_id)) self.session.add(message) self.session.add(push) self.session.flush() logger.info('add push, push id is {}'.format(push.id)) for u in user_list: um = UserMessage(user=u, message=message.id, update=time.time()) self.session.add(um) self.session.flush() logger.info( 'add user message, warn id is {}, cpu_id is {}, message id is {}, user message id is {}' .format(warn.id, warn.cpu_id, message.id, um.id)) mobile = user_mobile_map.get(u, '') if mobile: sms = Sms(create=time.time(), content=warn.content, target=mobile, device=device_id, type='warn') self.session.add(sms) self.session.flush() logger.info( 'add sms, warn id is {}, cpu_id is {}, sms id is {}'. format(warn.id, warn.cpu_id, sms.id)) self.session.commit()