Exemple #1
0
def update_topic_route(request, topic_id):
    """
    Update the topic.
    - Only the name can be changed.
    - Only by original author.
    """

    current_user = get_current_user(request)
    if not current_user:
        return abort(401)

    # ## STEP 1) Find existing topic instance ## #
    topic = Topic.get(id=topic_id)
    if not topic:
        return abort(404)
    if topic['user_id'] != current_user['id']:
        return abort(403)

    # ## STEP 2) Limit the scope of changes ## #
    topic_data = request['params']['topic']
    topic_data = pick(topic_data, ('name',))

    # ## STEP 3) Validate and save topic instance ## #
    topic, errors = topic.update(topic_data)
    if errors:
        errors = prefix_error_names('topic.', errors)
        return 400, {
            'errors': errors,
            'ref': 'k7ItNedf0I0vXfiIUcDtvHgQ',
        }

    # ## STEP 4) Return response ## #
    return 200, {'topic': topic.deliver()}
Exemple #2
0
def detail(id):
    t = Topic.get(id)
    print('t.created_time', t.created_time)
    # 传递 topic 的所有 reply 到 页面中
    u = current_user()
    board_id = int(request.args.get('board_id', -1))
    return render_template("topic/detail.html", topic=t, user=u, bid=board_id)
Exemple #3
0
def detail(id):
    board_id = int(request.args.get('board_id', -1))
    board = Board.one(id=board_id)
    log("topic detail:", board_id, board)
    m = Topic.get(id)

    return render_template("topic/detail.html", topic=m, board=board)
Exemple #4
0
def detail(id):
    m = Topic.get(id)
    # _u = m.get('user_id')
    u = m.user()
    # print(u)
    # 传递 topic 的所有 reply 到 页面中
    return render_template("topic/detail.html", topic=m, username=u)
Exemple #5
0
def collect_delete(id):
    m = Topic.get(id)
    u = current_user()
    collect_item = Collect.one(topic_id=m.id, user_id=u.id)
    Collect.delete(collect_item.id)
    c = 'n'
    return render_template("topic/detail.html", topic=m, u=u, c=c)
Exemple #6
0
def detail(id):
    t = Topic.get(id)
    if t is not None:
        token = new_csrf_token()
        return render_template("topic/detail.html", topic=t, token=token)
    else:
        return abort(404)
Exemple #7
0
def detail(id):
    m = Topic.get(id)
    u = current_user()
    c = 'n'  # 未收藏
    if Collect.one(topic_id=m.id, user_id=u.id) is not None:
        c = 'c'
    return render_template("topic/detail.html", topic=m, u=u, c=c)
