예제 #1
0
파일: cms.py 프로젝트: shihbas/blog-yuehu
    def post(self, request):
        top_code = request.data.get("top_code")
        level = request.data.get("level")
        name = request.data.get("name")
        display_image = request.data.get("display_image")
        description = request.data.get("description")

        try:
            level = int(level)
            if level < 0:
                raise ValueError
        except (ValueError, TypeError) as e:
            raise LogicException("level参数类型错误")

        if level > 0:
            top_tag = ArticleTag.objects.filter(open_code=top_code).first()
        else:
            top_tag = None

        if top_tag is None and level > 0:
            raise LogicException("top_code参数无效")

        article_tag = ArticleTag(name=name,
                                 level=level,
                                 display_image=display_image,
                                 description=description,
                                 root_tag=top_tag)
        article_tag.save()
        return Response({"created": article_tag.code})
예제 #2
0
파일: usual.py 프로젝트: shihbas/blog-yuehu
    def post(self, request):
        """
            admin登录(暂无验证)
        :param request:
        :return:
        """
        username = request.data.get("username", None)
        password = request.data.get("password", None)

        ve = ValidateException()
        if not password:
            ve.add_message("password1", "null")

        right, email = email_rightful(username)
        if not right:
            ve.add_message("email", "except")

        if ve.is_error():
            raise ve

        if not User.objects.filter(username=email).exists():
            raise LogicException("你走!你不是主人!")

        user = authenticate(request, username=email, password=password)

        if user is None:
            raise LogicException("登录失败,请检查账号密码是否正确")

        login(request, user)
        return Response({"login": "******", "token": user.open_id})
예제 #3
0
def change_password(request):
    password1 = request.data.get("password1")
    password2 = request.data.get("password2")

    ve = ValidateException()
    if not password1:
        ve.add_message("password1", "null")
    if not password2:
        ve.add_message("password2", "null")

    if account_constant.PWD_MIN_LENGTH > len(
            password1) or account_constant.PWD_MAX_LENGTH < len(password1):
        ve.add_message("password1", "except")

    if account_constant.PWD_MIN_LENGTH > len(
            password2) or account_constant.PWD_MAX_LENGTH < len(password2):
        ve.add_message("password2", "except")

    if password1 != password2:
        raise LogicException("两次密码不一致")

    user = request.user
    if user.change_pwd(password1):
        return Response({"change": "success"})
    else:
        raise LogicException("服务错误请重新提交")
예제 #4
0
    def add_article_comment_reply_by_code(cls, reply_user_id,
                                          comment_user_code,
                                          original_comment_code, content):
        """
            增加评论回复 通过code
        :param reply_user_id: 回复者ID
        :param comment_user_code: 原评论用户code
        :param original_comment_code: 原评论code
        :param content: 回复内容
        :return:
        """
        article_comment = ArticleComment.objects.filter(
            open_code=original_comment_code).only("id", "article_id").first()
        if article_comment is None:
            raise LogicException("无效的评论code")

        comment_user = User.objects.filter(
            oid=comment_user_code).only("id").first()

        if comment_user is None:
            raise LogicException("无效的用户code")

        comment = cls(article_id=article_comment.article_id,
                      original_comment_id=article_comment.id,
                      reply_user_id=reply_user_id,
                      comment_user_id=comment_user.id,
                      content=content)

        comment.save()
        return comment.code, article_comment.article_id
