Beispiel #1
0
def category_page(category_id):
    try:
        category_id = ObjectId(category_id)
        assert CategoryHelper.get(category_id)
    except :
        flash('invalid route_id')
        return redirect(url_for('home.home'))
    
    category = CategoryHelper.get(category_id)
    category.n_routes = len(category.routes)
    breadcrumb_list = [(category.title, category.id)]
    CategoryHelper.gene_bread(breadcrumb_list)

    son_routes = RouteHelper.remove_unfinished_route(CategoryHelper.get_son_routes(category.id))

    sons_hot_routes = RouteHelper.remove_unfinished_route(CategoryHelper.get_child_hot_route(category.id))

    son_categorys = CategoryHelper.get_son_categorys(category.id)

    return render_template(
        'route-father.html',
        breadcrumb_list=breadcrumb_list,
        category=category,
        son_routes=son_routes,
        sons_hot_routes=sons_hot_routes,
        son_categorys=son_categorys,
        get_category_helper=CategoryHelper.get
    )
Beispiel #2
0
    def is_route_finished(route_id):
        assert isinstance(route_id, ObjectId)
        assert RouteHelper.get(route_id)

        route = RouteHelper.get(route_id)
        entered_route = RouteHelper.get_entered_route(route_id)

        if not entered_route:
            return False

        return entered_route.percentage == 100
Beispiel #3
0
    def is_attach_finished(route_id, attach_id):
        assert isinstance(route_id, ObjectId)
        assert isinstance(attach_id, ObjectId)
        assert AttachmentHelper.get(attach_id)
        assert RouteHelper.get(route_id)

        route = RouteHelper.get(route_id)
        entered_route = RouteHelper.get_entered_route(route_id)

        if not entered_route:
            return False

        return attach_id in entered_route.attach_complete
Beispiel #4
0
    def finish_attach(route_id, attach_id):
        assert isinstance(route_id, ObjectId)
        assert isinstance(attach_id, ObjectId)
        assert AttachmentHelper.get(attach_id)
        assert RouteHelper.get(route_id)

        route = RouteHelper.get(route_id)
        entered_route = RouteHelper.get_entered_route(route_id)
        assert entered_route
        if attach_id in route.attached and attach_id not in entered_route.attach_complete:
            entered_route.attach_complete.append(attach_id)

        entered_route.percentage = len(entered_route.attach_complete) / float(route.n_attachment) * 100

        entered_route.save()
Beispiel #5
0
    def _recalculate_avg(route_id):
        assert isinstance(route_id, ObjectId)
        route = RouteHelper.get(route_id)
        assert route

        route.avg_rate = RateInfo.objects(route=route.id).average('score')
        route.save()
Beispiel #6
0
def rate(route_id):
    try:
        route_id = ObjectId(route_id)
        assert 'score' in request.form
        score = int(request.form['score'])
        assert RouteHelper.get(route_id)
    except AssertionError, e:
        return redirect(url_for('home.home'))
Beispiel #7
0
def finish_attach(route_id, attachment_id):
    try:
        route_id = ObjectId(route_id)
        attachment_id = ObjectId(attachment_id)
        assert RouteHelper.get(route_id)
        assert AttachmentHelper.get(attachment_id)
    except AssertionError, e:
        return redirect(url_for('home.home'))
Beispiel #8
0
    def get_user_routes(finished):
        assert isinstance(finished, bool)

        if finished:
            entered_routes = list(EnteredRoute.objects(Q(user=current_user.id) & Q(percentage=100)))
        else:
            entered_routes = list(EnteredRoute.objects(Q(user=current_user.id) & Q(percentage__ne=100)))

        routes = []
        for entered in entered_routes:
            routes.append(RouteHelper.get(entered.route))

        if not finished:
            for route in routes:
                route.percentage = RouteHelper.get_entered_route(route.id).percentage

        return routes
