Ejemplo n.º 1
0
 def post(form):
     topic = Topic()
     topic.title = form.title.data
     topic.content = html_clean(form.content.data)
     topic.is_markdown = True if form.choice.data == 1 else False
     topic.uid = make_uid()
     topic.author = current_user
     tags = sp(',|;|,|;| ', form.tags.data)
     tags = [x for x in list(set(tags)) if x != ''][:4]
     post_tags = []
     for tag in tags:
         if tag != '':
             exsit_tag = Tags.query.filter_by(tagname=tag).first()
             if exsit_tag is not None:
                 post_tags.append(exsit_tag)
                 if exsit_tag not in current_user.following_tags:
                     current_user.following_tags.append(exsit_tag)
             else:
                 t = Tags()
                 t.tagname = tag
                 post_tags.append(t)
                 current_user.following_tags.append(t)
     topic.tags = post_tags
     topic.board_id = form.category.data
     db.session.add(topic)
     db.session.commit()
     current_user.following_topics.append(topic)
     topic.board.count.topics += 1
     topic.board.count.all_topics += 1
     db.session.commit()
     RedisData.set_topics()
     return topic
Ejemplo n.º 2
0
def view(group):
    qid = request.args.get('qid')
    RedisData.set_read_count(qid)
    form = ReplyForm()
    question = Questions.load_by_id(qid)
    return render_template('question/content.html',
                           question=question,
                           form=form,
                           kind='group')
Ejemplo n.º 3
0
def view(group):
    qid = request.args.get('qid')
    RedisData.set_read_count(qid)
    form = ReplyForm()
    question = Questions.load_by_id(qid)
    return render_template('question/content.html',
                           question=question,
                           form=form,
                           kind='group')
Ejemplo n.º 4
0
 def get(self, uid):
     page = is_num(request.args.get('page'))
     if uid is None:
         topics = Topic.query.paginate(page,
                                       app.config['PER_PAGE'],
                                       error_out=True)
         return self.template_without_uid(topics)
     else:
         form = ReplyForm()
         topic = Topic.query.filter_by(uid=str(uid)).first_or_404()
         replies = topic.replies.paginate(page, 5, True)
         RedisData.set_read_count(topic.id)
         return self.template_with_uid(form, topic, replies)
Ejemplo n.º 5
0
 def get_detail(page, topicId, order='time'):
     topic = Topic.query.filter_by(uid=topicId).first_or_404()
     if order == 'like':
         replies = Reply.query.outerjoin(Like).filter(
             Reply.topic_id == topic.id).group_by(Reply.id).order_by(
                 func.count(Like.id).desc()).paginate(
                     page, current_app.config['PER_PAGE'], True)
     else:
         replies = Reply.query.filter_by(
             topic_id=topic.id).order_by(Reply.publish.asc()).paginate(
                 page, current_app.config['PER_PAGE'], True)
     RedisData.set_read_count(topic.id)
     return topic, replies
Ejemplo n.º 6
0
 def post_data(form, uid):
     reply = Reply()
     reply.content = form.content.data
     reply.author = current_user
     reply.topic_id = uid
     db.session.add(reply)
     db.session.commit()
     topic = reply.topic
     topic.board.count.all_topics += 1
     if topic.author_id != current_user.id:
         notice_reply(topic, reply)
     db.session.commit()
     RedisData.set_replies(uid)
     return reply
Ejemplo n.º 7
0
def question(class_url):
    error = None
    form = QuestionForm()
    fileform = PhotoForm()
    if form.validate_on_submit():
        title = form.title.data
        content = form.content.data
        choice = form.choice.data
        tags = form.tags.data
        tags = sp(',|;|,|;| ', tags)
        tags = list(set(tags))[:4]
        post_tags = []
        for tag in tags:
            if tag != '':
                t = Tags(author=current_user.name, name=tag)
                post_tags.append(t)
        question = Questions(title=title, content=content, kind=g.forums_url)
        question.tags = post_tags
        if choice == 'Markdown':
            question.is_markdown = True
        board = Board_S.load_by_name(class_url)
        board.count.topic += 1
        board.count.all_topic += 1
        board.board_f.count.topic += 1
        board.board_f.count.all_topic += 1
        question.board_id = board.id
        question.author_id = current_user.id
        current_user.infor.score -= 5
        '''随机赠送'''
        random_gift()
        db.session.add(question)
        db.session.commit()
        '''使用redis记录'''
        RedisData.set_question()
        RedisData.set_user()
        return jsonify(judge=True, error=error)
    else:
        if form.errors:
            return return_errors(form)
        else:
            pass
        board = Board_S.query.join(Board_F).\
            filter(Board_F.enname_f == g.forums_url).\
            filter(Board_S.enname_s == class_url).first_or_404()
        return render_template('question/question.html',
                               fileform=fileform,
                               form=form,
                               board=board)