예제 #5
0
    def post(self, request):
        email = request.data.get("email", None)
        password1 = request.data.get("password1")
        password2 = request.data.get("password2")

        if account_constant.PWD_MIN_LENGTH > len(
                password1) or account_constant.PWD_MAX_LENGTH < len(password1):
            raise LogicException(
                f"密码长度应大于{account_constant.PWD_MIN_LENGTH}位,小于{account_constant.PWD_MAX_LENGTH}"
            )

        right, email = email_rightful(email)
        if not right:
            raise LogicException("邮箱格式不正确")

        if User.objects.filter(username=email).exists():
            raise LogicException("邮箱已经注册了")

        if password1 != password2:
            raise LogicException("两次密码不一致")

        email_code = create_random_code(length=12, mode=common_constant.MODE5)
        if send_email_register(email_code, email):
            cache.set(
                account_constant.CACHE_REGISTER_EMAIL_CODE(
                    email_code=email_code), {
                        "email": email,
                        "password": password1
                    }, account_constant.EMAIL_CODE_CACHE_MAX)
        else:
            raise LogicException("发送失败!请检查邮箱或联系我!")

        return Response({"register": "success"})
예제 #6
0
파일: cms.py 프로젝트: shihbas/blog-yuehu
    def put(self, request):
        article_code = request.data.get("article_code")
        tag_code_list = request.data.get("tag_code_list")

        article = Article.objects.filter(open_code=article_code).first()

        if article is None:
            raise LogicException("这个文章不存在吽")

        article_tag_set = ArticleTag.objects.filter(
            open_code__in=tag_code_list)
        article_tag_count = len(article_tag_set)
        if article_tag_count == 0:
            raise LogicException("标签不存在吽")
        if article_tag_count > 3:
            raise LogicException("标签不得多于三个")

        article.article_tag.clear()

        ArticleRelationTag.objects.bulk_create([
            ArticleRelationTag(article_id=article.id, tag_id=tag.id)
            for tag in article_tag_set
        ])

        return Response({"add": "success"})
예제 #7
0
파일: user.py 프로젝트: shihbas/blog-yuehu
    def post(self, request):
        user = request.user
        email = request.data.get("email")

        if user.email:
            raise LogicException("绑定失败:用户已经绑定了邮箱")

        right, email = email_rightful(email)
        if not right:
            raise LogicException("绑定失败:邮箱格式不正确")

        if User.objects.filter(email=email).exists():
            raise LogicException("绑定失败:邮箱已经被注册或绑定了")

        email_code = create_random_code(length=12, mode=common_constant.MODE5)

        if send_email_html_bind_email(email_code, user.display_name, email):
            cache.set(
                account_constant.CACHE_BIND_EMAIL_CODE(email_code=email_code),
                {
                    "email": email,
                    "open_id": user.open_id
                }, account_constant.EMAIL_CODE_CACHE_MAX)
        else:
            raise LogicException("发送邮件失败:请检查邮箱或联系我!")

        return Response({"bind": "success"})
예제 #8
0
 def change_nickname(self, nickname):
     """
         修改昵称
     :param nickname:
     :return:
     """
     now_date = timezone.datetime.now().date()
     interval_date = now_date - self.nickname_change_date
     if account_constant.NICKNAME_CHANGE_INTERVAL_DAY > interval_date.days:
         raise LogicException(f'修改失败:在最近的{account_constant.NICKNAME_CHANGE_INTERVAL_DAY}天内你修改过了昵称,现在不能修改了')
     if User.objects.filter(nickname=nickname).exists():
         raise LogicException(f'修改失败:昵称{nickname}已经存在了!请更换一个')
     self.nickname = nickname
     self.nickname_change_date = now_date
     self.save()
     return 1
예제 #9
0
def web_logout(request):

    if not request.user.is_authenticated:
        raise LogicException("你已经退出登录了")

    logout(request)
    return Response({"logout": "success"})
예제 #10
0
파일: usual.py 프로젝트: shihbas/blog-yuehu
    def put(self, request):
        """
            发送admin登录验证码
        :param request:
        :return:
        """

        email = request.data.get("email", None)

        ve = ValidateException()

        right, email = email_rightful(email)
        if not right:
            ve.add_message("email", "except")

        if ve.is_error():
            raise ve

        if not User.objects.filter(username=email, is_superuser=True).exists():
            raise LogicException("Em....")

        email_code = create_random_code(mode=common_constant.MODE5)
        if send_email_html_admin_login(email_code, email):
            email_code = email_code.lower()
            cache.set(
                account_constant.CACHE_ADMIN_LOGIN_PWD_EMAIL_CODE(email=email),
                email_code,
                account_constant.EMAIL_CODE_CACHE_MAX
            )
            return Response({"send": "success"})
        else:
            return Response({"send": "failure"})
