Skip to content

Commit

Permalink
Merge pull request #990 from gustavofonseca/tk967-packtools_0.5
Browse files Browse the repository at this point in the history
Atualização para suportar packtools versão 0.5dev
  • Loading branch information
gustavofonseca committed Sep 24, 2014
2 parents a3b28c4 + 9d4c1c5 commit ac55d1a
Show file tree
Hide file tree
Showing 15 changed files with 276 additions and 317 deletions.
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ django-tastypie==0.9.16
django-waffle==0.9.1
-e git+git://github.com/scieloorg/django-htmlmin.git#egg=django-htmlmin
-e git+git://github.com/scieloorg/django-cache-machine.git#egg=django-cache-machine
packtools==0.4dev-20140908
packtools==0.5dev-20140924
Celery
django-celery
django-kombu
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,20 +148,17 @@ <h3>{% trans 'Notices' %}:</h3>
</li>
<li class="longer-tab">
<a href="#annotations" data-toggle="tab">
{% trans "Style Checking" %}
{% if not xml_data.can_be_analyzed.0 %}
{% trans "Style Checking" %} &nbsp;
{% if xml_exception or not xml_data.can_be_analyzed.0 %}
{# could not be analyzed #}
&nbsp;
<span class="badge badge-warning">&nbsp;</span>
{% elif not xml_data.annotations %}
<span class="label label-warning">{% trans "Exception" %}</span>
{% elif results.validation_errors|length == 0 %}
{# xml without annotations #}
&nbsp;
<span class="badge badge-success">&nbsp;</span>
<span class="label label-success">{% trans "No errors" %}</span>
{% else %}
{# xml analyzed and with annotations #}
&nbsp;
<span class="badge badge-important">
{{ xml_data.validation_errors.results|length }}
<span class="label label-important">
{{ results.validation_errors|length }} {% trans "Errors" %}
</span>
{% endif %}
</a>
Expand Down Expand Up @@ -213,7 +210,17 @@ <h3>{% trans 'Notices' %}:</h3>
</div>
<div class="tab-pane" id="annotations">
{# annotations #}
{% include "validator/includes/xml_annotated.html" %}
{% if not xml_data.can_be_analyzed.0 %}
{# could not be analyzed #}
<div class="alert alert-warning">
<h4><i class="icon-minus-sign"></i> {% trans "The XML could not be analyzed" %}</h4>
<p>{% trans "Detail:"%} <strong>{{ xml_data.can_be_analyzed.1 }}</strong></p>
</div>
{% elif results or xml_exception %}
{% with results as xml_data %}
{% include "validator/includes/xml_annotated.html" %}
{% endwith %}
{% endif %}
{# /annotations #}
</div>
</div>
Expand Down
34 changes: 0 additions & 34 deletions scielomanager/articletrack/tests/doubles.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from articletrack.balaio import BalaioAPI, BalaioRPC
from django.conf import settings
from urllib2 import urlopen
from packtools import stylechecker


def make_expected_generator(file_uri):
Expand Down Expand Up @@ -66,36 +65,3 @@ def is_up(self):

def call(self, method, args=()):
return True


# packtools.stylechecker double


class StylecheckerDouble(stylechecker.XML):
def __init__(self, file):
pass

def validate(self):
return (True, None)

def validate_style(self):
return (True, None)

def annotate_errors(self):
return None


class StylecheckerAnnotationsDouble(StylecheckerDouble):
def __str__(self):
return "some annotations in xml string"

def validate(self):
return (True, None)

def validate_style(self):
class Error(object):
line = 1
column = 6
message = u'Premature end of data in tag xml line 1, line 1, column 6'
level_name = 'ERROR'
return (False, [Error(), ])
107 changes: 47 additions & 60 deletions scielomanager/articletrack/tests/tests_pages.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from articletrack.tests.tests_models import create_notices
from . import modelfactories
from . import doubles
from validator.tests import doubles as packtools_double


def _makePermission(perm, model, app_label='articletrack'):
Expand Down Expand Up @@ -262,10 +263,10 @@ def test_notice_list_must_have_link_to_checkin_list(self):
response.mustcontain('<a href="%s">' % url_to_checkin_index)

def test_annotations_ok_if_xml_is_valid(self):
# with
self._addWaffleFlag()
notice = self._makeOne()

# MOCK/REPLACE/FAKE/PIMP MY BALAIO!!!
target_xml = "valid.xml"
expected_response = {
"filename": "1415-4757-gmb-37-0210.xml",
Expand All @@ -280,33 +281,28 @@ def get_xml_uri(self, attempt_id, target_name):
balaio()
self.mocker.result(BalaioTest())

# MOCK/REPLACE/FAKE/PIMP MY STYLECHECKER!!!
XML = self.mocker.replace('packtools.stylechecker.XML')
XML(mocker.ANY)
self.mocker.result(doubles.StylecheckerDouble(mocker.ANY))
XMLValidator = self.mocker.replace('packtools.stylechecker.XMLValidator')
XMLValidator(mocker.ANY)
self.mocker.result(packtools_double.XMLValidatorDouble(mocker.ANY))

self.mocker.replay()
# when
response = self.app.get(
reverse('notice_detail', args=[notice.checkin.pk]),
user=self.user)
# then
xml_data = response.context['xml_data']

self.assertEqual(response.status_code, 200)
self.assertTrue(xml_data['can_be_analyzed'][0])
self.assertIsNone(xml_data['annotations'])
self.assertIsNone(response.context['results'])
self.assertIsNone(response.context['xml_exception'])
self.assertEqual(xml_data['uri'], expected_response['uri'])
expect_errors = {
'errors_total_count': 0,
'results': []
}
self.assertEqual(xml_data['validation_errors'], expect_errors)
self.assertEqual(xml_data['file_name'], expected_response['filename'])

def test_annotations_warning_if_balaio_breaks(self):
self._addWaffleFlag()
notice = self._makeOne()

# MOCK/REPLACE/FAKE/PIMP MY BALAIO!!!
target_xml = "error.xml"
expected_response = {
"filename": "1415-4757-gmb-37-0210.xml",
Expand All @@ -328,11 +324,17 @@ def get_xml_uri(self, attempt_id, target_name):
xml_data = response.context['xml_data']

self.assertEqual(response.status_code, 200)
# check "could not be analyzed" message
self.assertFalse(xml_data['can_be_analyzed'][0])
self.assertIsNone(xml_data['annotations'])
self.assertEqual(
xml_data['can_be_analyzed'][1],
'Could not obtain the XML with this file name %s' % expected_response['filename']
)

self.assertEqual(xml_data['uri'], None)
self.assertIsNone(xml_data['validation_errors'])
self.assertEqual(xml_data['file_name'], expected_response['filename'])
from validator.tests.tests_pages import PACKTOOLS_VERSION
self.assertEqual(response.context['packtools_version'], PACKTOOLS_VERSION)

def test_annotations_of_error(self):
self._addWaffleFlag()
Expand All @@ -352,9 +354,12 @@ def get_xml_uri(self, attempt_id, target_name):
balaio()
self.mocker.result(BalaioTest())

XML = self.mocker.replace('packtools.stylechecker.XML')
XML(mocker.ANY)
self.mocker.result(doubles.StylecheckerAnnotationsDouble(mocker.ANY))
XMLValidator = self.mocker.replace('packtools.stylechecker.XMLValidator')
XMLValidator(mocker.ANY)
self.mocker.result(packtools_double.XMLValidatorAnnotationsDouble(mocker.ANY))
lxml_tostring = self.mocker.replace('lxml.etree.tostring')
lxml_tostring(mocker.ANY, mocker.KWARGS)
self.mocker.result("some annotations in xml string")

self.mocker.replay()

Expand All @@ -366,25 +371,14 @@ def get_xml_uri(self, attempt_id, target_name):

self.assertEqual(response.status_code, 200)
self.assertTrue(xml_data['can_be_analyzed'][0])
self.assertIsNotNone(xml_data['annotations'])
self.assertEqual(xml_data['uri'], expected_response['uri'])
self.assertEqual(xml_data['file_name'], expected_response['filename'])
self.assertIsNotNone(xml_data['validation_errors'])
self.assertEqual(1, xml_data['validation_errors']['errors_total_count'])
self.assertEqual(1, len(xml_data['validation_errors']['results']))
expect_errors = {
'errors_total_count': 1,
'results': [
{
'column': 6,
'count': 1,
'line': 1,
'message': u'Premature end of data in tag xml line 1, line 1, column 6',
'level': 'ERROR'
}
]
}
self.assertEqual(xml_data['validation_errors'], expect_errors)
results = response.context['results']
self.assertIsNotNone(results)
self.assertIsNotNone(results['annotations'])
validation_errors = results['validation_errors']
self.assertIsNotNone(validation_errors)
self.assertEqual(1, len(validation_errors))

def test_xml_not_found(self):
self._addWaffleFlag()
Expand All @@ -404,8 +398,8 @@ def get_xml_uri(self, attempt_id, target_name):
balaio()
self.mocker.result(BalaioTest())

XML = self.mocker.replace('packtools.stylechecker.XML')
XML(expected_response['uri'])
XMLValidator = self.mocker.replace('packtools.stylechecker.XMLValidator')
XMLValidator(expected_response['uri'])
self.mocker.throw(IOError)
self.mocker.replay()

Expand All @@ -416,13 +410,15 @@ def get_xml_uri(self, attempt_id, target_name):
xml_data = response.context['xml_data']

self.assertEqual(response.status_code, 200)
self.assertFalse(xml_data['can_be_analyzed'][0])
self.assertEqual(xml_data['can_be_analyzed'][1], "IOError while starting Stylechecker.XML(), please verify if the input is correct")
self.assertIsNone(xml_data['annotations'])
self.assertEqual(xml_data['uri'], expected_response['uri'])
self.assertEqual(xml_data['file_name'], expected_response['filename'])
expect_errors = {'errors_total_count': 0, 'results': []}
self.assertEqual(xml_data['validation_errors'], expect_errors)
self.assertTrue(xml_data['can_be_analyzed'][0])
# previous version display a IOError exception custom messsage
expected_results = None
expected_xml_exception = ''

self.assertEqual(response.context['results'], expected_results)
self.assertEqual(response.context['xml_exception'], expected_xml_exception)

def test_annotations_of_syntax_error(self):
self._addWaffleFlag()
Expand All @@ -449,8 +445,8 @@ def get_xml_uri(self, attempt_id, target_name):
'code': 77,
}

XML = self.mocker.replace('packtools.stylechecker.XML')
XML(expected_response['uri'])
XMLValidator = self.mocker.replace('packtools.stylechecker.XMLValidator')
XMLValidator(expected_response['uri'])
self.mocker.throw(
lxml.etree.XMLSyntaxError(
syntax_error_data['message'],
Expand All @@ -470,24 +466,15 @@ def get_xml_uri(self, attempt_id, target_name):

self.assertEqual(response.status_code, 200)
self.assertTrue(xml_data['can_be_analyzed'][0])
self.assertIsNotNone(xml_data['annotations'])
self.assertEqual(xml_data['uri'], expected_response['uri'])
self.assertEqual(xml_data['file_name'], expected_response['filename'])
self.assertIsNotNone(xml_data['validation_errors'])
self.assertEqual(1, xml_data['validation_errors']['errors_total_count'])
self.assertEqual(1, len(xml_data['validation_errors']['results']))
self.assertEqual(
xml_data['validation_errors']['results'],
[
{
'column': syntax_error_data['column'],
'count': 1,
'line': syntax_error_data['line'],
'message': syntax_error_data['message'],
'level': 'ERROR',
}
]
)

results = response.context['results']
xml_exception = response.context['xml_exception']
self.assertIsNone(results)
self.assertIsNotNone(xml_exception)
self.assertEqual(xml_exception, syntax_error_data['message'])



class CheckinWorkflowTests(WebTest, mocker.MockerTestCase):
Expand Down
13 changes: 5 additions & 8 deletions scielomanager/articletrack/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,12 @@
from django.template.defaultfilters import slugify
from django.contrib.sites.models import get_current_site

from packtools import stylechecker

from scielomanager.tools import get_paginated, get_referer_view
from scielomanager import tasks
from . import models
from .forms import CommentMessageForm, TicketForm, CheckinListFilterForm, CheckinRejectForm
from .balaio import BalaioAPI, BalaioRPC
from validator.utils import StyleCheckerAnalyzer
from validator import utils as stylechecker_utils
from .utils import checkin_send_email_by_action, ticket_send_mail_by_action, comment_send_mail_by_action

AUTHZ_REDIRECT_URL = '/accounts/unauthorized/'
Expand Down Expand Up @@ -380,6 +378,7 @@ def notice_detail(request, checkin_id):
'zip_filename': zip_filename,
'reject_form': reject_form,
'have_actions_to_do': have_actions_to_do,
'packtools_version': stylechecker_utils.PACKTOOLS_VERSION,
}

balaio = BalaioAPI()
Expand All @@ -388,8 +387,6 @@ def notice_detail(request, checkin_id):
'file_name': None,
'uri': None,
'can_be_analyzed': (False, ''),
'annotations': None,
'validation_errors': None,
}

try:
Expand Down Expand Up @@ -426,9 +423,9 @@ def notice_detail(request, checkin_id):
xml_data['can_be_analyzed'] = (False, "XML's URI is invalid (%s)" % xml_data['uri'])

if xml_data['can_be_analyzed'][0]:
analyzer = StyleCheckerAnalyzer(xml_data['uri'])
analyzer_results = analyzer.analyze()
xml_data.update(analyzer_results)
results, exc = stylechecker_utils.analyze_xml(xml_data['uri'])
context['results'] = results
context['xml_exception'] = getattr(exc, 'message', None)

context['files'] = files_list
context['xml_data'] = xml_data
Expand Down
4 changes: 2 additions & 2 deletions scielomanager/journalmanager/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@
from scielomanager.custom_fields import ContentTypeRestrictedFileField
from . import modelmanagers

User.__bases__ = (caching.base.CachingMixin, models.Model)
User.add_to_class('objects', caching.base.CachingManager())
#User.__bases__ = (caching.base.CachingMixin, models.Model)
#User.add_to_class('objects', caching.base.CachingManager())

logger = logging.getLogger(__name__)

Expand Down
3 changes: 2 additions & 1 deletion scielomanager/scielomanager/static/css/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,8 @@ table._listings h4 {
}

.longer-tab {
width: 178px;
/*width: 178px;*/
width: auto;
}

#search-form {
Expand Down
Loading

0 comments on commit ac55d1a

Please sign in to comment.