Skip to content

Commit

Permalink
Fold get_types into one function
Browse files Browse the repository at this point in the history
  • Loading branch information
oliverroick committed Apr 28, 2017
1 parent 08709fa commit 83dd095
Show file tree
Hide file tree
Showing 10 changed files with 128 additions and 204 deletions.
47 changes: 27 additions & 20 deletions cadasta/core/form_mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,39 @@
from .widgets import XLangSelect, XLangSelectMultiple


def get_types(question_name, questionnaire_id=None):
if not questionnaire_id:
return None
def get_types(question_name, default, questionnaire_id=None,
include_labels=False):
types = []
if questionnaire_id:
try:
question = Question.objects.get(
name=question_name,
questionnaire=questionnaire_id)
except Question.DoesNotExist:
pass
else:
types = []
options = QuestionOption.objects.filter(
question=question).values_list('name', 'label_xlat')

try:
question = Question.objects.get(
name=question_name,
questionnaire=questionnaire_id)
except Question.DoesNotExist:
return None
lang = get_language()
default_lang = question.questionnaire.default_language

types = []
options = QuestionOption.objects.filter(
question=question).values_list('name', 'label_xlat')
for o in options:
label = o[1]

lang = get_language()
default_lang = question.questionnaire.default_language
if isinstance(label, dict):
label = label.get(lang, label.get(default_lang))

for o in options:
label = o[1]
types.append((o[0], label))

if isinstance(label, dict):
label = label.get(lang, label.get(default_lang))
if not types:
types = default

types.append((o[0], label))
return types
if include_labels:
return types
else:
return [t[0] for t in types]


class SuperUserCheck:
Expand Down
82 changes: 73 additions & 9 deletions cadasta/core/tests/test_form_mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from organization.tests.factories import ProjectFactory
from party.models import Party
from party.tests.factories import PartyFactory
from party.choices import TENURE_RELATIONSHIP_TYPES as default_types
from questionnaires.tests import factories as q_factories

from .. import form_mixins
Expand Down Expand Up @@ -390,11 +391,17 @@ def test_add_attribute_fields(self):

class GetTypesTest(TestCase):
def test_get_types_no_questionnaire(self):
assert form_mixins.get_types('tenure_type') is None
types = form_mixins.get_types('tenure_type',
default_types,
include_labels=True)
assert types == default_types

def test_get_types_no_question(self):
assert (form_mixins.get_types('tenure_type', questionnaire_id='abc') is
None)
types = form_mixins.get_types('tenure_type',
default_types,
questionnaire_id='abc',
include_labels=True)
assert types == default_types

def test_get_types_questionnaire_default_label(self):
questionnaire = q_factories.QuestionnaireFactory.create()
Expand All @@ -411,9 +418,11 @@ def test_get_types_questionnaire_default_label(self):
name='BB',
label='BB Label')

labels = form_mixins.get_types('tenure_type',
questionnaire_id=questionnaire.id)
assert list(labels) == [('AA', 'AA Label'), ('BB', 'BB Label')]
types = form_mixins.get_types('tenure_type',
default_types,
questionnaire_id=questionnaire.id,
include_labels=True)
assert list(types) == [('AA', 'AA Label'), ('BB', 'BB Label')]

def test_get_types_questionnaire_multilang_label(self):
questionnaire = q_factories.QuestionnaireFactory.create(
Expand All @@ -432,6 +441,61 @@ def test_get_types_questionnaire_multilang_label(self):
name='BB',
label={'kar': 'BB Karen', 'cs': 'BB Czech'})

labels = form_mixins.get_types('tenure_type',
questionnaire_id=questionnaire.id)
assert list(labels) == [('AA', 'AA Karen'), ('BB', 'BB Karen')]
types = form_mixins.get_types('tenure_type',
default_types,
questionnaire_id=questionnaire.id,
include_labels=True)
assert list(types) == [('AA', 'AA Karen'), ('BB', 'BB Karen')]

def test_get_types_no_questionnaire_no_labels(self):
types = form_mixins.get_types('tenure_type',
default_types)
assert types == [t[0] for t in default_types]

def test_get_types_no_question_no_labels(self):
types = form_mixins.get_types('tenure_type',
default_types,
questionnaire_id='abc')
assert types == [t[0] for t in default_types]

def test_get_types_questionnaire_default_label_no_labels(self):
questionnaire = q_factories.QuestionnaireFactory.create()
question = q_factories.QuestionFactory.create(
type='S1',
name='tenure_type',
questionnaire=questionnaire)
q_factories.QuestionOptionFactory.create(
question=question,
name='AA',
label='AA Label')
q_factories.QuestionOptionFactory.create(
question=question,
name='BB',
label='BB Label')

types = form_mixins.get_types('tenure_type',
default_types,
questionnaire_id=questionnaire.id)
assert list(types) == ['AA', 'BB']

