async def get_task_from_args(token, message, group_id=None): task_code = baas32.normalize(message.args) if message.args else '' if not task_code: await message.chat.message( 'Welke taak? Protip: zet de taakcode achter het commando.') return try: task_id = baas32.decode(task_code) except ValueError: await message.chat.message(f'{task_code} is geen geldige taakcode.') return try: if group_id: task = await api.get_group_task(token, group_id, task_id) else: task = await api.get_task(token, task_id) except api.NotFoundError: await message.chat.message(f'Kan taak {task_code} niet vinden :(') return except api.PermissionDeniedError: await message.chat.message( f'Je hebt geen rechten voor taak {task_code}.') return return task, task_code
def parse_minute(content, group_id, minute_id): """ Parse the specified minutes for tasks and return them in a list. Same for DONE tasks and REMOVED tasks syntax within the content: ACTIE <name_1>, <name_2>, name_n>: <title of task> or TODO <name_1>, <name_2>, name_n>: <title of task> this creates a single task for one or multiple users ACTIES <name_1>, <name_2>, name_n>: <title of task> or TODOS <name_1>, <name_2>, name_n>: <title of task> this creates one or multiple tasks for one or multiple users DONE <task1>, <task2, <taskn> this sets the given tasks on 'done' usage: tasks, dones, removes = parse_minute(content, group_id, minute_id) where content is a string with the entire minute """ tasks_found = [] dones_found = [] removes_found = [] regex = re.compile(r"\s*(?:ACTIE|TODO)([\s:]\s*[^\n\r]*)") for i, line in enumerate(content.splitlines()): matches = regex.findall(line) for action in matches: try: listed_users, title = action.split(":", 1) except: print("could not split the line on ':'.\nSkipping hit.") flash("Kon niet verwerken: " + str(action), 'danger') continue users, message = PimpyAPI.get_list_of_users_from_string( group_id, listed_users) if not users: print(message) continue try: task = Task(title, "", group_id, users, minute_id, i, 0) except: print("wasnt given the right input to create a task") continue tasks_found.append(task) regex = re.compile("\s*(?:ACTIES|TODOS)([^\n\r]*:\s*[^\n\r]*)") for i, line in enumerate(content.splitlines()): matches = regex.findall(line) for action in matches: try: listed_users, title = action.split(":", 1) except: print("could not split the line on ':'.\nSkipping hit.") flash("Kon niet verwerken: " + action, 'danger') continue users, message = PimpyAPI.get_list_of_users_from_string( group_id, listed_users) if not users: print(message) continue for user in users: try: task = Task(title, "", group_id, [user], minute_id, i, 0) except: print("wasnt given the right input to create a task") continue tasks_found.append(task) regex = re.compile("\s*(?:DONE) ([^\n\r]*)") matches = regex.findall(content) for match in matches: done_ids = filter(None, match.split(",")) for b32_id in done_ids: b32_id_strip = b32_id.strip() if b32_id_strip == '': continue try: done_id = b32.decode(b32_id_strip) except ValueError: flash(_("Invalid DONE task id: ") + b32_id_strip, 'danger') continue done_task = Task.query.filter(Task.id == done_id).first() if done_task is None: flash(_("Could not find DONE task: ") + b32_id_strip, "danger") continue dones_found.append(done_task) regex = re.compile("\s*(?:REMOVE) ([^\n\r]*)") matches = regex.findall(content) for match in matches: remove_ids = filter(None, match.split(",")) for b32_id in remove_ids: b32_id_strip = b32_id.strip() if b32_id_strip == '': continue try: remove_id = b32.decode(b32_id_strip) except ValueError: flash(_("Invalid REMOVE task id: ") + b32_id, 'danger') continue remove_task = Task.query\ .filter(Task.id == remove_id).first() if remove_task is None: flash(_("Could not find REMOVE task: ") + b32_id_strip, "danger") continue removes_found.append(remove_task) return tasks_found, dones_found, removes_found
def test_decode_strict(self): self.assertRaises(ValueError, b32.decode, '16j', strict=True) self.assertEqual(b32.decode('16J', strict=True), 1234)
def test_decode_checksum(self): self.assertEqual(b32.decode('16JD', checksum=True), 1234)
def test_decode_unicode(self): self.assertEqual(b32.decode(u'16J'), 1234)
def find_task_by_b32_id(b32_task_id: str) -> Optional[Task]: try: task_id = baas32.decode(b32_task_id) return pimpy_repository.find_task_by_id(task_id) except ValueError: return None
def _parse_minute_into_tasks(content, group): """ Parse the specified minutes for tasks and return task, done and remove. Syntax within the content: ACTIE <name_1>, <name_2>, name_n>: <title of task> This creates a single task for one or multiple users ACTIES <name_1>, <name_2>, name_n>: <title of task> This creates one or multiple tasks for one or multiple users DONE <task1>, <task2, <taskn> This sets the given tasks on 'done' """ missing_colon_lines = [] unknown_task_ids = [] unknown_user = [] task_list = [] done_list = [] remove_list = [] for i, line in enumerate(content.splitlines()): try: if MISSING_COLON_REGEX.search(line): missing_colon_lines.append((i, line)) continue # Single task for multiple users. for names, task in TASK_REGEX.findall(line): users = get_list_of_users_from_string(group.id, names) task_list.append((i, task, users)) # Single task for individual users. for names, task in TASKS_REGEX.findall(line): users = get_list_of_users_from_string(group.id, names) for user in users: task_list.append((i, task, [user])) # Mark a comma separated list as done. for task_id_list in DONE_REGEX.findall(line): for b32_task_id in task_id_list.strip().split(","): b32_task_id = b32_task_id.strip() try: task_id = b32.decode(b32_task_id) except ValueError: unknown_task_ids.append((i, b32_task_id)) continue task = pimpy_repository. \ find_task_in_group_by_id(task_id, group.id) if not task: unknown_task_ids.append((i, b32_task_id)) else: done_list.append((i, task)) # Mark a comma separated list as removed. for task_id_list in REMOVE_REGEX.findall(line): for b32_task_id in task_id_list.strip().split(","): b32_task_id = b32_task_id.strip() try: task_id = b32.decode(b32_task_id) except ValueError: unknown_task_ids.append((i, b32_task_id)) continue task = pimpy_repository. \ find_task_in_group_by_id(task_id, group.id) if not task: unknown_task_ids.append((i, b32_task_id)) else: remove_list.append((i, task)) # Catch invalid user. except ValidationException: unknown_user.append((i, line)) if len(missing_colon_lines) or len(unknown_task_ids) or len(unknown_user): raise InvalidMinuteException(missing_colon_lines, unknown_task_ids, unknown_user) return task_list, done_list, remove_list