Beispiel #1
0
def createUser(req):
    """
    创建用户
    """
    userName = str(req.POST.get('userName'))
    password = str(req.POST.get('password'))

    err, curUser = UserManager.createUser(userName, password)

    if (err != 'succeed'):
        return HttpResponse("注册失败")

    email = str(req.POST.get('email'))
    avatar = str(req.POST.get('avatar'))
    if (avatar == ""):
        avatar = "https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png"

    err = UserManager.changeInfo(curUser.id, curUser.userName, password,
                                 avatar, email)

    if (err != 'succeed'):
        return HttpResponse("注册成功!")

    ret = HttpResponse("注册成功!")

    print(Util.setUserForSession(req, curUser.id), "===set session here")
    return ret
Beispiel #2
0
def delUserByID(req):
    """
    根据ID删除用户
    """
    userID = int(req.POST.get("userID"))

    err, curID = Util.getUserIDBySession(req)

    if (err != 'succeed'):
        return HttpResponse("请先登录")

    err, cur = UserManager.getUserByID(curID)
    if (err != 'succeed'):
        return HttpResponse("Error")

    err, user = UserManager.getUserByID(userID)
    if (err != 'succeed'):
        return HttpResponse("Error")

    if (curID != userID and not cur.isAdmin):
        return HttpResponse("权限不足")

    err = UserManager.delUserByID(curID, userID)

    if (err != 'succeed'):
        return HttpResponse('Error')

    if (curID == userID):
        Util.delUserForSession(req)

    return HttpResponse("删除成功")
Beispiel #3
0
    def testChangePass(self):
        err = UserManager.changePassword(self.user.id, "test", "test1")
        self.assertEqual(err, "succeed")

        err, user1 = UserManager.login("test1", "test")
        self.assertNotEqual(err, "succeed")

        err, user2 = UserManager.login("test1", "test1")
        self.assertEqual(err, "succeed")
Beispiel #4
0
    def testDelCurUser(self):
        """
        test if delUser function could run correctly
        """

        err3, user3 = UserManager.createUser("test3", "aaaa")
        self.assertEquals(err3, "succeed")

        err, user4 = UserManager.getUserByID(user3.id)
        self.assertEqual(err, "succeed")

        err = UserManager.delUserByID(self.user, user3)
        self.assertNotEqual(err, "succeed")

        err, user4 = UserManager.getUserByID(user3.id)
        self.assertEqual(err, "succeed")

        err = UserManager.delUserByID(user3.id, user3.id)
        self.assertEqual(err, "succeed")
Beispiel #5
0
def validatepwd(req):
    response = {}
    userId = str(req.POST.get('userId'))
    pwd = (req.POST.get('pwd'))
    print("view", pwd)
    info = UserManager.isValide(userId, pwd)
    if info == 'succeed':
        response['msg'] = 'succeed'
        response['err_num'] = 0
    else:
        response['err_num'] = 1
        response['msg'] = 'error'
    return JsonResponse(response)
Beispiel #6
0
def login(req):
    """
    登录
    """
    userName = str(req.POST.get('userName'))
    password = str(req.POST.get('password'))

    err, curUser = UserManager.login(userName, password)

    if (err != "succeed"):
        print('!!!!!!!')
        return HttpResponse(err)

    Util.setUserForSession(req, curUser.id)

    return HttpResponse(err)
Beispiel #7
0
def getUserByID(req):
    """
    根据ID获取用户
    """
    response = {}
    userId = str(req.POST.get('userId'))
    err, user = UserManager.getUserByID(userId)
    if err == 'succeed':
        # convert queryset to json
        response['user'] = json.loads(serializers.serialize("json", [user]))
        response['msg'] = 'succeed'
        response['err_num'] = 0
    else:
        response['err_num'] = 1
        response['msg'] = 'error'
    print("===get User here")
    return JsonResponse(response)
Beispiel #8
0
def getUserNameByID(req):
    """
    根据ID获取用户名
    """
    response = {}
    userId = str(req.GET.get('userId'))
    err, user = UserManager.getUserByID(userId)
    if err == 'succeed':
        # convert queryset to json
        response['user'] = user.userName
        response['msg'] = 'succeed'
        response['err_num'] = 0
    else:
        response['err_num'] = 1
        response['msg'] = 'error'
    print("===get User here")
    return JsonResponse(response)
Beispiel #9
0
    def testAddSen(self):
        """
        test if addSentences function could run correctly
        """
        print("=======  test add")
        err2, newsen = SenManager.addSentences(
            self.user.id, 'org sentence', 'correct sentence',
            ['spell', 'case', 'verb', 'replace'])
        self.assertEquals(err2, "succeed")
        for type in list(newsen.error_type.all()):
            print(type.type)
        err3, newsen = SenManager.addSentences(self.user.id, 'org sentence1',
                                               'correct sentence1',
                                               ['spell', 'case', 'verb'])
        err4, lists = SenManager.getSentencesByUserID(self.user.id)
        self.assertEqual(err4, 'succeed')
        for l in lists:
            print(l.org_sen, l.corr_sen, l.is_delete,
                  [t.type for t in l.error_type.all()], l.dateTime)

        err0, type = TypeManager.getTypeByName('spell', self.user.id)
        self.assertEqual(err0, 'succeed')
        print('newuser==============')
        err0, newuser = UserManager.createUser('ttt', 'ppp')
        err3, newsen = SenManager.addSentences(newuser.id, 'org sentence222',
                                               'correct sentence222',
                                               ['spell', 'case', 'verb'])
        err, type = TypeManager.getTypeByName('spell', newuser.id)
        err5, resList = SenManager.getSentencesByTypeName(
            'replace', self.user.id)
        self.assertEqual(err5, 'succeed')
        for l in resList:
            print(l.org_sen, l.corr_sen, l.is_delete,
                  [t.type for t in l.error_type.all()], l.dateTime)
        err0, type = TypeManager.getTypeByName('spell', newuser.id)

        print("===")
        err = TypeManager.updateType('spell', self.user.id, 10)
        err, lists = TypeManager.getTypeCntRank(self.user.id)
        for l in lists:
            print(l.type, l.error_counts)
        print("+++")
        err, typelist = TypeManager.getTypeBySentence(newsen.id)
        for l in typelist:
            print(l.type)
