courses.py 3.23 KB
Newer Older
1
2
3
4
"""
Courses service
"""

Barbora Kompišová's avatar
Barbora Kompišová committed
5
from typing import List
Barbora Kompišová's avatar
Barbora Kompišová committed
6

Peter Stanko's avatar
Peter Stanko committed
7
from portal import logger
8
from portal.database.models import Course, Group, Role, User
9
from portal.service.general import GeneralService
Barbora Kompišová's avatar
Barbora Kompišová committed
10

Peter Stanko's avatar
Peter Stanko committed
11
log = logger.get_logger(__name__)
Barbora Kompišová's avatar
Barbora Kompišová committed
12
13


14
15
16
17
class CourseService(GeneralService):
    @property
    def _entity_klass(self):
        return Course
Barbora Kompišová's avatar
Barbora Kompišová committed
18

19
20
    def _set_data(self, entity, **data: dict):
        return self.update_entity(entity, data, allowed=['name', 'codename', 'description'])
Peter Stanko's avatar
Peter Stanko committed
21
22
23

    @property
    def course(self):
24
        return self._entity
Peter Stanko's avatar
Peter Stanko committed
25

26
    def create(self, **data) -> Course:
Peter Stanko's avatar
Peter Stanko committed
27
28
29
30
31
32
33
34
35
        """Creates new course

        Keyword Args:
            name(str): name of the course
            codename(str): codename of the course

        Returns(Course): Course instance
        """
        course = Course()
36
37
        self._entity = course
        self._set_data(entity=course, **data)
38
        log.info(f"[CREATE] Course {course.log_name} by {self.client_name}: {course.log_name}")
Peter Stanko's avatar
Peter Stanko committed
39
40
41
42
43
44
45
46
47
48
49
50
        return course

    def copy_course(self, target: Course, config: dict) -> Course:
        """Copies course and it's other resources (roles, groups, projects)
        The other resources that should be copied are specified in the config

        Args:
            target(Course): To which course the resources will be copied
            config(dict): Configuration to specify which resource should be copied

        Returns(Course): Copied course instance (target)
        """
51
        log.info(f"[COPY] Copy course from {self.course.log_name} -> {target.log_name}: {config}")
Peter Stanko's avatar
Peter Stanko committed
52
53
        if config.get('roles'):
            for role in self.course.roles:
54
                self._rest_service.roles(role).copy_role(target, with_clients=config['roles'])
Peter Stanko's avatar
Peter Stanko committed
55
56
        if config.get('groups'):
            for group in self.course.groups:
57
                self._rest_service.groups(group).copy_group(target, with_users=config['groups'])
Peter Stanko's avatar
Peter Stanko committed
58
59
        if config.get('projects'):
            for project in self.course.projects:
60
                self._rest_service.projects(project).copy_project(target)
Peter Stanko's avatar
Peter Stanko committed
61

62
        self.write_entity(target)
63
64
        log.info(f"[IMPORT] From {self.course.log_name} "
                 f"to {target.log_name} by {self.client_name}.")
Peter Stanko's avatar
Peter Stanko committed
65
66
67
68
69
70
71
72
73
74
75
76

        return target

    def update_notes_token(self, token: str) -> Course:
        """Updates notes access token of a course.

        Args:
            token(str): The new token

        Returns(Course): Course instance
        """
        self.course.notes_access_token = token
77
        self.write_entity(self.course)
78
        log.info(f"[UPDATE] Notes access token {self.course.log_name} "
79
                 f"by {self.client_name}")
Peter Stanko's avatar
Peter Stanko committed
80
81
        return self.course

82
    def get_clients_filtered(self, groups: List[str], roles: List[str], client_type=None) -> List[User]:
Peter Stanko's avatar
Peter Stanko committed
83
84
85
86
87
88
        """Get all users for course filtered
        Args:
            groups(list): Group names list
            roles(list):  Role names list
        Returns(List[User]):
        """
Peter Stanko's avatar
Peter Stanko committed
89
90
        groups_entities = Group.query.filter(Group.id.in_(groups)).all() if groups else None
        roles_entities = Role.query.filter(Role.id.in_(roles)).all() if roles else None
91
92
        return self.course.get_clients_filtered(groups_entities, roles_entities,
                                                client_type=client_type)