Skip to content

Commit 18693fa

Browse files
jfunezGustavo Fonseca
authored and
Gustavo Fonseca
committed
Reconhecer os status de serviços de validação de um pacote #810
1 parent 6eaba28 commit 18693fa

File tree

6 files changed

+163
-30
lines changed

6 files changed

+163
-30
lines changed

scielomanager/articletrack/models.py

+41-5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import caching.base
33
import datetime
44
import logging
5+
from collections import deque
56

67
from django.db import models
78
from django.db.models.signals import post_save
@@ -13,9 +14,12 @@
1314

1415
from articletrack import modelmanagers
1516
from journalmanager.models import Journal
17+
from scielomanager.utils import misc
1618

1719
logger = logging.getLogger(__name__)
1820

21+
SERVICE_STATUS_MAX_STAGES = 2 # The count of pairs (SERV_BEGIN, SERV_END) in notice.status at the end of processing
22+
1923
MSG_WORKFLOW_ACCEPTED = 'Checkin Accepted'
2024
MSG_WORKFLOW_REJECTED = 'Checkin Rejected'
2125
MSG_WORKFLOW_REVIEWED = 'Checkin Reviewed'
@@ -100,14 +104,46 @@ class Meta:
100104
ordering = ['-created_at']
101105
permissions = (("list_checkin", "Can list Checkin"),)
102106

107+
@property
108+
def is_serv_status_completed(self):
109+
"""
110+
If:
111+
the count of SERV_END < SERVICE_STATUS_MAX_STAGES, or
112+
the count of SERV_BEGIN < SERVICE_STATUS_MAX_STAGES
113+
then:
114+
the checkin's notices sequence is UNCOMPLETED (possible more notices will arrive)
115+
-> Return False
116+
Else:
117+
if the count of SERV_* is equal to 2 * SERVICE_STATUS_MAX_STAGES, then
118+
call the ``misc.validate_sequence`` function
119+
to check if the checkin's notices sequence is COMPLETED
120+
-> Return True
121+
else:
122+
-> Return False
123+
"""
124+
count_serv_end_notices = self.notices.filter(status__iexact="SERV_END").count()
125+
count_serv_begin_notices = self.notices.filter(status__iexact="SERV_BEGIN").count()
126+
if (count_serv_end_notices < SERVICE_STATUS_MAX_STAGES) or (count_serv_begin_notices < SERVICE_STATUS_MAX_STAGES):
127+
return False
128+
else:
129+
serv_ocurrs = self.notices.filter(status__istartswith="serv_").order_by('created_at')
130+
if serv_ocurrs.count() == (2 * SERVICE_STATUS_MAX_STAGES):
131+
sequence = [sym.status for sym in serv_ocurrs]
132+
return misc.validate_sequence(sequence)
133+
else:
134+
return False
135+
103136
@property
104137
def get_error_level(self):
105-
if self.notices.filter(status__iexact="error").count() > 0:
106-
return "error"
107-
elif self.notices.filter(status__iexact="warning").count() > 0:
108-
return "warning"
138+
if self.is_serv_status_completed:
139+
if self.notices.filter(status__iexact="error").count() > 0:
140+
return "error"
141+
elif self.notices.filter(status__iexact="warning").count() > 0:
142+
return "warning"
143+
else:
144+
return "ok"
109145
else:
110-
return "ok"
146+
return "in progress"
111147

112148
@property
113149
def get_newest_checkin(self):

scielomanager/articletrack/templates/articletrack/includes/checkin_list_and_filterform.html

+5-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,11 @@ <h3><i class="{{ icon_class|default:''}}"></i> {% trans list_title %}:</h3>
4949
<td>{{ checkin.article.issue_label }}</td>
5050
<td>{{ checkin.created_at|date:"d/m/Y - H:i" }}</td>
5151
<td>
52-
<span class="label label-{% trans_status checkin.get_error_level to_label='True' %}">
52+
{% if checkin.get_error_level == 'in progress' %}
53+
<span class="label label-info">
54+
{% else %}
55+
<span class="label label-{% trans_status checkin.get_error_level to_label='True' %}">
56+
{% endif %}
5357
{{ checkin.get_error_level }}
5458
</span>
5559
</td>

