Skip to content

Commit

Permalink
project_questions_form refactored
Browse files Browse the repository at this point in the history
  • Loading branch information
jochenklar committed Mar 23, 2016
1 parent 08c16e9 commit ab4a905
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 66 deletions.
68 changes: 51 additions & 17 deletions apps/projects/static/projects/js/project_questions_form.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,20 @@ app.config(['$httpProvider', '$interpolateProvider', function($httpProvider, $in

app.factory('FormService', ['$http', '$timeout', function($http, $timeout) {

function newValue() {
return {'text': ''};
}

service = {
options: {},
values: [],
errors: [],
valueset: null
values: {},
errors: {}
};

service.init = function(options) {
service.options = options;

if (service.options.attributeset) {
service.initValueSets();
} else {
service.initValues();
}
};

service.prev = function() {
Expand All @@ -46,22 +47,55 @@ app.factory('FormService', ['$http', '$timeout', function($http, $timeout) {
console.log(service.values);
};

service.addValue = function() {
service.values.push(newValue());
service.initValues = function() {
if (service.options.attribute.is_collection) {
service.values[service.options.attribute.tag] = [''];
} else {
service.values[service.options.attribute.tag] = '';
}
};

service.removeValue = function(index) {
service.values.splice(index, 1);
service.addValue = function(tag) {
if (angular.isUndefined(service.values)) {
service.values = {};
}

if (angular.isUndefined(service.values[tag])) {
service.values[tag] = [''];
} else {
service.values[tag].push('');
}
};

service.addValueSet = function() {
service.removeValue = function(tag, index) {
service.values[tag].splice(index, 1);
};

service.initValueSets = function() {
service.valueset = {};
service.initValueSetValues();

if (service.options.attributeset.is_collection) {
service.values[service.options.attributeset.tag] = [service.valueset];
} else {
service.values[service.options.attributeset.tag] = service.valueset;
}
};

angular.forEach(service.options.tags, function(tag) {
service.valueset[tag] = [];
service.initValueSetValues = function() {
angular.forEach(service.options.attributeset.attributes, function(attribute) {
if (attribute.is_collection) {
service.valueset[attribute.tag] = [''];
} else {
service.valueset[attribute.tag] = '';
}
});
};

service.values.push(service.valueset);
service.addValueSet = function() {
service.valueset = {};
service.values[service.options.attributeset.tag].push(service.valueset);
service.initValueSetValues();
};

service.removeValueSet = function() {
Expand All @@ -77,9 +111,9 @@ app.factory('FormService', ['$http', '$timeout', function($http, $timeout) {

service.addValueSetValue = function(tag) {
if (angular.isUndefined(service.valueset[tag])) {
service.valueset[tag] = [newValue()];
service.valueset[tag] = [''];
} else {
service.valueset[tag].push(newValue());
service.valueset[tag].push('');
}
};

Expand Down
50 changes: 30 additions & 20 deletions apps/projects/templates/projects/project_questions_form.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ <h4>

{% if questionset %}

{% if questionset.attributeset.is_collection %}

<div class="questionset-head">
<div class="pull-right">
<button type="button" class="btn btn-success" ng-click="service.addValueSet()">
Expand All @@ -43,87 +45,92 @@ <h4>

<ul class="pagination">
<li ng-class="{ active: valueset == service.valueset }"
ng-repeat="valueset in service.values">
ng-repeat="valueset in service.values['{{ questionset.attributeset }}']">
<a href="" ng-click="service.valueset = valueset">#{$ $index + 1 $}</a>
</li>
</ul>
</div>

{% endif %}

{% for question in questionset.questions.all %}
{% with question.attribute.tag as tag %}

<div ng-show="service.valueset">
<div class="form-group field-{{ question.tag }}">
<label for="id_{{ question.tag }}">{{ question.text }}</label>
<div>
<div class="form-group field-{{ tag }}">
<label for="id_{{ tag }}">{{ question.text }}</label>

{% if question.is_collection %}
{% if question.attribute.is_collection %}

<div class="input-group input-collection" ng-repeat="value in service.valueset['{{ question.tag }}']">
<div class="input-group input-collection" ng-repeat="value in service.valueset['{{ tag }}'] track by $index">

<input class="form-control" type="text" ng-model="value.text"/>
<input class="form-control" type="text" ng-model="service.valueset['{{ tag }}'][$index]"/>

<div class="input-group-btn">
<button type="button" class="btn btn-danger" tabindex="-1"
ng-click="service.removeValueSetValue('{{ question.tag }}', $index)">
ng-click="service.removeValueSetValue('{{ tag }}', $index)">
{% trans 'Remove' %}
</button>
</div>
</div>

<div class="form-group">
<button type="button" class="btn btn-success" ng-click="service.addValueSetValue('{{ question.tag }}')">
<button type="button" class="btn btn-success" ng-click="service.addValueSetValue('{{ tag }}')">
{% trans 'Add new item' %}
</button>
</div>

{% else %}

<input class="form-control" type="text" ng-model="service.valueset['{{ question.tag }}'][0].text"/>
<input class="form-control" type="text" ng-model="service.valueset['{{ tag }}']"/>

{% endif %}

</div>
</div>

{% endwith %}
{% endfor %}


{% else %}
{% with question.attribute.tag as tag %}

<div class="form-group field-{{ question.tag }}">
<label for="id_{{ question.tag }}">{{ question.text }}</label>
<div class="form-group field-{{ tag }}">
<label for="id_{{ tag }}">{{ question.text }}</label>

{% if is_collection %}
{% if question.attribute.is_collection %}

<div class="input-group input-collection" ng-repeat="value in service.values track by $index">
<div class="input-group input-collection" ng-repeat="value in service.values['{{ tag }}'] track by $index">

<input class="form-control" type="text" ng-model="service.values[$index].text"/>
<input class="form-control" type="text" ng-model="service.values['{{ tag }}'][$index]"/>

<div class="input-group-btn">
<button type="button" class="btn btn-danger" tabindex="-1"
ng-click="service.removeValue($index)">
ng-click="service.removeValue('{{ tag }}', $index)">
{% trans 'Remove' %}
</button>
</div>
</div>

{% else %}

<input class="form-control" type="text" ng-model="service.values[0].text"/>
<input class="form-control" type="text" ng-model="service.values['{{ tag }}']"/>

{% endif %}

</div>

{% if is_collection %}
{% if question.attribute.is_collection %}

<div class="form-group">
<button type="button" class="btn btn-success" ng-click="service.addValue()">
<button type="button" class="btn btn-success" ng-click="service.addValue('{{ tag }}')">
{% trans 'Add new item' %}
</button>
</div>

{% endif %}

{% endwith %}
{% endif %}

<div class="help-block error" ng-show="service.errors"></div>
Expand All @@ -146,6 +153,9 @@ <h4>
</button>
</div>

<pre>{$ service.values $}</pre>
<pre>{$ service.valueset $}</pre>

</form>
</div>

Expand Down
48 changes: 36 additions & 12 deletions apps/projects/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,25 +72,49 @@ def project_questions_form(request, project_id, question_entity_id=None):
prev_question_entity_id = question_entity_id_list[current_index - 1] if current_index > 0 else None
next_question_entity_id = question_entity_id_list[current_index + 1] if current_index + 1 < len(question_entity_id_list) else None

context = {
'project': project,
'title': question_entity.title,
'progress': (100.0 * (1 + current_index)/len(question_entity_id_list))
}

options = {
'is_set': question_entity.is_set,
'prev': False if prev_question_entity_id is None else True,
'next': False if next_question_entity_id is None else True,
'next': False if next_question_entity_id is None else True
}

if question_entity.is_set:
options['tags'] = [question.tag for question in question_entity.questionset.questions.all()]
questionset = question_entity.questionset

context['question'] = False
context['questionset'] = questionset

options['attribute'] = False
options['attributeset'] = {
'tag': questionset.attributeset.tag,
'is_collection': questionset.attributeset.is_collection,
'attributes': [{
'tag': question.attribute.tag,
'is_collection': question.attribute.is_collection
} for question in questionset.questions.all()]
}

else:
options['tags'] = [question_entity.question.tag]
question = question_entity.question

return render(request, 'projects/project_questions_form.html', {
'project': project,
'title': question_entity.title,
'is_collection': question_entity.is_collection,
'question': False if question_entity.is_set else question_entity.question,
'questionset': False if not question_entity.is_set else question_entity.questionset,
'progress': (100.0 * (1 + current_index)/len(question_entity_id_list)),
'options_json': json.dumps(options)
})
context['question'] = question
context['questionset'] = False

options['attribute'] = {
'tag': qquestion.attribute.tag,
'is_collection': question.attribute.is_collection
}
options['attributeset'] = False

context['options_json'] = json.dumps(options)

return render(request, 'projects/project_questions_form.html', context)

# if question_entity.is_set:
# # get all valuesets for this questionset
Expand Down
14 changes: 0 additions & 14 deletions apps/questions/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,13 +144,6 @@ class QuestionSet(QuestionEntity):

attributeset = models.ForeignKey(AttributeSet, blank=True, null=True, on_delete=models.SET_NULL, related_name='questionsets')

@property
def tag(self):
if self.attributeset:
return self.attributeset.tag
else:
return 'none'

class Meta:
verbose_name = _('QuestionSet')
verbose_name_plural = _('QuestionSets')
Expand Down Expand Up @@ -187,13 +180,6 @@ class Question(QuestionEntity):
def text(self):
return self.trans('text')

@property
def tag(self):
if self.attribute:
return self.attribute.tag
else:
return 'none'

class Meta:
ordering = ('subsection__section__order', 'subsection__order', 'order')
verbose_name = _('Question')
Expand Down
6 changes: 3 additions & 3 deletions apps/questions/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def get_success_url(self):

class SubsectionCreateQuestionView(ProtectedCreateView):
model = Question
fields = ['order', 'attribute', 'is_collection', 'text_en', 'text_de', 'widget_type']
fields = ['order', 'attribute', 'text_en', 'text_de', 'widget_type']

def dispatch(self, *args, **kwargs):
self.subsection = get_object_or_404(Subsection, pk=self.kwargs['pk'])
Expand All @@ -105,7 +105,7 @@ def form_valid(self, form):

class SubsectionCreateQuestionSetView(ProtectedCreateView):
model = QuestionSet
fields = ['order', 'attributeset', 'is_collection', 'title_en', 'title_de']
fields = ['order', 'attributeset', 'title_en', 'title_de']

def dispatch(self, *args, **kwargs):
self.subsection = get_object_or_404(Subsection, pk=self.kwargs['pk'])
Expand Down Expand Up @@ -154,7 +154,7 @@ def get_success_url(self):

class QuestionSetCreateQuestionView(ProtectedCreateView):
model = Question
fields = ['order', 'attribute', 'is_collection', 'text_en', 'text_de', 'widget_type']
fields = ['order', 'attribute', 'text_en', 'text_de', 'widget_type']

def dispatch(self, *args, **kwargs):
self.questionset = get_object_or_404(QuestionSet, pk=self.kwargs['pk'])
Expand Down

0 comments on commit ab4a905

Please sign in to comment.