예제 #11
0
    def delete(self, request):
        code = request.GET.get("code")

        timeline = TimelineLine.objects.filter(open_code=code).first()
        if timeline is None:
            raise LogicException("时间线删除失败,code无效")

        return Response({"deleted": timeline.delete_timeline()})
예제 #12
0
    def change_default_avatar(self, avatar_code):
        """
            挑选默认头像替换原头像
        :param avatar_code:
        :return:
        """
        now_date = timezone.datetime.now().date()
        interval_date = now_date - self.avatar_change_date
        if account_constant.AVATAR_CHANGE_INTERVAL_DAY > interval_date.days:
            raise LogicException(f'修改失败:在最近的{account_constant.AVATAR_CHANGE_INTERVAL_DAY}天内你修改过了头像,现在不能修改了')

        avatar = UserDefaultAvatar.objects.filter(open_code=avatar_code).first()
        if avatar is None:
            raise LogicException(f'修改失败:你所选的头像不是python-dog提供的默认头像集内的')
        self.avatar = avatar.avatar_url
        self.avatar_change_date = now_date
        self.save()
        return 1
예제 #13
0
    def put(self, request):
        old_name = request.data.get("old_name")
        new_name = request.data.get("new_name")

        if Group.objects.filter(name=new_name).exists():
            raise LogicException("已有该名称的组了!")

        updated = Group.objects.filter(name=old_name).update(name=new_name)

        return Response({"updated": updated})
예제 #14
0
파일: cms.py 프로젝트: shihbas/blog-yuehu
    def get(self, request):
        code = request.GET.get("code")

        top_tag = ArticleTag.objects.filter(level=0, open_code=code).first()
        if top_tag is None:
            raise LogicException("没有该顶级标签code")

        tags = ArticleTag.objects.filter(
            Q(root_tag=top_tag) | Q(id=top_tag.id))
        tags_ser = CMSArticleTagManageList(tags, many=True)
        return Response(tags_ser.data)
예제 #15
0
파일: cms.py 프로젝트: shihbas/blog-yuehu
    def get(self, request):
        code = request.GET.get("code")

        article = Article.objects.filter(open_code=code).first()

        if article is None:
            raise LogicException("文章不存在啊!")

        article_ser = CMSArticleDetailSerializers(article)

        return Response(article_ser.data)
예제 #16
0
def get_account_detail_by_username(request):
    """
        通过用户名找到账户
    :param request:
    :return:
    """
    username = request.GET.get("username")
    account = User.objects.filter(username=username).first()
    if account is None:
        raise LogicException(f"用户名{username}无效")
    account_ser = CMSAccountDetailForGroup(account)
    return Response(data=account_ser.data)
예제 #17
0
파일: user.py 프로젝트: shihbas/blog-yuehu
 def post(self, request):
     """
         修改密码
     :param request:
     :return:
     """
     user = request.user
     password1 = request.data.get("pwd1")
     password2 = request.data.get("pwd2")
     if password1 != password2:
         raise LogicException("两次密码不一致")
     return Response(data=user.change_pwd(password1))
