Exemple #1
0
    def post(self, *args, **kwargs):
        rep = BaseResponse()

        news_id = self.get_argument('news_id', None)
        if not news_id:
            rep.summary = "新闻ID不能为空."
        else:
            user_info_id = self.session['user_info']['nid']
            conn = ORM.session()
            has_favor = conn.query(ORM.Favor).filter(
                ORM.Favor.user_info_id == user_info_id,
                ORM.Favor.news_id == news_id).count()
            if has_favor:
                conn.query(ORM.Favor).filter(
                    ORM.Favor.user_info_id == user_info_id,
                    ORM.Favor.news_id == news_id).delete()
                conn.query(ORM.News).filter(ORM.News.nid == news_id).update(
                    {"favor_count": ORM.News.favor_count - 1},
                    synchronize_session="evaluate")
                rep.code = StatusCodeEnum.FavorMinus
            else:
                conn.add(
                    ORM.Favor(user_info_id=user_info_id,
                              news_id=news_id,
                              ctime=datetime.datetime.now()))
                conn.query(ORM.News).filter(ORM.News.nid == news_id).update(
                    {"favor_count": ORM.News.favor_count + 1},
                    synchronize_session="evaluate")
                rep.code = StatusCodeEnum.FavorPlus
            conn.commit()
            conn.close()

            rep.status = True

        self.write(json.dumps(rep.__dict__))
Exemple #2
0
    def post(self, *args, **kwargs):
        rep = response.BaseResponse()
        form = account.LoginForm()
        # print(USER_LIST, username)
        # self.render("cors.html", user_list=USER_LIST,news_list=NEWS_LIST)  # 重点坑渲染的时候带参数

        if form.valid(self):
            print(form._value_dict, self.session['CheckCode'])
            if form._value_dict['code'].lower() != self.session['CheckCode'].lower():
                rep.message = {'code': '验证码错误'}
                self.write(json.dumps(rep.__dict__))
                return
            conn = ORM.session()
            obj = conn.query(ORM.UserInfo).filter(
                or_(
                    and_(ORM.UserInfo.email == form._value_dict['user'],
                         ORM.UserInfo.password == form._value_dict['pwd']),
                    and_(ORM.UserInfo.username == form._value_dict['user'],
                         ORM.UserInfo.password == form._value_dict['pwd'])
                )
            ).first()
            if not obj:
                rep.message = {'user': '******'}
                self.write(json.dumps(rep.__dict__))
                return
            self.session['is_login'] = True
            self.session['user_info'] = obj.__dict__
            rep.status = True
        else:
            rep.message = form._error_dict

        self.write(json.dumps(rep.__dict__))
Exemple #3
0
    def get(self, page=1):

        conn = ORM.session()

        all_count = conn.query(ORM.News).count()

        obj = Pagination(page, all_count)

        current_user_id = self.session['user_info']['nid'] if self.session[
            'is_login'] else 0
        result = conn.query(
            ORM.News.nid, ORM.News.title, ORM.News.url, ORM.News.content,
            ORM.News.ctime, ORM.UserInfo.username, ORM.NewsType.caption,
            ORM.News.favor_count, ORM.News.comment_count,
            ORM.Favor.nid.label('has_favor')).join(
                ORM.NewsType,
                isouter=True).join(ORM.UserInfo, isouter=True).join(
                    ORM.Favor,
                    and_(ORM.Favor.user_info_id == current_user_id,
                         ORM.News.nid == ORM.Favor.news_id),
                    isouter=True)[obj.start:10]
        conn.close()

        str_page = obj.string_pager('/index/')

        self.render('home/index.html', str_page=str_page, news_list=result)
Exemple #4
0
    def post(self, *args, **kwargs):
        rep = response.BaseResponse()
        form = account.SendMsgForm()
        # print(email)
        if form.valid(self):
            email = form._value_dict['email']
            conn = ORM.session()

            has_exists_email = conn.query(ORM.UserInfo).filter(ORM.UserInfo.email == form._value_dict['email']).count()
            if has_exists_email:
                rep.summary = "此邮箱已经被注册"
                self.write(json.dumps(rep.__dict__))
                return
            current_date = datetime.datetime.now()
            code = commons.generate_verification_code()
            # print(code)

            count = conn.query(ORM.SendCode).filter_by(**form._value_dict).count()
            if not count:
                insert = ORM.SendCode(code=code,
                                      email=email,
                                      ctime=current_date)
                conn.add(insert)
                conn.commit()
                rep.status = True
                #首次注册
                message.send_email([email,],code)
            else:
                limit_day = current_date - datetime.timedelta(hours=1)
                times = conn.query(ORM.SendCode).filter(ORM.SendCode.email == email,
                                                        ORM.SendCode.ctime > limit_day,
                                                        ORM.SendCode.times >= 10,
                                                        ).count()
                if times:
                    rep.summary = "'已经超过今日最大次数(1小时后重试)'"
                else:
                    unfreeze = conn.query(ORM.SendCode).filter(ORM.SendCode.email == email,
                                                               ORM.SendCode.ctime < limit_day).count()
                    if unfreeze:
                        conn.query(ORM.SendCode).filter_by(email=email).update({"times": 0})

                    conn.query(ORM.SendCode).filter_by(email=email).update({"times": ORM.SendCode.times + 1,
                                                                            "code": code,
                                                                            "ctime": current_date},
                                                                           synchronize_session="evaluate")
                    conn.commit()
                    rep.status = True
                    # 再次注册
                    message.send_email([email,],code)
            conn.close()
        else:
            rep.summary = form._error_dict['email']
        self.write(json.dumps(rep.__dict__))
