Beispiel #1
0
    def get_context_data(self, *args, **kwargs):
        context = super().get_context_data(*args, **kwargs)
        context['relationships'] = self.object.tenurerelationship_set.all(
        ).select_related('party').defer('party__attributes')

        project = context['object']
        if project.current_questionnaire:
            question = Question.objects.get(
                name='location_type',
                questionnaire_id=project.current_questionnaire)
            context['type_labels'] = template_xlang_labels(question.label_xlat)

            option = QuestionOption.objects.get(question=question,
                                                name=context['location'].type)
            context['type_choice_labels'] = template_xlang_labels(
                option.label_xlat)

            tenure_type = Question.objects.get(
                name='tenure_type',
                questionnaire_id=project.current_questionnaire)
            tenure_opts = QuestionOption.objects.filter(question=tenure_type)
            tenure_opts = dict(tenure_opts.values_list('name', 'label_xlat'))

            for rel in context['relationships']:
                rel.type_labels = template_xlang_labels(
                    tenure_opts.get(rel.tenure_type_id))

        location = context['location']
        user = self.request.user
        context['is_allowed_edit_location'] = user.has_perm(
            'spatial.update', location)
        context['is_allowed_delete_location'] = user.has_perm(
            'spatial.delete', location)

        return context
    def get_context_data(self, *args, **kwargs):
        context = super().get_context_data(*args, **kwargs)

        project = context['object']
        if project.current_questionnaire:
            try:
                tenure_type = Question.objects.get(
                    name='tenure_type',
                    questionnaire_id=project.current_questionnaire)
                context['type_labels'] = template_xlang_labels(
                    tenure_type.label_xlat)
            except Question.DoesNotExist:
                pass
            else:
                try:
                    option = QuestionOption.objects.get(
                        question=tenure_type,
                        name=context['relationship'].tenure_type)
                    context['type_choice_labels'] = template_xlang_labels(
                        option.label_xlat)
                except QuestionOption.DoesNotExist:
                    pass
        user = self.request.user
        context['is_allowed_delete_rel'] = user.has_perm(
            'tenure_rel.delete', context['relationship'])
        context['is_allowed_update_rel'] = user.has_perm(
            'tenure_rel.update', context['relationship'])
        return context
Beispiel #3
0
    def set_standard_field(self, name, empty_choice=None, field_name=None):
        if not field_name:
            field_name = name
        q = Questionnaire.objects.get(id=self.project.current_questionnaire)
        default_lang = q.default_language
        try:
            question = Question.objects.get(name=name, questionnaire=q)
            self.fields[field_name].labels_xlang = template_xlang_labels(
                question.label_xlat)

            if question.has_options:
                choices = QuestionOption.objects.filter(
                    question=question).values_list('name', 'label_xlat')

                try:
                    choices, xlang_labels = zip(*[((c[0],
                                                    c[1].get(default_lang)),
                                                   (c[0], c[1]))
                                                  for c in choices])
                except AttributeError:
                    choices = choices
                    xlang_labels = ''

                choices = ([('', empty_choice)] +
                           list(choices) if empty_choice else list(choices))
                self.fields[field_name].widget = XLangSelect(
                    attrs=self.fields[field_name].widget.attrs,
                    choices=choices,
                    xlang_labels=dict(xlang_labels))
        except Question.DoesNotExist:
            pass
    def set_standard_field(self, name, empty_choice=None, field_name=None):
        if not field_name:
            field_name = name
        q = Questionnaire.objects.get(id=self.project.current_questionnaire)
        default_lang = q.default_language
        try:
            question = Question.objects.get(name=name, questionnaire=q)
            self.fields[field_name].labels_xlang = template_xlang_labels(
                    question.label_xlat)

            if question.has_options:
                choices = QuestionOption.objects.filter(
                    question=question).values_list('name', 'label_xlat')

                try:
                    choices, xlang_labels = zip(
                        *[((c[0], c[1].get(default_lang)),
                          (c[0], c[1])) for c in choices])
                except AttributeError:
                    choices = choices
                    xlang_labels = ''

                choices = ([('', empty_choice)] + list(choices)
                           if empty_choice else list(choices))
                self.fields[field_name].widget = XLangSelect(
                    attrs=self.fields[field_name].widget.attrs,
                    choices=choices,
                    xlang_labels=dict(xlang_labels)
                )
        except Question.DoesNotExist:
            pass
Beispiel #5
0
 def render_option(self, selected_choices, option_value, option_label):
     rendered = super().render_option(
         selected_choices, option_value, option_label)
     rendered = rendered.replace(
         '<option',
         '<option ' + template_xlang_labels(
             self.xlang_labels.get(option_value, ''))
     )
     return rendered
