Coverage for middle_layer/propose/application_layer/orm_repositories/author.py: 100.00%

25 statements  

« prev     ^ index     » next       coverage.py v7.10.5, created at 2026-03-09 06:13 +0000

1# Copyright 2024 Associated Universities, Inc. 

2# 

3# This file is part of Telescope Time Allocation Tools (TTAT). 

4# 

5# TTAT is free software: you can redistribute it and/or modify 

6# it under the terms of the GNU General Public License as published by 

7# the Free Software Foundation, either version 3 of the License, or 

8# any later version. 

9# 

10# TTAT is distributed in the hope that it will be useful, 

11# but WITHOUT ANY WARRANTY; without even the implied warranty of 

12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

13# GNU General Public License for more details. 

14# 

15# You should have received a copy of the GNU General Public License 

16# along with TTAT. If not, see <https://www.gnu.org/licenses/>. 

17# 

18from sqlalchemy.orm import Session 

19 

20from common.application_layer.orm_repositories import add_entity, get_object_by_id, list_entities 

21from propose.domain_layer.entities.proposal import Author 

22from propose.domain_layer.repositories.proposal import AuthorRepository 

23 

24 

25class AuthorORMRepository(AuthorRepository): 

26 def __init__(self, session: Session): 

27 self.session = session 

28 

29 def by_id(self, author_id: int) -> Author: 

30 return get_object_by_id(self.session, author_id, Author, Author.author_id) 

31 

32 def by_user_id_and_proposal_id(self, user_id: int, proposal_id: int) -> Author | None: 

33 return ( 

34 self.session.query(Author) 

35 .filter(Author.user_id == user_id) 

36 .filter(Author.proposal_id == proposal_id) 

37 .one_or_none() 

38 ) 

39 

40 def list_all(self) -> list[Author]: 

41 return list_entities(self.session, Author, Author.author_id) 

42 

43 def add(self, author: Author) -> int: 

44 add_entity(self.session, author) 

45 return author.author_id 

46 

47 # authors should never have their proposal or user id change; instead, create a new author. 

48 def update(self, author: Author) -> None: 

49 a = self.by_id(author.author_id) 

50 a.first_name = author.first_name 

51 a.last_name = author.last_name 

52 a.is_primary = author.is_primary 

53 self.session.flush() 

54 

55 def delete(self, author: Author) -> None: 

56 self.session.delete(author) 

57 self.session.flush()