Exemple #5
0
    def post(self, *args, **kwargs):
        rep = response.BaseResponse()
        form = account.RegisterForm()
        if form.valid(self):
            current_date = datetime.datetime.now()
            limit_day = current_date - datetime.timedelta(minutes=1)
            conn = ORM.session()
            is_valid_code = conn.query(ORM.SendCode).filter(ORM.SendCode.email == form._value_dict['email'],
                                                            ORM.SendCode.code == form._value_dict['email_code'],
                                                            ORM.SendCode.ctime > limit_day).count()
            if not is_valid_code:
                rep.message['email_code'] = '邮箱验证码不正确或过期'
                self.write(json.dumps(rep.__dict__))
                return
            has_exists_email = conn.query(ORM.UserInfo).filter(ORM.UserInfo.email == form._value_dict['email']).count()
            if has_exists_email:
                rep.message['email'] = '邮箱已经存在'
                self.write(json.dumps(rep.__dict__))
                return
            has_exists_username = conn.query(ORM.UserInfo).filter(
                ORM.UserInfo.username == form._value_dict['username']).count()
            if has_exists_username:
                rep.message['email'] = '用户名已经存在'
                self.write(json.dumps(rep.__dict__))
                return
            form._value_dict['ctime'] = current_date
            form._value_dict.pop('email_code')
            # 优化下插入的数据。去掉emai_code 加入创建时间
            obj = ORM.UserInfo(**form._value_dict)#传了个字典,和= 一样的。

            conn.add(obj)
            conn.flush()
            conn.refresh(obj)

            user_info_dict = {'nid': obj.nid, 'email': obj.email, 'username': obj.username}

            conn.query(ORM.SendCode).filter_by(email=form._value_dict['email']).delete()#删掉发送表中的记录
            conn.commit()
            conn.close()

            self.session['is_login'] = True
            self.session['user_info'] = user_info_dict#把用户信息放到session中
            rep.status = True

        else:
            rep.message = form._error_dict

        self.write(json.dumps(rep.__dict__))
Exemple #6
0
    def post(self, *args, **kwargs):
        rep = BaseResponse()

        form = IndexForm()
        if form.valid(self):
            # title,content,href,news_type,user_info_id

            input_dict = copy.deepcopy(form._value_dict)
            input_dict['ctime'] = datetime.datetime.now()
            input_dict['user_info_id'] = self.session['user_info']['nid']
            conn = ORM.session()
            conn.add(ORM.News(**input_dict))
            conn.commit()
            conn.close()
            rep.status = True
        else:
            rep.message = form._error_dict

        self.write(json.dumps(rep.__dict__))
Exemple #7
0
    def post(self, *args, **kwargs):
        rep = BaseResponse()

        form = CommentForm()

        if form.valid(self):
            form._value_dict['ctime'] = datetime.datetime.now()

            conn = ORM.session()
            obj = ORM.Comment(user_info_id=self.session['user_info']['nid'],
                              news_id=form._value_dict['news_id'],
                              reply_id=form._value_dict['reply_id'],
                              content=form._value_dict['content'],
                              up=0,
                              down=0,
                              ctime=datetime.datetime.now())

            conn.add(obj)
            conn.flush()
            conn.refresh(obj)

            rep.data = {
                'user_info_id': self.session['user_info']['nid'],
                'username': self.session['user_info']['username'],
                'nid': obj.nid,
                'news_id': obj.news_id,
                'ctime': obj.ctime.strftime("%Y-%m-%d %H:%M:%S"),
                'reply_id': obj.reply_id,
                'content': obj.content,
            }

            conn.query(ORM.News).filter(
                ORM.News.nid == form._value_dict['news_id']).update(
                    {"comment_count": ORM.News.comment_count + 1},
                    synchronize_session="evaluate")
            conn.commit()
            conn.close()

            rep.status = True
        else:
            rep.message = form._error_dict
        print(rep.__dict__)
        self.write(json.dumps(rep.__dict__))
Exemple #8
0
    def get(self, *args, **kwargs):
        # comment_list需要按照时间从小到大排列
        nid = self.get_argument('nid', 0)
        conn = ORM.session()
        comment_list = conn.query(
            ORM.Comment.nid,  #评论id
            ORM.Comment.content,  #回复内容
            ORM.Comment.reply_id,  #回复到
            ORM.UserInfo.username,
            ORM.Comment.ctime,
            ORM.Comment.up,
            ORM.Comment.down,
            ORM.Comment.news_id).join(
                ORM.UserInfo,
                isouter=True).filter(ORM.Comment.news_id == nid).all()

        conn.close()
        """
        comment_list = [
            (1, '111',None),
            (2, '222',None),
            (3, '33',None),
            (9, '999',5),
            (4, '444',2),
            (5, '555',1),
            (6, '666',4),
            (7, '777',2),
            (8, '888',4),
        ]
        """
        # comment_list-所有评论,是个列表
        # [(1, '1111', None, 'cjx', datetime.datetime(2017, 11, 16, 15, 49, 44), 0, 0, 1), (2, '222222', None, 'cjx', datetime.datetime(2017, 11, 16, 15, 49, 48), 0, 0, 1)]

        comment_tree = commons.build_tree(comment_list)

        self.render('include/comment.html', comment_tree=comment_tree)