Exemple #1
0
def edit_user_set(request, thread_id):
     #Function get an inforamtion from tornado app about
     # delete or add some user and username of target,
     # it also get thread_id

     print("Request get")
     if not request.method == "POST":
         json_response({"error":"Please use POST method"})

     try:
          thread = Thread.objects.get(id = thread_id)
     except Thread.DoesNotExist:
         json_response({"error":"This thread does not exist"})


     username = request.POST.get("username")
     try:
         target_user = User.objects.get(username = username)
     except User.DoesNotExist:
         json_response("This user does not exist")

     if request.POST.get("operation") == "add_user":

         print ("b3_!")
         try:
             thread.participants.get (username = username)
             print ("User not added")
         except User.DoesNotExist:
             thread.participants.add(target_user)
             print ("User added")
             return json_response({"status":"Ok"})

     return json_response({"error":"This user is already in thread"})
Exemple #2
0
def send_message_api_view(request, thread_id):
    if not request.method == "POST":
        return json_response({"error": "Please use POST."})

    api_key = request.POST.get("api_key")

    if api_key != settings.API_KEY:
        return json_response({"error": "Please pass a correct API key."})

    try:
        thread = Thread.objects.get(id=thread_id)
    except Thread.DoesNotExist:
        return json_response({"error": "No such thread."})

    try:
        sender = User.objects.get(id=request.POST.get("sender_id"))
    except User.DoesNotExist:
        return json_response({"error": "No such user."})

    message_text = request.POST.get("message")

    if not message_text:
        return json_response({"error": "No message found."})

    if len(message_text) > 10000:
        return json_response({"error": "The message is too long."})

    send_message(
                    thread.id,
                    sender.id,
                    message_text
                )

    return json_response({"status": "ok"})
Exemple #3
0
def send_message_api_view(request, thread_id):
    if not request.method == "POST":
        return json_response({"error": "Please use POST."})

    api_key = request.POST.get("api_key")

    if api_key != settings.API_KEY:
        return json_response({"error": "Please pass a correct API key."})

    try:
        thread = Thread.objects.get(id=thread_id)
    except Thread.DoesNotExist:
        return json_response({"error": "No such thread."})

    try:
        sender = User.objects.get(id=request.POST.get("sender_id"))
    except User.DoesNotExist:
        return json_response({"error": "No such user."})

    message_text = request.POST.get("message")

    if not message_text:
        return json_response({"error": "No message found."})

    if len(message_text) > 10000:
        return json_response({"error": "The message is too long."})

    send_message(
                    thread.id,
                    sender.id,
                    message_text
                )

    return json_response({"status": "ok"})
Exemple #4
0
def send_message_api_view(request,thread_id):
    print ("message")
    if not request.method=='POST':
        return json_response({"error":"Please use POST"})

    try:
        thread = Thread.objects.get(id = thread_id)
    except Thread.DoesNotExist:
        return json_response({"error":"No such thread"})

    try:
        sender=User.objects.get(id=request.POST.get("sender_id"))
    except User.DoesNotExist:
        return json_response({"error":"No such user."})

    message_text=request.POST.get("message")

    if not message_text:
        return json_response({"error":"Message text cannot be empty"})

    if len(message_text)>1000:
        return json_response({"error":"The message must be less than 1000 characters."})

    send_message(
        thread.id,
        sender.id,
        message_text
    )

    return json_response({"status":"Ok"})