Beispiel #6
0
    def get_context_data(self, *args, **kwargs):
        context = super().get_context_data(*args, **kwargs)

        project = context['object']
        if project.current_questionnaire:
            tenure_type = Question.objects.get(
                name='tenure_type',
                questionnaire_id=project.current_questionnaire)
            context['type_labels'] = template_xlang_labels(
                tenure_type.label_xlat)

            option = QuestionOption.objects.get(
                question=tenure_type,
                name=context['relationship'].tenure_type_id)
            context['type_choice_labels'] = template_xlang_labels(
                option.label_xlat)

        return context
Beispiel #7
0
    def get_context_data(self, *args, **kwargs):
        context = super().get_context_data(*args, **kwargs)
        context['relationships'] = self.object.tenurerelationship_set.all(
        ).select_related('spatial_unit').defer('spatial_unit__attributes')

        project = context['object']
        if project.current_questionnaire:
            name = Question.objects.get(
                name='party_name',
                questionnaire_id=project.current_questionnaire)
            context['name_labels'] = template_xlang_labels(name.label_xlat)

            party_type = Question.objects.get(
                name='party_type',
                questionnaire_id=project.current_questionnaire)
            context['type_labels'] = template_xlang_labels(
                party_type.label_xlat)

            option = QuestionOption.objects.get(question=party_type,
                                                name=context['party'].type)
            context['type_choice_labels'] = template_xlang_labels(
                option.label_xlat)

            tenure_type = Question.objects.get(
                name='tenure_type',
                questionnaire_id=project.current_questionnaire)
            tenure_opts = QuestionOption.objects.filter(question=tenure_type)
            tenure_opts = dict(tenure_opts.values_list('name', 'label_xlat'))

            location_type = Question.objects.get(
                name='location_type',
                questionnaire_id=project.current_questionnaire)
            location_opts = QuestionOption.objects.filter(
                question=location_type)
            location_opts = dict(
                location_opts.values_list('name', 'label_xlat'))

            for rel in context['relationships']:
                rel.type_labels = template_xlang_labels(
                    tenure_opts.get(rel.tenure_type_id))
                rel.location_labels = template_xlang_labels(
                    location_opts.get(rel.spatial_unit.type))

        return context
Beispiel #8
0
    def create_model_fields(self, field_prefix, attribute_map, new_item=False):
        for selector, attributes in attribute_map.items():
            for name, attr in attributes.items():
                fieldname = '{}::{}::{}'.format(field_prefix, selector.lower(),
                                                name)
                atype = attr.attr_type

                field_kwargs = {
                    'label': attr.long_name,
                    'required': attr.required
                }
                field = form_field_from_name(atype.form_field)
                if not new_item:
                    self.set_initial(field_kwargs, attr.name, attr)
                if atype.form_field in ['ChoiceField', 'MultipleChoiceField']:
                    if (attr.choice_labels is not None
                            and attr.choice_labels != []):
                        chs = list(zip(attr.choices, attr.choice_labels))
                    else:
                        chs = [(c, c) for c in attr.choices]

                    field_kwargs['choices'] = chs
                if atype.form_field == 'BooleanField':
                    field_kwargs['required'] = attr.required
                    if len(attr.default) > 0:
                        self.set_default(field_kwargs, attr, boolean=True)
                else:
                    if attr.required and new_item:
                        field_kwargs['required'] = True
                    if len(attr.default) > 0 and len(
                            str(field_kwargs.get('initial', ''))) == 0:
                        self.set_default(field_kwargs, attr)

                f = field(**field_kwargs)

                if hasattr(f.widget, 'choices'):
                    try:
                        xlang_labels = dict(
                            zip(attr.choices, attr.choice_labels_xlat))
                    except TypeError:
                        xlang_labels = {}

                    widget_args = {
                        'attrs': f.widget.attrs,
                        'choices': f.widget.choices,
                        'xlang_labels': xlang_labels
                    }

                    if isinstance(f, MultipleChoiceField):
                        f.widget = XLangSelectMultiple(**widget_args)
                    else:
                        f.widget = XLangSelect(**widget_args)

                f.labels_xlang = template_xlang_labels(attr.long_name_xlat)
                self.fields[fieldname] = f
    def create_model_fields(self, field_prefix, attribute_map, new_item=False):
        for selector, attributes in attribute_map.items():
            for name, attr in attributes.items():
                fieldname = '{}::{}::{}'.format(
                    field_prefix, selector.lower(), name)
                atype = attr.attr_type

                field_kwargs = {
                    'label': attr.long_name, 'required': attr.required
                }
                field = form_field_from_name(atype.form_field)
                if not new_item:
                    self.set_initial(field_kwargs, attr.name, attr)
                if atype.form_field in ['ChoiceField', 'MultipleChoiceField']:
                    if (attr.choice_labels is not None and
                            attr.choice_labels != []):
                        chs = list(zip(attr.choices, attr.choice_labels))
                    else:
                        chs = [(c, c) for c in attr.choices]

                    field_kwargs['choices'] = chs
                if atype.form_field == 'BooleanField':
                    field_kwargs['required'] = attr.required
                    if len(attr.default) > 0:
                        self.set_default(field_kwargs, attr, boolean=True)
                else:
                    if attr.required and new_item:
                        field_kwargs['required'] = True
                    if len(attr.default) > 0 and len(str(
                            field_kwargs.get('initial', ''))) == 0:
                        self.set_default(field_kwargs, attr)

                f = field(**field_kwargs)

                if hasattr(f.widget, 'choices'):
                    try:
                        xlang_labels = dict(zip(attr.choices,
                                                attr.choice_labels_xlat))
                    except TypeError:
                        xlang_labels = {}

                    widget_args = {
                        'attrs': f.widget.attrs,
                        'choices': f.widget.choices,
                        'xlang_labels': xlang_labels
                    }

                    if isinstance(f, MultipleChoiceField):
                        f.widget = XLangSelectMultiple(**widget_args)
                    else:
                        f.widget = XLangSelect(**widget_args)

                f.labels_xlang = template_xlang_labels(attr.long_name_xlat)
                self.fields[fieldname] = f
