def create_km_triggered_task_instances(community_id, km):
    """
    Checks if there are new task instances that have to be created and creates them.
    :param community_id: Community to check the task instance creation for.
    :param km: Current km clock.
    """
    tasks = TaskModel.find_by_community(community_id)

    # Iterate over all km triggered tasks
    for task in [t for t in tasks if t.km_interval]:

        try:
            # If current km are higher then trigger, add a new task instance
            if km >= task.km_next_instance:
                # Create and persist task instance
                new_task_instance = TaskInstanceModel()
                new_task_instance.task = task
                new_task_instance.km_created_at = km
                new_task_instance.is_open = True
                new_task_instance.community = task.community
                new_task_instance.persist()

                # Update km trigger and persist task
                task.km_next_instance += task.km_interval
                if task.km_next_instance <= km:
                    task.km_next_instance = km + 1
                task.persist()
        except:
            # Don't fail on all just because one instance is bad
            pass
def create_time_triggered_task_instances():
    """
    Checks if there are new task instances that have to be created and creates them.
    """
    tasks = TaskModel.return_all()

    # Iterate over all time triggered tasks
    for task in [t for t in tasks if t.time_interval]:

        try:
            # If current time is higher then trigger, add a new task instance
            now = datetime.now(pytz.utc)
            then = task.time_next_instance.replace(tzinfo=pytz.utc)

            if now >= then:
                # Create and persist task instance
                new_task_instance = TaskInstanceModel()
                new_task_instance.task = task
                new_task_instance.is_open = True
                new_task_instance.community = task.community
                new_task_instance.persist()

                # Update time trigger and persist task
                task.time_next_instance = now + task.time_interval
                task.persist()
        except:
            # Don't fail on all just because one instance is bad
            pass
    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
    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
    def put(self, task_instance_id):
        user = UserModel.find_by_username(get_jwt_identity())
        task_instance: TaskInstanceModel = TaskInstanceModel.find_by_id(
            task_instance_id)

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

        task_instance.is_open = False
        task_instance.time_finished = datetime.now(pytz.utc)
        task_instance.finished_by = user
        task_instance.persist()

        return task_instance, 200
class GetOpenAccountTaskInstances(Resource):
    @jwt_required
    @marshal_with(TaskInstanceModel.get_marshaller())
    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
class GetOpenCommunityTaskInstances(Resource):
    @jwt_required
    @marshal_with(TaskInstanceModel.get_marshaller())
    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
    def post(self, community_id):
        parser = reqparse.RequestParser()
        parser.add_argument('time_next_instance', type=moment, required=False)
        parser.add_argument('time_interval', type=int, required=False)
        parser.add_argument('name', type=str)
        parser.add_argument('description', type=str)
        parser.add_argument('km_interval', type=int, required=False)
        parser.add_argument('km_next_instance', type=float, required=False)
        parser.add_argument('is_reocurrent', type=bool, required=True)
        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 data['is_reocurrent'] and (
                not (data['time_next_instance'] and data['time_interval'] or data['km_interval'] and data[
            'km_next_instance']) or data['km_interval'] and (data['time_interval'] or data['time_next_instance']) or \
                data['time_interval'] and (data['km_interval'] or data['km_next_instance'])):
            abort(400, message=TASK_MUST_BE_EITHER_TIME_OR_KM_TRIGGERED)

        newest_tour: TourModel = TourModel.find_newest_tour_for_community(community_id)
        if data['km_next_instance'] and data['km_next_instance'] < newest_tour.end_km:
            abort(400, message=TASK_KM_NEXT_INSTANCE_MUST_BE_HIGHER_THEN_CURRENT_KM)

        if data['time_next_instance'] and data['time_next_instance'] < datetime.now(pytz.timezone('Europe/Berlin')):
            abort(400, message=TASK_TIME_NEXT_INSTANCE_MUST_BE_HIGHER_THEN_CURRENT_TIME)

        time_interval = None
        if data['time_interval']:
            time_interval = timedelta(days=data['time_interval'])

        new_task = TaskModel(
            owner=owner,
            community=community,
            time_interval=time_interval,
            time_next_instance=data['time_next_instance'],
            km_interval=data['km_interval'],
            km_next_instance=data['km_next_instance'],
            name=data['name'],
            description=data['description'],
            is_reocurrent=data['is_reocurrent']
        )

        try:
            new_task.persist()
            set_km_to_next_instance(new_task)

            if not data['is_reocurrent']:
                new_task_instance = TaskInstanceModel(
                    task=new_task,
                    is_open=True,
                    community=new_task.community
                )
                new_task_instance.persist()

            return new_task, 201
        except:
            abort(500, message=INTERNAL_SERVER_ERROR)