예제 #18
0
    def put(self, request):
        code = request.data.get("code", "")
        title = request.data.get("title")
        link = request.data.get("link")
        sort_no = request.data.get("sort_no")
        description = request.data.get("description")
        is_original = request.data.get("is_original")
        original_time = request.data.get("original_time")
        original_place = request.data.get("original_place")
        source_name = request.data.get("source_name")
        source_link = request.data.get("source_link")

        if is_original:
            try:
                original_time = datetime_str_fmt_utc8(original_time)
            except (ValueError, TypeError):
                raise LogicException("原创必须增加拍摄时间")
            updated = PhotoItem.objects.filter(open_code=code).update(
                title=title,
                link=link,
                sort_no=sort_no,
                description=description,
                is_original=is_original,
                original_datetime=original_time,
                original_place=original_place)
        else:
            if source_link and source_name:
                updated = PhotoItem.objects.filter(open_code=code).update(
                    title=title,
                    link=link,
                    sort_no=sort_no,
                    description=description,
                    is_original=is_original,
                    source_name=source_name,
                    source_link=source_link)
            else:
                raise LogicException("非原创必须增加原链接和名称")

        return Response({"updated": updated})
예제 #19
0
 def change_pwd(self, password):
     """
         修改用户密码
     :param password:
     :return:
     """
     try:
         self.set_password(password)
         self.save()
         return 1
     except Exception as e:
         logger.error(e)
         raise LogicException(f'修改失败:未知错误修改失败')
예제 #20
0
def get_article_detail(request):

    code = request.GET.get("code", None)

    if request.user.is_authenticated:
        user_id = request.user.id
    else:
        user_id = None

    ve = ValidateException()

    if not code:
        ve.add_message("code", "invalid")

    if ve.is_error():
        raise ve

    article = Article.objects.select_related("author")\
        .filter(open_code=code).defer(
        'description',
        'author__avatar',
        'author__avatar_change_date',
        'author__nickname_change_date',
        'author__remark_name',
        'author__date_joined',
        'author__gender',
        'author__register_type',
        'author__is_cms',
        'author__is_active',
        'author__is_staff'
    ).first()

    if article is None:
        raise LogicException("无效的文章码")

    if user_id:
        is_like = LikeArticleRelation.check_user_like_article(
            user_id=user_id, article_id=article.id)
    else:
        is_like = False

    article_ser = ShowArticleDetailSerializers(article,
                                               context={
                                                   "is_like": is_like
                                               }).data

    # 使用消息队列记录访问log
    article_visit_add.delay(user_id=user_id, article_id=article.pk)

    return Response(data=article_ser)
예제 #21
0
    def post(self, request):
        code = request.data.get("code")
        date = request.data.get("date")
        title = request.data.get("title")
        content = request.data.get("content")

        timeline = TimelineLine.objects.filter(open_code=code).first()
        if timeline is None:
            raise LogicException(f"code为{code}的时间线不存在")

        if timeline.year != date.year:
            raise LogicException(f"不能在年份为{timeline.year}创建的{date}节点项")

        if TimelineItem.objects.filter(date=date).exists():
            raise LogicException(f"{date}的时间项已经存在了")

        timeline_item = TimelineItem.objects.create(
            date=date,
            title=title,
            content=content,
            timeline=timeline
        )
        return Response({"created": timeline_item.code})
예제 #22
0
    def post(self, request):
        year = request.data.get("year")
        title = request.data.get("title")
        content = request.data.get("content")

        if TimelineLine.objects.filter(year=year).exists():
            raise LogicException(f"{year}年份的时间线已经存在了")

        timeline = TimelineLine.objects.create(
            year=year,
            title=title,
            content=content
        )
        return Response({"created": timeline.code})
예제 #23
0
def web_login(request):
    username = request.data.get("username", None)
    password = request.data.get("password", None)

    if request.user.is_authenticated:
        raise LogicException("你已经登录了")

    ve = ValidateException()
    if not username:
        ve.add_message("username", "null")
    if not password:
        ve.add_message("password", "null")

    if ve.is_error():
        raise ve

    user = authenticate(request, username=username, password=password)

    if user is None:
        raise LogicException("登录失败,请检查账号密码是否正确")

    login(request, user)
    return Response(user.display_info)
