Esempio n. 1
0
def conversation(database, user, admin_user):
    conversation = Conversation(user_id=user.id,
                                from_user_id=user.id,
                                to_user_id=admin_user.id,
                                shared_id=uuid.uuid4())
    conversation.save()
    return conversation
Esempio n. 2
0
def start_new_conversation_from_profile(request, recipent_pk):
    recipent = get_user_model().objects.get(pk=recipent_pk)

    if request.method == 'POST':
        form = forms.StartNewConversationFromProfile(request.POST)

        if form.is_valid():
            new_conversation = Conversation(
                subject=form.cleaned_data['subject'],
                initiator=request.user,
                recipent=recipent)
            new_conversation.save()

            first_message = Message(coversation=new_conversation,
                                    sender=request.user,
                                    receiver=recipent,
                                    content=form.cleaned_data['message'])
            first_message.save()

            return redirect('conversations:conversation_panel',
                            conversation_pk=new_conversation.pk)

        else:
            # TODO add error page
            print('error creating conversation - need to have page here')

    elif request.method == 'GET':
        form = forms.StartNewConversationFromProfile()
        return render(request,
                      'conversations/start_new_conversation.html',
                      context={
                          'recipent': recipent,
                          'form': form
                      })