def send_team_admin_message(team: Team, su: User, mu: User): # tid:团队id,su:发起添加管理员的用户,mu:刚被设为管理员的用户 # 我存的数据库原始id,使用msg/info给我发消息时请加密 m = Message() m.owner = mu m.sender = su m.title = su.name + " 将你设为团队管理员:" + team.name m.portrait = team.portrait if team.portrait else '' m.related_id = team.id m.type = 'admin' try: m.save() except: return False return True
def send_team_accept_message(team: Team, su: User, mu: User, if_accept: bool): # tid:团队id,su:发起邀请的用户,mu:处理邀请的用户,if_accept:是否接受邀请 # 我存的数据库原始id,使用msg/info给我发消息时请加密 m = Message() m.owner = su m.sender = mu m.title = mu.name + " 接受" if if_accept else " 拒绝" + "了您的团队邀请:" + team.name m.portrait = team.portrait if team.portrait else '' m.related_id = team.id m.type = 'accept' try: m.save() except: return False return True
def send_team_invite_message(team: Team, su: User, mu: User): # tid:团队id,suid:发起邀请的用户,muid:接收邀请的用户 # 我存的数据库原始id,使用msg/info给我发消息时请加密 m = Message() m.owner = mu m.sender = su m.title = su.name + " 邀请你加入团队:" + team.name m.portrait = team.portrait if team.portrait else '' m.related_id = team.id m.type = 'join' try: m.save() except: return False return True
def send_team_out_message(team: Team, mu: User): # mu: 被踢出的 m = Message() m.owner = mu m.title = "您已被移出团队:" + team.name m.portrait = team.portrait if team.portrait else '' m.related_id = team.id m.type = 'out' try: m.save() except: return False return True
def send_team_dismiss_message(team: Team, mu: User): # mu: 团队解散 m = Message() m.owner = mu m.title = "团队已解散:" + team.name m.portrait = team.portrait if team.portrait else '' m.related_id = team.id m.type = 'dismiss' print(m) try: m.save() except: return False return True
def post(self, request): if request.user.is_authenticated: data = json.loads(request.body.decode('utf-8')) user_2_id = str(data['user_2_id']) database = Database() id_room = database.check_box_chat(request.user.id, user_2_id) # lưu tin nhắn if data['content'] != '': Mess = Message() Mess.from_user = MyUser.objects.get(id=request.user.id) Mess.conversation = Conversation.objects.get(c_id=id_room) Mess.content = data['content'] Mess.save() return HttpResponse('Gửi thành công rồi nhé Lalal') else: return redirect('home:home')
def send_message(recipient): current_user = session['id'] user_recipient = User.query.filter_by(id=recipient).first_or_404() form = MessageForm() if form.validate_on_submit(): print("data:", form.message.data) print("data type:", type(form.message.data)) print("data end") print("user_recipient:", user_recipient) msg = Message(sender_id=current_user, recipient_id=user_recipient.id, body=form.message.data) db.session.add(msg) db.session.commit() notifMsg = "New Message from " + form.physician_name.data newNotif = Notification(user_recipient.id, current_user, notifMsg, "MESSAGE") db.session.add(newNotif) db.session.commit() flash(('Your message has been sent.')) return redirect(url_for('patient_app.patient_home')) return render_template('user/send_message.html', title= ('Send Message'), form=form, recipient=recipient)
def patient_messages(): #if no sent_messages/received messages, it'll be [] sent_messages = db.session.query(Message).filter(Message.sender_id==session["id"])[::-1] #reverse so it's listed in most recent received_messages = db.session.query(Message).filter(Message.recipient_id==session["id"])[::-1] #merge messages by most recent merged_messages = sorted([(m.id, m) for m in sent_messages] + [(m.id, m) for m in received_messages], key=lambda x:x[0], reverse=True) num_messages = len(merged_messages) message_bodies = [] for m_id, message in merged_messages: s = "" r = "" if message.sender_id == session["id"]: s = "You" else: sender = User.query.filter_by(id=message.sender_id).first() s = sender.fname + " " + sender.lname + " ID:"+ str(message.sender_id) if message.recipient_id == session["id"]: r = "You" else: recipient = User.query.filter_by(id=message.recipient_id).first() r = recipient.fname + " " + recipient.lname + " ID:"+ str(message.recipient_id) b = message.body #(sender name, recipient name, id, message body) message_bodies.append([s,r,b]) print(message_bodies) cipher_suite = Fernet(FERNET_KEY.encode('utf-8')) for i in range(len(message_bodies)): b_msg = bytes(message_bodies[i][2], encoding='utf-8') message_bodies[i][2] = cipher_suite.decrypt(b_msg).decode() #deals with sending the message user_physicians = db.session.query(TreatedBy).filter(TreatedBy.patient_id==session["id"]) physician_ids = [i.physician_id for i in user_physicians] physician_names = [] for i in physician_ids: physician = db.session.query(Physician).filter(Physician.physician_id==i) for a in physician: physician_names.append(a.practice_name + " ID:"+ str(i)) #physician_names[0].append(physician.practice_name) form = PatientSendMessageForm() #physician name and message body print("in patient") #physician_id = None if form.validate_on_submit(): physician_id = int(form.name.data.split()[-1][3:]) #e.g. Joptics ID:3 -> 3 #return redirect(url_for('patient_app.send_message', recipient = physician_id)) body_encrypted = cipher_suite.encrypt(bytes(form.message.data, encoding='utf-8')) msg = Message(sender_id=session['id'], recipient_id=physician_id, body=body_encrypted) db.session.add(msg) db.session.commit() notifMsg = "New Message" newNotif = Notification(physician_id, session['id'], notifMsg, "MESSAGE") db.session.add(newNotif) db.session.commit() flash(('Your message has been sent.')) return redirect(url_for('patient_app.patient_home')) loggedIn = 'id' in session.keys() and 'user_type' in session.keys() notifs = session['notifications'] if 'notifications' in session.keys() else [] usertype = session['user_type'] return render_template ('patient/messages.html', messages=merged_messages, message_bodies=message_bodies, num=num_messages, form=form, recipients=physician_names, notifs=notifs, num_notifs=len(notifs), loggedIn = loggedIn, usertype=usertype)
def add_comment(request): data = request.data parent_id = data.get('parent_id', 0) content = data.get("content") article_id = data.get("article_id") if not (content and article_id): return Response({"status": 400, "msg": "参数错误"}) kwargs = {"id": article_id} article = articleRepository.load(**kwargs) if not article: return Response({"status": 400, "msg": "文章不存在"}) utc_time = TimeUtil.get_utc_time() catalog_type = data.get("catalog_type") workspace = WorkspaceUtil.get_workspace(request) passport_id = workspace.passport_id message = Message() message.reply_id = passport_id message.reply_name = workspace.name message.passport_id = article.passport_id message.article_id = article.id message.article_title = article.title message.created = utc_time message.status = 0 message.reply_type = Message.Type.reply.value flag = True if parent_id == 0: message.reply_type = Message.Type.answer.value if article.passport_id == passport_id: flag = False # 判断是否有根节点存在 kwargs = {} kwargs["layer"] = 0 kwargs["article_id"] = article_id catalog_res = commentRepository.load(**kwargs) # 根节点不存在 先创建一个根节点 if not catalog_res: catalog = Comment() catalog.passport_id = passport_id catalog.parent_id = 0 catalog.layer = 0 catalog.left_id = 1 catalog.right_id = 4 catalog.content = "" catalog.catalog_type = catalog_type catalog.version = 1 catalog.created = utc_time catalog.article_id = article_id catalog.save() catalog_id = catalog.id catalog = Comment() catalog.parent_id = catalog_id catalog.layer = 1 catalog.left_id = 2 catalog.right_id = 3 catalog.content = data["content"] catalog.passport_id = passport_id catalog.catalog_type = catalog_type catalog.article_id = article_id catalog.version = 1 catalog.created = utc_time catalog.save() else: # 更新需要更新的左节点 kwargs = {} kwargs["catalog_type"] = catalog_type kwargs["right_id"] = catalog_res.right_id commentRepository.update_left_id(**kwargs) # 更新需要更新的右节点 commentRepository.update_right_id(**kwargs) parent_id = catalog_res.id catalog = Comment() catalog.parent_id = parent_id catalog.layer = catalog_res.layer + 1 catalog.left_id = catalog_res.right_id catalog.right_id = catalog_res.right_id + 1 catalog.catalog_type = catalog_type catalog.passport_id = passport_id catalog.article_id = article_id catalog.content = data["content"] catalog.version = 1 catalog.created = utc_time catalog.save() else: # 新增节点 kwargs = {} kwargs["id"] = parent_id catalog_res = commentRepository.load(**kwargs) message.passport_id = catalog_res.passport_id if catalog_res.passport_id == passport_id: flag = False # 更新需要更新的左节点 kwargs = {} kwargs["catalog_type"] = catalog_res.catalog_type kwargs["right_id"] = catalog_res.right_id commentRepository.update_left_id(**kwargs) # 更新需要更新的右节点 commentRepository.update_right_id(**kwargs) catalog = Comment() catalog.parent_id = parent_id catalog.layer = catalog_res.layer + 1 catalog.left_id = catalog_res.right_id catalog.right_id = catalog_res.right_id + 1 catalog.passport_id = passport_id catalog.article_id = article_id catalog.catalog_type = catalog_res.catalog_type catalog.content = data["content"] catalog.version = 1 catalog.created = utc_time catalog.save() catalog = CommentSerializers(catalog).data if flag: message.save() return Response({"status": 200, "msg": "新增评论成功", "data": catalog})
def send_comment_message(comment=(), su=User(), mu=User()): # tid:团队id,su:发表评论的用户,mu:文档的拥有者 # 我存的数据库原始id,使用msg/info给我发消息时请加密 # 这里没有写完,注释的地方需要完善 m = Message() m.owner = mu m.sender = su m.title = su.name + " 评论了您的文档:" # + comment.doc.title m.content = comment.content m.portrait = su.portrait m.related_id = comment.id m.type = 'doc' try: m.save() except: return False return True