def edit_comment(request) -> HttpResponse:
    """
    编辑评论,需求评论ID,新的content
    """
    try:
        c = MakeComment.objects.get(id=request.POST['comment_ID'])

        if not auth.auth_with_user(request, c.user.username):
            return HttpResponse(json.dumps({
                'status': -100,
                'errMsg': 'cookies 错误',
            }),
                                content_type="application/json")

        c.comment.content = request.POST['content']

        c.comment.teacher = Teacher.objects.get(
            name=request.POST['teacher_name'])
        # c.comment.edit_time = datetime.datetime.now()
        c.comment.save()
    except BaseException:
        return HttpResponse(json.dumps({
            'status': -1,
            'errMsg': '更新评论失败',
        }),
                            content_type="application/json")
    else:
        return HttpResponse(json.dumps({
            'status': 1,
            'length': 1,
            'body': {
                'message': "更新评论成功"
            }
        }),
                            content_type="application/json")
Exemple #2
0
def delete_user(request):
    try:
        if not auth.auth_with_user(request, request.POST['username']):
            return HttpResponse(json.dumps({
                'status': -100,
                'errMsg': 'cookies 错误',
            }),
                                content_type="application/json")
        user = User.objects.get(username=request.POST['username'])
        user.username = "******" + user.id
        user.email = "" + user.id
        user.profile_photo = "https://i.loli.net/2019/05/14/5cda6706c2f0861301.jpg"

        allchar = string.ascii_letters + string.punctuation + string.digits
        user.password = "".join(
            random.choice(allchar) for x in range(random.randint(15, 25)))
    except:
        return HttpResponse(json.dumps({
            'status': -1,
            'errMsg': '删除失败',
        }),
                            content_type="application/json")
    else:
        return HttpResponse(json.dumps({
            'status': 1,
            'body': '删除成功',
        }),
                            content_type="application/json")
Exemple #3
0
def logout(request) -> HttpResponse:
    '''
    用户登出:提供username
    '''
    try:
        if not auth.auth_with_user(request, request.POST['username']):
            return HttpResponse(json.dumps({
                'status': -100,
                'errMsg': 'cookies 错误',
            }),
                                content_type="application/json")
        del request.session['auth_sess']
        response = HttpResponse(json.dumps({
            'status': 1,
            'length': 1,
            'body': {
                'message': '登出成功'
            }
        }),
                                content_type="application/json")
        response.delete_cookie('username')
        response.delete_cookie('password')

    except:
        return HttpResponse(json.dumps({
            'status': -1,
            'errMsg': '登出失败',
        }),
                            content_type="application/json")
    else:
        return response
def update_user_profile_photo(request) -> HttpResponse:
    """
    用户更新头像接口
    我们使用了sm.ms的api,因此只保存了图床URL
    :param request:
    :return:
    """
    try:
        if not auth.auth_with_user(request, request.POST['username']):
            return HttpResponse(json.dumps({
                'status': -100,
                'errMsg': 'cookies 错误',
            }), content_type="application/json")
        user_name = request.POST['username']
        profile_photo = request.POST['profile_photo']
        user = User.objects.get(username=user_name)
        user.profile_photo = profile_photo
        user.save()
    except Exception:
        return HttpResponse(json.dumps({
            'status': -1,
            'errMsg': 'Update Error',
        }), content_type="application/json")
    return HttpResponse(json.dumps({
        'status': 1,
        'length': 1,
        'body': {
            'message': "成功更新用户{0}的头像".format(user.username)
        }
    }), content_type="application/json")
def reset_password(request):
    try:
        if not auth.auth_with_user(request, request.POST['username']):
            return HttpResponse(json.dumps({
                'status': -100,
                'errMsg': 'cookies 错误',
            }),
                                content_type="application/json")
        username = request.POST['username']
        question = request.POST['question']
        answer = request.POST['answer']
        npassword = request.POST['new_password']
    except:
        return HttpResponse(json.dumps({
            'status': -1,
            'errMsg': '缺失信息',
        }),
                            content_type="application/json")
    else:
        try:
            qs = PasswordQuestion.objects.filter(user__username=username)
            if len(qs) == 0:
                return HttpResponse(json.dumps({
                    'status':
                    -1,
                    'errMsg':
                    '用户未设置保护问题,无法重置密码,请联系管理员',
                }),
                                    content_type="application/json")
            else:
                c = qs[0]
                if c.question == question and c.answer == answer:
                    c.user.password = npassword
                    c.save()
                else:
                    return HttpResponse(json.dumps({
                        'status': -1,
                        'errMsg': '密码或问题错误',
                    }),
                                        content_type="application/json")
        except:
            return HttpResponse(json.dumps({
                'status': -2,
                'errMsg': '设置失败',
            }),
                                content_type="application/json")
    finally:
        return HttpResponse(json.dumps({
            'status': 1,
            'errMsg': '重置成功',
        }),
                            content_type="application/json")
