Exemplo n.º 1
0
class AllCommunities(Resource):
    @jwt_required
    @marshal_with(
        CommunityModel.add_is_fav_to_marshaller(
            CommunityModel.get_detailed_marshaller()))
    def post(self):
        data = post_parser.parse_args()

        founder = UserModel.find_by_username(get_jwt_identity())
        car = CarModel.find_by_id(data['car'])

        if not car:
            abort(404, message=CAR_DOESNT_EXIST)

        if not car.owner.id == founder.id:
            abort(400, message=CANNOT_CREATE_COMMUNITY_WITH_FOREIGN_CAR)

        if CommunityModel.find_by_car_id(car.id):
            abort(400, message=COMMUNIY_WITH_THIS_CAR_ALREADY_EXISTS)

        new_community = CommunityModel(name=data['name'],
                                       car=car,
                                       users=[founder])

        try:
            new_community.persist()
            faved_community: CommunityUserLinkModel = assure_favourite_community(
                founder.id)
            if faved_community and faved_community.community_id == new_community.id:
                new_community.is_favourite = faved_community.is_favourite
            return new_community, 201
        except:
            abort(500, message=INTERNAL_SERVER_ERROR)
Exemplo n.º 2
0
class SingleCommunity(Resource):
    @jwt_required
    @marshal_with(CommunityModel.get_detailed_marshaller())
    def get(self, id):

        user = UserModel.find_by_username(get_jwt_identity())

        community = CommunityModel.find_by_id(id)
        if not community:
            abort(404, message=COMMUNIY_DOESNT_EXIST)

        if user not in community.users:
            abort(401, message=UNAUTHORIZED)

        is_owner = CommunityUserLinkModel.find_by_user_and_community(
            user.id, community.id).is_owner
        community.is_deletable = is_owner
        community.is_editable = is_owner

        return community, 200

    @jwt_required
    @marshal_with(CommunityModel.get_marshaller())
    def put(self, id):
        data = put_parser.parse_args()

        community = CommunityModel.find_by_id(id)

        if not community:
            abort(404, message=COMMUNIY_DOESNT_EXIST)

        community.name = data['name']
        community.persist()

        return community, 200

    @jwt_required
    @marshal_with(SimpleMessage.get_marshaller())
    def delete(self, id):
        user = UserModel.find_by_username(get_jwt_identity())

        try:
            link = CommunityUserLinkModel.find_by_user_and_community(
                user.id, id)
            if not link or not link.is_owner:
                abort(401, message=UNAUTHORIZED)
        except NoData:
            abort(404, message=COMMUNIY_DOESNT_EXIST)

        all_links = CommunityUserLinkModel.find_by_community(id)
        for link in all_links:
            link.delete()

        try:
            CommunityModel.delete_by_id(id)
        except NoData:
            abort(404, message=COMMUNIY_DOESNT_EXIST)

        return SimpleMessage(COMMUNIY_DELETED), 200
Exemplo n.º 3
0
class UserCommunities(Resource):
    @jwt_required
    @marshal_with(
        CommunityModel.add_is_fav_to_marshaller(
            CommunityModel.get_detailed_marshaller()))
    def get(self):
        user = UserModel.find_by_username(get_jwt_identity())

        community_user_links = CommunityUserLinkModel.find_by_user(user.id)
        for cul in community_user_links:
            cul.community.is_favourite = cul.is_favourite

        user_communities = [
            c.community for c in community_user_links if c.invitation_accepted
        ]

        for c in user_communities:
            is_owner = CommunityUserLinkModel.find_by_user_and_community(
                user.id, c.id).is_owner
            c.is_deletable = is_owner
            c.is_editable = is_owner

        return user_communities, 200