Exemplo n.º 1
0
def create_read_notice(request):
	try:
		notice_id = int(request.GET['notice_id'])
		Notice.read_notice(notice_id)
		response = create_response(200)
	except:
		response = create_response(500)
		response.innerErrMsg = unicode_full_stack()	
	return response.get_response()
Exemplo n.º 2
0
def reply(request, topic_id):

    try:
        topic = Topic.objects.get(id=topic_id)
        comment_list = Comment.objects.filter(topic=topic).order_by('created_on')
    except Topic.DoesNotExist:
        raise Http404

    if request.method == 'POST':
        form = ReplyForm(request.POST)
        if form.is_valid():
            comment = form.save(commit=False)
            request.user.comment_num += 1
            request.user.calculate_au()
            request.user.save()
            comment.author = request.user

            try:
                topic = Topic.objects.get(id=topic_id)
            except Topic.DoesNotExist:
                raise Http404

            comment.topic = topic
            comment.save()

            # --- 解析@ ---

            #team_name_pattern = re.compile('(?<=@)(\w+)', re.UNICODE)
            team_name_pattern = re.compile('(?<=@)([0-9a-zA-Z.]+)', re.UNICODE)
            at_name_list = set(re.findall(team_name_pattern, comment.content))
            if at_name_list:
                for at_name in at_name_list:
                    if at_name != comment.author.username and at_name != comment.topic.author.username:
                        try:
                            at_user = User.objects.get(username=at_name)
                            if at_user:
                                notice = Notice(from_user=comment.author,to_user=at_user    ,topic=comment.topic,content=comment.content)
                                notice.save()
                        except:
                            pass

            # --- 解析@ ---

            topic.num_comments += 1
            topic.updated_on = timezone.now()
            topic.last_reply = request.user
            topic.save()
            return HttpResponseRedirect(reverse("bbs:topic" ,args=(topic_id,)))
    else:
        form = ReplyForm()

    return render(request,"bbs/topic.html",{"node":node,"topic":topic,"form":form,"comment_list":comment_list})
Exemplo n.º 3
0
def new(request, node_slug):
    context = {}
    try:
        node = Node.objects.get(slug=node_slug)
    except Node.DoesNotExist:
        raise Http404

    if request.method == 'POST':
        form = TopicForm(request.POST)
        if form.is_valid():
            last_topic = Topic.objects.filter(author=request.user).order_by('-created_on')[:1]
            last_topic = last_topic.first()
            if last_topic and last_topic.title == form.clean()['title'] and ((timezone.now()-last_topic.created_on).seconds < 10):
                messages.error(request,'你是否正在尝试连续提交两次相同的内容?');
                return HttpResponseRedirect(reverse("bbs:topic" ,args=(last_topic.id,)))
            else:
                topic = form.save(commit=False)
                topic.node = node
                request.user.topic_num += 1
                request.user.calculate_au()
                request.user.save()
                topic.author = request.user
                topic.last_reply = request.user
                topic.updated_on = timezone.now()
                topic.save()
                node.num_topics += 1
                node.save()

                # --- 解析@ ---

                team_name_pattern = re.compile('(?<=@)(\w+)', re.UNICODE)
                at_name_list = set(re.findall(team_name_pattern, topic.content))
                if at_name_list:
                    for at_name in at_name_list:
                        if at_name != topic.author.username:
                            try:
                                at_user = User.objects.get(username=at_name)
                                if at_user:
                                    notice = Notice(from_user=topic.author,to_user=at_user,topic=topic,content='')
                                    notice.save()
                            except:
                                pass

                # --- 解析@ ---

                return HttpResponseRedirect(reverse("bbs:topic" ,args=(topic.id,)))
    else:
        form = TopicForm()

    return render(request,'bbs/new.html',{'node':node,'form':form})
def write_notice():
    if request.method == 'GET':
        return render_template('write_notice.html')
    else:
        # 写入数据库
        title = request.form.get('title')
        content = request.form.get('content')
        school = session.get('school')
        notice = Notice(title=title, content=content, school=school)
        user_id = session.get('user_id')  # 获取id
        user = User.query.filter(User.id == user_id).first()
        notice.author = user
        db.session.add(notice)
        db.session.commit()
        return redirect(url_for('notice'))
Exemplo n.º 5
0
def new(request, node_slug):
    context = {}
    try:
        node = Node.objects.get(slug=node_slug)
    except Node.DoesNotExist:
        raise Http404

    if request.method == 'POST':
        form = TopicForm(request.POST)
        if form.is_valid():
            topic = form.save(commit=False)
            topic.node = node
            request.user.topic_num += 1
            request.user.calculate_au()
            request.user.save()
            topic.author = request.user
            topic.last_reply = request.user
            topic.updated_on = timezone.now()
            topic.save()
            node.num_topics += 1
            node.save()

            # --- 解析@ ---

            team_name_pattern = re.compile('(?<=@)(\w+)', re.UNICODE)
            at_name_list = set(re.findall(team_name_pattern, topic.content))
            if at_name_list:
                for at_name in at_name_list:
                    if at_name != topic.author.username:
                        try:
                            at_user = User.objects.get(username=at_name)
                            if at_user:
                                notice = Notice(from_user=topic.author,to_user=at_user,topic=topic,content='')
                                notice.save()
                        except:
                            pass

            # --- 解析@ ---

            return HttpResponseRedirect(reverse("bbs:topic" ,args=(topic.id,)))
    else:
        form = TopicForm()

    return render(request,'bbs/new.html',{'node':node,'form':form})