Ejemplo n.º 8
0
def question(group):
    group = Group.load_by_name(group)
    form = QuestionForm()
    fileform = PhotoForm()
    error = None
    if form.validate_on_submit():
        title = form.title.data
        content = form.content.data
        choice = form.choice.data
        tags = form.tags.data
        tags = sp(',|;|,|;| ', tags)
        tags = list(set(tags))[:4]
        post_tags = []
        for tag in tags:
            if tag != '':
                t = Tags(author=current_user.name, name=tag)
                post_tags.append(t)
        question = Questions(title=title, content=content, kind=group.kind)
        question.tags = post_tags
        if choice == 'Markdown':
            question.is_markdown = True
        question.is_group = True
        question.group_id = group.id
        question.author_id = current_user.id
        group.count.topic += 1
        group.count.all_topic += 1
        current_user.infor.score -= 5
        '''随机赠送'''
        random_gift()
        db.session.add(question)
        db.session.commit()
        '''使用redis记录'''
        RedisData.set_question()
        RedisData.set_user()
        return jsonify(judge=True, error=error)
    else:
        if form.errors:
            return return_errors(form)
        else:
            pass
        return render_template('group/question.html',
                               group=group,
                               form=form,
                               fileform=fileform)
Ejemplo n.º 9
0
def question(group):
    group = Group.load_by_name(group)
    form = QuestionForm()
    fileform = PhotoForm()
    error = None
    if form.validate_on_submit():
        title = form.title.data
        content = form.content.data
        choice = form.choice.data
        tags = form.tags.data
        tags = sp(',|;|,|;| ', tags)
        tags = list(set(tags))[:4]
        post_tags = []
        for tag in tags:
            if tag != '':
                t = Tags(author=current_user.name, name=tag)
                post_tags.append(t)
        question = Questions(title=title, content=content, kind=group.kind)
        question.tags = post_tags
        if choice == 'Markdown':
            question.is_markdown = True
        question.is_group = True
        question.group_id = group.id
        question.author_id = current_user.id
        group.count.topic += 1
        group.count.all_topic += 1
        current_user.infor.score -= 5
        '''随机赠送'''
        random_gift()
        db.session.add(question)
        db.session.commit()
        '''使用redis记录'''
        RedisData.set_question()
        RedisData.set_user()
        return jsonify(judge=True, error=error)
    else:
        if form.errors:
            return return_errors(form)
        else:
            pass
        return render_template('group/question.html',
                               group=group,
                               form=form,
                               fileform=fileform)
Ejemplo n.º 10
0
def groups(group):
    group = Group.load_by_name(group)
    form = ApplyForm()
    if form.validate_on_submit():
        content = form.content.data
        letter = Message(send_user=current_user.name,
                         rece_user=group.admin,
                         kind='letter',
                         content=content)
        db.session.add(letter)
        db.session.commit()
        user = User.query.filter_by(name= group.name).first()
        RedisData.set_notice(user)
        return redirect(url_for('group.group', group=group.name))
    else:
        if form.errors:
            flash_errors(form)
            return redirect(url_for('group.group', group=group.name))
        else:
            pass
        abort(404)
