from django.core.exceptions import ObjectDoesNotExist


def add_value_list(data_list, value=None):
    if value:
        data_list.append(value)


def add_dict(data_dict=dict, field=None, value=None):
    if value and field and isinstance(field, str):
        data_dict[field] = value


def bool_string(value=bool):
    return '1' if value else '0'


def value_string(value):
    if value:
        return str(value)

    return None


def date_time_string(value):
    if value:
        return value.strftime('%Y-%m-%dT%H:%M:%SZ')

    return None


def date_string(value):
    if hasattr(value, "strftime"):
        if hasattr(value, "hour"):
            offset = value.utcoffset()
            if offset:
                value = value - offset

            value = value.replace(tzinfo=None).isoformat() + "Z"
        else:
            value = "%sT00:00:00Z" % value.isoformat()

        return value

    return None


def decimal_string(value):
    if value:
        return str(value)

    return None


def none_or_value(value):
    try:
        return value
    except ObjectDoesNotExist:
        return None


def get_narrative_lang_list(data):
    lang_list = list()
    narrative_list = list()
    for narrative in data.narratives.all():
        add_value_list(lang_list, narrative.language_id)
        add_value_list(narrative_list, narrative.content)

    return lang_list, narrative_list


def add_reporting_org(serializer, activity):
    reporting_organisation = activity.reporting_organisations.first()
    if reporting_organisation:
        serializer.add_field('reporting_org_ref', reporting_organisation.ref)
        serializer.add_field(
            'reporting_org_type',
            reporting_organisation.type_id
        )
        serializer.add_field(
            'reporting_org_secondary_reporter',
            bool_string(reporting_organisation.secondary_reporter)
        )
        serializer.add_field(
            'reporting_org_narrative',
            getattr(reporting_organisation.organisation, 'primary_name', None)
        )


def get_child_attr(data, field):
    attrs = field.split('.')
    value = None
    for attr in attrs:
        value = getattr(data, attr, None)
        if value:
            data = value
        else:
            break

    return value


def field_narrative(serializer, field, key):
    if field:
        for narrative in field.narratives.all():
            serializer.add_value_list(key, narrative.content)

@@ -940,32 +940,27 @@
 value):%0A    
-if value
+try
 :%0A        re
@@ -967,33 +967,58 @@
 turn str(value)%0A
-%0A
+    except NameError:%0A    
     return None%0A

-fix OIPA-2248 "0.00" Values Not Output In CSV