Passed
Push — master ( 1c6cce...6ac011 )
by Markus
02:31
created

chaoswg.models.ModelBase.get_all()   A

Complexity

Conditions 1

Size

Total Lines 3
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
from datetime import datetime
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
3
from peewee import (CharField, IntegerField, DateTimeField, SmallIntegerField, ForeignKeyField,
4
                    FloatField, DoesNotExist)
5
from playhouse.flask_utils import FlaskDB
6
from werkzeug.security import generate_password_hash, check_password_hash
7
8
db_wrapper = FlaskDB()
0 ignored issues
show
Coding Style Naming introduced by
The name db_wrapper does not conform to the constant naming conventions ((([A-Z_][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...
9
10
11
def init_database(app):
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...
12
    db_wrapper.init_app(app)
13
    return db_wrapper.database
14
15
16
def create_tables():
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...
17
    db_wrapper.database.connect()
18
    db_wrapper.database.create_tables([User, Room, Task, History], safe=True)
19
    db_wrapper.database.close()
20
21
22
def insert_testdata():
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...
23
    pwhash = generate_password_hash('123456')
24
    with db_wrapper.database.atomic():
25
        # User.get_or_create(username='User1', defaults={'password': pwhash})
26
        # User.get_or_create(username='User2', defaults={'password': pwhash})
27
        # User.get_or_create(username='User3', defaults={'password': pwhash})
28
        # User.get_or_create(username='User4', defaults={'password': pwhash})
29
        # User.get_or_create(username='User5', defaults={'password': pwhash})
30
31
        Room.get_or_create(room='Bibliothek')
32
        Room.get_or_create(room='Flur')
33
        Room.get_or_create(room='Küche')
34
        Room.get_or_create(room='großes Bad')
35
        Room.get_or_create(room='kleines Bad')
36
        Room.get_or_create(room='Abstellzimmer')
37
        Room.get_or_create(room='Dachterrasse')
38
39
        Task.get_or_create(task='Kühlschrankcheck',
40
                           defaults={'base_points': 2, 'time_factor': 0.0, 'state': Task.BACKLOG})
41
        Task.get_or_create(task='Grünabfall',
42
                           defaults={'base_points': 1, 'time_factor': 0.0, 'state': Task.BACKLOG})
43
        Task.get_or_create(task='Fenster putzen',
44
                           defaults={'base_points': 3, 'time_factor': 0.0, 'state': Task.BACKLOG})
45
        Task.get_or_create(task='Ofen reinigen',
46
                           defaults={'base_points': 3, 'time_factor': 0.0, 'state': Task.BACKLOG})
47
        Task.get_or_create(task='Tiefkühler enteisen',
48
                           defaults={'base_points': 8, 'time_factor': 0.0, 'state': Task.BACKLOG})
49
        Task.get_or_create(task='Saugen + Wischen',
50
                           defaults={'base_points': 13, 'time_factor': 0.0, 'state': Task.TODO})
51
        Task.get_or_create(task='großes Bad',
52
                           defaults={'base_points': 8, 'time_factor': 0.0, 'state': Task.TODO})
53
        Task.get_or_create(task='kleines Bad',
54
                           defaults={'base_points': 3, 'time_factor': 0.0, 'state': Task.DONE})
55
        Task.get_or_create(task='Rasen mähen + harken',
56
                           defaults={'base_points': 13, 'time_factor': 0.0, 'state': Task.TODO})
57
        Task.get_or_create(task='Küche putzen',
58
                           defaults={'base_points': 2, 'time_factor': 0.0, 'state': Task.DONE})
59
        Task.get_or_create(task='Abwaschen',
60
                           defaults={'base_points': 2, 'time_factor': 0.0, 'state': Task.DONE})
61
        Task.get_or_create(task='Einkaufen',
62
                           defaults={'base_points': 3, 'time_factor': 0.0, 'state': Task.DONE})
63
        Task.get_or_create(task='Pappe entsorgen',
64
                           defaults={'base_points': 2, 'time_factor': 0.0, 'state': Task.DONE})
65
        Task.get_or_create(task='Müll entsorgen',
66
                           defaults={'base_points': 1, 'time_factor': 0.0, 'state': Task.DONE})
67
        Task.get_or_create(task='Glas wegbringen',
68
                           defaults={'base_points': 2, 'time_factor': 0.0, 'state': Task.DONE})
69
        Task.get_or_create(task='Geschirrspüler ausräumen',
70
                           defaults={'base_points': 2, 'time_factor': 0.0, 'state': Task.DONE})
71
72
73
class ModelBase(db_wrapper.Model):
1 ignored issue
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...
74
    @classmethod
75
    def get_all(cls):
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...
76
        return list(cls.select().dicts())
77
78
79
class User(ModelBase):
1 ignored issue
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...
80
    username = CharField(unique=True)
81
    password = CharField()
82
    points = IntegerField(default=0)
83
    last_update = DateTimeField(default=datetime.utcnow)
84
85
    @classmethod
86
    def get_all(cls):
87
        """
88
        without password
89
        :return:
90
        """
91
        return list(cls.select(cls.username, cls.points, cls.last_update).dicts())
92
93
    @classmethod
94
    def get_by_name(cls, username):
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...
95
        try:
96
            return cls.get(cls.username == username)
97
        except DoesNotExist:
98
            return False
99
100
    @classmethod
101
    def get_usernames(cls):
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
        query = cls.select(cls.username)
103
        result = set()
104
        for user in query:
105
            result.add(user.username)
106
        return result
107
108
    def set_password(self, password):
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...
109
        self.password = generate_password_hash(password)
110
111
    def check_password(self, password):
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...
112
        return check_password_hash(self.password, password)
113
114
    # Flask-Login required functions
115
    @property
116
    def is_active(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...
117
        return True
118
119
    @property
120
    def is_authenticated(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...
121
        return True
122
123
    @property
124
    def is_anonymous(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...
125
        return False
126
127
    def get_id(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...
128
        return self.id
129
130
    # Required for administrative interface
131
    def __unicode__(self):
132
        return self.username
133
134
135
class Room(db_wrapper.Model):
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...
136
    room = CharField(unique=True)
137
138
139
class Task(ModelBase):
1 ignored issue
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...
140
    task = CharField(unique=True)
141
    base_points = SmallIntegerField()
142
    time_factor = FloatField(default=0.0)
143
    state = SmallIntegerField(index=True, default=0)
144
    BACKLOG = 0
145
    TODO = 1
146
    DONE = 2
147
    room = ForeignKeyField(Room, null=True)
148
    todo_time = DateTimeField(null=True)
149
    last_done = DateTimeField(null=True)
150
151
    @property
152
    def points(self):
153
        """
154
        Calculate real point value based on time_factor.
155
        :return:
156
        """
157
        now = datetime.utcnow()
158
        # set last_time to now if todo_time is not set.
159
        last_time = now if not self.todo_time else self.todo_time
160
        real_points = self.base_points + (self.time_factor * (now - last_time).days)
161
        return int(real_points)
162
163
    @classmethod
164
    def get_all(cls):
165
        """
166
        Overwrite get_all() method because we want to have active tasks only and
167
        need the property but dicts() doesn't work with it
168
        :return:
169
        """
170
        return list(cls.select().order_by(cls.base_points.desc()))
171
172
    @classmethod
173
    def set_state(cls, task_id, state, 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...
174
        now = datetime.utcnow()
175
        points_obtained = 0
176
        with db_wrapper.database.atomic():
177
            # update task state and time
178
            task = cls.get(cls.id == task_id)
179
            task.state = state
180
            if state == cls.DONE:
181
                points_obtained = task.points
182
                task.todo_time = None
183
                task.last_done = now
184
            elif state == cls.TODO:
185
                task.todo_time = now
186
            elif state == cls.BACKLOG:
187
                task.todo_time = None
188
            task.save()
189
190
            # update user points if new state is DONE (user got points)
191
            if points_obtained > 0:
192
                User.update(points=User.points + points_obtained, last_update=now).where(
193
                    User.id == user_id).execute()
194
195
                # add to history
196
                History.create(task=task.task, user=user_id, points=points_obtained, time=now)
197
198
    @staticmethod
199
    def do_custom_task(task, points, 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...
200
        now = datetime.utcnow()
201
        with db_wrapper.database.atomic():
202
            # update user points
203
            User.update(points=User.points + points, last_update=now).where(User.id == user_id).execute()
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (105/100).

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

Loading history...
204
            # add to history
205
            History.create(task=task, user=user_id, points=points, time=now)
206
207
208
class History(ModelBase):
1 ignored issue
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...
209
    task = CharField()
210
    user = ForeignKeyField(User)
211
    points = SmallIntegerField()
212
    time = DateTimeField(default=datetime.utcnow())
213
214
    @classmethod
215
    def get_user_history(cls, 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...
216
        return list(
217
            cls.select(cls.time, cls.task, cls.points)
218
                .join(User, on=(cls.user == User.id))
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation (remove 4 spaces).
Loading history...
219
                .where(User.username == user)
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation (remove 4 spaces).
Loading history...
220
                .order_by(cls.time.desc()).dicts())
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation (remove 4 spaces).
Loading history...
221
222
    @classmethod
223
    def get_full_history(cls):
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...
224
        return list(
225
            cls.select(cls.time, cls.points, User.username)
226
                .join(User, on=(cls.user == User.id))
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation (remove 4 spaces).
Loading history...
227
                .order_by(cls.time.asc()).dicts())
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation (remove 4 spaces).
Loading history...
228