Ejemplo n.º 11
0
def groups(group):
    group = Group.load_by_name(group)
    form = ApplyForm()
    if form.validate_on_submit():
        content = form.content.data
        letter = Message(send_user=current_user.name,
                         rece_user=group.admin,
                         kind='letter',
                         content=content)
        db.session.add(letter)
        db.session.commit()
        user = User.query.filter_by(name=group.name).first()
        RedisData.set_notice(user)
        return redirect(url_for('group.group', group=group.name))
    else:
        if form.errors:
            flash_errors(form)
            return redirect(url_for('group.group', group=group.name))
        else:
            pass
        abort(404)
Ejemplo n.º 12
0
def collect():
    if request.method == "POST":
        qid = request.values.get('qid')
        collect = Collector.load(qid, current_user.id)
        if collect is not None:
            db.session.delete(collect)
            db.session.commit()
            RedisData.set_collect(current_user, -1)
            return jsonify(judge=True)
        else:
            collect = Collector()
            collect.question_id = qid
            collect.user_id = current_user.id
            db.session.add(collect)
            db.session.commit()
            user = User.load_by_id(collect.user_id)
            if current_user.name != user.name:
                message = Message(send_user=current_user.name,
                                  rece_user=user.name,
                                  kind='collect',
                                  content='a')
                message.question_id = qid
                db.session.add(message)
                db.session.commit()
                RedisData.set_notice(user, 1)
            else:
                pass
            RedisData.set_collect(current_user, 1)
            return jsonify(judge=True)
    else:
        abort(404)
Ejemplo n.º 13
0
def love():
    '''点赞'''
    if request.method == "POST":
        rid = request.values.get('rid')
        print(rid)
        love = Lover.load(rid, current_user.id)
        if love is not None:
            db.session.delete(love)
            db.session.commit()
            RedisData.set_love(current_user, -1)
            flash('成功取消赞')
            return jsonify(judge=True)
        else:
            love = Lover()
            love.reply_id = rid
            love.user_id = current_user.id
            db.session.add(love)
            db.session.commit()
            user = User.load_by_id(love.user_id)
            if current_user.name != user.name:
                message = Message(send_user=current_user.name,
                                  rece_user=user.name,
                                  kind='love',
                                  content='a')
                message.reply_id = rid
                db.session.add(message)
                db.session.commit()
                RedisData.set_notice(user, 1)
            else:
                pass
            RedisData.set_love(current_user, 1)
            flash('赞成功')
            return jsonify(judge=True)
    else:
        abort(404)
Ejemplo n.º 14
0
def reply():
    error = None
    form = ReplyForm()
    if form.validate_on_submit() and request.method == "POST":
        qid = request.args.get('qid')
        qid = load_qid(qid)
        quote = request.get_json()['quote']
        content = form.content.data
        reply = Replies(content=content, quote=quote)
        reply.question_id = qid
        reply.author_id = current_user.id
        current_user.infor.score -= 1
        '''随机赠送'''
        random_gift()
        db.session.add(reply)
        db.session.commit()
        '''消息通知'''
        if current_user.name != reply.question.author.name:
            message = Message(send_user=current_user.name,
                              rece_user=reply.question.author.name,
                              kind='reply',
                              content=reply.content)
            message.reply_id = reply.id
            RedisData.set_notice(reply.question.author)
            db.session.add(message)
        reply.question.last_author = current_user.name
        reply.question.last_time = datetime.now()
        if reply.question.is_group:
            reply.question.group.count.all_topic += 1
        else:
            reply.question.board.count.all_topic += 1
            reply.question.board.board_f.count.all_topic += 1
        db.session.commit()
        '''使用redis记录'''
        RedisData.set_replies(qid)
        RedisData.set_user_all()
        return jsonify(judge=True, error=error)
    else:
        if form.content.errors:
            error = form.content.errors
            return jsonify(judge=False, error=error)
        else:
            pass
        return redirect(url_for('forums.forums'))
Ejemplo n.º 15
0
def get_forums_count():
    from maple.main.models import RedisData
    rd = RedisData()
    return rd.get_all_users(), rd.get_all_topics(), rd.get_all_replies()
Ejemplo n.º 16
0
 def reply_count(self, topic, reply):
     topic.board.count.all_topics += 1
     RedisData.set_replies(topic.id)
Ejemplo n.º 17
0
 def confirm_models(self, user):
     user.is_confirmed = True
     self.db.session.commit()
     RedisData.set_user()