WorkspaceApi.enable_notifications()   A
last analyzed

Complexity

Conditions 3

Size

Total Lines 4
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 4
nop 3
dl 0
loc 4
rs 10
c 0
b 0
f 0
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
from sqlalchemy.orm import Query
5
from sqlalchemy.orm import Session
6
from tracim.lib.utils.translation import fake_translator as _
7
8
from tracim.lib.core.userworkspace import RoleApi
9
from tracim.models.auth import Group
10
from tracim.models.auth import User
11
from tracim.models.data import UserRoleInWorkspace
12
from tracim.models.data import Workspace
13
14
__author__ = 'damien'
15
16
17
class WorkspaceApi(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...
18
19
    def __init__(
20
            self,
21
            session: Session,
22
            current_user: User,
23
            force_role: bool=False
0 ignored issues
show
Coding Style introduced by
Exactly one space required around keyword argument assignment
Loading history...
24
    ):
25
        """
26
        :param current_user: Current user of context
27
        :param force_role: If True, app role in queries even if admin
28
        """
29
        self._session = session
30
        self._user = current_user
31
        self._force_role = force_role
32
33
    def _base_query_without_roles(self):
34
        return self._session.query(Workspace).filter(Workspace.is_deleted == False)
0 ignored issues
show
introduced by
Comparison to False should be 'not expr' or 'expr is False'
Loading history...
35
36
    def _base_query(self):
37
        if not self._force_role and self._user.profile.id>=Group.TIM_ADMIN:
0 ignored issues
show
Coding Style introduced by
Exactly one space required around comparison
Loading history...
38
            return self._base_query_without_roles()
39
40
        return self._session.query(Workspace).\
41
            join(Workspace.roles).\
42
            filter(UserRoleInWorkspace.user_id == self._user.user_id).\
43
            filter(Workspace.is_deleted == False)
0 ignored issues
show
introduced by
Comparison to False should be 'not expr' or 'expr is False'
Loading history...
44
45
    def create_workspace(
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...
46
            self,
47
            label: str='',
0 ignored issues
show
Coding Style introduced by
Exactly one space required around keyword argument assignment
Loading history...
48
            description: str='',
0 ignored issues
show
Coding Style introduced by
Exactly one space required around keyword argument assignment
Loading history...
49
            calendar_enabled: bool=False,
0 ignored issues
show
Coding Style introduced by
Exactly one space required around keyword argument assignment
Loading history...
50
            save_now: bool=False,
0 ignored issues
show
Coding Style introduced by
Exactly one space required around keyword argument assignment
Loading history...
51
    ) -> Workspace:
52
        if not label:
53
            label = self.generate_label()
54
55
        workspace = Workspace()
56
        workspace.label = label
57
        workspace.description = description
58
        workspace.calendar_enabled = calendar_enabled
59
60
        # By default, we force the current user to be the workspace manager
61
        # And to receive email notifications
62
        role_api = RoleApi(
63
            session=self._session,
64
            current_user=self._user,
65
        )
66
67
        role = role_api.create_one(
68
            self._user,
69
            workspace,
70
            UserRoleInWorkspace.WORKSPACE_MANAGER,
71
            with_notif=True,
72
        )
73
74
        self._session.add(workspace)
75
        self._session.add(role)
76
77
        if save_now:
78
            self._session.flush()
79
80
        # TODO - G.M - 28-03-2018 - [Calendar] Reenable calendar stuff
0 ignored issues
show
Coding Style introduced by
TODO and FIXME comments should generally be avoided.
Loading history...
81
        # if calendar_enabled:
82
        #     self._ensure_calendar_exist(workspace)
83
        # else:
84
        #     self._disable_calendar(workspace)
85
86
        return workspace
87
88
    def get_one(self, id):
0 ignored issues
show
Coding Style Naming introduced by
The name id does not conform to the argument 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...
Bug Best Practice introduced by
This seems to re-define the built-in id.

It is generally discouraged to redefine built-ins as this makes code very hard to read.

Loading history...
89
        return self._base_query().filter(Workspace.workspace_id == id).one()
90
91
    def get_one_by_label(self, label: str) -> Workspace:
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...
92
        return self._base_query().filter(Workspace.label == label).one()
93
94
    """
95
    def get_one_for_current_user(self, id):
96
        return self._base_query().filter(Workspace.workspace_id==id).\
97
            session.query(ZKContact).filter(ZKContact.groups.any(ZKGroup.id.in_([1,2,3])))
98
            filter(sqla.).one()
99
    """
0 ignored issues
show
Unused Code introduced by
This string statement has no effect and could be removed.
Loading history...
100
101
    def get_all(self):
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...
102
        return self._base_query().all()
103
104
    def get_all_for_user(self, user: User, ignored_ids=None):
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...
Coding Style introduced by
This method could be written as a function/class method.

If a method does not access any attributes of the class, it could also be implemented as a function or static method. This can help improve readability. For example

class Foo:
    def some_method(self, x, y):
        return x + y;

could be written as

class Foo:
    @classmethod
    def some_method(cls, x, y):
        return x + y;
Loading history...
105
        workspaces = []
106
107
        for role in user.roles:
108
            if not role.workspace.is_deleted:
109
                if not ignored_ids:
110
                    workspaces.append(role.workspace)
111
                elif role.workspace.workspace_id not in ignored_ids:
112
                        workspaces.append(role.workspace)
0 ignored issues
show
Coding Style introduced by
The indentation here looks off. 20 spaces were expected, but 24 were found.
Loading history...
113
                else:
114
                    pass  # do not return workspace
115
116
        workspaces.sort(key=lambda workspace: workspace.label.lower())
117
        return workspaces
118
119
    def get_all_manageable(self) -> typing.List[Workspace]:
120
        """Get all workspaces the current user has manager rights on."""
121
        workspaces = []  # type: typing.List[Workspace]
122
        if self._user.profile.id == Group.TIM_ADMIN:
123
            workspaces = self._base_query().order_by(Workspace.label).all()
124
        elif self._user.profile.id == Group.TIM_MANAGER:
125
            workspaces = self._base_query() \
126
                .filter(
127
                    UserRoleInWorkspace.role ==
128
                    UserRoleInWorkspace.WORKSPACE_MANAGER
129
                ) \
130
                .order_by(Workspace.label) \
131
                .all()
132
        return workspaces
133
134
    def disable_notifications(self, user: User, workspace: Workspace):
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...
Coding Style introduced by
This method could be written as a function/class method.

If a method does not access any attributes of the class, it could also be implemented as a function or static method. This can help improve readability. For example

class Foo:
    def some_method(self, x, y):
        return x + y;

could be written as

class Foo:
    @classmethod
    def some_method(cls, x, y):
        return x + y;
Loading history...
135
        for role in user.roles:
136
            if role.workspace==workspace:
0 ignored issues
show
Coding Style introduced by
Exactly one space required around comparison
Loading history...
137
                role.do_notify = False
138
139
    def enable_notifications(self, user: User, workspace: Workspace):
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...
Coding Style introduced by
This method could be written as a function/class method.

If a method does not access any attributes of the class, it could also be implemented as a function or static method. This can help improve readability. For example

class Foo:
    def some_method(self, x, y):
        return x + y;

could be written as

class Foo:
    @classmethod
    def some_method(cls, x, y):
        return x + y;
Loading history...
140
        for role in user.roles:
141
            if role.workspace==workspace:
0 ignored issues
show
Coding Style introduced by
Exactly one space required around comparison
Loading history...
142
                role.do_notify = True
143
144
    def get_notifiable_roles(self, workspace: Workspace) -> [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...
145
        roles = []
146
        for role in workspace.roles:
147
            if role.do_notify==True \
0 ignored issues
show
Coding Style introduced by
Exactly one space required around comparison
Loading history...
introduced by
Comparison to True should be just 'expr' or 'expr is True'
Loading history...
148
                    and role.user!=self._user \
0 ignored issues
show
Coding Style introduced by
Exactly one space required around comparison
Loading history...
149
                    and role.user.is_active:
150
                roles.append(role)
151
        return roles
152
153
    def save(self, workspace: Workspace):
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 workspace seems to be unused.
Loading history...
154
        self._session.flush()
155
156
    def delete_one(self, 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...
157
        workspace = self.get_one(workspace_id)
158
        workspace.is_deleted = True
159
160
        if flush:
161
            self._session.flush()
162
163
    def restore_one(self, 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...
164
        workspace = self._session.query(Workspace)\
165
            .filter(Workspace.is_deleted==True)\
0 ignored issues
show
Coding Style introduced by
Exactly one space required around comparison
Loading history...
introduced by
Comparison to True should be just 'expr' or 'expr is True'
Loading history...
166
            .filter(Workspace.workspace_id==workspace_id).one()
0 ignored issues
show
Coding Style introduced by
Exactly one space required around comparison
Loading history...
167
        workspace.is_deleted = False
168
169
        if flush:
170
            self._session.flush()
171
172
        return workspace
173
174
    def execute_created_workspace_actions(self, workspace: Workspace) -> None:
0 ignored issues
show
Coding Style Naming introduced by
The name execute_created_workspace_actions 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...
175
        pass
176
        # TODO - G.M - 28-03-2018 - [Calendar] Re-enable this calendar stuff
0 ignored issues
show
Coding Style introduced by
TODO and FIXME comments should generally be avoided.
Loading history...
177
        # self.ensure_calendar_exist(workspace)
178
179
    # TODO - G.M - 28-03-2018 - [Calendar] Re-enable this calendar stuff
0 ignored issues
show
Coding Style introduced by
TODO and FIXME comments should generally be avoided.
Loading history...
180
    # def ensure_calendar_exist(self, workspace: Workspace) -> None:
181
    #     # Note: Cyclic imports
182
    #     from tracim.lib.calendar import CalendarManager
183
    #     from tracim.model.organisational import WorkspaceCalendar
184
    #
185
    #     calendar_manager = CalendarManager(self._user)
186
    #
187
    #     try:
188
    #         calendar_manager.enable_calendar_file(
189
    #             calendar_class=WorkspaceCalendar,
190
    #             related_object_id=workspace.workspace_id,
191
    #             raise_=True,
192
    #         )
193
    #     # If previous calendar file no exist, calendar must be created
194
    #     except FileNotFoundError:
195
    #         self._user.ensure_auth_token()
196
    #
197
    #         # Ensure database is up-to-date
198
    #         self.session.flush()
199
    #         transaction.commit()
200
    #
201
    #         calendar_manager.create_then_remove_fake_event(
202
    #             calendar_class=WorkspaceCalendar,
203
    #             related_object_id=workspace.workspace_id,
204
    #         )
205
    #
206
    # def disable_calendar(self, workspace: Workspace) -> None:
207
    #     # Note: Cyclic imports
208
    #     from tracim.lib.calendar import CalendarManager
209
    #     from tracim.model.organisational import WorkspaceCalendar
210
    #
211
    #     calendar_manager = CalendarManager(self._user)
212
    #     calendar_manager.disable_calendar_file(
213
    #         calendar_class=WorkspaceCalendar,
214
    #         related_object_id=workspace.workspace_id,
215
    #         raise_=False,
216
    #     )
217
218
    def get_base_query(self) -> Query:
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...
219
        return self._base_query()
220
221
    def generate_label(self) -> str:
222
        """
223
        :return: Generated workspace label
224
        """
225
        query = self._base_query_without_roles() \
226
            .filter(Workspace.label.ilike('{0}%'.format(
227
                _('Workspace'),
228
            )))
229
230
        return _('Workspace {}').format(
231
            query.count() + 1,
232
        )
233
234
235
class UnsafeWorkspaceApi(WorkspaceApi):
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...
236
    def _base_query(self):
237
        return self.session.query(Workspace).filter(Workspace.is_deleted==False)
0 ignored issues
show
Coding Style introduced by
Exactly one space required around comparison
Loading history...
Bug introduced by
Instance of 'UnsafeWorkspaceApi' has no 'session' member; maybe '_session'?

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
introduced by
Comparison to False should be 'not expr' or 'expr is False'
Loading history...
238