scielomanager/articletrack/templates/articletrack/notice_detail.html

+23-23
Original file line numberDiff line numberDiff line change
@@ -170,30 +170,30 @@ <h3>{% trans 'Notices' %}:</h3>
170170
</thead>
171171
<tbody>
172172
{% for notice in notices %}
173-
<tr class="{% trans_status notice.status %}">
174-
<td>
175-
{{ notice.stage }}
176-
</td>
177-
<td>
178-
<span class="label label-{% trans_status notice.status to_label='True' %}">
179-
{{ notice.status }}
180-
</span>
181-
</td>
182-
<td>
183-
{{ notice.message }}
184-
</td>
185-
<td>
186-
{{ notice.created_at|date:"d/m/Y - H:i" }}
187-
</td>
188-
</tr>
173+
<tr class="{% trans_status notice.status %}">
174+
<td>
175+
{{ notice.stage }}
176+
</td>
177+
<td>
178+
<span class="label label-{% trans_status notice.status to_label='True' %}">
179+
{{ notice.status }}
180+
</span>
181+
</td>
182+
<td>
183+
{{ notice.message }}
184+
</td>
185+
<td>
186+
{{ notice.created_at|date:"d/m/Y - H:i" }}
187+
</td>
188+
</tr>
189189
{% empty %}
190-
<tr>
191-
<td colspan="5">
192-
<div class="alert alert-info">
193-
<i class="icon-info-sign"></i> {% trans "There are no notices." %}
194-
</div>
195-
</td>
196-
</tr>
190+
<tr>
191+
<td colspan="5">
192+
<div class="alert alert-info">
193+
<i class="icon-info-sign"></i> {% trans "There are no notices." %}
194+
</div>
195+
</td>
196+
</tr>
197197
{% endfor %}
198198
<tbody>
199199
</table>

scielomanager/articletrack/tests/tests_models.py

+80
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from django.conf import settings
66
from articletrack import models
77
from . import modelfactories
8+
from scielomanager.utils import misc
89

910

1011
class CommentTests(TestCase):
@@ -203,6 +204,20 @@ def test_if_expiration_date_is_today_then_checkin_is_expirable(self):
203204
self.assertEqual(checkin.expiration_at.date(), now.date())
204205
self.assertTrue(checkin.is_expirable)
205206

207+
def test_validate_sequence_function(self):
208+
sequences = (
209+
(True, ["SERV_BEGIN", "SERV_END", "SERV_BEGIN", "SERV_END"]),
210+
(True, ["SERV_BEGIN", "SERV_BEGIN", "SERV_END", "SERV_END"]),
211+
(False, ["SERV_BEGIN", "SERV_BEGIN", "SERV_END", "ANOTHER_SYMBOL"]),
212+
(False, ["SERV_BEGIN", "SERV_BEGIN", "SERV_END"]),
213+
(False, ["SERV_BEGIN", "SERV_END", "SERV_END"]),
214+
(False, ["SERV_BEGIN", "SERV_BEGIN", "SERV_BEGIN", "SERV_BEGIN", ]),
215+
(False, ["SERV_END", "SERV_END", "SERV_END", "SERV_END", ]),
216+
)
217+
for expected_result, sequence in sequences:
218+
validation_result = misc.validate_sequence(sequence)
219+
self.assertEqual(expected_result, validation_result)
220+
206221

207222
class ArticleTests(TestCase):
208223