Beispiel #9
0
    def rate(route_id, score):
        assert isinstance(route_id, ObjectId)
        route = RouteHelper.get(route_id)
        assert route
        assert isinstance(score, int)

        rate_info = RateInfo.objects(Q(route=route_id) & Q(user=current_user.id)).first()
        if rate_info:
            rate_info.score = score
            rate_info.save()
        else:
            rate_info = RateInfo()
            rate_info.route = route_id
            rate_info.user = current_user.id
            rate_info.score = score
            rate_info.save()

        RouteHelper._recalculate_avg(route_id)
Beispiel #10
0
def finish_edit(route_id):
    try:
        route_id = ObjectId(route_id)
        assert RouteHelper.get(route_id)
        assert not RouteHelper.get(route_id).finished
    except :
        flash('invalid route_id')
        return redirect(url_for('home.home'))

    route = RouteHelper.get(route_id)
    
    if route.author != current_user.id:
        flash('you are not author of the route')
        return redirect(url_for('home.home'))

    RouteHelper.finish_edit(route.id)

    return redirect(url_for('route.route_page', route_id=route.id))
Beispiel #11
0
def add_attach(route_id):
    try:
        route_id = ObjectId(route_id)
        assert RouteHelper.get(route_id)
        assert not RouteHelper.get(route_id).finished
    except :
        flash('invalid route_id')
        return redirect(url_for('home.home'))

    route = RouteHelper.get(route_id)
    
    if route.author != current_user.id:
        flash('you are not the author of the route')
        return redirect(url_for('home.home'))

    try:
        check_form_para(['key', 'type'])
    except AssertionError, e:
        flash(e.message)
        return redirect(url_for('route.add_attach_page', route_id=route.id))
Beispiel #12
0
def like_attach(route_id, attachment_id):
    try:
        route_id = ObjectId(route_id)
        attachment_id = ObjectId(attachment_id)
        assert RouteHelper.get(route_id)
        assert AttachmentHelper.get(attachment_id)
        state, number = AttachmentHelper.toggle_vote(attachment_id)
    except AssertionError:
        return jsonify({'code': 0})

    return jsonify({'code': state, 'number': number})
Beispiel #13
0
    def delete(attach_id):
        from app.helpers.route import RouteHelper

        assert isinstance(attach_id, ObjectId)
        assert AttachmentHelper.get(attach_id)

        attach = AttachmentHelper.get(attach_id)
        f_route = RouteHelper.get(attach.route)
        f_route.attached.remove(attach_id)
        f_route.save()
        attach.delete()
Beispiel #14
0
    def get_route_stat(route_id):
        assert isinstance(route_id, ObjectId)
        route = RouteHelper.get(route_id)
        assert route

        stat = dict()
        stat['n_joined'] = EnteredRoute.objects(route=route.id).count()
        stat['n_completed'] = EnteredRoute.objects(Q(route=route.id) & Q(percentage=100)).count()
        stat['n_rate'] = RateInfo.objects(route=route.id).count()

        return stat
Beispiel #15
0
    def get_user_rate(route_id):
        assert isinstance(route_id, ObjectId)
        route = RouteHelper.get(route_id)
        assert route

        rate_info = RateInfo.objects(Q(route=route_id) & Q(user=current_user.id)).first()

        if rate_info:
            return rate_info.score
        else:
            return None
Beispiel #16
0
    def add(route_id, attach_type, key):
        from app.helpers.route import RouteHelper

        assert isinstance(route_id, ObjectId)
        assert RouteHelper.get(route_id)
        assert isinstance(attach_type, AttachType)

        # get book's info by isbn from douban
        if attach_type == AttachType.DOUBAN:
            r = requests.get(app.config['DOUBAN_ISBN_API'] + key)

            json_info = json.loads(r.text)
            if 'code' in json_info:
                assert json_info['code'] != 6000, u'未找到该书籍'

            info = r.text
        elif attach_type == AttachType.URL:
            req_paras = {
                'url': key,
                'key': app.config['EMBEDLY_KEY']
            }
            r = requests.get(app.config['EMBEDLY_EXTRACT_API'], params=req_paras)

            json_info = json.loads(r.text)
            assert json_info['type'] != 'error', u'无法获取相关网页信息'

            info = r.text
        else:
            info = key

        new_attach = Attachment()
        new_attach.route = route_id
        new_attach.atype = attach_type.value
        new_attach.info = info
        new_attach.save()

        f_route = RouteHelper.get(route_id)
        f_route.attached.append(new_attach.id)
        f_route.save()

        return new_attach