def test_get_types_questionnaire_multilang_label_no_labels(self):
questionnaire = q_factories.QuestionnaireFactory.create(
default_language='kar')
question = q_factories.QuestionFactory.create(
type='S1',
name='tenure_type',
label={'kar': 'Karen Type', 'cs': 'Czech Type'},
questionnaire=questionnaire)
q_factories.QuestionOptionFactory.create(
question=question,
name='AA',
label={'kar': 'AA Karen', 'cs': 'AA Czech'})
q_factories.QuestionOptionFactory.create(
question=question,
name='BB',
label={'kar': 'BB Karen', 'cs': 'BB Czech'})

types = form_mixins.get_types('tenure_type',
default_types,
questionnaire_id=questionnaire.id)
assert list(types) == ['AA', 'BB']
16 changes: 11 additions & 5 deletions cadasta/organization/views/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@
from questionnaires.exceptions import InvalidQuestionnaire
from questionnaires.models import Questionnaire
from resources.models import ContentObject, Resource
from party.utils import get_tenure_types
from spatial.utils import get_location_types
from core.form_mixins import get_types
from party.choices import TENURE_RELATIONSHIP_TYPES
from spatial.choices import TYPE_CHOICES

from . import mixins
from .. import messages as error_messages
Expand Down Expand Up @@ -872,9 +873,14 @@ def done(self, form_list, **kwargs):
map_attrs_data = self.storage.get_step_data('map_attributes')
project = self.get_project()
org = project.organization
allowed_tenure_types = get_tenure_types(project.current_questionnaire)
allowed_location_types = get_location_types(
project.current_questionnaire)
allowed_tenure_types = get_types(
'tenure_types',
TENURE_RELATIONSHIP_TYPES,
questionnaire_id=project.current_questionnaire)
allowed_location_types = get_types(
'location_types',
TYPE_CHOICES,
questionnaire_id=project.current_questionnaire)

config_dict = {
'project': project,
Expand Down
7 changes: 5 additions & 2 deletions cadasta/party/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
from rest_framework import serializers

from .models import Party, PartyRelationship, TenureRelationship
from .choices import TENURE_RELATIONSHIP_TYPES
from core.serializers import FieldSelectorSerializer
from spatial.serializers import SpatialUnitSerializer
from party.utils import get_tenure_types
from core.form_mixins import get_types


class PartySerializer(FieldSelectorSerializer, serializers.ModelSerializer):
Expand Down Expand Up @@ -93,7 +94,9 @@ class Meta:

def validate_tenure_type(self, value):
prj = self.context['project']
allowed_types = get_tenure_types(prj.current_questionnaire)
allowed_types = get_types('tenure_types',
TENURE_RELATIONSHIP_TYPES,
questionnaire_id=prj.current_questionnaire)

if value not in allowed_types:
msg = "'{}' is not a valid choice for field 'tenure_type'."
Expand Down
68 changes: 0 additions & 68 deletions cadasta/party/tests/test_utils.py

This file was deleted.

14 changes: 0 additions & 14 deletions cadasta/party/utils.py

This file was deleted.

11 changes: 7 additions & 4 deletions cadasta/search/views/async.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
from spatial.models import SpatialUnit
from spatial.choices import TYPE_CHOICES as SPATIAL_TYPE_CHOICES
from party.models import Party, TenureRelationship
from party.utils import get_tenure_types
from party.choices import TENURE_RELATIONSHIP_TYPES
from core.form_mixins import get_types
from resources.models import Resource

api = settings.ES_SCHEME + '://' + settings.ES_HOST + ':' + settings.ES_PORT
Expand All @@ -37,10 +38,12 @@ def post(self, request, *args, **kwargs):
dataTablesDraw = int(request.data['draw'])

project = self.get_project()
types = get_types('tenure_type',
TENURE_RELATIONSHIP_TYPES,
questionnaire_id=project.current_questionnaire,
include_labels=True)

self.tenure_types = dict(get_tenure_types(
questionnaire_id=project.current_questionnaire,
include_labels=True))
self.tenure_types = dict(types)

results_as_html = []
num_hits = 0
Expand Down
7 changes: 5 additions & 2 deletions cadasta/spatial/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
from rest_framework_gis import serializers as geo_serializers

from .models import SpatialUnit, SpatialRelationship
from .utils import get_location_types
from .choices import TYPE_CHOICES
from core.form_mixins import get_types
from core.serializers import FieldSelectorSerializer


Expand All @@ -21,7 +22,9 @@ class Meta:

def validate_type(self, value):
prj = self.context['project']
allowed_types = get_location_types(prj.current_questionnaire)
allowed_types = get_types('location_type',
TYPE_CHOICES,
questionnaire_id=prj.current_questionnaire)

if value not in allowed_types:
msg = "'{}' is not a valid choice for field 'type'."
Expand Down
Loading

0 comments on commit 83dd095

Please sign in to comment.