Passed
Push — master ( b94d48...186be2 )
by
unknown
02:40
created

tracim.lib.core.user.UserApi.create_user()   A

Complexity

Conditions 4

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 10
nop 4
dl 0
loc 15
rs 9.2
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 threading
0 ignored issues
show
Unused Code introduced by
The import threading seems to be unused.
Loading history...
3
4
import transaction
5
import typing as typing
0 ignored issues
show
introduced by
standard import "import typing as typing" should be placed before "import transaction"
Loading history...
6
7
from tracim.models.auth import User
8
9
10
class UserApi(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...
11
12
    def __init__(self, current_user: typing.Optional[User], session, config):
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable typing does not seem to be defined.
Loading history...
13
        self._session = session
14
        self._user = current_user
15
        self._config = config
16
17
    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...
18
        return self._session.query(User).order_by(User.display_name).all()
19
20
    def _base_query(self):
21
        return self._session.query(User)
22
23
    def get_one(self, user_id: int):
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...
24
        return self._base_query().filter(User.user_id==user_id).one()
0 ignored issues
show
Coding Style introduced by
Exactly one space required around comparison
Loading history...
25
26
    def get_one_by_email(self, email: str):
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...
27
        return self._base_query().filter(User.email==email).one()
0 ignored issues
show
Coding Style introduced by
Exactly one space required around comparison
Loading history...
28
29
    def get_one_by_id(self, id: int) -> User:
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...
30
        return self._base_query().filter(User.user_id==id).one()
0 ignored issues
show
Coding Style introduced by
Exactly one space required around comparison
Loading history...
31
32
    def update(
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...
33
            self,
34
            user: User,
35
            name: str=None,
0 ignored issues
show
Coding Style introduced by
Exactly one space required around keyword argument assignment
Loading history...
36
            email: str=None,
0 ignored issues
show
Coding Style introduced by
Exactly one space required around keyword argument assignment
Loading history...
37
            do_save=True,
38
            timezone: str='',
0 ignored issues
show
Coding Style introduced by
Exactly one space required around keyword argument assignment
Loading history...
39
    ):
40
        if name is not None:
41
            user.display_name = name
42
43
        if email is not None:
44
            user.email = email
45
46
        user.timezone = timezone
47
48
        if do_save:
49
            self.save(user)
50
51
    def user_with_email_exists(self, email: str):
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...
52
        try:
53
            self.get_one_by_email(email)
54
            return True
55
        # TODO - G.M - 09-04-2018 - Better exception
0 ignored issues
show
Coding Style introduced by
TODO and FIXME comments should generally be avoided.
Loading history...
56
        except:
0 ignored issues
show
Coding Style Best Practice introduced by
General except handlers without types should be used sparingly.

Typically, you would use general except handlers when you intend to specifically handle all types of errors, f.e. when logging. Otherwise, such general error handlers can mask errors in your application that you want to know of.

Loading history...
57
            return False
58
59
    def create_user(self, email=None, groups=[], save_now=False) -> User:
0 ignored issues
show
Bug Best Practice introduced by
The default value [] might cause unintended side-effects.

Objects as default values are only created once in Python and not on each invocation of the function. If the default object is modified, this modification is carried over to the next invocation of the method.

# Bad:
# If array_param is modified inside the function, the next invocation will
# receive the modified object.
def some_function(array_param=[]):
    # ...

# Better: Create an array on each invocation
def some_function(array_param=None):
    array_param = array_param or []
    # ...
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...
60
        user = User()
61
62
        if email:
63
            user.email = email
64
65
        for group in groups:
66
            user.groups.append(group)
67
68
        self._session.add(user)
69
70
        if save_now:
71
            self._session.flush()
72
73
        return user
74
75
    def save(self, user: User):
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 user seems to be unused.
Loading history...
76
        self._session.flush()
77
78
    def execute_created_user_actions(self, created_user: User) -> None:
79
        """
80
        Execute actions when user just been created
81
        :return:
82
        """
83
        # NOTE: Cyclic import
84
        # 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...
85
        #from tracim.lib.calendar import CalendarManager
86
        #from tracim.model.organisational import UserCalendar
87
88
        # TODO - G.M - 04-04-2018 - [auth]
0 ignored issues
show
Coding Style introduced by
TODO and FIXME comments should generally be avoided.
Loading history...
89
        # Check if this is already needed with
90
        # new auth system
91
        created_user.ensure_auth_token(
92
            session=self._session,
93
            validity_seconds=self._config.USER_AUTH_TOKEN_VALIDITY
94
        )
95
96
        # Ensure database is up-to-date
97
        self._session.flush()
98
        transaction.commit()
99
100
        # 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...
101
        # calendar_manager = CalendarManager(created_user)
102
        # calendar_manager.create_then_remove_fake_event(
103
        #     calendar_class=UserCalendar,
104
        #     related_object_id=created_user.user_id,
105
        # )
106