Beispiel #17
0
def delete_attach(route_id, attach_id):
    try:
        route_id = ObjectId(route_id)
        attach_id = ObjectId(attach_id)
        assert RouteHelper.get(route_id)
        assert AttachmentHelper.get(attach_id)
        assert not RouteHelper.get(route_id).finished
    except :
        flash('invalid route_id or attach_id')
        return redirect(url_for('home.home'))

    route = RouteHelper.get(route_id)
    
    if route.author != current_user.id:
        flash('you are not author of the route')
        return redirect(url_for('home.home'))

    AttachmentHelper.delete(attach_id)

    flash(u'删除成功')
    return redirect(url_for('route.add_attach_page', route_id=route.id))
Beispiel #18
0
def add_route(category_id):
    try:
        category_id = ObjectId(category_id)
        assert len(request.form['title']) > 0, 'please input title'
        assert 'content' in request.form, 'please input route content'
        assert isinstance(category_id, ObjectId)
        cate = CategoryHelper.get(category_id)
        assert cate

        new_route = RouteHelper.add(request.form['title'], cate.id, request.form['content'])
    except AssertionError, e:
        flash(e.message)
        return redirect(url_for('home.home'))
Beispiel #19
0
def route_page(route_id):
    try:
        route_id = ObjectId(route_id)
    except :
        flash('invalid route_id')
        return redirect(url_for('home.home'))
    
    route = RouteHelper.get(route_id)

    route.author = UserHelper.get(route.author)
    route.formatted_time = get_formatted_time(route.create_ts)
    route.stat = RouteHelper.get_route_stat(route.id)
    route.cleaned_content = route.content.read().decode('utf-8')  # TODO: use bleach to sanitise html
    route.my_rate = RouteHelper.get_user_rate(route.id)
    attachs = RouteHelper.get_attachs(route.id)
    joined = route.id in current_user.entered_routes
    finished = RouteHelper.is_route_finished(route.id)

    if not route.finished:
        return redirect(url_for('route.add_attach_page', route_id=route.id))

    return render_template('route-detail.html', route=route, attachs=attachs, joined=joined, finished=finished)
Beispiel #20
0
    def add_route(category_id, route_id):
        assert isinstance(category_id, ObjectId)
        assert CategoryHelper.get(category_id)
        assert isinstance(route_id, ObjectId)
        assert RouteHelper.get(route_id)

        category = CategoryHelper.get(category_id)

        category.routes.append(route_id)
        category.routes = list(set(category.routes))
        category.save()

        return category
Beispiel #21
0
def add_attach_page(route_id):
    try:
        route_id = ObjectId(route_id)
        assert RouteHelper.get(route_id)
        assert not RouteHelper.get(route_id).finished
    except :
        flash('invalid route_id')
        return redirect(url_for('home.home'))

    route = RouteHelper.get(route_id)

    if route.author != current_user.id:
        flash('you are not the author of the route')
        return redirect(url_for('home.home'))

    attachs = []
    for attach_id in route.attached:
        attachment = AttachmentHelper.get(ObjectId(attach_id))
        if attachment.atype != AttachType.TEXT.value:
            attachment.info = json.loads(attachment.info)
        attachs.append(attachment)
    
    return render_template('create-attach.html', route=route, attachs=attachs)
Beispiel #22
0
    def clear(route_id):
        from app.helpers.route import RouteHelper
        assert isinstance(route_id, ObjectId)
        route = RouteHelper.get(route_id)
        assert route
        assert not route.finished

        for attach_id in route.attached:
            AttachmentHelper.delete(attach_id)

        while len(route.attached) > 0:
            route.attached.pop()

        route.save()
