tracim.lib.core.userworkspace   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 133
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 87
dl 0
loc 133
rs 10
c 0
b 0
f 0
wmc 17

13 Methods

Rating   Name   Duplication   Size   Complexity  
A RoleApi._get_all_for_user() 0 3 1
A RoleApi.get_one() 0 2 1
A RoleApi.create_one() 0 16 2
A RoleApi._get_one_rsc() 0 9 1
A RoleApi.delete_one() 0 4 2
A RoleApi.role_can_read_member_role() 0 11 2
A RoleApi.get_roles_for_select_field() 0 14 2
A RoleApi.get_all_for_user_order_by_workspace() 0 6 1
A RoleApi.get_all_for_workspace() 0 3 1
A RoleApi.save() 0 2 1
A RoleApi.__init__() 0 3 1
A RoleApi.create_role() 0 5 1
A RoleApi.get_all_for_user() 0 2 1
1
# -*- coding: utf-8 -*-
0 ignored issues
show
Coding Style introduced by
This module should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
2
import typing
3
4
__author__ = 'damien'
5
6
from sqlalchemy.orm import Session
7
from tracim.models.auth import User
8
from tracim.models.data import Workspace
9
from tracim.models.data import UserRoleInWorkspace
10
from tracim.models.data import RoleType
11
12
13
class RoleApi(object):
0 ignored issues
show
Coding Style introduced by
This class should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
Unused Code introduced by
The variable __class__ seems to be unused.
Loading history...
14
15
    ALL_ROLE_VALUES = UserRoleInWorkspace.get_all_role_values()
16
    # Dict containing readable members roles for given role
17
    members_read_rights = {
18
        UserRoleInWorkspace.NOT_APPLICABLE: [],
19
        UserRoleInWorkspace.READER: [
20
            UserRoleInWorkspace.WORKSPACE_MANAGER,
21
        ],
22
        UserRoleInWorkspace.CONTRIBUTOR: [
23
            UserRoleInWorkspace.WORKSPACE_MANAGER,
24
            UserRoleInWorkspace.CONTENT_MANAGER,
25
            UserRoleInWorkspace.CONTRIBUTOR,
26
        ],
27
        UserRoleInWorkspace.CONTENT_MANAGER: [
28
            UserRoleInWorkspace.WORKSPACE_MANAGER,
29
            UserRoleInWorkspace.CONTENT_MANAGER,
30
            UserRoleInWorkspace.CONTRIBUTOR,
31
            UserRoleInWorkspace.READER,
32
        ],
33
        UserRoleInWorkspace.WORKSPACE_MANAGER: [
34
            UserRoleInWorkspace.WORKSPACE_MANAGER,
35
            UserRoleInWorkspace.CONTENT_MANAGER,
36
            UserRoleInWorkspace.CONTRIBUTOR,
37
            UserRoleInWorkspace.READER,
38
        ],
39
    }
40
41
    @classmethod
42
    def role_can_read_member_role(cls, reader_role: int, tested_role: int) \
43
            -> bool:
44
        """
45
        :param reader_role: role as viewer
46
        :param tested_role: role as viwed
47
        :return: True if given role can view member role in workspace.
48
        """
49
        if reader_role in cls.members_read_rights:
50
            return tested_role in cls.members_read_rights[reader_role]
51
        return False
52
53
    @classmethod
54
    def create_role(cls) -> UserRoleInWorkspace:
0 ignored issues
show
Coding Style introduced by
This method should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
55
        role = UserRoleInWorkspace()
56
57
        return role
58
59
    def __init__(self, session: Session, current_user: typing.Optional[User]):
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable typing does not seem to be defined.
Loading history...
60
        self._session = session
61
        self._user = current_user
62
63
    def _get_one_rsc(self, user_id, workspace_id):
64
        """
65
        :param user_id:
66
        :param workspace_id:
67
        :return: a Query object, filtered query but without fetching the object.
68
        """
69
        return self._session.query(UserRoleInWorkspace).\
70
            filter(UserRoleInWorkspace.workspace_id == workspace_id).\
71
            filter(UserRoleInWorkspace.user_id == user_id)
72
73
    def get_one(self, user_id, workspace_id):
0 ignored issues
show
Coding Style introduced by
This method should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
74
        return self._get_one_rsc(user_id, workspace_id).one()
75
76
    def create_one(
0 ignored issues
show
Coding Style introduced by
This method should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
best-practice introduced by
Too many arguments (6/5)
Loading history...
77
            self,
78
            user: User,
79
            workspace: Workspace,
80
            role_level: int,
81
            with_notif: bool,
82
            flush: bool=True
0 ignored issues
show
Coding Style introduced by
Exactly one space required around keyword argument assignment
Loading history...
83
    ) -> UserRoleInWorkspace:
84
        role = self.create_role()
85
        role.user_id = user.user_id
86
        role.workspace = workspace
87
        role.role = role_level
88
        role.do_notify = with_notif
89
        if flush:
90
            self._session.flush()
91
        return role
92
93
    def delete_one(self, user_id, workspace_id, flush=True):
0 ignored issues
show
Coding Style introduced by
This method should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
94
        self._get_one_rsc(user_id, workspace_id).delete()
95
        if flush:
96
            self._session.flush()
97
98
    def _get_all_for_user(self, user_id):
99
        return self._session.query(UserRoleInWorkspace)\
100
            .filter(UserRoleInWorkspace.user_id == user_id)
101
102
    def get_all_for_user(self, user_id):
0 ignored issues
show
Coding Style introduced by
This method should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
103
        return self._get_all_for_user(user_id).all()
104
105
    def get_all_for_user_order_by_workspace(
0 ignored issues
show
Coding Style Naming introduced by
The name get_all_for_user_order_by_workspace does not conform to the method naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
Coding Style introduced by
This method should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
106
            self,
107
            user_id: int
108
    ) -> UserRoleInWorkspace:
109
        return self._get_all_for_user(user_id)\
110
            .join(UserRoleInWorkspace.workspace).order_by(Workspace.label).all()
111
112
    def get_all_for_workspace(self, workspace_id):
0 ignored issues
show
Coding Style introduced by
This method should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
113
        return self._session.query(UserRoleInWorkspace)\
114
            .filter(UserRoleInWorkspace.workspace_id == workspace_id).all()
115
116
    def save(self, role: UserRoleInWorkspace):
0 ignored issues
show
Coding Style introduced by
This method should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
Unused Code introduced by
The argument role seems to be unused.
Loading history...
117
        self._session.flush()
118
119
    @classmethod
120
    def get_roles_for_select_field(cls):
121
        """
122
123
        :return: list of DictLikeClass instances representing available Roles
124
        (to be used in select fields)
125
        """
126
        result = list()
127
128
        for role_id in UserRoleInWorkspace.get_all_role_values():
129
            role = RoleType(role_id)
130
            result.append(role)
131
132
        return result
133