def swap_levels(project_id: int): first = models.Priority.get_for_identifier_and_project( session = SC.session, project_id = project_id, identifier = request.data.get('first'), ) second = models.Priority.get_for_identifier_and_project( session = SC.session, project_id = project_id, identifier = request.data.get('second'), ) if first is None or second is None: return 'invalid priority', status.HTTP_400_BAD_REQUEST SC.session.execute('SET CONSTRAINTS ALL DEFERRED') first.level, second.level = second.level, first.level SC.session.commit() schema = schemas.PrioritySchema() return { 'priorities': [ schema.serialize(tag) for tag in (first, second) ] }
def get_priority_level( session: Session, level: t.Union[str, int, None], project: models.Project, ) -> int: if isinstance(level, int): return level return models.Priority.get_for_identifier_or_raise( session, level, schemas.PrioritySchema(), base_query=session.query(models.Priority).filter( models.Priority.project_id == project.id, ), ).level
def priorities_list(project_id: int): priorities = SC.session.query(models.Priority).filter( models.Priority.project_id == project_id, ).order_by(models.Priority.level.asc()) schema = schemas.PrioritySchema() return { 'priorities': [ schema.serialize(tag) for tag in priorities ] }
def create_todo(): schema = ToDoSchema() try: todo_data = schema.deserialize_raw(request.data) create_data = schemas.CreateTodoSchema().deserialize_raw(request.data) except DeserializationError as e: return e.serialized, status.HTTP_400_BAD_REQUEST project = models.Project.get_for_identifier_or_raise( SC.session, create_data.get('project'), schemas.ProjectSchema(), ) todo_data['project_id'] = project.id priority = models.Priority.get_for_identifier_or_raise( session=SC.session, identifier=create_data.get('priority'), schema=schemas.PrioritySchema(), base_query=SC.session.query( models.Priority).filter(models.Priority.project_id == project.id)) todo_data['priority_id'] = priority.id todo = models.ToDo(**todo_data) for tag in create_data['tags']: todo.tags.append( models.Tag.get_for_identifier_or_raise( SC.session, tag, schemas.TagSchema(), )) for parent in create_data['parents']: parent = get_todo_for_project_and_identifier(SC.session, parent, project) todo.parents.append(parent) SC.session.add(todo) SC.session.commit() return schema.serialize(todo)
def create_priority(): try: priority = schemas.PriorityCreateSchema().deserialize(request.data) except DeserializationError as e: return e.serialized, status.HTTP_400_BAD_REQUEST if priority.is_default: SC.session.query(models.Priority).filter( project_id = priority.project_id, ).update({models.Priority.is_default: False}, synchronize_session = False) SC.session.add(priority) try: SC.session.commit() except IntegrityError: return 'Invalid priority', status.HTTP_400_BAD_REQUEST return schemas.PrioritySchema().serialize(priority)