def make_comment(request) -> HttpResponse:
    """
    发表评论,需要用户名,课程ID,教师名字,以及内容。父评论id不是必须选项
    """
    try:
        if not auth.auth_with_user(request, request.POST['username']):
            return HttpResponse(json.dumps({
                'status': -100,
                'errMsg': 'cookies 错误',
            }),
                                content_type="application/json")
        username = request.POST['username']
        course_ID = request.POST['course_ID']
        content = request.POST['content']
        teacher_name = request.POST['teacher_name']
        try:
            parent_comment = request.POST['parent_comment']
        except:
            parent_comment = -1
    except BaseException:
        return HttpResponse(json.dumps({
            'status': -1,
            'errMsg': '缺失信息',
        }),
                            content_type="application/json")
    else:
        try:
            c = Comment(content=content,
                        parent_comment=parent_comment,
                        teacher=Teacher.objects.get(name=teacher_name))
            c.save()
            b = MakeComment(user=User.objects.get(username=username),
                            course=Course.objects.get(course_ID=course_ID),
                            comment=c)
            b.save()
        except BaseException:
            return HttpResponse(json.dumps({
                'status': -1,
                'errMsg': '发表评论失败',
            }),
                                content_type="application/json")
        else:
            return HttpResponse(json.dumps({
                'status': 1,
                'length': 1,
                'body': {
                    'message': "发表评论成功"
                }
            }),
                                content_type="application/json")
def set_question(request):
    try:
        if not auth.auth_with_user(request, request.POST['username']):
            return HttpResponse(json.dumps({
                'status': -100,
                'errMsg': 'cookies 错误',
            }),
                                content_type="application/json")
        username = request.POST['username']
        question = request.POST['question']
        answer = request.POST['answer']
    except:
        return HttpResponse(json.dumps({
            'status': -1,
            'errMsg': '缺失信息',
        }),
                            content_type="application/json")
    else:
        try:
            qs = PasswordQuestion.objects.filter(user__username=username)
            if len(qs) == 0:
                c = PasswordQuestion(user=User.objects.get(name=username),
                                     question=question,
                                     answer=answer)
                c.save()
            else:
                c = qs[0]
                c.question = question
                c.answer = answer
                c.save()
        except:
            return HttpResponse(json.dumps({
                'status': -2,
                'errMsg': '设置失败',
            }),
                                content_type="application/json")
    finally:
        return HttpResponse(json.dumps({
            'status': 1,
            'errMsg': '设置成功',
        }),
                            content_type="application/json")
Exemple #8
0
def update_user(request) -> HttpResponse:
    """
    注册用户。传入POST,应至少包含 username, 其他可选项是role, gender, self introduction。 \n
    对于正常合法传入,更新用户信息并返回成功信息(status=1) \n
    其他非法情况返回错误信息(status=-1) 错误信息保存在errMsg中 \n
    """
    try:
        if not auth.auth_with_user(request, request.POST['username']):
            return HttpResponse(json.dumps({
                'status': -100,
                'errMsg': 'cookies 错误',
            }),
                                content_type="application/json")
        username = request.POST['username']
        user = User.objects.get(username=username)
    except Exception:
        return HttpResponse(json.dumps({
            'status': -1,
            'errMsg': '不存在此用户',
        }),
                            content_type="application/json")
    else:
        try:
            user.gender = request.POST['gender']
            user.role = request.POST['role']
            user.self_introduction = request.POST['self_introduction']
            user.save()
        except Exception:
            return HttpResponse(json.dumps({
                'status': -1,
                'errMsg': '保存失败,请检查内容正确性',
            }),
                                content_type="application/json")
        else:
            return HttpResponse(json.dumps({
                'status': 1,
                'length': 1,
                'body': {
                    'message': "用户{0}信息更新成功".format(username)
                }
            }),
                                content_type="application/json")
