Skip to content

Commit

Permalink
✅ 💚
Browse files Browse the repository at this point in the history
  • Loading branch information
puria committed Oct 19, 2018
1 parent e5f7573 commit 88f68ca
Show file tree
Hide file tree
Showing 19 changed files with 165 additions and 305 deletions.
2 changes: 1 addition & 1 deletion ksweb/ksweb/controllers/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ def import_document(self, workspace, file_import):
workspace))
}
content.append(c)
values['output_' + output['content']] = '${output_' + c['content'] + '}'
values[output['content']] = '#{' + c['content'] + '}'
html = Template(imported_document['html']).safe_substitute(**values)
model.Document(
_owner=owner,
Expand Down
4 changes: 2 additions & 2 deletions ksweb/ksweb/controllers/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def _validate_precondition_with_qa(self, precondition, html):
return

_filter = Precondition.query.get(_id=ObjectId(precondition))
answers = re.findall(r'%%([^\W]+)\b', html)
answers = re.findall(r'@{([^\W]+)\b}', html)
problematic = set(answers) - set(_filter.response_interested)
if len(problematic):
response.status_code = 412
Expand Down Expand Up @@ -179,7 +179,7 @@ def human_readable_details(self, _id, **kw):
return dict(output={
'_id': output._id,
'title': output.title,
'content': output.human_readbale_content,
'content': output.human_readable_content,
'human_readable_content': output.human_readable_content,
'_owner': output._owner,
'owner': output.owner.display_name,
Expand Down
18 changes: 9 additions & 9 deletions ksweb/ksweb/controllers/precondition/advanced.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ def new(self, workspace, **kw):
@expose('json')
@validate({
'title': StringLengthValidator(min=2),
'category': WorkspaceExistValidator(required=True),
'workspace': WorkspaceExistValidator(required=True),
'conditions': LengthValidator(min=1, required=True),
}, error_handler=validation_errors_response)
def post(self, title, category, conditions, **kw):
def post(self, title, workspace, conditions, **kw):
error, condition = self._marshall_complex_filter(conditions)
if error:
response.status_code = 412
Expand All @@ -47,29 +47,29 @@ def post(self, title, category, conditions, **kw):
user = request.identity['user']
Precondition(
_owner=user._id,
_category=ObjectId(category),
_category=ObjectId(workspace),
title=title,
type='advanced',
condition=condition
)

flash(_("Now you can create an output <a href='%s'>HERE</a>" % lurl('/output?workspace='+ str(category))))
flash(_("Now you can create an output <a href='%s'>HERE</a>" % lurl('/output?workspace='+ str(workspace))))
return dict(errors=None)

@decode_params('json')
@expose('json')
@validate({
'_id': PreconditionExistValidator(required=True),
'title': StringLengthValidator(min=2),
'category': WorkspaceExistValidator(required=True),
'workspace': WorkspaceExistValidator(required=True),
'conditions': LengthValidator(min=1, required=True),
}, error_handler=validation_errors_response)
@require(
CanManageEntityOwner(
msg=l_(u'You are not allowed to edit this filter.'),
field='_id',
entity_model=Precondition))
def put(self, _id, title, category, conditions, **kw):
def put(self, _id, title, workspace, conditions, **kw):
error, condition = self._marshall_complex_filter(conditions)
if error:
response.status_code = 412
Expand All @@ -82,18 +82,18 @@ def put(self, _id, title, category, conditions, **kw):
_id=_id,
title=title,
condition=list(map(str, condition)),
_category=category,
_category=workspace,
auto_generated=False,
entity='precondition/advanced',
)
session['entity'] = entity # overwrite always same key for avoiding conflicts
session.save()
return dict(redirect_url=tg.url('/resolve', params=dict(workspace=category)))
return dict(redirect_url=tg.url('/resolve', params=dict(workspace=workspace)))

precondition = Precondition.query.get(_id=ObjectId(_id))
precondition.title = title
precondition.condition = condition
precondition._category = category
precondition._category = workspace

return dict(errors=None, redirect_url=None)

Expand Down
16 changes: 8 additions & 8 deletions ksweb/ksweb/controllers/qa.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,14 @@ def new(self, workspace, **kw):
@expose('json')
@validate({
'title': StringLengthValidator(min=2),
'category': WorkspaceExistValidator(required=True),
'workspace': WorkspaceExistValidator(required=True),
'question': StringLengthValidator(min=2),
'tooltip': StringLengthValidator(min=0, max=100),
'link': StringLengthValidator(min=0, max=100),
'answer_type': OneOfValidator(values=Qa.QA_TYPE, required=True),
'precondition': PreconditionExistValidator(required=False),
}, error_handler=validation_errors_response)
def post(self, title, category, question, tooltip, link, answer_type, precondition=None, answers=None, **kw):
def post(self, title, workspace, question, tooltip, link, answer_type, precondition=None, answers=None, **kw):
if not self._are_answers_valid(answer_type, answers):
response.status_code = 412
return dict(errors={'answers': _('Please add at least one more answer')})
Expand All @@ -82,7 +82,7 @@ def post(self, title, category, question, tooltip, link, answer_type, preconditi

qa = Qa(
_owner=user._id,
_category=ObjectId(category),
_category=ObjectId(workspace),
_parent_precondition=to_object_id(precondition),
title=title,
question=question,
Expand All @@ -103,14 +103,14 @@ def post(self, title, category, question, tooltip, link, answer_type, preconditi
@validate({
'_id': QAExistValidator(required=True),
'title': StringLengthValidator(min=2),
'category': WorkspaceExistValidator(required=True),
'workspace': WorkspaceExistValidator(required=True),
'question': StringLengthValidator(min=2),
'tooltip': StringLengthValidator(min=0, max=100),
'link': StringLengthValidator(min=0, max=100),
'answer_type': OneOfValidator(values=Qa.QA_TYPE, required=True),
'precondition': PreconditionExistValidator(required=False),
}, error_handler=validation_errors_response)
def put(self, _id, title, category, question, tooltip, link, answer_type,
def put(self, _id, title, workspace, question, tooltip, link, answer_type,
precondition=None, answers=None, **kw):
if not self._are_answers_valid(answer_type, answers):
response.status_code = 412
Expand All @@ -120,7 +120,7 @@ def put(self, _id, title, category, question, tooltip, link, answer_type,

if check.get("entities"):
entity = dict(
_id=_id, _category=category,
_id=_id, _category=workspace,
title=title, entity='qa',
question=question,
tooltip=tooltip, link=link,
Expand All @@ -134,10 +134,10 @@ def put(self, _id, title, category, question, tooltip, link, answer_type,
session['entity'] = entity # overwrite always same key for avoiding conflicts
session.save()

return dict(redirect_url=tg.url('/resolve', params=dict(workspace=category)))
return dict(redirect_url=tg.url('/resolve', params=dict(workspace=workspace)))

qa = Qa.query.get(_id=ObjectId(_id))
qa._category = ObjectId(category)
qa._category = ObjectId(workspace)
qa._parent_precondition = to_object_id(precondition)
qa.title = title
qa.question = question
Expand Down
4 changes: 3 additions & 1 deletion ksweb/ksweb/controllers/questionary.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,9 @@ def responde(self, _id=None, qa_id=None, qa_response=None, **kwargs):
questionary.qa_values[qa_id] = {'qa_response': qa_response, 'order_number': order_number}
quest_compiled = questionary.evaluate_questionary

return dict(questionary=questionary, quest_compiled=quest_compiled, html=self.get_questionary_html(_id))
return dict(questionary=questionary,
quest_compiled=quest_compiled,
html=self.get_questionary_html(_id))

@expose('ksweb.templates.questionary.completed')
@validate({
Expand Down
2 changes: 2 additions & 0 deletions ksweb/ksweb/lib/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,8 @@ def get_related_entities_for_filters(_id):


def get_entities_from_str(html):
if not html:
return [], []
import re
outputs_ids = re.findall(r'#{([^\W]+)\b}', html)
answers_ids = re.findall(r'@{([^\W]+)\b}', html)
Expand Down
7 changes: 6 additions & 1 deletion ksweb/ksweb/model/questionary.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import tg
from bson import ObjectId
from ksweb.model import DBSession, Document, User, Output, Precondition
from ksweb.model import DBSession, Document, User, Output, Precondition, Qa
from markupsafe import Markup
from ming import schema as s
from ming.odm import FieldProperty, ForeignIdProperty, RelationProperty
Expand Down Expand Up @@ -238,5 +238,10 @@ def evaluate_expression(self, output_id):
'completed': True
}

@property
def answers(self):
questions = sorted(self.qa_values.items(), key=lambda i: i[1].order_number)
return [dict(question=Qa.query.get(_id=ObjectId(q)).title, answer=a.qa_response) for q, a in questions]


__all__ = ['Questionary']
2 changes: 1 addition & 1 deletion ksweb/ksweb/templates/precondition/advanced/new.xhtml
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@
self.set('saving', false);
self.set('categories', []);
self.set('conditions', []);
self.set('create.category', self.workspace);
self.set('create.workspace', self.workspace);
var load_conditions = function() {};

// Check if document is provided
Expand Down
2 changes: 1 addition & 1 deletion ksweb/ksweb/templates/qa/new.xhtml
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@
self.set('answer_type', null);
self.set('input_type', null);
self.set('available_response', []);
self.set('create.category', self.workspace);
self.set('create.workspace', self.workspace);

self.set('create.title', self.qa['title']);
self.set('create.question', self.qa['question']);
Expand Down
14 changes: 11 additions & 3 deletions ksweb/ksweb/templates/questionary/compile.xhtml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,14 @@
</div>
<hr/>
<div class="row mt-5">
<!--<div class="col">-->
<!--<dl>-->
<!--{{questionary.answers}}-->
<!--&lt;!&ndash;<dt>{{question}}</dt>&ndash;&gt;-->
<!--&lt;!&ndash;<dd>{{answer}}</dd>&ndash;&gt;-->
<!--&lt;!&ndash;{{/each}}&ndash;&gt;-->
<!--</dl>-->
<!--</div>-->
<div id="html" class="col pt-3">
{{{html}}}
</div>
Expand Down Expand Up @@ -109,7 +117,7 @@
var self = this;
self.set('questionary', self.questionary);
self.set('quest_compiled', self.quest_compiled);
self.set('html', KS.getMDConverter().makeHtml(self.html));
self.set('html', self.html);
self.set('qa_response', self.qa_response);
self.checkButtons();
self.set('errors', {});
Expand Down Expand Up @@ -174,7 +182,7 @@
self.set('saving', false);
self.set('questionary', data['questionary']);
self.set('quest_compiled', data['quest_compiled']);
self.set('html', KS.getMDConverter().makeHtml(data['html']));
self.set('html', data['html']);
self.set('qa_response', data['previous_response']);
self.set('errors', {});
self.checkButtons();
Expand Down Expand Up @@ -208,7 +216,7 @@
self.set('saving', false);
self.set('questionary', data['questionary']);
self.set('quest_compiled', data['quest_compiled']);
self.set('html', KS.getMDConverter().makeHtml(data['html']));
self.set('html', data['html']);
self.set('errors', {});
self.checkButtons();
self.next_qa();
Expand Down
45 changes: 13 additions & 32 deletions ksweb/ksweb/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def _create_qa(self, title, category_id, question, tooltip, link, qa_type, answe
self.app.post_json(
'/qa/post', params={
'title': title,
'category': str(category_id),
'workspace': str(category_id),
'question': question,
'tooltip': tooltip,
'link': link,
Expand Down Expand Up @@ -181,7 +181,7 @@ def _create_advanced_precondition(self, title, category_id, conditions=[]):
self.app.post_json(
'/precondition/advanced/post', params={
'title': title,
'category': str(category_id),
'workspace': str(category_id),
'conditions': conditions
}
)
Expand All @@ -202,14 +202,14 @@ def _create_fake_advanced_precondition_red_animal(self, title):

qa_color_param = {
'title': 'Favourite color',
'category': str(category1._id),
'workspace': str(category1._id),
'question': 'What is your favourite color?',
'tooltip': 'Favourite color',
'link': 'http://www.axant.it',
'answer_type': 'single',
'answers': ['Red', 'Blu', 'Yellow', 'Green']
}
qa_color = self._create_qa(qa_color_param['title'], qa_color_param['category'], qa_color_param['question'], qa_color_param['tooltip'], qa_color_param['link'], qa_color_param['answer_type'], qa_color_param['answers'])
qa_color = self._create_qa(qa_color_param['title'], qa_color_param['workspace'], qa_color_param['question'], qa_color_param['tooltip'], qa_color_param['link'], qa_color_param['answer_type'], qa_color_param['answers'])

prec_red_color = {
'title': 'Red is Favourite',
Expand All @@ -224,14 +224,14 @@ def _create_fake_advanced_precondition_red_animal(self, title):

qa_animal_liked_param = {
'title': 'Animal liked',
'category': str(category1._id),
'workspace': str(category1._id),
'question': 'What animal do you like?',
'tooltip': 'Animalsss',
'link': 'http://www.axant.it',
'answer_type': 'multi',
'answers': ['Dog', 'Cat', 'Pig', 'Turtle', 'Rabbit']
}
qa_animal_liked = self._create_qa(qa_animal_liked_param['title'], qa_animal_liked_param['category'], qa_animal_liked_param['question'], qa_animal_liked_param['tooltip'], qa_animal_liked_param['link'], qa_animal_liked_param['answer_type'], qa_animal_liked_param['answers'])
qa_animal_liked = self._create_qa(qa_animal_liked_param['title'], qa_animal_liked_param['workspace'], qa_animal_liked_param['question'], qa_animal_liked_param['tooltip'], qa_animal_liked_param['link'], qa_animal_liked_param['answer_type'], qa_animal_liked_param['answers'])

prec_animal_liked_pig_dog = {
'title': 'Like pig and dog',
Expand All @@ -250,7 +250,7 @@ def _create_fake_advanced_precondition_red_animal(self, title):
# Ora che ho i 2 filtri posso creare quelli composti
precond_advanced = {
'title': title,
'category': str(category1._id),
'workspace': str(category1._id),
'conditions': [
{
'type': 'precondition',
Expand Down Expand Up @@ -279,14 +279,10 @@ def _get_output(self, id):
def _get_output_by_title(self, title):
return model.Output.query.get(title=title)

def _create_output(self, title, category_id, precondition_id, content, html=''):
if isinstance(content, (str, unicode)):
content = []

def _create_output(self, title, category_id, precondition_id, html=''):
self.app.post_json(
'/output/post', params={
'title': title,
'content': content,
'workspace': str(category_id),
'precondition': str(precondition_id),
'html': html,
Expand All @@ -299,44 +295,29 @@ def _create_fake_output(self, title, category_id=None):
category_id = self._get_or_create_category("Fake_cat_%s" % title)._id
return self._create_output(title, category_id,
self._create_fake_simple_precondition(title)._id,
"Fake content text", "Fake html")
"Fake html")

def _get_document_by_title(self, title):
return model.Document.query.get(title=title)

def _create_document(self, title, category_id, content, html):
def _create_document(self, title, category_id, html):
if not category_id:
category_id = self._get_or_create_category("Fake_cat_%s" % title)._id

if isinstance(content, (str, unicode)):
content = [{
'type': "text",
'content': content,
'title': ""
}]

self.app.post_json(
'/document/post', params={
'title': title,
'workspace': str(category_id),
'content': content,
'html': html
}
).json

return self._get_document_by_title(title)

def _create_fake_document(self, title, html='', category_id=None):
def _create_fake_document(self, title, html=None, category_id=None):
output1 = self._create_fake_output(title)

content = [
{
'type': "output",
'content': str(output1._id),
'title': output1.title
},
]
return self._create_document(title, category_id, content, html)
html = html if html else "#{%s}" % str(output1._id)
return self._create_document(title, category_id, html)

# Questionary Utility
def _get_questionary(self, id):
Expand Down
Loading

0 comments on commit 88f68ca

Please sign in to comment.