コード例 #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)
コード例 #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
コード例 #3
0
    def post(self):
        data = invitation_parser.parse_args()

        user = UserModel.find_by_username(data['user'])
        community = CommunityModel.find_by_id(data['community'])

        if not user:
            abort(404, message=USER_DOESNT_EXIST)

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

        if user in community.users:
            abort(400, message=USER_ALREADY_INVITED)

        new_community_user_link = CommunityUserLinkModel(
            user_id=user.id,
            community_id=community.id,
            invitation_accepted=False,
            is_owner=False)

        try:
            new_community_user_link.persist()
            return SimpleMessage(COMMUNIY_INVITATION_SENT), 200
        except:
            abort(500, message=INTERNAL_SERVER_ERROR)
コード例 #4
0
    def post(self, community_id):
        data = parser.parse_args()

        owner = UserModel.find_by_username(get_jwt_identity())
        community: CommunityModel = CommunityModel.find_by_id(community_id)
        community_member_ids = [m.id for m in community.users]

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

        if owner.id not in community_member_ids:
            abort(401, message=UNAUTHORIZED)

        if not data['end'] > data['start']:
            abort(400, message=END_MUST_BE_AFTER_START)

        new_event = EventModel(owner=owner,
                               title=data['title'],
                               description=data['description'],
                               start=data['start'],
                               end=data['end'],
                               community_id=community.id)

        try:
            new_event.persist()
            return new_event, 201
        except:
            abort(500, message=INTERNAL_SERVER_ERROR)
コード例 #5
0
    def put(self, community_id, id):
        data = finish_tour_parser.parse_args()

        tour = TourModel.find_by_id(id)
        user = UserModel.find_by_username(get_jwt_identity())
        community: CommunityModel = CommunityModel.find_by_id(community_id)
        community_member_ids = [m.id for m in community.users]

        if not tour:
            abort(404, message=TOUR_NOT_FOUND)

        if not user.id == tour.owner.id:
            abort(401, message=UNAUTHORIZED)

        if tour.end_km:
            abort(400, message=TOUR_HAS_ALREADY_BEEN_FINISHED)

        passengers = []
        if data['passengers']:
            for passenger_id in set(data['passengers']):
                if passenger_id not in community_member_ids:
                    abort(400, message=PASSENGERS_MUST_BE_COMMUNITY_MEMBERS)
                else:
                    passengers.append([u for u in community.users if u.id == passenger_id][0])

        tour.end_time = datetime.datetime.now(pytz.utc)
        tour.passengers = passengers
        tour.end_km = data['end_km']
        tour.comment = data['comment']
        tour.parking_position = data['parking_position']
        tour.persist()

        create_km_triggered_task_instances(community_id, tour.end_km)

        return tour, 200
コード例 #6
0
ファイル: tour.py プロジェクト: Carbulator/carbulator-server
 def get_marshaller():
     return {
         'id':
         fields.Integer,
         'time_created':
         fields.DateTime,
         'time_updated':
         fields.DateTime,
         'start_time':
         fields.DateTime,
         'end_time':
         fields.DateTime,
         'owner':
         fields.Nested(UserModel.get_marshaller()),
         'community':
         fields.Nested(CommunityModel.get_marshaller()),
         'start_km':
         fields.String,
         'end_km':
         fields.String,
         'parking_position':
         fields.String,
         'comment':
         fields.String,
         'is_force_finished':
         fields.Boolean,
         'force_finished_by':
         fields.Nested(UserModel.get_marshaller(), allow_null=True),
         'is_open':
         fields.Boolean,
         'passengers':
         fields.Nested(UserModel.get_marshaller())
     }
コード例 #7
0
    def post(self, community_id):
        data = parser.parse_args()

        owner = UserModel.find_by_username(get_jwt_identity())
        community: CommunityModel = CommunityModel.find_by_id(community_id)

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

        if owner.id not in [u.id for u in community.users]:
            abort(401, message=UNAUTHORIZED)

        new_refuel = RefuelModel(
            owner=owner,
            community=community,
            costs=round(data['costs'], 2),
            liters=data['liters'],
            gas_station_name=data['gas_station_name'],
        )

        try:
            new_refuel.persist()
            return new_refuel, 201
        except:
            abort(500, message=INTERNAL_SERVER_ERROR)
