def save_message(user_id, message): type_name = message.get('type_name') args = message.get('args') file = message.get('file', '') conversation_id = message.get('conversation_id') mt = MessageType.select().where(MessageType.name == type_name).first() u = User.select().where(User.id == user_id).first() cps = ConversationParty.select().where(ConversationParty.conversation == conversation_id) number_of_conversationees = cps.count() if not mt or not u or not cps or not number_of_conversationees: raise InvalidMessageDataException('Couldn\'t save message: invalid message data') m = Message() with database.transaction(): m.conversation = conversation_id m.message_type = mt m.sender_id = user_id m.ts = datetime.now() m.file = file m.content, m.display_content = __save_content(args, mt, user_id, cps) m.save() update_conversation(conversation_id=conversation_id, last_message=m) mark_message_as_read(user_id=user_id, message=m, conversation_id=conversation_id) message_object = get_message_json(message=m) message_object['recipient_ids'] = [cp.user.id for cp in cps] return json.dumps(message_object)
def save_message(type_name, args, file): mt = MessageType.select().where(MessageType.name == type_name).first() if mt: m = Message() m.conversation_party = mt m.ts = datetime.now() m.file = file with models.transactions(): m.save() m.run_constructor(args) m.save() return m else: return None
from models import BaseModel, User, Conversation, ConversationParty, Message, MessageType, database from datetime import datetime # Messages m = Message() m.conversation = Conversation.get(Conversation.id==1) m.sender = User.get(User.id==1) m.message_type = MessageType.get(MessageType.name=='directive_quotation_mt') m.ts = datetime.now() args = {'currency':'R$', 'per_day_beneficiary_value':'20.50', 'number_of_beneficiaries':'5', 'company_name':'VR', 'company_picture':'' } with database.transaction(): m.save() m.run_constructor(args) m.save()