예제 #24
0
    def post(self, request):
        title = request.data.get("title")
        link = request.data.get("link")
        sort_no = request.data.get("sort_no")
        description = request.data.get("description")
        is_original = request.data.get("is_original")
        original_time = request.data.get("original_time")
        original_place = request.data.get("original_place")
        source_name = request.data.get("source_name")
        source_link = request.data.get("source_link")

        if is_original:
            try:
                original_time = datetime_str_fmt_utc8(original_time)
            except (ValueError, TypeError):
                raise LogicException("原创必须增加拍摄时间")
            photo = PhotoItem(title=title,
                              link=link,
                              sort_no=sort_no,
                              description=description,
                              is_original=is_original,
                              original_datetime=original_time,
                              original_place=original_place)
        else:
            if source_link and source_name:
                photo = PhotoItem(title=title,
                                  link=link,
                                  sort_no=sort_no,
                                  description=description,
                                  is_original=is_original,
                                  source_name=source_name,
                                  source_link=source_link)
            else:
                raise LogicException("非原创必须增加原链接和名称")

        photo.save()
        return Response({"created": photo.code})
예제 #25
0
 def add_article_comment_by_code(cls, user_id, article_code, content):
     """
         增加评论 通过code
     :param user_id:
     :param article_code:
     :param content:
     :return:
     """
     article = Article.objects.filter(
         open_code=article_code).only("id").first()
     if article is None:
         raise LogicException("无效的文章code")
     comment = cls(article_id=article.id, user_id=user_id, content=content)
     comment.save()
     return comment.code, article.id
예제 #26
0
    def put(self, request):
        open_id = request.data.get("open_id")
        group_list = request.data.get("group_list")

        account = User.objects.filter(oid=open_id).first()

        if account is None:
            raise LogicException(f"没有open_id为{open_id} 的账号")

        groups = Group.objects.filter(name__in=group_list)
        if group_list:
            account.groups.set(groups)
        else:
            account.groups.clear()

        return Response({"updated": True})
예제 #27
0
파일: cms.py 프로젝트: shihbas/blog-yuehu
def get_all_tag_in_top(request):
    """
        根据顶级标签查询该标签组下的所有标签
    :param request:
    :return:
    """
    code = request.GET.get("code")

    top_tag = ArticleTag.objects.filter(level=0, open_code=code).first()

    if top_tag is None:
        raise LogicException("没有该顶级标签code")

    tags = ArticleTag.objects.filter(Q(root_tag=top_tag) | Q(id=top_tag.id))
    tags_ser = CMSUsualArticleTagSerializers(tags, many=True)
    return Response(tags_ser.data)
예제 #28
0
파일: cms.py 프로젝트: shihbas/blog-yuehu
    def get(self, request):
        user = request.user
        code = request.GET.get("code")

        article = Article.objects.filter(
            author=user,
            open_code=code,
            status__in=article_constant.CMS_SELF_ARTICLE_SHOW_STATUS_LIST
        ).first()

        if article is None:
            raise LogicException("这不是你的文章呦")

        article_ser = CMSArticleDetailSerializers(article)

        return Response(article_ser.data)
예제 #29
0
    def put(self, request):
        name = request.data.get("name")
        permission_list = request.data.get("permission_list")

        group = Group.objects.filter(name=name).first()

        if group is None:
            raise LogicException(f"名称为{name}的权限组找不到")

        permissions = Permission.objects.filter(codename__in=permission_list)

        if permissions:
            group.permissions.set(permissions)
        else:
            group.permissions.clear()

        return Response({"updated": True})
예제 #30
0
    def delete(self, request):
        open_id = request.GET.get("open_id")

        account = User.objects.filter(oid=open_id).first()
        if account is None:
            raise LogicException(f"没有open_id为{open_id} 的账号")

        try:
            deleted = True
            with transaction.atomic():
                account.groups.clear()
                account.is_cms = False
                account.save()
        except Exception as e:
            logger.error(e)
            deleted = False

        return Response({"deleted": deleted})