def rate_comment(request) -> HttpResponse:
    """
    赞/踩评论,逻辑同百度贴吧逻辑
    """
    try:
        if not auth.auth_with_user(request, request.POST['username']):
            return HttpResponse(json.dumps({
                'status': -100,
                'errMsg': 'cookies 错误',
            }),
                                content_type="application/json")
        username = request.POST['username']
        comment_ID = request.POST['comment_ID']
        type = request.POST['type']
    except BaseException:
        return HttpResponse(json.dumps({
            'status': -1,
            'errMsg': '缺失信息',
        }),
                            content_type="application/json")
    else:
        try:
            rate = RateComment.objects.get(
                user=User.objects.get(username=username),
                comment=Comment.objects.get(id=comment_ID))
        except ObjectDoesNotExist:
            rate = RateComment(user=User.objects.get(username=username),
                               comment=Comment.objects.get(id=comment_ID))
            rate.save()
            c = Comment.objects.get(id=comment_ID)
            if type == 'agree':
                c.rate += 1
                rate.rate = 1
            else:
                c.rate -= 1
                rate.rate = -1
            c.save()
            rate.save()
            return HttpResponse(json.dumps({
                'status': 1,
                'length': 1,
                'body': {
                    'message': "评价评论成功"
                }
            }),
                                content_type="application/json")
        except BaseException:
            return HttpResponse(json.dumps({
                'status': -1,
                'errMsg': '评价评论失败',
            }),
                                content_type="application/json")
        else:
            c = Comment.objects.get(id=comment_ID)
            if type == 'agree':
                if rate.rate == 0:
                    rate.rate = 1
                    c.rate += 1
                    c.save()
                    rate.save()
                    return HttpResponse(json.dumps({
                        'status': 1,
                        'length': 1,
                        'body': {
                            'message': "赞同评论成功"
                        }
                    }),
                                        content_type="application/json")
                elif rate.rate == -1:
                    rate.rate = 1
                    c.rate += 2
                    c.save()
                    rate.save()
                    return HttpResponse(json.dumps({
                        'status': 1,
                        'length': 1,
                        'body': {
                            'message': "已赞同评论"
                        }
                    }),
                                        content_type="application/json")
                else:
                    rate.rate = 0
                    c.rate -= 1
                    c.save()
                    rate.save()
                    return HttpResponse(json.dumps({
                        'status': 1,
                        'length': 1,
                        'body': {
                            'message': "已取消赞同评论"
                        }
                    }),
                                        content_type="application/json")
            else:
                if rate.rate == 0:
                    rate.rate = -1
                    c.rate -= 1
                    c.save()
                    rate.save()
                    return HttpResponse(json.dumps({
                        'status': 1,
                        'length': 1,
                        'body': {
                            'message': "反对评论成功"
                        }
                    }),
                                        content_type="application/json")
                elif rate.rate == 1:
                    rate.rate = -1
                    c.rate -= 2
                    c.save()
                    rate.save()
                    return HttpResponse(json.dumps({
                        'status': 1,
                        'length': 1,
                        'body': {
                            'message': "已反对评论"
                        }
                    }),
                                        content_type="application/json")
                else:
                    rate.rate = 0
                    c.rate += 1
                    c.save()
                    rate.save()
                    return HttpResponse(json.dumps({
                        'status': 1,
                        'length': 1,
                        'body': {
                            'message': "已取消反对评论"
                        }
                    }),
                                        content_type="application/json")
Exemple #10
0
def make_rank(request) -> HttpResponse:
    """
    发表评分,需要用户名,课程ID,以及分数
    """
    try:
        if not auth.auth_with_user(request, request.POST['username']):
            return HttpResponse(json.dumps({
                'status': -100,
                'errMsg': 'cookies 错误',
            }), content_type="application/json")
        username = request.POST['username']
        course_ID = request.POST['course_ID']
        difficulty_score = int(request.POST['difficulty_score'])
        funny_score = int(request.POST['funny_score'])
        gain_score = int(request.POST['gain_score'])
        recommend_score = int(request.POST['recommend_score'])
        if difficulty_score > 5 or difficulty_score < 0 or funny_score > 5 or funny_score < 0 or gain_score > 5 or gain_score < 0 or recommend_score > 5 or recommend_score < 0:
            return HttpResponse(json.dumps({
                'status': -1,
                'errMsg': '非法分数',
            }), content_type="application/json")

    except BaseException:
        return HttpResponse(json.dumps({
            'status': -1,
            'errMsg': '缺失信息',
        }), content_type="application/json")
    else:
        try:
            b = MakeRank.objects.get(
                user=User.objects.get(
                    username=username), course=Course.objects.get(
                    course_ID=course_ID))
        except ObjectDoesNotExist:
            # create new
            r = Rank(difficulty_score=difficulty_score,
                     funny_score=funny_score,
                     gain_score=gain_score,
                     recommend_score=recommend_score,
                     )
            r.save()
            b1 = MakeRank(user=User.objects.get(username=username),
                          course=Course.objects.get(course_ID=course_ID),
                          rank=r)
            b1.save()
            return HttpResponse(json.dumps({
                'status': 1,
                'length': 1,
                'body': {
                    'message': "发表评分成功"
                }
            }), content_type="application/json")
        except BaseException:
            return HttpResponse(json.dumps({
                'status': -1,
                'errMsg': '缺失信息',
            }), content_type="application/json")
        else:
            # edit
            r = b.rank
            r.difficulty_score = difficulty_score
            r.funny_score = funny_score
            r.gain_score = gain_score
            r.recommend_score = recommend_score
            # r.edit_time=datetime.datetime.now()
            r.save()
            return HttpResponse(json.dumps({
                'status': 1,
                'length': 1,
                'body': {
                    'message': "更新评分成功"
                }
            }), content_type="application/json")