@@ -360,3 +375,68 @@ def test_do_expires_generate_log_entry(self):
360375
self.assertIsNone(logs[0].user)
361376
self.assertEqual(logs[0].status, 'expired')
362377
self.assertEqual(logs[0].description, models.MSG_WORKFLOW_EXPIRED)
378+
379+
def test_checkin_with_notices_incomplete_service_status_must_be_in_progress(self):
380+
"""
381+
For one checkin, generate various notices like incomplete processing, such as only one SERV_BEGIN
382+
"""
383+
checkin = modelfactories.CheckinFactory()
384+
modelfactories.NoticeFactory(
385+
checkin=checkin,
386+
stage=" ", message=" ", status="SERV_BEGIN",
387+
created_at=datetime.datetime.now())
388+
389+
self.assertFalse(checkin.is_serv_status_completed)
390+
self.assertEqual('in progress', checkin.get_error_level)
391+
392+
def test_checkin_notices_with_less_serv_status_than_SERVICE_STATUS_MAX_STAGES_is_incompleted(self):
393+
"""
394+
If a checkin's notices, are less than SERVICE_STATUS_MAX_STAGES service status (SERV_END), is incomplete
395+
"""
396+
checkin = modelfactories.CheckinFactory()
397+
serv_status_count = models.SERVICE_STATUS_MAX_STAGES - 1
398+
for step in xrange(0, serv_status_count):
399+
modelfactories.NoticeFactory(
400+
checkin=checkin,
401+
stage=" ", message=" ", status="SERV_END",
402+
created_at=datetime.datetime.now())
403+
404+
self.assertFalse(checkin.is_serv_status_completed)
405+
self.assertEqual('in progress', checkin.get_error_level)
406+
407+
def test_checkin_notices_with_equal_serv_status_than_SERVICE_STATUS_MAX_STAGES_is_incompleted(self):
408+
"""
409+
If a checkin's notices, are equal to SERVICE_STATUS_MAX_STAGES service status (SERV_END), still incomplete
410+
because only have: "SERV_END" as service status.
411+
"""
412+
checkin = modelfactories.CheckinFactory()
413+
serv_status_count = models.SERVICE_STATUS_MAX_STAGES
414+
for step in xrange(0, serv_status_count):
415+
modelfactories.NoticeFactory(
416+
checkin=checkin,
417+
stage=" ", message=" ", status="SERV_END",
418+
created_at=datetime.datetime.now())
419+
420+
self.assertFalse(checkin.is_serv_status_completed)
421+
self.assertEqual('in progress', checkin.get_error_level)
422+
423+
def test_checkin_notices_with_correct_pair_of_service_status_is_completed(self):
424+
"""
425+
If checkin's notices, are equal to SERVICE_STATUS_MAX_STAGES service status (SERV_END), is complete
426+
because each service status has a pair: "SERV_BEGIN"/"SERV_END" as service status, and checkin.error_level
427+
is 'ok' because has no "error"/"warning" notices
428+
"""
429+
checkin = modelfactories.CheckinFactory()
430+
serv_status_count = models.SERVICE_STATUS_MAX_STAGES
431+
for step in xrange(0, serv_status_count):
432+
notice_serv_begin = modelfactories.NoticeFactory(
433+
checkin=checkin,
434+
stage=" ", message=" ", status="SERV_BEGIN",
435+
created_at=datetime.datetime.now())
436+
notice_serv_end = modelfactories.NoticeFactory(
437+
checkin=checkin,
438+
stage=" ", message=" ", status="SERV_END",
439+
created_at=datetime.datetime.now())
440+
self.assertTrue(checkin.is_serv_status_completed)
441+
self.assertEqual('ok', checkin.get_error_level)
442+

scielomanager/articletrack/views.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ def checkin_history(request, checkin_id):
257257
def notice_detail(request, checkin_id):
258258

259259
checkin = get_object_or_404(models.Checkin.userobjects.active(), pk=checkin_id)
260-
notices = checkin.notices.all()
260+
notices = checkin.notices.exclude(status__istartswith="serv_")
261261
tickets = checkin.article.tickets.all()
262262
opened_tickets = tickets.filter(finished_at__isnull=True)
263263
closed_tickets = tickets.filter(finished_at__isnull=False)
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# coding: utf-8
2+
def validate_sequence(sequence, open_symbol='SERV_BEGIN', close_symbol='SERV_END'):
3+
opened = 0
4+
for item in sequence:
5+
if item == open_symbol:
6+
opened += 1
7+
elif item == close_symbol:
8+
opened -= 1
9+
10+
if opened < 0: # quebra a invariante
11+
return False
12+
13+
return opened == 0

0 commit comments

Comments
 (0)