コード例 #8
0
 def get_marshaller():
     return {
         'community': fields.Nested(CommunityModel.get_marshaller()),
         'statistic_start': fields.DateTime,
         'statistic_end': fields.DateTime,
         'km_per_user': fields.Nested(KmPerUserModel.get_marshaller()),
         'costs_per_user': fields.Nested(CostsPerUserModel.get_marshaller())
     }
コード例 #9
0
    def get(self, community_id):
        user = UserModel.find_by_username(get_jwt_identity())
        community = CommunityModel.find_by_id(community_id)

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

        return community.users, 200
コード例 #10
0
class FavouriteCommunity(Resource):
    @jwt_required
    @marshal_with(CommunityModel.get_marshaller())
    def get(self):
        user = UserModel.find_by_username(get_jwt_identity())
        cul = CommunityUserLinkModel.find_favourite_by_user(user.id)

        if not cul:
            abort(404, message=NO_FAVOURITE_COMMUNITY_FOUND)

        return cul.community, 200
コード例 #11
0
    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
コード例 #12
0
    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
コード例 #13
0
    def get(self, community_id):
        tasks: List[TaskModel] = TaskModel.find_by_community(community_id)
        community: CommunityModel = CommunityModel.find_by_id(community_id)
        community_member_ids = [m.id for m in community.users]
        user = UserModel.find_by_username(get_jwt_identity())

        if user.id not in community_member_ids:
            abort(401, message=UNAUTHORIZED)

        set_km_to_next_instance(tasks)

        return tasks, 200
コード例 #14
0
    def delete(self, community_id):
        user = UserModel.find_by_username(get_jwt_identity())
        invitation = CommunityUserLinkModel.find_by_user_and_community(
            user.id, community_id)

        if not invitation:
            abort(404, message=COMMUNITY_INVITATION_DOESNT_EXIST)

        if invitation.user_id != user.id:
            abort(401, message=NOT_AUTHORIZED_TO_REMOVE_USER_FROM_COMMUNITY)

        if invitation.invitation_accepted and not invitation.is_owner:
            invitation.delete()
            return SimpleMessage(COMMUNIY_LEFT_SUCCESSFULLY), 200

        if invitation.invitation_accepted and invitation.is_owner:
            CommunityModel.delete_by_id(invitation.community.id)
            return SimpleMessage(COMMUNIY_LEFT_AND_DELETED)

        invitation.delete()
        return SimpleMessage(COMMUNITY_INVITATION_DECLINED), 200
コード例 #15
0
    def get(self, community_id):

        user = UserModel.find_by_username(get_jwt_identity())
        community = CommunityModel.find_by_id(community_id)

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

        if user.id not in [u.id for u in community.users]:
            abort(401, message=UNAUTHORIZED)

        return TourModel.find_running_by_community(community_id), 200
コード例 #16
0
    def get(self):
        user = UserModel.find_by_username(get_jwt_identity())
        all_communities = CommunityModel.return_all()
        user_communities = [
            c for c in all_communities if user.id in [u.id for u in c.users]
        ]

        task_instances = []
        for c in user_communities:
            task_instances += TaskInstanceModel.find_by_community(c.id)
        open_task_instances = [i for i in task_instances if i.is_open]

        return open_task_instances, 200
コード例 #17
0
    def get(self, id):
        community = CommunityModel.find_by_id(id)
        user = UserModel.find_by_username(get_jwt_identity())

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

        if user.id not in [u.id for u in community.users]:
            abort(401, message=UNAUTHORIZED)

        debts = DebtModel.find_unsettled_by_community(id)

        return debts, 200
コード例 #18
0
    def get(self, community_id: int):

        user = UserModel.find_by_username(get_jwt_identity())
        community = CommunityModel.find_by_id(community_id)

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

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

        invitations = CommunityUserLinkModel.find_open_invitations_by_community(
            community_id)
        return [i.user for i in invitations], 200
