RevisionsIntegrity.add_to_updatable()   A
last analyzed

Complexity

Conditions 3

Size

Total Lines 7
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nop 2
dl 0
loc 7
rs 9.4285
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
from sqlalchemy.orm import Session
3
from sqlalchemy import inspect
4
from sqlalchemy.orm.unitofwork import UOWTransaction
5
from transaction import TransactionManager
6
from contextlib import contextmanager
0 ignored issues
show
introduced by
standard import "from contextlib import contextmanager" should be placed before "from sqlalchemy.orm import Session"
Loading history...
7
8
from tracim.exceptions import ContentRevisionDeleteError
9
from tracim.exceptions import ContentRevisionUpdateError
10
from tracim.exceptions import SameValueError
11
12
from tracim.models.data import ContentRevisionRO
13
from tracim.models.data import Content
14
from tracim.models.meta import DeclarativeBase
15
16
17
def prevent_content_revision_delete(
0 ignored issues
show
Coding Style introduced by
This function 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
        session: Session,
19
        flush_context: UOWTransaction,
0 ignored issues
show
Unused Code introduced by
The argument flush_context seems to be unused.
Loading history...
20
        instances: [DeclarativeBase]
0 ignored issues
show
Unused Code introduced by
The argument instances seems to be unused.
Loading history...
21
) -> None:
22
    for instance in session.deleted:
23
        if isinstance(instance, ContentRevisionRO) \
24
                and instance.revision_id is not None:
25
            raise ContentRevisionDeleteError(
26
                "ContentRevision is not deletable. " +
27
                "You must make a new revision with" +
28
                "is_deleted set to True. Look at " +
29
                "tracim.model.new_revision context " +
30
                "manager to make a new revision"
31
            )
32
33
34
class RevisionsIntegrity(object):
0 ignored issues
show
Unused Code introduced by
The variable __class__ seems to be unused.
Loading history...
35
    """
36
    Simple static used class to manage a list with list of ContentRevisionRO
37
    who are allowed to be updated.
38
39
    When modify an already existing (understood have an identity in databse)
40
    ContentRevisionRO, if it's not in RevisionsIntegrity._updatable_revisions
41
    list, a ContentRevisionUpdateError thrown.
42
43
    This class is used by tracim.model.new_revision context manager.
44
    """
45
    _updatable_revisions = []
46
47
    @classmethod
48
    def add_to_updatable(cls, revision: 'ContentRevisionRO') -> 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...
49
        if inspect(revision).has_identity:
50
            raise ContentRevisionUpdateError("ContentRevision is not updatable. %s already have identity." % revision)  # nopep8
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (128/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
51
52
        if revision not in cls._updatable_revisions:
53
            cls._updatable_revisions.append(revision)
54
55
    @classmethod
56
    def remove_from_updatable(cls, revision: 'ContentRevisionRO') -> 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...
57
        if revision in cls._updatable_revisions:
58
            cls._updatable_revisions.remove(revision)
59
60
    @classmethod
61
    def is_updatable(cls, revision: 'ContentRevisionRO') -> bool:
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...
62
        return revision in cls._updatable_revisions
63
64
65
@contextmanager
66
def new_revision(
0 ignored issues
show
Coding Style Naming introduced by
The name tm 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...
67
        session: Session,
68
        tm: TransactionManager,
69
        content: Content,
70
        force_create_new_revision: bool=False,
0 ignored issues
show
Coding Style introduced by
Exactly one space required around keyword argument assignment
Loading history...
71
) -> Content:
72
    """
73
    Prepare context to update a Content. It will add a new updatable revision
74
    to the content.
75
    :param session: Database _session
76
    :param tm: TransactionManager
77
    :param content: Content instance to update
78
    :param force_create_new_revision: Decide if new_rev should or should not
79
    be forced.
80
    :return:
81
    """
82
    with session.no_autoflush:
83
        try:
84
            if force_create_new_revision \
85
                    or inspect(content.revision).has_identity:
86
                content.new_revision()
87
            RevisionsIntegrity.add_to_updatable(content.revision)
88
            yield content
89
        except SameValueError or ValueError as e:
0 ignored issues
show
introduced by
Exception to catch is the result of a binary "or" operation
Loading history...
Coding Style Naming introduced by
The name e does not conform to the variable 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...
90
            # INFO - 20-03-2018 - renew transaction when error happened
91
            # This avoid bad _session data like new "temporary" revision
92
            # to be add when problem happen.
93
            tm.abort()
94
            tm.begin()
95
            raise e
96
        finally:
97
            RevisionsIntegrity.remove_from_updatable(content.revision)
98