Esempio n. 1
0
def subscribe(community_id):
    subscription = True if request.form['subscription'] == 'subscribe' \
        else False if request.form['subscription'] == 'unsubscribe' \
        else None
    user = utils.auth.service.get_user()
    if not user.is_authorized:
        abort(403)

    community = Community.get(community_id)
    if not community:
        abort(404)

    if community.owner != user:
        if subscription is True and not community.has_member(user):
            cm = CommunityMember(user=user, community=community)
            if community.type == community.TYPE.PUBLIC:
                cm.status = cm.STATUS.ACCEPTED
            db.session.add(cm)
            db.session.commit()
        elif subscription is False and community.has_member(user):
            community.members.remove(user)
            db.session.commit()

    res = {
        'status': 'ok',
        'community': community.as_dict(),
        'subscribed': community.has_member(user)
    }
    res['community']['type'] = community.TYPE.TITLE[community.type]
    res['community']['count_members'] = community.count_members

    return jsonify(res)
Esempio n. 2
0
def community_edit(id=None):
    user = auth.service.get_user()
    community = Community.get(id)
    if user == community.owner or user.is_admin or ('manage_communities' in user.get_permissions()):
        return render_template('community/form.html', **{'community': community})
    else:
        abort(403)
Esempio n. 3
0
def delete(id):
    user = auth.service.get_user()
    if user.is_authorized():
        community = Community.get(id)
        if (community and community.owner == user) or user.is_admin or ('manage_communities' in user.get_permissions()):
            community.status = community.STATUS.DELETED
            db.session.commit()
            return jsonify({'status': 'ok'})

    return jsonify({'status': 'fail'})
Esempio n. 4
0
def post_form(community_id, id=None):
    user = auth.service.get_user()
    community = Community.get(community_id)
    post = Post.get(id) if id else Post()

    if not community or not post:
        abort(404)

    if not community.has_member(user) and not (id and post.author == user):
        abort(403)

    return render_template('community/post_form.html', **{'community': community, 'post': post})
Esempio n. 5
0
def save():
    data = dict(request.form)
    data['image'] = request.files.getlist('image')
    v = Validator(data)
    v.fields('id').integer(nullable=True)
    v.field('name').required()
    v.field('private').boolean()
    v.field('description').required()
    v.field('image').image()
    user = auth.service.get_user()

    if not user.is_authorized():
        abort(403)
    if not v.is_valid():
        return jsonify({
            'status': 'fail',
            'errors': v.errors
        })

    data = v.valid_data
    community = Community.get(data.id)

    if community:
        community.type = Community.TYPE.PRIVATE if data.private else Community.TYPE.PUBLIC
        community.name = data.name
        community.description = data.description
    else:
        community = Community()
        community.type = Community.TYPE.PRIVATE if data.private else Community.TYPE.PUBLIC
        community.name = data.name
        community.description = data.description
        community.owner = user

    db.session.add(community)
    db.session.flush()
    image = data.image
    if image:
        img = community.image = community.image or File.create(name='image.png', module='community', entity=community)
        img.makedir()
        img.update_hash()
        utils.image.thumbnail(image, width=200, height=200, fill=utils.image.COVER).save(img.get_path())
    db.session.commit()
    return jsonify({'status': 'ok',
                    'community': community.as_dict()})
Esempio n. 6
0
def accept_member(community_id, member_id):
    user = utils.auth.service.get_user()

    if not user.is_authorized:
        abort(403)

    community = Community.get(community_id)
    cm = CommunityMember.query.filter(
        CommunityMember.user_id == member_id,
        CommunityMember.community_id == community_id).first()
    if not cm or not community or community.owner != user:
        abort(404)

    cm.status = cm.STATUS.ACCEPTED
    db.session.commit()

    res = {'status': 'ok', 'user': {'status': cm.STATUS.TITLE[cm.status]}}
    return jsonify(res)
Esempio n. 7
0
def reject_member(community_id, member_id):
    user = utils.auth.service.get_user()

    if not user.is_authorized:
        abort(403)

    community = Community.get(community_id)
    member = User.get(member_id)
    # cm = CommunityMember.query.filter(
    #     CommunityMember.user_id == member_id,
    #     CommunityMember.community_id == community_id).first()
    if not member or not community or community.owner != user:
        abort(404)

    # cm.status = cm.STATUS.REJECTED
    community.members.remove(member)
    db.session.commit()

    res = {'status': 'ok', 'user': {'status': CommunityMember.STATUS.TITLE[CommunityMember.STATUS.REJECTED]}}
    return jsonify(res)
Esempio n. 8
0
def community_create():
    community = Community()
    return render_template('community/form.html', **{'community': community})
Esempio n. 9
0
def community_page(id):
    community = Community.get(id)
    return render_template('community/one.html', **{'community': community})
Esempio n. 10
0
def mine_communities():
    communities = Community.all_mine()
    return render_template('community/all.html', **{'communities': communities})
Esempio n. 11
0
def list_communities():
    communities = Community.all_active()
    return render_template('community/all.html', **{'communities': communities})