コード例 #19
0
    def get(self, community_id):
        community: CommunityModel = CommunityModel.find_by_id(community_id)
        community_member_ids = [m.id for m in community.users]
        user = UserModel.find_by_username(get_jwt_identity())

        if user.id not in community_member_ids:
            abort(401, message=UNAUTHORIZED)

        task_instances: List[
            TaskInstanceModel] = TaskInstanceModel.find_by_community(
                community_id)
        open_task_instances = [i for i in task_instances if i.is_open]

        return open_task_instances, 200
コード例 #20
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
コード例 #21
0
    def get(self, community_id, number_of_events):

        owner = UserModel.find_by_username(get_jwt_identity())
        community: CommunityModel = CommunityModel.find_by_id(community_id)
        community_member_ids = [m.id for m in community.users]

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

        if owner.id not in community_member_ids:
            abort(401, message=UNAUTHORIZED)

        events: EventModel = EventModel.find_next_n_by_community(
            community_id, number_of_events)

        return events, 200
コード例 #22
0
ファイル: task.py プロジェクト: Carbulator/carbulator-server
 def get_marshaller():
     return {
         'id': fields.Integer,
         'time_created': fields.DateTime,
         'time_updated': fields.DateTime,
         'time_next_instance': fields.DateTime,
         'time_interval': TimedeltaDays,
         'owner': fields.Nested(UserModel.get_marshaller()),
         'community': fields.Nested(CommunityModel.get_marshaller()),
         'name': fields.String,
         'description': fields.String,
         'km_interval': fields.Integer,
         'km_next_instance': fields.Float,
         'km_to_next_instance': fields.Float,
         'is_reocurrent': fields.Boolean
     }
コード例 #23
0
    def get(self, community_id):
        user = UserModel.find_by_username(get_jwt_identity())
        community = CommunityModel.find_by_id(community_id)

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

        if user.id not in [u.id for u in community.users]:
            abort(401, message=UNAUTHORIZED)

        tour = TourModel.find_newest_tour_for_community(community_id)

        if not tour:
            abort(400, message=NO_TOUR_EXISTING)

        return tour, 200
コード例 #24
0
    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
コード例 #25
0
def get_community_statistic(community_id, from_datetime, to_datetime):
    community: CommunityModel = CommunityModel.find_by_id(community_id)

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

    user = UserModel.find_by_username(get_jwt_identity())

    community_member_ids = [m.id for m in community.users]
    if user.id not in community_member_ids:
        abort(401, message=UNAUTHORIZED)

    statistic = CommunityStatisticModel()
    statistic.community = community
    statistic.statistic_start = from_datetime
    statistic.statistic_end = to_datetime

    all_tours = TourModel.find_finished_by_community(community_id)
    all_costs = RefuelModel.find_by_community(community_id)
    km_per_user_dict = {}
    costs_per_user_dict = {}
    for user in community.users:
        km_per_user_dict[user.id] = KmPerUserModel()
        km_per_user_dict[user.id].user = user
        statistic.km_per_user.append(km_per_user_dict[user.id])
        costs_per_user_dict[user.id] = CostsPerUserModel()
        costs_per_user_dict[user.id].user = user
        statistic.costs_per_user.append(costs_per_user_dict[user.id])
    for tour in all_tours:
        if from_datetime <= tour.end_time.astimezone(pytz.utc) <= to_datetime:
            tour_km = tour.end_km - tour.start_km
            km_per_user_dict[tour.owner_id].km += tour_km
            # Divide km of passengers
            all_passengers_ids = [tour.owner_id] + [
                passenger.id for passenger in tour.passengers
            ]
            for passenger_id in all_passengers_ids:
                km_per_user_dict[
                    passenger_id].km_accounted_for_passengers += tour_km / len(
                        all_passengers_ids)
    for cost in all_costs:
        if from_datetime <= cost.time_created.astimezone(
                pytz.utc) <= to_datetime:
            costs_per_user_dict[cost.owner_id].costs += cost.costs

    return statistic
