def insertThread(request):
    cursor = connection.cursor()

    POST = json.loads(request.body)
    forum = POST["forum"]
    title = POST["title"]
    email = POST["user"]
    dateThread = POST["date"]
    message = POST["message"]
    slug = POST["slug"]

    isClosed = POST.get("isClosed",0)
    isClosed = getBollean(isClosed)

    isDeleted = POST.get("isDeleted",0)
    isDeleted = getBollean(isDeleted)

    cursor.execute('''INSERT IGNORE INTO Thread(forum,user,title,slug,message,dateThread,isClosed,isDeleted) 
                      VALUES ('%s','%s','%s','%s','%s','%s','%d','%d');
                   ''' % (forum,email,title,slug,message,dateThread,isClosed,isDeleted,)) 

    cursor.execute('''SELECT LAST_INSERT_ID();
                   ''')
    idThread = cursor.fetchone()[0]

    requestCopy = request.GET.copy()
    requestCopy.__setitem__('id',idThread)#setdefault()
    code = 0
    responce = { "code": code, "response": requestCopy }
    responce = json.dumps(responce)
    cursor.close()
    return HttpResponse(responce,content_type="application/json")
def DetailsUser(cursor,email):#GET

    following = getFollowing(cursor,email)
    followers = getFollowers(cursor,email)
    subscriptions = getSubscriptions(cursor,email)

    cursor.execute('''SELECT username,about,name,isAnonymous,idUser
                      FROM User
                      WHERE email = '%s'
                   ''' % (email,))
    userInfo = cursor.fetchone()

    username = userInfo[0]
    about = userInfo[1]
    name = userInfo[2]
    isAnonymous = getBollean(userInfo[3])
    idUser = userInfo[4]

    code = 0
    responce = {'following': following,
                 'followers': followers,
                 'subscriptions': subscriptions,
                 'username': username,
                 'about': about,
                 'name': name,
                 'isAnonymous': isAnonymous,
                 'id': idUser,
                 'email': email,
    }
    return responce
def insertUser(request):#Insert User
    cursor = connection.cursor()

    POST = json.loads(request.body)
    username = POST.get('username',None)
    about = POST.get('about',None)
    name = POST.get('name',None)
    email = POST['email']
    isAnonymous = POST.get('isAnonymous',False)
    isAnonymous =getBollean(isAnonymous)

    cursor.execute('''SELECT email
                      FROM User
                      WHERE email = '%s'
                   ''' %email)
    user =  cursor.fetchone()
    if user:
        code = 5
        responce = { "code": code, "response": "user exist" }
        responce = json.dumps(responce)
        return HttpResponse(responce,content_type="application/json")

    cursor.execute('''INSERT INTO User(email,username,about,name,isAnonymous) 
                      VALUES ('%s',%s,%s,%s,%d);
                   ''' % (email,helperQuotes(username),helperQuotes(about),helperQuotes(name),isAnonymous,)) 

    cursor.execute('''SELECT LAST_INSERT_ID();
                   ''')
    idUser = cursor.fetchone()[0]

    requestCopy = request.GET.copy()
    requestCopy.__setitem__('id',idUser)#setdefault()
    code = 0
    responce = { "code": code, "response": requestCopy }
    responce = json.dumps(responce)
    cursor.close()
    return HttpResponse(responce,content_type="application/json")
def insertPost(request):#Post(level,parent)
    cursor = connection.cursor()


    POST = json.loads(request.body)

    message = POST["message"]
    datePost = POST["date"]
    email = POST["user"]
    idThread = int(POST["thread"])
    forum = POST["forum"]
    parent = POST.get("parent",None)#None == NULL ?
    if parent is not None:
        parent = int(parent)

    isApproved = POST.get("isApproved",0)
    isApproved = getBollean(isApproved)

    isDeleted = POST.get("isDeleted",0)
    isDeleted = getBollean(isDeleted)

    isEdited = POST.get("isEdited",0)
    isEdited = getBollean(isEdited)

    isHighlighted = POST.get("isHighlighted",0)
    isHighlighted = getBollean(isHighlighted)

    isSpam = POST.get("isSpam",0)
    isSpam = getBollean(isSpam)

    ###########
    #if parent is None:
    #    level = 1
    #    cursor.execute('''SELECT COUNT(*) + 1
    #                      FROM Post 
    #                      WHERE level = %d
    #                   ''' %level )
    #    number = cursor.fetchone()[0]
    #    path = str(number)
    #else:
    #    cursor.execute('''SELECT `path`,level
    #                      FROM Post
    #                      WHERE idPost = %d
    #                  ''' %parent )
    #    pathResponse = cursor.fetchall()[0]
    #    pathParent = pathResponse[0]
    #    level = int(pathResponse[1])+1
    #    cursor.execute('''SELECT COUNT(*)
    #                      FROM Post
    #                      WHERE level = %d AND parent = %d
    #                   ''' %(level,parent))
    #    countThisLevelPost = cursor.fetchone()[0]
    #    countThisLevelPost = int(countThisLevelPost)
    #    path = pathParent + '.' + str(countThisLevelPost+1)
    ##вроде работает но слишком много запросов 
    ########### path и level получены,но надо в десятичной системе -
    ########### максимум сколько можно коментариев на коментарий - 10 
    ########### нужно перевести в другую

    level = 1
    path = "1"

    if parent is None:
        cursor.execute('''INSERT INTO Post(forum, idThread, user, datePost, message,  isEdited, isDeleted, isSpam,  isHighlighted, isApproved , `path`,level) 
                          VALUES          ( '%s',    '%d',  '%s',    '%s'  ,  '%s'     ,'%d'      ,'%d'  , '%d'   ,   '%d'       ,    '%d'    , '%s', %d  );
                       '''              % (forum, idThread, email, datePost, message, isEdited, isDeleted, isSpam,  isHighlighted,  isApproved, path, level  )) 
    else:
        cursor.execute('''INSERT INTO Post(forum, idThread, user, datePost, message,  isEdited, isDeleted, isSpam,  isHighlighted, isApproved, parent  , `path`,level) 
                          VALUES          ( '%s',    '%d',  '%s',    '%s'  ,  '%s'     ,'%d'      ,'%d'  , '%d'   ,   '%d'       ,    '%d'    , '%s'   , '%s',   %d );
                       '''              % (forum, idThread, email, datePost, message, isEdited, isDeleted, isSpam,  isHighlighted,  isApproved, parent , path ,  level ))

    #обновляю количество постов в форуме и в теме
    updateCountPost(cursor,forum,idThread,1)

    cursor.execute('''SELECT LAST_INSERT_ID();
                   ''')
    idPost = cursor.fetchone()[0]

    requestCopy = request.GET.copy()

    requestCopy.__setitem__('id',idPost)
    requestCopy.__setitem__('parent',parent)


    code = 0
    responce = { "code": code, "response": requestCopy }
    responce = json.dumps(responce)
    cursor.close()
    return HttpResponse(responce,content_type="application/json")