def create_conversation(user_id, conversation): conversation_type = conversation.get('conversation_type') conversationees_list = conversation.get('conversationees_list') conversationees_list = list(set(conversation.get('conversationees_list',[]))) if conversation_type == conversations_config.get('CONVERSATION_DIRECT_TYPE') and len(conversationees_list) != 2: raise InvalidConversationException('Direct conversations should have 2 conversationees') ct = ConversationType.get(ConversationType.name == conversation_type) c = Conversation() c.conversation_type = ct p_url = conversation.get('picture','') picture = None if p_url: picture = Photo() picture.url = p_url name = conversation.get('name','') cps = [] myself = None for index, conversationee in enumerate(conversationees_list): n = None p = None if conversation_type == 'group': n = name p = picture else: data = get_user_data(index, conversationees_list) n = data[0] p = data[1] cp = ConversationParty() cp.conversation = c cp.name = n cp.user = User.get(User.id==conversationee) cp.picture = p cps.append(cp) if conversationee == user_id: myself = cp with database.transaction(): c.save() if picture: picture.save() for cp in cps: cp.save() return __jsonify_one_conversation(myself)
from models import BaseModel, User, Conversation, ConversationParty, ConversationType, Message, database from web.chat_config import config as conversation_config from datetime import datetime # Conversations and ConversationParties conversations = [] conversationparties = [] grupo = ConversationType() grupo.name = conversation_config.get('CONVERSATION_GROUP_TYPE') c1 = Conversation() c1.conversation_type = grupo conversations.append(c1) # c2 = Conversation() # c2.conversation_type = grupo # conversations.append(c2) # cp133 = ConversationParty() # cp133.conversation=c2 # cp133.user=User.get(User.username=='ricardinho') # cp133.name='Vale-Refeição' # conversationparties.append(cp133) # cp144 = ConversationParty() # cp144.conversation=c2 # cp144.user=User.get(User.username=='felipinho') # cp144.name='Vale-Refeição' # conversationparties.append(cp144)