Skip to content

Commit

Permalink
Remove the ability to upload TestPlan document files
Browse files Browse the repository at this point in the history
if users wish to import their old test plan documents then they
can create a simple script to parse the input content and use
the API method TestPlan.create() to create a new TP with the
desired text.

This is much more flexible instead of relying on Kiwi TCMS to
support arbitrary data formats and make it clever enough to parse
all the possible attributes from these input formats.
  • Loading branch information
atodorov committed Aug 20, 2018
1 parent de6c3f2 commit a02d8dc
Show file tree
Hide file tree
Showing 5 changed files with 3 additions and 151 deletions.
3 changes: 0 additions & 3 deletions requirements/base.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,5 @@ django-vinaigrette
# because of https://github.com/pycontribs/jira/issues/501
jira==1.0.10
Markdown
odfpy
python-bugzilla
PyGithub

beautifulsoup4
5 changes: 0 additions & 5 deletions tcms/templates/plan/edit.html
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,6 @@
<div>
{{ form.text }}
</div>
<div >{{ form.text.errors }}</div>
<span>Or upload a plan document</span>
{{ form.upload_plan_text }}
<span class="grey">Html, Plain text or ODT is acceptable.</span>
<div >{{ form.upload_plan_text.errors }}</div>
<div class="errors">{{ form.text.errors }}</div>
</td>
</tr>
Expand Down
6 changes: 1 addition & 5 deletions tcms/templates/plan/new.html
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,7 @@ <h2>Create New Test Plan</h2>
<td valign="top"><label class="lab" for="id_summary">Plan Document: </label></td>
<td>
<div class="mec">{{ form.text }}</div>
<div >{{ form.text.errors }}</div>
<span>Or upload a plan document</span>
{{ form.upload_plan_text }}
<span class="grey">Html, Plain text or ODT is acceptable.</span>
<div >{{ form.upload_plan_text.errors }}</div>
<div>{{ form.text.errors }}</div>
</td>
</tr>
<tr>
Expand Down
136 changes: 0 additions & 136 deletions tcms/testplans/forms.py
Original file line number Diff line number Diff line change
@@ -1,140 +1,11 @@
# -*- coding: utf-8 -*-
from django import forms

from odf.odf2xhtml import ODF2XHTML, load

from tcms.core.widgets import SimpleMDE
from tcms.core.utils import string_to_list
from tcms.core.forms.fields import UserField, StripURLField
from tcms.management.models import Product, Version, EnvGroup, Tag
from .models import TestPlan, PlanType
# ===========Plan Fields==============


MIMETYPE_HTML = 'text/html'
MIMETYPE_PLAIN = 'text/plain'
MIMETYPE_OCTET_STREAM = 'application/octet-stream'
MIMETYPE_OPENDOCUMENT = 'application/vnd.oasis.opendocument.text'


class UploadedFile: # pylint: disable=too-few-public-methods
"""Base class for all classes representing a concrete uploaded file"""

def __init__(self, uploaded_file):
self.uploaded_file = uploaded_file

def get_content(self):
raise NotImplementedError('Must be implemented in subclass.')


class UploadedPlainTextFile(UploadedFile): # pylint: disable=too-few-public-methods
"""Represent an uploaded plain text file"""

def get_content(self):
return '<pre>{0}</pre>'.format(self.uploaded_file.read())


class UploadedHTMLFile(UploadedFile): # pylint: disable=too-few-public-methods
"""Represent an uploaded HTML file
While uploading an HTML file, several tags, attributee have to be deleted,
because they would break Kiwi TCMS's internal JavaScript features and styles
and make some features unusable. Especially to the JavaScript surrounded by
SCRIPT or referenced from unknown external resources, security issue must
be considered.
Currently, tags SCRIPT, STYLE AND LINK are removed. And attributes class,
style and id are removed.
"""

def get_content(self):
def remove_tag(tag):
return tag.extract()

from bs4 import BeautifulSoup
from itertools import chain

soup = BeautifulSoup(self.uploaded_file.read(), 'html.parser')
find_all = soup.body.find_all

map(remove_tag, chain(find_all('script'),
find_all('style'),
find_all('link')))

for tag in soup.body.find_all():
pop = tag.attrs.pop
pop('class', None)
pop('CLASS', None)
pop('style', None)
pop('STYLE', None)
pop('id', None)
pop('ID', None)

return soup.body


class UploadedODTFile(UploadedFile): # pylint: disable=too-few-public-methods
"""Represent an uploaded ODT file"""

def get_content(self):
generatecss = True
embedable = True
odhandler = ODF2XHTML(generatecss, embedable)

doc = load(self.uploaded_file)
return odhandler.odf2xhtml(doc)


class PlanFileField(forms.FileField):
ODT_CONTENT_TYPES = (MIMETYPE_OCTET_STREAM, MIMETYPE_OPENDOCUMENT)

default_error_messages = {
'invalid_file_type': 'The file you uploaded is not a correct, '
'Html/Plain text/ODT file.',
'unexcept_odf_error': 'Unable to analyse the file or the file you '
'upload is not Open Document.',
'unexpected_html_error': 'Invalid HTML document.',
}

def clean(self, data, initial=None):
plan_file_field = super(PlanFileField, self).clean(data, initial)

if plan_file_field is None:
return None

if not data and initial:
return initial

if data.content_type in self.ODT_CONTENT_TYPES:
try:
return UploadedODTFile(data).get_content()
except Exception:
raise forms.ValidationError(
self.error_messages['unexcept_odf_error'])

if data.content_type == MIMETYPE_HTML:
try:
return UploadedHTMLFile(data).get_content()
except Exception:
raise forms.ValidationError(
self.error_messages['unexpected_html_error'])

if data.content_type == MIMETYPE_PLAIN:
return UploadedPlainTextFile(data).get_content()

raise forms.ValidationError(self.error_messages['invalid_file_type'])


# =========== New Plan ModelForm ==============


class PlanModelForm(forms.ModelForm):
class Meta:
model = TestPlan
exclude = ('author', )


# =========== Forms for create/update ==============


class BasePlanForm(forms.Form):
Expand Down Expand Up @@ -194,7 +65,6 @@ def populate(self, product_id):


class NewPlanForm(BasePlanForm):
upload_plan_text = PlanFileField(required=False)
tag = forms.CharField(
label="Tag",
required=False
Expand Down Expand Up @@ -231,12 +101,6 @@ def clean_tag(self):
name__in=string_to_list(self.cleaned_data['tag'])
)

def clean(self):
cleaned_data = self.cleaned_data
if cleaned_data.get('upload_plan_text'):
cleaned_data['text'] = cleaned_data['upload_plan_text']
return cleaned_data


class EditPlanForm(NewPlanForm):
product_version = forms.ModelChoiceField(
Expand Down
4 changes: 2 additions & 2 deletions tcms/testplans/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def new(request, template_name='plan/new.html'):
# If the form has been submitted...
if request.method == 'POST':
# A form bound to the POST data
form = NewPlanForm(request.POST, request.FILES)
form = NewPlanForm(request.POST)
form.populate(product_id=request.POST.get('product'))

if form.is_valid():
Expand Down Expand Up @@ -436,7 +436,7 @@ def edit(request, plan_id, template_name='plan/edit.html'):

# If the form is submitted
if request.method == "POST":
form = EditPlanForm(request.POST, request.FILES)
form = EditPlanForm(request.POST)
form.populate(product_id=request.POST.get('product'))

# FIXME: Error handle
Expand Down

0 comments on commit a02d8dc

Please sign in to comment.