コード例 #26
0
    def get(self, id):

        payoff = PayoffModel.find_by_id(id)

        if not payoff:
            abort(404, message=PAYOFF_DOESNT_EXIST)

        community = CommunityModel.find_by_id(payoff.community_id)
        user = UserModel.find_by_username(get_jwt_identity())

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

        if user.id not in [u.id for u in community.users]:
            abort(401, message=UNAUTHORIZED)

        return payoff, 200
コード例 #27
0
    def get(self):
        data = search_parser.parse_args()
        username = get_jwt_identity()
        users = UserModel.search_by_username(data['q'], username)
        if not data['only-uninvited']:
            return users
        else:
            if not data['community']:
                abort(400, message=NO_COMMUNITY_ID_GIVEN)

            community = CommunityModel.find_by_id(data['community'])
            if not community:
                abort(404, message=COMMUNIY_DOESNT_EXIST)

            if username not in [u.username for u in community.users]:
                abort(401, message=UNAUTHORIZED)

            invitations = CommunityUserLinkModel.find_by_community(data['community'])
            return [u for u in users if u.id not in [i.user_id for i in invitations]]
コード例 #28
0
    def get(self, community_id, from_datetime, to_datetime):

        owner = UserModel.find_by_username(get_jwt_identity())
        community: CommunityModel = CommunityModel.find_by_id(community_id)
        community_member_ids = [m.id for m in community.users]

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

        if owner.id not in community_member_ids:
            abort(401, message=UNAUTHORIZED)

        from_datetime = moment(from_datetime)
        to_datetime = moment(to_datetime)

        if not to_datetime > from_datetime:
            abort(400, message=TO_MUST_BE_AFTER_FROM)

        events: EventModel = EventModel.find_by_community(
            community_id, from_datetime, to_datetime)

        return events, 200
コード例 #29
0
    def post(self, community_id):
        data = parser.parse_args()

        owner = UserModel.find_by_username(get_jwt_identity())
        community: CommunityModel = CommunityModel.find_by_id(community_id)
        community_member_ids = [m.id for m in community.users]

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

        if owner.id not in community_member_ids:
            abort(401, message=UNAUTHORIZED)

        if TourModel.find_running_by_community(community_id):
            abort(400, message=CANT_START_TOUR_WHEN_HAVING_UNFINISHED_TOURS_IN_COMMUNITY)

        passengers = []
        if data['passengers']:
            for passenger_id in set(data['passengers']):
                if passenger_id not in community_member_ids:
                    abort(400, message=PASSENGERS_MUST_BE_COMMUNITY_MEMBERS)
                else:
                    passengers.append([u for u in community.users if u.id == passenger_id][0])

        new_tour = TourModel(
            owner=owner,
            community=community,
            start_time=datetime.datetime.now(pytz.utc),
            start_km=data['start_km'],
            passengers=passengers
        )

        try:
            new_tour.persist()
            return new_tour, 201
        except:
            abort(500, message=INTERNAL_SERVER_ERROR)
コード例 #30
0
    def put(self, community_id, id):
        data = edit_tour_parser.parse_args()

        tour = TourModel.find_by_id(id)
        user = UserModel.find_by_username(get_jwt_identity())
        community: CommunityModel = CommunityModel.find_by_id(community_id)
        community_member_ids = [m.id for m in community.users]

        if not tour:
            abort(404, message=TOUR_NOT_FOUND)

        if not user.id == tour.owner.id:
            abort(401, message=UNAUTHORIZED)

        passengers = []
        if data['passengers']:
            for passenger_id in set(data['passengers']):
                if passenger_id not in community_member_ids:
                    abort(400, message=PASSENGERS_MUST_BE_COMMUNITY_MEMBERS)
                else:
                    passengers.append([u for u in community.users if u.id == passenger_id][0])

        tour.comment = data['comment']
        tour.parking_position = data['parking_position']

        if not tour.is_open:
            abort(400, message=CANNOT_UPDATE_SENSITIVE_TOUR_DATA_WHEN_TOUR_IS_ALREADY_PAYED_FOR)
        else:
            if data['end_km'] <= data['start_km']:
                abort(400, message=END_KM_MUST_BE_GREATER_START_KM)
            tour.end_km = data['end_km']
            tour.start_km = data['start_km']
            tour.passengers = passengers

        tour.persist()

        return tour, 200