Beispiel #10
0
    def tests(self):
        err, user1 = UserManager.createUser("tttt", "t1")

        err, word = WordManager.addWords(self.user.id, 'fottball')
        self.assertEqual(err, 'succeed')
        err, word1 = WordManager.addWords(self.user.id, 'facebook')
        err = WordManager.updateWord(word1.word, self.user.id, 5)
        self.assertEqual(err, 'succeed')

        err, words = WordManager.searchWord(self.user.id, "f")
        self.assertEqual(err, 'succeed')
        for w in words:
            print('aaa', w.word, w.use_counts)

        err, words = WordManager.getWordByUserID(self.user.id)
        for w in words:
            print(w.word, w.use_counts)
        self.assertEqual(err, 'succeed')
        print("========")
        err, word = WordManager.delWord(word1.id)
        self.assertEqual(err, 'succeed')


        err, word = WordManager.addWords(user1.id, 'iphone')
        err, word = WordManager.addWords(user1.id, 'huawei')
        err = WordManager.updateWord(word.word, user1.id, 10)

        err, words = WordManager.getMostUsedWord(user1.id)
        self.assertEqual(err, 'succeed')
        for word in words:
            print(word.word, word.use_counts)

        err, word22 = WordManager.delWord(word1.id)
        self.assertEqual(err, 'succeed')
        print(word22.is_delete)
        err, w = WordManager.searchWord(self.user.id, word22.word)
        self.assertNotEqual(err, 'succeed')
        err, w = WordManager.addWords(self.user.id, word22.word)
        self.assertEqual(w.id, word22.id)
        print(w.is_delete, w.id, word22.id, word22.is_delete)
        err, words = WordManager.getWordByUserID(user1.id)
        self.assertEqual(err, 'succeed')
        for w in words:
            print(w.word, w.use_counts)
Beispiel #11
0
def changeInfo(req):
    """
    编辑用户信息
    """
    userId = str(req.POST.get('userId'))
    nickName = str(req.POST.get('nickName'))
    userName = str(req.POST.get('userName'))
    password = str(req.POST.get('password'))
    sex = str(req.POST.get('sex'))
    print(sex)
    if (sex == "男"):
        sex = 1
    else:
        sex = 0
    email = str(req.POST.get('email'))
    avatar = str(req.POST.get('avatar'))
    if (avatar == ""):
        avatar = "https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png"

    err = UserManager.changeInfo(userId, userName, password, avatar, email)
    print(err)
    return HttpResponse("succeed")
Beispiel #12
0
    def testDel(self):
        print("=======  test delete")
        err2, newsen = SenManager.addSentences(
            self.user.id, 'org sentence', 'correct sentence',
            ['spell', 'case', 'verb', 'replace'])
        err3, newsen = SenManager.addSentences(self.user.id, 'org sentence1',
                                               'correct sentence1',
                                               ['spell', 'case', 'verb'])

        err0, newuser = UserManager.createUser('ttt', 'ppp')
        err3, newsen1 = SenManager.addSentences(newuser.id, 'org sentence1',
                                                'correct sentence1',
                                                ['spell', 'case', 'verb'])
        self.assertNotEqual(newsen1.id, newsen.id)
        err3 = SenManager.delSentences(newsen.id)
        self.assertEqual(err3, 'succeed')
        err4, sen = SenManager.getSentencesByID(newsen.id)
        self.assertNotEqual(err4, 'succeed')
        err4, lists = SenManager.getSentencesByUserID(newuser.id)
        self.assertEqual(err4, 'succeed')
        for l in lists:
            print(l.user.userName, l.org_sen, l.corr_sen, l.is_delete,
                  [t.type for t in l.error_type.all()], l.dateTime)

        err4, lists = SenManager.getSentencesByUserID(self.user.id)
        self.assertEqual(err4, 'succeed')
        for l in lists:
            print(l.org_sen, l.corr_sen, l.is_delete,
                  [t.type for t in l.error_type.all()], l.dateTime)
        err0, type = TypeManager.getTypeByName('case', self.user.id)
        print("++++")
        err5, resList = SenManager.getSentencesByTypeName('case', self.user.id)
        self.assertEqual(err5, 'succeed')
        for l in resList:
            print(l.org_sen, l.corr_sen, l.is_delete,
                  [t.type for t in l.error_type.all()], l.dateTime)
Beispiel #13
0
 def setUp(self):
     err, self.user = UserManager.createUser("test1", "test")
     self.assertEquals(err, "succeed")
Beispiel #14
0
 def testChangeInfo(self):
     err = UserManager.changeInfo(self.user.id, 'liu', 'avatar', 'email@')
     err, user = UserManager.getUserByID(self.user.id)
     print(user.userName, user.avatar, user.email)
     self.assertEqual(err, "succeed")
Beispiel #15
0
    def testLogin(self):
        err, user1 = UserManager.login("test1", "test1")
        self.assertNotEqual(err, "succeed")

        err, user2 = UserManager.login("test1", "test")
        self.assertEqual(err, "succeed")
Beispiel #16
0
 def testCreateUser(self):
     """
     test if createUser function could run correctly
     """
     err2, user2 = UserManager.createUser("test2", "aaaa")
     self.assertEquals(err2, "succeed")