Skip to content

Commit 1a78539

Browse files
jfunezGustavo Fonseca
authored and
Gustavo Fonseca
committed
Primeira versão de frontend do style checker
- removido código das annotations (agora usa só o html do include "validator/includes/xml_annotated.html") - adiciono template tag: field_attr para modificar atributos de um form.field, como estilos, classe css, etc. - adiciono settings: VALIDATOR_MAX_UPLOAD_SIZE com valor máximo do upload (512KB) - corrigido contador de erros nas annotations (Fixes: #899) - adiciono clean do campo field, para validar tamanho do arquivo e content_type - melhoras na interface do formulário, validação js, etc
1 parent 15d793b commit 1a78539

File tree

16 files changed

+544
-88
lines changed

16 files changed

+544
-88
lines changed

scielomanager/articletrack/templates/articletrack/notice_detail.html

+2-63
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,6 @@
99
{% block content %}
1010
<link href="{% static 'css/prism.css' %}" rel="stylesheet" />
1111
<style type="text/css">
12-
/* OVERRIDE STYLES OF PRIMSM
13-
.line-highlight { left: 15px;}
14-
FIXES STYLES OF PRIMSM */
15-
code, pre { font-size: 12px; }
16-
code[class*="language-"],
17-
pre[class*="language-"]{ -moz-tab-size: 2; -o-tab-size: 2; tab-size: 2;}
18-
.line-highlight:before, .line-highlight[data-end]:after {
19-
border-radius: 0;
20-
left: 0;
21-
min-height: 20px;
22-
min-width: 30px;
23-
padding: 0;
24-
top: 0;
25-
}
26-
2712
.tab_add_ticket:hover { background-color: #51A351 !important; }
2813
</style>
2914

@@ -149,7 +134,7 @@ <h3>{% trans 'Notices' %}:</h3>
149134
{# xml analyzed and with annotations #}
150135
&nbsp;
151136
<span class="badge badge-important">
152-
{{ xml_data.validation_errors.error_lines|length }}
137+
{{ xml_data.validation_errors.results|length }}
153138
</span>
154139
{% endif %}
155140
</a>
@@ -201,52 +186,7 @@ <h3>{% trans 'Notices' %}:</h3>
201186
</div>
202187
<div class="tab-pane" id="annotations">
203188
{# annotations #}
204-
{% if not xml_data.can_be_analyzed.0 %}
205-
{# could not be analyzed #}
206-
<div class="alert alert-warning">
207-
<h4><i class="icon-minus-sign"></i> {% trans "The XML could not be analyzed" %}</h4>
208-
<p>{{ xml_data.can_be_analyzed.1 }}</p>
209-
</div>
210-
{% elif not xml_data.annotations %}
211-
{# xml without annotations #}
212-
<div class="alert alert-success">
213-
<i class="icon-ok"></i> {% trans "The XML has no errors" %}
214-
</div>
215-
{% else %}
216-
{# xml analyzed and with annotations #}
217-
<div class="alert alert-block alert-error">
218-
<h4 class="alert-heading"> {% trans "The XML have some errors" %}:</h4>
219-
</div>
220-
{% if xml_data.validation_errors %}
221-
<table class="table table-striped table-condensed">
222-
<thead>
223-
<tr>
224-
<th>{% trans "Level" %}:</th>
225-
<th>{% trans "Line" %}:</th>
226-
<th>{% trans "Column" %}:</th>
227-
<th>{% trans "Message" %}:</th>
228-
</tr>
229-
</thead>
230-
<tbody>
231-
{% for error in xml_data.validation_errors.results %}
232-
<tr>
233-
<td><span class="label label-{% trans_status error.level to_label='True' %}">{{ error.level }}</span></td>
234-
<td>{{ error.line }}</td>
235-
<td>{{ error.column }}</td>
236-
<td>{{ error.message }}</td>
237-
</tr>
238-
{% endfor %}
239-
</tbody>
240-
</table>
241-
{% endif %}
242-
{# XML ANNOTATED #}
243-
<pre
244-
class="line-numbers language-markup pre-scrollable"
245-
{% if xml_data.validation_errors %}
246-
data-line='{{ xml_data.validation_errors.error_lines }}'
247-
{% endif %}
248-
><code class="language-markup">{{ xml_data.annotations }}</code></pre>
249-
{% endif %}
189+
{% include "validator/includes/xml_annotated.html" %}
250190
{# /annotations #}
251191
</div>
252192
</div>
@@ -432,7 +372,6 @@ <h3>{% trans "Reject this Check-in?" %}</h3>
432372
{% endblock %}
433373

434374
{% block extrafooter %}
435-
<script type="text/javascript" src="{% static 'js/prism.js' %}"></script>
436375
<script type="text/javascript">
437376
$(document).ready(function(){
438377
var fail_msg = '<i class="icon-warning-sign"></i> {% trans "Unable to communicate with the file server. Please try again later." %}';

scielomanager/articletrack/views.py

+1-23
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
from . import models
2525
from .forms import CommentMessageForm, TicketForm, CheckinListFilterForm, CheckinRejectForm
2626
from .balaio import BalaioAPI, BalaioRPC
27+
from validator.utils import extract_validation_errors
2728

2829

2930
AUTHZ_REDIRECT_URL = '/accounts/unauthorized/'
@@ -36,29 +37,6 @@
3637
logger = logging.getLogger(__name__)
3738

3839

39-
def extract_validation_errors(validation_errors):
40-
"""
41-
Return a "parsed" dict of validation errors returned by stylechecker
42-
"""
43-
# iterate over the errors and get the relevant data
44-
results = []
45-
error_lines = [] # only to simplify the line's highlights of prism.js plugin on template
46-
for error in validation_errors:
47-
error_data = {
48-
'line': error.line or '--',
49-
'column': error.column or '--',
50-
'message': error.message or '',
51-
'level': error.level_name or 'ERROR',
52-
}
53-
results.append(error_data)
54-
if error.line:
55-
error_lines.append(str(error.line))
56-
return {
57-
'results': results,
58-
'error_lines': ", ".join(error_lines)
59-
}
60-
61-
6240
@waffle_flag('articletrack')
6341
@permission_required('articletrack.list_checkin', login_url=AUTHZ_REDIRECT_URL)
6442
def checkin_index(request):
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from django import template
2+
3+
register = template.Library()
4+
5+
@register.filter_function
6+
def attr(obj, arg1):
7+
"""
8+
Use in templates:
9+
{% load field_attrs %}
10+
then, in a form field:
11+
{{ form.phone|attr:"style=width:143px;background-color:yellow"|attr:"size=30" }}
12+
13+
"""
14+
att, value = arg1.split("=")
15+
obj.field.widget.attrs[att] = value
16+
return obj

scielomanager/scielomanager/settings.py

+2
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@
163163
'articletrack',
164164
'djcelery',
165165
'kombu.transport.django',
166+
'validator',
166167
)
167168

168169
TEMPLATE_CONTEXT_PROCESSORS = (
@@ -256,6 +257,7 @@
256257
IMAGE_MAX_UPLOAD_SIZE = 5242880 # must be an integer of bytes allowed, see comment on custom_fields.ContentTypeRestrictedFileField for reference
257258
JOURNAL_COVER_MAX_SIZE = IMAGE_MAX_UPLOAD_SIZE
258259
JOURNAL_LOGO_MAX_SIZE = IMAGE_MAX_UPLOAD_SIZE
260+
VALIDATOR_MAX_UPLOAD_SIZE = 512 * 1024 # max size in byte to upload xml to validator
259261

260262
FILE_UPLOAD_HANDLERS = (
261263
"django.core.files.uploadhandler.MemoryFileUploadHandler",

scielomanager/scielomanager/templates/footer.html

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{% load i18n %}
2+
{% load waffle_tags %}
23

34
<div class="footer row-fluid">
45
<div class="span3">
@@ -29,10 +30,14 @@ <h4>{% trans 'Development' %}</h4>
2930
</ul>
3031
</div>
3132
<div class="span2">
32-
<h4>{% trans 'SciELO Project' %}</h4>
33+
{% flag 'packtools_validator' %}
34+
<h4>{% trans "Tools" %}</h4>
3335
<ul>
34-
<li><a href="http://docs.scielo.org/en/latest/index.html">{% trans 'About' %}</a></li>
36+
<li>
37+
<a href="{% url validator.packtools.stylechecker %}">{% trans "Packtools StyleChecker" %}</a>
38+
</li>
3539
</ul>
40+
{% endflag %}
3641
</div>
3742
<div class="span2">
3843
<form name="form_language" id="form_language" action="/i18n/setlang/" method="post">

scielomanager/scielomanager/urls.py

+3
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@
6767

6868
#AJAX
6969
url(r'^ajx/ajx3/$', views.ajx_list_users, name="ajx.ajx_list_users"),
70+
71+
# Validator URLs:
72+
url(r'^tools/validators/', include('validator.urls')),
7073
)
7174

7275
if settings.DEBUG:

scielomanager/validator/__init__.py

Whitespace-only changes.

scielomanager/validator/forms.py

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# coding: utf-8
2+
3+
from django import forms
4+
from django.conf import settings
5+
from django.utils.translation import ugettext as _
6+
7+
8+
STYLECHECKER_TYPE_CHOICES = (
9+
('url', _('URL')),
10+
('file', _('File')),
11+
)
12+
13+
14+
class StyleCheckerForm(forms.Form):
15+
type = forms.ChoiceField(label=_("Type"), choices=STYLECHECKER_TYPE_CHOICES, ) # widget=forms.RadioSelect
16+
url = forms.URLField(label=_("URL"), required=False)
17+
file = forms.FileField(label=_("File"), required=False)
18+
19+
def clean_file(self):
20+
_file = self.cleaned_data.get('file', None)
21+
if _file:
22+
if _file.content_type != 'text/xml':
23+
raise forms.ValidationError(_(u"This type of file is not allowed! Please select another file."))
24+
25+
if _file.size > settings.VALIDATOR_MAX_UPLOAD_SIZE:
26+
raise forms.ValidationError(_(u"The file's size is too large! Please select a smaller file."))
27+
28+
return _file
29+
30+
def clean(self):
31+
type = self.cleaned_data['type']
32+
url = self.cleaned_data.get('url', None)
33+
file = self.cleaned_data.get('file', None)
34+
35+
if type == 'url' and not url:
36+
raise forms.ValidationError('if trying to validate via URL, please submit a valid URL')
37+
if type == 'file' and not file:
38+
raise forms.ValidationError('if trying to validate a File, please upload a valid XML file')
39+
40+
return self.cleaned_data

scielomanager/validator/models.py

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from django.db import models
2+
3+
# Create your models here.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{% extends "base_lv0.html" %}
2+
3+
{% load i18n %}
4+
{% load static %}
5+
6+
{% block base_struture %}
7+
<style type="text/css">
8+
body { margin: 8px !important;}
9+
.container-base {padding-top: 40px;}
10+
</style>
11+
12+
<div class="navbar navbar-inverse navbar-fixed-top">
13+
<div class="navbar-inner">
14+
<div class="container">
15+
<button type="button" class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
16+
<span class="icon-bar"></span>
17+
<span class="icon-bar"></span>
18+
<span class="icon-bar"></span>
19+
</button>
20+
<a class="brand" href="/">SciELO Manager</a>
21+
<div class="nav-collapse collapse">
22+
<ul class="nav">
23+
<li class="active"><a href="{% url validator.packtools.stylechecker %}">Style Checker</a></li>
24+
</ul>
25+
</div><!--/.nav-collapse -->
26+
</div>
27+
</div>
28+
</div>
29+
30+
<div class="container container-base">
31+
{% block main_content %}
32+
33+
{% endblock main_content %}
34+
</div> <!-- /container -->
35+
36+
{% endblock base_struture %}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
{% load i18n %}
2+
{% load trans_status %}
3+
{% load static %}
4+
5+
<link href="{% static 'css/prism.css' %}" rel="stylesheet" />
6+
<style type="text/css">
7+
/* OVERRIDE STYLES OF PRIMSM
8+
.line-highlight { left: 15px;}
9+
FIXES STYLES OF PRIMSM */
10+
code, pre { font-size: 12px; }
11+
code[class*="language-"],
12+
pre[class*="language-"]{ -moz-tab-size: 2; -o-tab-size: 2; tab-size: 2;}
13+
.line-highlight:before, .line-highlight[data-end]:after {
14+
border-radius: 0;
15+
left: 0;
16+
min-height: 20px;
17+
min-width: 30px;
18+
padding: 0;
19+
top: 0;
20+
}
21+
</style>
22+
23+
{% if not xml_data.can_be_analyzed.0 %}
24+
{# could not be analyzed #}
25+
<div class="alert alert-warning">
26+
<h4><i class="icon-minus-sign"></i> {% trans "The XML could not be analyzed" %}</h4>
27+
<p>{{ xml_data.can_be_analyzed.1 }}</p>
28+
</div>
29+
{% elif not xml_data.annotations %}
30+
{# xml without annotations #}
31+
<div class="alert alert-success">
32+
<i class="icon-ok"></i> {% trans "The XML has no errors" %}
33+
</div>
34+
{% else %}
35+
{# xml analyzed and with annotations #}
36+
<div class="alert alert-block alert-error">
37+
<h4 class="alert-heading"> {% trans "The XML have some errors" %}:</h4>
38+
</div>
39+
{% if xml_data.validation_errors %}
40+
<table class="table table-striped table-condensed">
41+
<thead>
42+
<tr>
43+
<th>{% trans "Level" %}:</th>
44+
<th>{% trans "Line" %}:</th>
45+
<th>{% trans "Column" %}:</th>
46+
<th>{% trans "Message" %}:</th>
47+
</tr>
48+
</thead>
49+
<tbody>
50+
{% for error in xml_data.validation_errors.results %}
51+
<tr>
52+
<td><span class="label label-{% trans_status error.level to_label='True' %}">{{ error.level }}</span></td>
53+
<td>{{ error.line }}</td>
54+
<td>{{ error.column }}</td>
55+
<td>{{ error.message }}</td>
56+
</tr>
57+
{% endfor %}
58+
</tbody>
59+
</table>
60+
{% endif %}
61+
{# XML ANNOTATED #}
62+
<pre
63+
class="line-numbers language-markup pre-scrollable"
64+
{% if xml_data.validation_errors and xml_data.validation_errors.error_lines %}
65+
data-line='{{ xml_data.validation_errors.error_lines }}'
66+
{% endif %}
67+
><code class="language-markup">{{ xml_data.annotations }}</code></pre>
68+
{% endif %}
69+
<script type="text/javascript" src="{% static 'js/prism.js' %}"></script>

0 commit comments

Comments
 (0)