group.py 1.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. # -*- coding: utf-8 -*-
  2. import typing
  3. from sqlalchemy.orm.exc import NoResultFound
  4. from tracim.exceptions import GroupDoesNotExist
  5. from tracim import CFG
  6. __author__ = 'damien'
  7. from tracim.models.auth import Group, User
  8. from sqlalchemy.orm import Query
  9. from sqlalchemy.orm import Session
  10. class GroupApi(object):
  11. def __init__(
  12. self,
  13. session: Session,
  14. current_user: typing.Optional[User],
  15. config: CFG
  16. ):
  17. self._user = current_user
  18. self._session = session
  19. self._config = config
  20. def _base_query(self) -> Query:
  21. return self._session.query(Group)
  22. def get_one(self, group_id) -> Group:
  23. try:
  24. group = self._base_query().filter(Group.group_id == group_id).one()
  25. return group
  26. except NoResultFound:
  27. raise GroupDoesNotExist()
  28. def get_one_with_name(self, group_name) -> Group:
  29. try:
  30. group = self._base_query().filter(Group.group_name == group_name).one()
  31. return group
  32. except NoResultFound:
  33. raise GroupDoesNotExist()
  34. def get_all(self):
  35. return self._base_query().order_by(Group.group_id).all()