Exemple #8
0
def detail(id):

    v = cache.get("site_translate_list").decode('utf-8')
    tranlates = json.loads(v)
    m = Topic.get(id)
    now_time = int(time.time())
    diff = now_time - int(m.created_time)
    diff = round(diff / 60 / 60)
    u = current_user()

    now_time = datetime.datetime.now()
    created_time = datetime.datetime.fromtimestamp(int(m.created_time))
    delta = now_time - created_time
    hours = round(delta.total_seconds() // 3600 - delta.days * 24)
    minutes = round((delta.total_seconds() % 3600) // 60)
    seconds = round(delta.total_seconds() - minutes * 60)
    if delta.days > 0:
        diff = "{}日{}時間".format(delta.days, hours)
    elif hours > 0 and delta.days <= 0:
        # diff = delta.days + "時間" + delta.minutes + "分"
        diff = "{}時間{}分".format(hours, minutes)
    elif minutes > 0 and hours <= 0:
        # diff = delta.minutes + "分" + delta.seconds + "秒"
        diff = "{}分{}秒".format(minutes, seconds)
    elif delta.minutes <= 0:
        # diff = delta.seconds + "秒"
        diff = "{}秒".format(delta.seconds)

    # 传递 topic 的所有 reply 到 页面中
    return render_template("topic/detail.html",
                           topic=m,
                           user=u,
                           time=diff,
                           tranlates=tranlates)
Exemple #9
0
    def save(self):
        """
        Overwrite save method to add to Elasticsearch.
        """

        # TODO-2 should we validate the save worked before going to ES?

        from models.topic import Topic
        from models.user import User

        data = json_prep(self.deliver())
        topic = Topic.get(id=self['topic_id'])
        if topic:
            data['topic'] = json_prep(topic.deliver())
        user = User.get(id=self['user_id'])
        if user:
            data['user'] = json_prep(user.deliver())

        es.index(
            index='entity',
            doc_type='post',
            body=data,
            id=self['id'],
        )
        return super().save()
Exemple #10
0
def detail(id):
    t = Topic.get(id)
    json = t.json()
    json['user'] = get_user_data(t.user_id)
    b = Board.one(id=json['board_id'])
    json['board_name'] = b.name
    return jsonify(json)
Exemple #11
0
def detail(id):
    m = Topic.get(id)
    bid = int(m.board_id)
    b = Board.one(id=bid)
    u = current_user()
    # 传递 topic 的所有 reply 到 页面中
    return render_template("topic/detail.html", topic=m, board=b, user=u)
Exemple #12
0
def detail(id):
    topic = Topic.get(id)
    board = topic.board()
    user = topic.user()
    return render_template("topic/detail.html",
                           topic=topic,
                           board=board,
                           user=user)
Exemple #13
0
def detail(id):
    m = Topic.get(id)
    user_id = m.user_id
    u = User.get(user_id)
    b = Board.find(m.board_id)
    return render_template('/topic/detail.html',
                           topic=m,
                           user=u,
                           board_title=b.title)
Exemple #14
0
def detail(id):
    m = Topic.get(id)
    u = current_user()
    if u is None:
        token = ''
    else:
        token = new_csrf_token()
    # 传递 topic 的所有 reply 到 页面中
    return render_template("topic/detail.html", topic=m, u=u, token=token)
Exemple #15
0
def detail(id):
    m = Topic.get(id)
    board_id = m.board_id
    print('board id: {}'.format(board_id))
    print('type board id: {}'.format(type(board_id)))
    board = Board.get(board_id)
    print('({})'.format(board))
    # 传递 topic 的所有 reply 到 页面中
    return render_template("topic/detail.html", topic=m, board=board)
Exemple #16
0
def detail(id):
    u = current_user()
    m = Topic.get(id)
    other_topic = m.other_topic()

    return render_template("topic/detail.html",
                           user=u,
                           topic=m,
                           other_topic=other_topic)
Exemple #17
0
def detail(id):
    t = time.time()
    m = Topic.get(id)
    bid = m.board_id
    uid = m.user_id
    b = Board.find_by(id=bid)
    u = User.find_by(id=uid)
    # 传递 topic 的所有 reply 到 页面中
    return render_template("topic/detail.html", topic=m, u=u, b=b, t=t)
Exemple #18
0
def detail(id):
    m = Topic.get(id)
    u = current_user()
    # 传递 topic 的所有 reply 到 页面中
    return render_template(
        "topic/detail.html",
        topic=m,
        user=u,
    )
Exemple #19
0
def detail(t_id):
    # get调用了find_by方法,同时浏览数+1
    t = Topic.get(t_id)
    if t is None:
        abort(404)
    else:
        u = t.user()
        # 传递 topic 的所有 reply 到 页面中
        return render_template("topic/detail.html", topic=t, author=u)
Exemple #20
0
def detail(id):
    u = current_user()
    m = Topic.get(id)
    # 传递 topic 的所有 reply 到 页面中
    liked = m.liked(u.id)
    return render_template("topic/detail.html",
                           topic=m,
                           user=u,
                           liked=liked,
                           id=m.id)
Exemple #21
0
def detail(id):
    m = Topic.get(id)
    t = format_time(m)
    board_id = int(request.args.get('board_id', -1))
    # 传递 topic 的所有 reply 到 页面中
    return render_template("topic/detail.html",
                           topic=m,
                           t=t,
                           u=current_user(),
                           bid=board_id)
Exemple #22
0
def detail(id):
    m = Topic.get(id)
    u = User.one(id=m.user_id)
    b = m.board()
    token = new_csrf_token()
    return render_template("topic/detail.html",
                           topic=m,
                           user=u,
                           board=b,
                           csrf_token=token)
Exemple #23
0
def detail(id):
    m: Topic = Topic.get(id)
    data = dict(
        id=m.id,
        user_id=m.user_id,
        title=m.title,
        content=m.content,
        views=m.views,
    )
    return response(data=data, status=status.HTTP_200_OK)
Exemple #24
0
Fichier : topic.py Projet : ql1/bbs
def detail(id):
    m = Topic.get(id)
    # some_day = time.time
    m.post_time = translate_time(m.update_time)
    user_id = m.user_id
    u = User.find_by(id = user_id)
    author = u.username
    b = Board.find(m.board_id)
    place = b.title
    return render_template('topic/detail.html',topic = m,author = author,place = place)
Exemple #25
0
def detail(id):
    # http://localhost:3000/topic/1
    m = Topic.get(id)
    u = current_user()
    # 不应该放在路由里面
    # m.views += 1
    # m.save()

    # 传递 topic 的所有 reply 到 页面中
    return render_template("topic/detail.html", topic=m, u=u)
Exemple #26
0
def detail(id):
	u = current_user()
	t = Topic.get(id)
	# 传递 topic 的所有 reply 到 页面中
	user = User.get(id=t.user_id)
	board = Board.get(id=t.board_id)
	if u is None:
		login = False
		return render_template('topic/detail.html', topic=t, user=user, board=board, login=login)
	login = True
	return render_template('topic/detail.html', topic=t, user=user, board=board, login=login, my=u)
Exemple #27
0
def detail():
    result = request.args.get('result', ' ')
    topic_id = request.args['id']
    m = Topic.get(topic_id)
    rs = Reply.all(topic_id=topic_id)
    token = new_csrf_token()
    return render_template("topic/detail.html",
                           topic=m,
                           replies=rs,
                           token=token,
                           result=result)
Exemple #28
0
def detail(id):
    m = Topic.get(id)
    board = Board.get(m.board_id)
    u = User.get(m.user_id)
    board_title = board.title
    username = u.username
    # 传递 topic 的所有 reply 到 页面中
    return render_template("topic/detail.html",
                           topic=m,
                           board_title=board_title,
                           username=username)
Exemple #29
0
def edit_refresh():
    form = request.form
    u = current_user()
    if int(form.get('board_id', -1)) == -1:
        return abort(Response('您还未选择板块'))
    topic_id = form.get('topic_id', -1)
    t = Topic.get(topic_id)
    if t.user_id != u.id:
        return abort(Response('您无权限修改他人的话题'))
    t.edit(form)
    return redirect(url_for('.detail', id=t.id, u=u))
Exemple #30
0
def detail(id):
    t = Topic.get(id)
    b = Board.find_by(id=t.board_id)
    u = User.find_by(id=t.user_id)
    now = time.time()
    # 传递 topic 的所有 reply 到 页面中
    return render_template("topic/detail.html",
                           topic=t,
                           board=b,
                           user=u,
                           now=now)
Exemple #31
0
def detail(id):
    t = Topic.get(id)
    b_id = t.board_id
    b = Board.find_by(id=b_id)
    user_id = t.user_id
    auther = User.find(user_id)

    return render_template('topic/detail.html',
                           topic=t,
                           b_title=b.title,
                           auther=auther.username)
Exemple #32
0
def follow_route(request):
    """
    Follow a card, unit, or set.
    """

    # TODO-3 simplify this method. does some of this belong in the model?

    current_user = get_current_user(request)
    if not current_user:
        return abort(401)

    follow_data = dict(**request['params'])
    follow_data['user_id'] = current_user['id']

    follow = Follow(follow_data)
    errors = follow.validate()
    if errors:
        return 400, {
            'errors': errors,
            'ref': '4Qn9oWVWiGKvXSONQKHSy1T6'
        }

    # Ensure the entity exists   TODO-3 should this be a model validation?
    if follow['entity']['kind'] == 'topic':
        entity = Topic.get(id=follow['entity']['id'])
    else:
        entity = get_latest_accepted(follow['entity']['kind'],
                                     follow['entity']['id'])
    if not entity:
        return abort(404)

    # Ensure we don't already follow   TODO-3 should this be a model validation?
    prev = Follow.list(user_id=current_user['id'],
                       entity_id=follow_data['entity']['id'])
    if prev:
        return abort(409)

    follow, errors = follow.save()
    if errors:
        return 400, {
            'errors': errors,
            'ref': 'gKU6wgTItxpKyDs0eAlonCmi',
        }

    return 200, {'follow': follow.deliver(access='private')}
Exemple #33
0
def detail(id):
    m = Topic.get(id)
    # 传递 topic 的所有 reply 到 页面中
    return render_template("topic/detail.html", topic=m)
Exemple #34
0
def get_posts_route(request, topic_id):
    """
    Get a reverse chronological listing of posts for given topic.
    Includes topic meta data and posts (or proposals or votes).
    Paginates.
    """

    # Is the topic valid?
    topic = Topic.get(id=topic_id)
    if not topic:
        return 404, {
            'errors': [{
                'name': 'topic_id',
                'message': c('no_topic'),
            }],
            'ref': 'pgnNbqSP1VUWkOYq8MVGPrSS',
        }

    # Pull the entity
    entity_kind = topic['entity']['kind']
    entity = get_latest_accepted(entity_kind,
                                 topic['entity']['id'])

    # Pull all kinds of posts
    posts = get_posts_facade(
        limit=request['params'].get('limit') or 10,
        skip=request['params'].get('skip') or 0,
        topic_id=topic_id
    )

    # For proposals, pull up the proposal entity version
    # ...then pull up the previous version
    # ...make a diff between the previous and the proposal entity version
    diffs = {}
    entity_versions = {}
    for post_ in posts:
        if post_['kind'] == 'proposal':
            entity_version = entity_versions[post_['id']] = get_version(
                post_['entity_version']['kind'],
                post_['entity_version']['id']
            )
            previous_version = get_version(
                post_['entity_version']['kind'],
                entity_version['previous_id']
            )
            if previous_version:
                diffs[post_['id']] = object_diff(previous_version.deliver(),
                                                 entity_version.deliver())

    # TODO-2 SPLITUP create new endpoint for this instead
    users = {}
    for post_ in posts:
        user_id = post_['user_id']
        if user_id not in users:
            user = User.get(id=user_id)
            if user:
                users[user_id] = {
                    'name': user['name'],
                    'avatar': user.get_avatar(48)
                }

    # TODO-2 SPLITUP create new endpoints for these instead
    output = {
        'topic': topic.deliver(),
        'posts': [p.deliver() for p in posts],
        'entity_versions': {
            p: ev.deliver()
            for p, ev in entity_versions.items()
        },
        # 'diffs': diffs,  TODO-2 this causes a circular dependency
        'users': users,
    }
    if entity:
        output[entity_kind] = entity.deliver()
    return 200, output
Exemple #35
0
def create_post_route(request, topic_id):
    """
    Create a new post on a given topic.
    Proposal: must include entity (card, unit, or set) information.
    Vote: must refer to a valid proposal.
    """

    current_user = get_current_user(request)
    if not current_user:
        return abort(401)

    topic = Topic.get(id=topic_id)
    if not topic:
        return 404, {
            'errors': [{
                'name': 'topic_id',
                'message': c('no_topic'),
            }],
            'ref': 'PCSFCxsJtnlP0x9WzbPoKcwM',
        }

    # ## STEP 1) Create post (and entity) instances
    post_data = request['params'].get('post')
    if not post_data:
        return 400, {
            'errors': [{
                'name': 'post',
                'message': 'Missing post data.',
            }],
            'ref': 'ykQpZwJKq54MTCxgkx0p6baW'
        }
    post_data = omit(post_data, ('id', 'created', 'modified',))
    post_data['user_id'] = current_user['id']
    post_data['topic_id'] = topic_id
    post_ = instance_post_facade(post_data)
    post_kind = post_['kind']
    if post_kind == 'proposal':
        entity = instance_new_entity(request['params'])
        entity_kind = get_kind(request['params'])
        post_['entity_version'] = {
            'id': entity['id'],
            'kind': entity_kind,
        }

    # ## STEP 2) Validate post (and entity) instances
    errors = prefix_error_names('post.', post_.validate())
    if post_kind == 'proposal':
        errors = errors + prefix_error_names('entity.', entity.validate())
    if len(errors):
        return 400, {
            'errors': errors,
            'ref': 'tux33ztgFj9ittSpS7WKIkq7'
        }

    # ## STEP 3) Save post (and entity)
    post_.save()
    if post_kind == 'proposal':
        entity.save()

    # ## STEP 4) Add author as a follower
    Follow.insert({
        'user_id': current_user['id'],
        'entity': {
            'id': topic['id'],
            'kind': 'topic',
        }
    })
    # TODO-2 also follow the entity

    # ## STEP 5) Make updates based on proposal / vote status
    if post_kind == 'proposal':
        update_entity_status(post_)
    if post_kind == 'vote':
        proposal = Proposal.get(id=post_['replies_to_id'])
        update_entity_status(proposal)

    # ## STEP 6) Return response
    return 200, {'post': post_.deliver()}