Exemplo n.º 6
0
def get_land_notice(request):
	try:
		landing_notices = list(Notice.objects.filter(owner_id=1))
		response = create_response(200)
		response.data.notices = Notice.notices_json_array(landing_notices)
	except:
		response = create_response(500)
		response.errMsg = u'系统繁忙,请稍候重试'
		response.innerErrMsg = unicode_full_stack()

	return response.get_jsonp_response(request)
Exemplo n.º 7
0
def addNotice():
    if request.method == 'GET':
        return render_template('T_add_notice.html')
    else:
        title = request.form.get('title')
        content = request.form.get('content')
        with app.app_context():
            notice = Notice(title=title, content=content)
            db.session.add(notice)
            # 事务提交
            db.session.commit()
        return redirect(url_for('noticeManage'))
Exemplo n.º 8
0
def like_shell():
    session = db_session()
    shell_id = request.json.get("shell_id")
    type = request.json.get("type")
    receiver_id = session.query(Shell).filter(Shell.id == shell_id).first().user_id
    # type = 'like'
    sender_id = g.user.id
    token = g.user.generate_auth_token().decode('ascii')
    notice = Notice(sender_id=sender_id, receiver_id=receiver_id, type=type)
    usr = session.query(UserShellRelationship).filter(UserShellRelationship.user_id == sender_id,
                                                      UserShellRelationship.shell_id == shell_id).first()
    if usr is None:
        usr = UserShellRelationship(user_id=sender_id, shell_id=shell_id, type=type)
        session.add(usr)
        session.add(notice)
    elif usr.type == type:
        print "delete!"
        session.query(Notice).filter(Notice.sender_id == sender_id, Notice.receiver_id == receiver_id,
                                     Notice.type == type).delete(synchronize_session=False)
        session.delete(usr)
    elif usr.type == "like_collection":
        session.query(Notice).filter(Notice.sender_id == sender_id, Notice.receiver_id == receiver_id,
                                     Notice.type == type).delete(synchronize_session=False)
        usr.type = "like" if type == "collection" else "collection"
    else:
        usr.type = 'like_collection'
        session.add(notice)
    try:
        session.commit()
    finally:
        session.close()
    return jsonify({
        'user_shell_relationship': {
            'id': sender_id,
            'token': token,
            'shell_id': shell_id
        },
        'info': generate_return_info(1, '操作成功')
    })
Exemplo n.º 9
0
def reply(request, topic_id):

    try:
        topic = Topic.objects.get(id=topic_id)
        # comment ---
        comment_list = Comment.objects.filter(topic=topic).order_by('created_on')
        paginator = Paginator(comment_list, NUM_COMMENT_PER_PAGE)
        page = request.GET.get('page')
        if page == None:
            page = paginator.num_pages

        try:
            comment_list = paginator.page(page)
        except PageNotAnInteger:
            comment_list = paginator.page(1)
        except EmptyPage:
            comment_list = paginator.page(paginator.num_pages)
        # comment ---
    except Topic.DoesNotExist:
        raise Http404

    if request.method == 'POST':
        form = ReplyForm(request.POST)
        if form.is_valid():
            last_comment = Comment.objects.filter(author=request.user).order_by('-created_on')[:1]
            last_comment = last_comment.first()
            if last_comment and last_comment.content == form.clean()['content'] and ((timezone.now()-last_comment.created_on).seconds < 10):
                messages.error(request,'你是否正在尝试连续提交两次相同的回复?');
            else:
                comment = form.save(commit=False)
                request.user.comment_num += 1
                request.user.calculate_au()
                request.user.save()
                comment.author = request.user

                try:
                    topic = Topic.objects.get(id=topic_id)
                except Topic.DoesNotExist:
                    raise Http404

                comment.topic = topic
                comment.save()

                # --- 解析@ ---

                #team_name_pattern = re.compile('(?<=@)(\w+)', re.UNICODE)
                team_name_pattern = re.compile('(?<=@)([0-9a-zA-Z_.]+)', re.UNICODE)
                at_name_list = set(re.findall(team_name_pattern, comment.content))
                if at_name_list:
                    for at_name in at_name_list:
                        if at_name != comment.author.username and at_name != comment.topic.author.username:
                            try:
                                at_user = User.objects.get(username=at_name)
                                if at_user:
                                    notice = Notice(from_user=comment.author,to_user=at_user    ,topic=comment.topic,content=comment.content)
                                    notice.save()
                            except:
                                pass

                # --- 解析@ ---

                topic.num_comments += 1
                topic.updated_on = timezone.now()
                topic.last_reply = request.user
                topic.save()
                return HttpResponseRedirect(reverse("bbs:topic" ,args=(topic_id,)))
    else:
        form = ReplyForm()

    return render(request,"bbs/topic.html",{"node":node,"topic":topic,"form":form,"comment_list":comment_list,'paginator':paginator})
Exemplo n.º 10
0
    username = profile['username']
    mail = profile['mail']
    password = generate_password_hash('123')
    # Save in database
    my_user = User(username=username, mail=mail, password=password)
    db.session.add(my_user)

print('Users created')

# Make 1000 fake news
for num in range(1000):
    title = fake.sentence()
    url = fake.uri()
    user_id = randint(1, 100)
    # Save in database
    my_notice = Notice(title=title, url=url, user_id=user_id)
    db.session.add(my_notice)

print('News created')

# Make 10000 fake comments
for num in range(10000):
    text = fake.text()
    notice_id = randint(1, 1000)
    user_id = randint(1, 100)
    # Save in database
    my_comment = Comment(text=text, notice_id=notice_id, user_id=user_id)
    db.session.add(my_comment)

print('Comments created')