Beispiel #23
0
    def get_child_hot_route(category_id):
        """ Give a catetory, find the hotest route in its sons."""
        assert isinstance(category_id, ObjectId)
        assert CategoryHelper.get(category_id)

        category = CategoryHelper.get(category_id)

        hot_routes = []
        if category.sons:
            for son_cate in category.sons:
                son_hot_route_id = CategoryHelper._recursive_find_hot_route(son_cate)
                if son_hot_route_id:
                    son_hot_route = RouteHelper.get(son_hot_route_id)
                    son = CategoryHelper.get(son_cate)
                    hot_routes.append({
                        'category_title': son.title,
                        'category_id': son.id,
                        'route': son_hot_route,
                        'n_rate': RouteHelper.get_route_stat(son_hot_route.id)['n_rate'],
                        'date': get_formatted_time(son_hot_route.create_ts)
                    })

        return hot_routes
Beispiel #24
0
    def get_son_routes(category_id):
        assert isinstance(category_id, ObjectId)
        assert CategoryHelper.get(category_id)

        category = CategoryHelper.get(category_id)

        result_list = []
        for son_route_id in category.routes:
            result_list.append(RouteHelper.get(son_route_id))

        for son_route in result_list:
            son_route.formatted_date = get_formatted_time(son_route.create_ts)
            son_route.n_comment = 0 #TODO use disqus api to get comment count
            son_route.n_rate = RateInfo.objects(route=son_route.id).count()

        return result_list
Beispiel #25
0
    def delete_route(route_id):
        assert isinstance(route_id, ObjectId)
        route = RouteHelper.get(route_id)
        assert route

        for attach_id in route.attached:
            AttachmentHelper.delete(attach_id)

        f_cate = Category.objects(id=route.father).first()
        f_cate.routes.remove(route.id)
        f_cate.save()

        enters = EnteredRoute.objects(route=route.id)
        for entered_route in enters:
            user = UserHelper.get(entered_route.user)
            user.entered_routes.remove(route.id)
            user.save()
        enters.delete()

        RateInfo.objects(route=route.id).delete()

        route.delete()
Beispiel #26
0
def join(route_id):
    try:
        route_id = ObjectId(route_id)
        assert RouteHelper.get(route_id)
    except AssertionError, e:
        return redirect(url_for('home.home'))
Beispiel #27
0
    new_user = UserHelper.create_user(request.form['nickname'], request.form['email'], request.form['password'], gender)

    login_user(new_user)

    return redirect(url_for('home.home'))


@user_blueprint.route('/home/', methods=['GET'])
@login_required
def user_home_page():
    try:
        avatar = current_user.avatar.read()
    except AssertionError, e:
        avatar = None

    unfinished_routes = RouteHelper.get_user_routes(False)
    finished_routes = RouteHelper.get_user_routes(True)
    my_routes = RouteHelper.get_user_create_routes()

    return render_template('user-home.html', avatar=avatar, unfinished_routes=unfinished_routes,
                           finished_routes=finished_routes, my_routes=my_routes)


@user_blueprint.route('/modify_info/', methods=['GET'])
@login_required
def modify_info():
    try:
        avatar = current_user.avatar.read()
    except AssertionError, e:
        avatar = None
Beispiel #28
0
 def _reduce_find_hot_route(x, y):
     """ Give x and y, select a hotter one."""
     route_x = RouteHelper.get(x) if x else None
     route_y = RouteHelper.get(y) if y else None
     return x if route_x else y
Beispiel #29
0
        flash(e.message)
        return redirect(url_for('home.home'))

    return redirect(url_for('route.add_attach_page', route_id=new_route.id))


@route_blueprint.route('/join/<route_id>/', methods=['GET'])
@login_required
def join(route_id):
    try:
        route_id = ObjectId(route_id)
        assert RouteHelper.get(route_id)
    except AssertionError, e:
        return redirect(url_for('home.home'))

    RouteHelper.join(route_id)
    return redirect(url_for('route.route_page', route_id=route_id))


@route_blueprint.route('/finish_attach/<route_id>/<attachment_id>/', methods=['GET'])
@login_required
def finish_attach(route_id, attachment_id):
    try:
        route_id = ObjectId(route_id)
        attachment_id = ObjectId(attachment_id)
        assert RouteHelper.get(route_id)
        assert AttachmentHelper.get(attachment_id)
    except AssertionError, e:
        return redirect(url_for('home.home'))

    RouteHelper.finish_attach(route_id, attachment_id)