18
18
from __future__ import unicode_literals
19
19
20
20
import json
21
+ import logging
21
22
import six
22
23
23
24
from sqlalchemy import Column
32
33
from timesketch .models import BaseModel
33
34
from timesketch .models import db_session
34
35
36
+ logger = logging .getLogger ("timesketch.models.annotations" )
37
+
35
38
36
39
class BaseAnnotation (object ):
37
40
"""Base class with common attributes."""
@@ -140,9 +143,20 @@ def remove_label(self, label):
140
143
Args:
141
144
label: Name of the label.
142
145
"""
143
- for label_obj in self .labels :
144
- if label_obj .label .lower () != label .lower ():
145
- continue
146
+ labels_to_remove = [
147
+ label_obj
148
+ for label_obj in self .labels
149
+ if label_obj .label .lower () == label .lower ()
150
+ ]
151
+
152
+ if not labels_to_remove :
153
+ logger .warning (
154
+ "Attempted to remove non-existent label: %s from object: %s" ,
155
+ str (label ),
156
+ str (type (self ).__name__ ),
157
+ )
158
+
159
+ for label_obj in labels_to_remove :
146
160
self .labels .remove (label_obj )
147
161
db_session .add (self )
148
162
db_session .commit ()
@@ -244,15 +258,25 @@ def remove_comment(self, comment_id):
244
258
245
259
Args:
246
260
comment_id: Id of the comment.
261
+
262
+ Returns:
263
+ True if the comment was removed, False otherwise.
247
264
"""
248
- for comment_obj in self .comments :
249
- if comment_obj .id == int (comment_id ):
250
- self .comments .remove (comment_obj )
251
- db_session .add (self )
252
- db_session .commit ()
253
- return True
254
265
255
- return False
266
+ comments_to_remove = [
267
+ comment_obj
268
+ for comment_obj in self .comments
269
+ if comment_obj .id == int (comment_id )
270
+ ]
271
+ if not comments_to_remove :
272
+ logger .debug ("Comment to delete not found" )
273
+ return False # Comment not found
274
+ for comment_obj in comments_to_remove :
275
+ logger .debug ("Removing comment" )
276
+ self .comments .remove (comment_obj )
277
+ db_session .add (self )
278
+ db_session .commit ()
279
+ return True
256
280
257
281
def get_comment (self , comment_id ):
258
282
"""Retrieves a comment.
@@ -329,8 +353,7 @@ def set_status(self, status):
329
353
Args:
330
354
status: Name of the status
331
355
"""
332
- for _status in self .status :
333
- self .status .remove (_status )
356
+ self .status = [] # replace the list with an empty list.
334
357
self .status .append (self .Status (user = None , status = status ))
335
358
db_session .add (self )
336
359
db_session .commit ()
@@ -339,11 +362,30 @@ def set_status(self, status):
339
362
def get_status (self ):
340
363
"""Get the current status.
341
364
365
+ Only one status should be in the database at a time.
366
+
367
+ Raises:
368
+ RuntimeError: If more than one status is available.
369
+
342
370
Returns:
343
371
The status as a string
344
372
"""
345
373
if not self .status :
346
374
self .status .append (self .Status (user = None , status = "new" ))
375
+ if len (self .status ) > 1 :
376
+ self_id = self .id if hasattr (self , "id" ) else None
377
+ # TODO: Change from warning to raising an exception once we ensured
378
+ # it won't affect the deployment.
379
+ # raise RuntimeError(
380
+ # "More than one status available for object [%s] with ID: [%s]",
381
+ # str(type(self).__name__),
382
+ # str(self_id),
383
+ # )
384
+ logging .warning (
385
+ "More than one status available for object [%s] with ID: [%s]" ,
386
+ str (type (self ).__name__ ),
387
+ str (self_id ),
388
+ )
347
389
return self .status [0 ]
348
390
349
391
0 commit comments