Beispiel #10
0
    def get_context_data(self, *args, **kwargs):
        context = super().get_context_data(*args, **kwargs)

        project = context['object']
        if project.current_questionnaire:
            name = Question.objects.get(
                name='party_name',
                questionnaire_id=project.current_questionnaire)
            context['name_labels'] = template_xlang_labels(name.label_xlat)

            party_type = Question.objects.get(
                name='party_type',
                questionnaire_id=project.current_questionnaire)
            context['type_labels'] = template_xlang_labels(
                party_type.label_xlat)

            option = QuestionOption.objects.get(question=party_type,
                                                name=context['party'].type)
            context['type_choice_labels'] = template_xlang_labels(
                option.label_xlat)

        return context
Beispiel #11
0
    def get_context_data(self, *args, **kwargs):
        context = super().get_context_data(*args, **kwargs)
        project = context['object']

        if project.current_questionnaire:
            party_type = Question.objects.get(
                name='party_type',
                questionnaire_id=project.current_questionnaire)
            party_opts = QuestionOption.objects.filter(question=party_type)
            party_opts = dict(party_opts.values_list('name', 'label_xlat'))

            for party in context['object_list']:
                party.type_labels = template_xlang_labels(
                    party_opts.get(party.type))

        return context
Beispiel #12
0
    def get_context_data(self, *args, **kwargs):
        context = super().get_context_data(*args, **kwargs)
        context['relationships'] = self.object.tenurerelationship_set.all(
        ).select_related('spatial_unit').defer('spatial_unit__attributes')

        project = context['object']
        if project.current_questionnaire:
            try:
                name = Question.objects.get(
                    name='party_name',
                    questionnaire_id=project.current_questionnaire)
                context['name_labels'] = template_xlang_labels(name.label_xlat)
            except Question.DoesNotExist:
                pass

            try:
                party_type = Question.objects.get(
                    name='party_type',
                    questionnaire_id=project.current_questionnaire)
                context['type_labels'] = template_xlang_labels(
                    party_type.label_xlat)
            except Question.DoesNotExist:
                pass
            else:
                try:
                    option = QuestionOption.objects.get(
                        question=party_type, name=context['party'].type)
                    context['type_choice_labels'] = template_xlang_labels(
                        option.label_xlat)
                except QuestionOption.DoesNotExist:
                    pass

            try:
                tenure_type = Question.objects.get(
                    name='tenure_type',
                    questionnaire_id=project.current_questionnaire)
                tenure_opts = QuestionOption.objects.filter(
                    question=tenure_type)
                tenure_opts = dict(
                    tenure_opts.values_list('name', 'label_xlat'))
            except Question.DoesNotExist:
                tenure_opts = None

            try:
                location_type = Question.objects.get(
                    name='location_type',
                    questionnaire_id=project.current_questionnaire)
                location_opts = QuestionOption.objects.filter(
                    question=location_type)
                location_opts = dict(
                    location_opts.values_list('name', 'label_xlat'))
            except Question.DoesNotExist:
                location_opts = None

            for rel in context['relationships']:
                if tenure_opts:
                    rel.type_labels = template_xlang_labels(
                        tenure_opts.get(rel.tenure_type))
                if location_opts:
                    rel.location_labels = template_xlang_labels(
                        location_opts.get(rel.spatial_unit.type))
        context['is_allowed_update_party'] = self.request.user.has_perm(
            'party.update', context['party'])
        context['is_allowed_delete_party'] = self.request.user.has_perm(
            'party.delete', context['party'])
        return context
Beispiel #13
0
 def test_none(self):
     assert mixins.template_xlang_labels(None) == ''
Beispiel #14
0
 def test_string(self):
     assert mixins.template_xlang_labels('Field 1') == ''
Beispiel #15
0
 def test_dict(self):
     res = mixins.template_xlang_labels({'en': 'Field 1', 'de': 'Feld 1'})
     assert 'data-label-en="Field 1"' in res
     assert 'data-label-de="Feld 1"' in res