def test_add_snapshot(self): task = TaskModel(17, 3, ['+', '-'], (10, 49)) Log.log('d', f'Test format: {task.to_sqlite_format()}') name = ''.join([choice(ascii_letters) for _ in range(randint(5, 15))]) self.hm.add_snaptshot([name, *task.to_sqlite_format()]) # self.dbm.save() Log.log('d', self.hm.get_snapshot_names())
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 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 generate_task_action(self, with_answers=False): config = self.validate_task_config() Log.log('i', 'with answers: ', with_answers) if config: task = TaskModel(int(config.expressions_count), int(config.columns), config.operations, (int(config.min_number), int(config.max_number))) em = ExportManager(task) em.save('Math 1.docx', with_answers)
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
def save_snapshot(self): config = self.validate_task_config() if not config: return name, is_pressed = QInputDialog.getText(self.view, "Save Snapshot", "Enter name") if is_pressed: if self.history_provider.get_snapshot(name): dialog = ErrorDialog(self.view) dialog.exec_() return task = TaskModel(int(config.expressions_count), int(config.columns), config.operations, (int(config.min_number), int(config.max_number))) self.history_provider.add_snaptshot( [name, *task.to_sqlite_format()]) self.view.add_snapshot_entry(name) self.dbm.save() else: return
class UpdateTask(Resource): @jwt_required @marshal_with(TaskModel.get_marshaller()) def put(self, task_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) data = parser.parse_args() task: TaskModel = TaskModel.find_by_id(task_id) if not task: abort(404, message=TASK_DOESNT_EXIST) if not task.is_reocurrent: abort(401, message=NON_REOCURRENT_TASKS_CANNOT_BE_UPDATED) community_member_ids = [m.id for m in task.community.users] user = UserModel.find_by_username(get_jwt_identity()) if user.id not in community_member_ids: abort(401, message=UNAUTHORIZED) if 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) time_interval = None if data['time_interval']: time_interval = timedelta(days=data['time_interval']) task.time_interval = time_interval task.time_next_instance = data['time_next_instance'] task.km_interval = data['km_interval'] task.km_next_instance = data['km_next_instance'] task.name = data['name'] task.description = data['description'] try: task.persist() set_km_to_next_instance(task) return task, 200 except: abort(500, message=INTERNAL_SERVER_ERROR)
class GetCommunityTasks(Resource): @jwt_required @marshal_with(TaskModel.get_marshaller()) 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
def get(self, task_id): task: TaskModel = TaskModel.find_by_id(task_id) if not task: abort(404, message=TASK_DOESNT_EXIST) community_member_ids = [m.id for m in task.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(task) return task, 200
def delete(self, task_id): task: TaskModel = TaskModel.find_by_id(task_id) if not task: abort(400, message=TASK_DOESNT_EXIST) community_member_ids = [m.id for m in task.community.users] user = UserModel.find_by_username(get_jwt_identity()) if user.id not in community_member_ids: abort(401, message=UNAUTHORIZED) try: task.delete_by_id(task_id) return SimpleMessage(TASK_DELETED), 200 except: abort(500, message=INTERNAL_SERVER_ERROR)
def get_marshaller(): return { 'id': fields.Integer, 'time_created': fields.DateTime, 'time_updated': fields.DateTime, 'time_finished': fields.DateTime, 'task': fields.Nested(TaskModel.get_marshaller()), 'is_open': fields.Boolean, 'km_created_at': fields.Float, 'finished_by': fields.Nested(UserModel.get_marshaller(), allow_null=True) }
class GetTask(Resource): @jwt_required @marshal_with(TaskModel.get_marshaller()) def get(self, task_id): task: TaskModel = TaskModel.find_by_id(task_id) if not task: abort(404, message=TASK_DOESNT_EXIST) community_member_ids = [m.id for m in task.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(task) return task, 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)
def test_get_snapshot(self): ret = self.hm.get_snapshot('test1') if not(ret == ('+,-', 17, 3, 10, 49)): Log.log('er', f'Not equal {ret}') task = TaskModel.from_sqlite_format(ret) Log.log('d', task)
def sample_TaskModel(self, ex_range=None, cols_range=None): if ex_range is None: ex_range = randint(1, 10) if cols_range is None: cols_range = randint(1, 10) return TaskModel(ex_range, cols_range, choices(['+', '-', '*', '/']), (randint(0, 40), randint(40, 80)))
def __init__(self): self.task = TaskModel(17, 3, ['+', '-'], (10, 49)) # self.ex_generator = ExampleGeneratorServiceself.task, ['+', '-'], (10, 49)) # self.ex_generator.generate_final() self.task.ex_generator.generate_final()