Skip to content

Commit

Permalink
Primeira versão de frontend do style checker
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
jfunez authored and Gustavo Fonseca committed Jul 25, 2014
1 parent d8fa378 commit db817ec
Show file tree
Hide file tree
Showing 16 changed files with 544 additions and 88 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,6 @@
{% block content %}
<link href="{% static 'css/prism.css' %}" rel="stylesheet" />
<style type="text/css">
/* OVERRIDE STYLES OF PRIMSM
.line-highlight { left: 15px;}
FIXES STYLES OF PRIMSM */
code, pre { font-size: 12px; }
code[class*="language-"],
pre[class*="language-"]{ -moz-tab-size: 2; -o-tab-size: 2; tab-size: 2;}
.line-highlight:before, .line-highlight[data-end]:after {
border-radius: 0;
left: 0;
min-height: 20px;
min-width: 30px;
padding: 0;
top: 0;
}

.tab_add_ticket:hover { background-color: #51A351 !important; }
</style>

Expand Down Expand Up @@ -149,7 +134,7 @@ <h3>{% trans 'Notices' %}:</h3>
{# xml analyzed and with annotations #}
&nbsp;
<span class="badge badge-important">
{{ xml_data.validation_errors.error_lines|length }}
{{ xml_data.validation_errors.results|length }}
</span>
{% endif %}
</a>
Expand Down Expand Up @@ -201,52 +186,7 @@ <h3>{% trans 'Notices' %}:</h3>
</div>
<div class="tab-pane" id="annotations">
{# annotations #}
{% 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>{{ xml_data.can_be_analyzed.1 }}</p>
</div>
{% elif not xml_data.annotations %}
{# xml without annotations #}
<div class="alert alert-success">
<i class="icon-ok"></i> {% trans "The XML has no errors" %}
</div>
{% else %}
{# xml analyzed and with annotations #}
<div class="alert alert-block alert-error">
<h4 class="alert-heading"> {% trans "The XML have some errors" %}:</h4>
</div>
{% if xml_data.validation_errors %}
<table class="table table-striped table-condensed">
<thead>
<tr>
<th>{% trans "Level" %}:</th>
<th>{% trans "Line" %}:</th>
<th>{% trans "Column" %}:</th>
<th>{% trans "Message" %}:</th>
</tr>
</thead>
<tbody>
{% for error in xml_data.validation_errors.results %}
<tr>
<td><span class="label label-{% trans_status error.level to_label='True' %}">{{ error.level }}</span></td>
<td>{{ error.line }}</td>
<td>{{ error.column }}</td>
<td>{{ error.message }}</td>
</tr>
{% endfor %}
</tbody>
</table>
{% endif %}
{# XML ANNOTATED #}
<pre
class="line-numbers language-markup pre-scrollable"
{% if xml_data.validation_errors %}
data-line='{{ xml_data.validation_errors.error_lines }}'
{% endif %}
><code class="language-markup">{{ xml_data.annotations }}</code></pre>
{% endif %}
{% include "validator/includes/xml_annotated.html" %}
{# /annotations #}
</div>
</div>
Expand Down Expand Up @@ -432,7 +372,6 @@ <h3>{% trans "Reject this Check-in?" %}</h3>
{% endblock %}

{% block extrafooter %}
<script type="text/javascript" src="{% static 'js/prism.js' %}"></script>
<script type="text/javascript">
$(document).ready(function(){
var fail_msg = '<i class="icon-warning-sign"></i> {% trans "Unable to communicate with the file server. Please try again later." %}';
Expand Down
24 changes: 1 addition & 23 deletions scielomanager/articletrack/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from . import models
from .forms import CommentMessageForm, TicketForm, CheckinListFilterForm, CheckinRejectForm
from .balaio import BalaioAPI, BalaioRPC
from validator.utils import extract_validation_errors


AUTHZ_REDIRECT_URL = '/accounts/unauthorized/'
Expand All @@ -36,29 +37,6 @@
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)
}


@waffle_flag('articletrack')
@permission_required('articletrack.list_checkin', login_url=AUTHZ_REDIRECT_URL)
def checkin_index(request):
Expand Down
16 changes: 16 additions & 0 deletions scielomanager/journalmanager/templatetags/field_attrs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from django import template

register = template.Library()

@register.filter_function
def attr(obj, arg1):
"""
Use in templates:
{% load field_attrs %}
then, in a form field:
{{ form.phone|attr:"style=width:143px;background-color:yellow"|attr:"size=30" }}
"""
att, value = arg1.split("=")
obj.field.widget.attrs[att] = value
return obj
2 changes: 2 additions & 0 deletions scielomanager/scielomanager/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@
'articletrack',
'djcelery',
'kombu.transport.django',
'validator',
)

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

FILE_UPLOAD_HANDLERS = (
"django.core.files.uploadhandler.MemoryFileUploadHandler",
Expand Down
9 changes: 7 additions & 2 deletions scielomanager/scielomanager/templates/footer.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{% load i18n %}
{% load waffle_tags %}

<div class="footer row-fluid">
<div class="span3">
Expand Down Expand Up @@ -29,10 +30,14 @@ <h4>{% trans 'Development' %}</h4>
</ul>
</div>
<div class="span2">
<h4>{% trans 'SciELO Project' %}</h4>
{% flag 'packtools_validator' %}
<h4>{% trans "Tools" %}</h4>
<ul>
<li><a href="http://docs.scielo.org/en/latest/index.html">{% trans 'About' %}</a></li>
<li>
<a href="{% url validator.packtools.stylechecker %}">{% trans "Packtools StyleChecker" %}</a>
</li>
</ul>
{% endflag %}
</div>
<div class="span2">
<form name="form_language" id="form_language" action="/i18n/setlang/" method="post">
Expand Down
3 changes: 3 additions & 0 deletions scielomanager/scielomanager/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@

#AJAX
url(r'^ajx/ajx3/$', views.ajx_list_users, name="ajx.ajx_list_users"),

# Validator URLs:
url(r'^tools/validators/', include('validator.urls')),
)

if settings.DEBUG:
Expand Down
Empty file.
40 changes: 40 additions & 0 deletions scielomanager/validator/forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# coding: utf-8

from django import forms
from django.conf import settings
from django.utils.translation import ugettext as _


STYLECHECKER_TYPE_CHOICES = (
('url', _('URL')),
('file', _('File')),
)


class StyleCheckerForm(forms.Form):
type = forms.ChoiceField(label=_("Type"), choices=STYLECHECKER_TYPE_CHOICES, ) # widget=forms.RadioSelect
url = forms.URLField(label=_("URL"), required=False)
file = forms.FileField(label=_("File"), required=False)

def clean_file(self):
_file = self.cleaned_data.get('file', None)
if _file:
if _file.content_type != 'text/xml':
raise forms.ValidationError(_(u"This type of file is not allowed! Please select another file."))

if _file.size > settings.VALIDATOR_MAX_UPLOAD_SIZE:
raise forms.ValidationError(_(u"The file's size is too large! Please select a smaller file."))

return _file

def clean(self):
type = self.cleaned_data['type']
url = self.cleaned_data.get('url', None)
file = self.cleaned_data.get('file', None)

if type == 'url' and not url:
raise forms.ValidationError('if trying to validate via URL, please submit a valid URL')
if type == 'file' and not file:
raise forms.ValidationError('if trying to validate a File, please upload a valid XML file')

return self.cleaned_data
3 changes: 3 additions & 0 deletions scielomanager/validator/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.db import models

# Create your models here.
36 changes: 36 additions & 0 deletions scielomanager/validator/templates/validator/base.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{% extends "base_lv0.html" %}

{% load i18n %}
{% load static %}

{% block base_struture %}
<style type="text/css">
body { margin: 8px !important;}
.container-base {padding-top: 40px;}
</style>

<div class="navbar navbar-inverse navbar-fixed-top">
<div class="navbar-inner">
<div class="container">
<button type="button" class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="brand" href="/">SciELO Manager</a>
<div class="nav-collapse collapse">
<ul class="nav">
<li class="active"><a href="{% url validator.packtools.stylechecker %}">Style Checker</a></li>
</ul>
</div><!--/.nav-collapse -->
</div>
</div>
</div>

<div class="container container-base">
{% block main_content %}

{% endblock main_content %}
</div> <!-- /container -->

{% endblock base_struture %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
{% load i18n %}
{% load trans_status %}
{% load static %}

<link href="{% static 'css/prism.css' %}" rel="stylesheet" />
<style type="text/css">
/* OVERRIDE STYLES OF PRIMSM
.line-highlight { left: 15px;}
FIXES STYLES OF PRIMSM */
code, pre { font-size: 12px; }
code[class*="language-"],
pre[class*="language-"]{ -moz-tab-size: 2; -o-tab-size: 2; tab-size: 2;}
.line-highlight:before, .line-highlight[data-end]:after {
border-radius: 0;
left: 0;
min-height: 20px;
min-width: 30px;
padding: 0;
top: 0;
}
</style>

{% 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>{{ xml_data.can_be_analyzed.1 }}</p>
</div>
{% elif not xml_data.annotations %}
{# xml without annotations #}
<div class="alert alert-success">
<i class="icon-ok"></i> {% trans "The XML has no errors" %}
</div>
{% else %}
{# xml analyzed and with annotations #}
<div class="alert alert-block alert-error">
<h4 class="alert-heading"> {% trans "The XML have some errors" %}:</h4>
</div>
{% if xml_data.validation_errors %}
<table class="table table-striped table-condensed">
<thead>
<tr>
<th>{% trans "Level" %}:</th>
<th>{% trans "Line" %}:</th>
<th>{% trans "Column" %}:</th>
<th>{% trans "Message" %}:</th>
</tr>
</thead>
<tbody>
{% for error in xml_data.validation_errors.results %}
<tr>
<td><span class="label label-{% trans_status error.level to_label='True' %}">{{ error.level }}</span></td>
<td>{{ error.line }}</td>
<td>{{ error.column }}</td>
<td>{{ error.message }}</td>
</tr>
{% endfor %}
</tbody>
</table>
{% endif %}
{# XML ANNOTATED #}
<pre
class="line-numbers language-markup pre-scrollable"
{% if xml_data.validation_errors and xml_data.validation_errors.error_lines %}
data-line='{{ xml_data.validation_errors.error_lines }}'
{% endif %}
><code class="language-markup">{{ xml_data.annotations }}</code></pre>
{% endif %}
<script type="text/javascript" src="{% static 'js/prism.js' %}"></script>
Loading

0 comments on commit db817ec

Please sign in to comment.