diff --git a/scielomanager/articletrack/templates/articletrack/notice_detail.html b/scielomanager/articletrack/templates/articletrack/notice_detail.html index b5ce3684..ccaac3a0 100644 --- a/scielomanager/articletrack/templates/articletrack/notice_detail.html +++ b/scielomanager/articletrack/templates/articletrack/notice_detail.html @@ -9,21 +9,6 @@ {% block content %} @@ -149,7 +134,7 @@

{% trans 'Notices' %}:

{# xml analyzed and with annotations #}   - {{ xml_data.validation_errors.error_lines|length }} + {{ xml_data.validation_errors.results|length }} {% endif %} @@ -201,52 +186,7 @@

{% trans 'Notices' %}:

{# annotations #} - {% if not xml_data.can_be_analyzed.0 %} - {# could not be analyzed #} -
-

{% trans "The XML could not be analyzed" %}

-

{{ xml_data.can_be_analyzed.1 }}

-
- {% elif not xml_data.annotations %} - {# xml without annotations #} -
- {% trans "The XML has no errors" %} -
- {% else %} - {# xml analyzed and with annotations #} -
-

{% trans "The XML have some errors" %}:

-
- {% if xml_data.validation_errors %} - - - - - - - - - - - {% for error in xml_data.validation_errors.results %} - - - - - - - {% endfor %} - -
{% trans "Level" %}:{% trans "Line" %}:{% trans "Column" %}:{% trans "Message" %}:
{{ error.level }}{{ error.line }}{{ error.column }}{{ error.message }}
- {% endif %} - {# XML ANNOTATED #} -
{{ xml_data.annotations }}
- {% endif %} + {% include "validator/includes/xml_annotated.html" %} {# /annotations #}
@@ -432,7 +372,6 @@

{% trans "Reject this Check-in?" %}

{% endblock %} {% block extrafooter %} - \ No newline at end of file diff --git a/scielomanager/validator/templates/validator/packtools.html b/scielomanager/validator/templates/validator/packtools.html new file mode 100644 index 00000000..2f06c509 --- /dev/null +++ b/scielomanager/validator/templates/validator/packtools.html @@ -0,0 +1,246 @@ +{% extends "validator/base.html" %} + +{% load i18n %} +{% load field_attrs %} + +{% block main_content %} + +
+
+

{% trans "SciELO Style Checker" %}

+

+ {% blocktrans %} + Use this tool to confirm whether an XML file conforms to SciELO Style as defined in the SciELO Publishing Schema Tagging Guidelines. + {% endblocktrans %} +

+

+ {% blocktrans %} + Enter the URL or browse to your local XML file and click "Validate". The results will be displayed below. + {% endblocktrans %} +

+ +
+ {% csrf_token %} + + +
+
+ {# URL FIELD #} + {% with form.url as field %} +
+ {{ field|attr:"class=span12"|attr:"placeholder=Insert a valid URL" }} + + {# field errors #} + {% for error in field.errors %} +
+ {{ error }} +
+ {% endfor %} +
+ {% endwith %} +
+
+ {# FILE FIELD #} + {% with form.file as field %} +
+ {{ field }} {# use of |attr doesnt work, 'cause bootstrap js plugin:filestyle create a new input #} + {% trans "Max. upload size" %}: {{ SETTINGS_MAX_UPLOAD_SIZE|filesizeformat }} + {# field errors #} + {% for error in field.errors %} +
+ {{ error }} +
+ {% endfor %} +
+ {% endwith %} +
+
+
+   + {% blocktrans %} + If you have any problems with the tool or with the SPS Tagging Guidelines, please contact: + scielo-xml@googlegroups.com. + {% endblocktrans %} +
+
+
+ + {# NON FIELD ERRORS #} + {% for error in form.non_field_errors %} +
+ {{ error }} +
+ {% endfor %} + + + {# TYPE FIELD WILL BE HIDDEN only updated by JS #} +
+ {{ form.type }} +
+ +
+ + +
+ +
+
+
+ + {% if results %} + +
+
+ {# annotations #} + {% with results as xml_data %} + {% include "validator/includes/xml_annotated.html" %} + {% endwith %} + {# /annotations #} +
+
+ + {% endif %} + +{% endblock main_content %} +{% block extrafooter %} + + + + {% endblock extrafooter %} \ No newline at end of file diff --git a/scielomanager/validator/tests.py b/scielomanager/validator/tests.py new file mode 100644 index 00000000..501deb77 --- /dev/null +++ b/scielomanager/validator/tests.py @@ -0,0 +1,16 @@ +""" +This file demonstrates writing tests using the unittest module. These will pass +when you run "manage.py test". + +Replace this with more appropriate tests for your application. +""" + +from django.test import TestCase + + +class SimpleTest(TestCase): + def test_basic_addition(self): + """ + Tests that 1 + 1 always equals 2. + """ + self.assertEqual(1 + 1, 2) diff --git a/scielomanager/validator/urls.py b/scielomanager/validator/urls.py new file mode 100644 index 00000000..2927e71a --- /dev/null +++ b/scielomanager/validator/urls.py @@ -0,0 +1,7 @@ +# -*- encoding: utf-8 -*- +from django.conf.urls.defaults import * +from . import views + +urlpatterns = patterns('', + url(r'^stylechecker/$', views.packtools_home, name="validator.packtools.stylechecker"), +) \ No newline at end of file diff --git a/scielomanager/validator/utils.py b/scielomanager/validator/utils.py new file mode 100644 index 00000000..562179a2 --- /dev/null +++ b/scielomanager/validator/utils.py @@ -0,0 +1,51 @@ +# coding: utf-8 +import logging + +from packtools import stylechecker + +logger = logging.getLogger(__name__) + + +def extract_validation_errors(validation_errors): + """ + Return a "parsed" dict of validation errors returned by stylechecker + """ + # iterate over the errors and get the relevant data + results = [] + error_lines = [] # only to simplify the line's highlights of prism.js plugin on template + for error in validation_errors: + error_data = { + 'line': error.line or '--', + 'column': error.column or '--', + 'message': error.message or '', + 'level': error.level_name or 'ERROR', + } + results.append(error_data) + if error.line: + error_lines.append(str(error.line)) + return { + 'results': results, + 'error_lines': ", ".join(error_lines) + } + + +def stylechecker_analyze(data_type, data_input): + results = { + 'can_be_analyzed': (False, ''), + 'annotations': None, + 'validation_errors': None, + } + try: + xml_check = stylechecker.XML(data_input) + except Exception as e: # any exception means that cannot be analyzed + results['can_be_analyzed'] = (False, "Error while starting Stylechecker.XML()") + # logger.error('ValueError while creating: Stylechecker.XML(%s) of type: %s. Traceback: %s' % (data_input, data_type, e)) + else: + results['can_be_analyzed'] = (True, None) + status, errors = xml_check.validate_style() + if not status: # have errors + xml_check.annotate_errors() + results['annotations'] = str(xml_check) + results['validation_errors'] = extract_validation_errors(errors) + + return results \ No newline at end of file diff --git a/scielomanager/validator/views.py b/scielomanager/validator/views.py new file mode 100644 index 00000000..a5e8467c --- /dev/null +++ b/scielomanager/validator/views.py @@ -0,0 +1,45 @@ +# coding: utf-8 +from django.template.context import RequestContext +from django.shortcuts import render_to_response +from django.conf import settings +from waffle.decorators import waffle_flag + +from . import forms +from . import utils + +# "http://192.168.1.162:7000/api/v1/article?code=S1516-635X2014000100012&format=xmlrsps" + +def __prepare_and_analyze(data_type, data_input): + """ Normalize input to feed the stylechecker and obtain results """ + results = utils.stylechecker_analyze(data_type, data_input) + return results + + +@waffle_flag('packtools_validator') +def packtools_home(request, template_name='validator/packtools.html'): + context = { + 'SETTINGS_MAX_UPLOAD_SIZE' : settings.VALIDATOR_MAX_UPLOAD_SIZE, + } + + form = forms.StyleCheckerForm() + if request.method == 'POST': + form = forms.StyleCheckerForm(request.POST, request.FILES) + if form.is_valid(): + type = form.cleaned_data['type'] + if type == 'url': + url = form.cleaned_data['url'] + results = __prepare_and_analyze(type, url) + else: + xml_file = request.FILES['file'] + results = __prepare_and_analyze(type, xml_file) + context['results'] = results + else: + form = forms.StyleCheckerForm() + + context['form'] = form + + return render_to_response( + template_name, + context, + context_instance=RequestContext(request) + )