Skip to content

Utils

combine_text(lines)

Combines several lines of text together into a str and returns it. If the text strips down to the empty string, None is returned. Only single characters are inserted between lines even if there are multiple new lines between text in the lines parameter.

:param lines: a number of lines of text in an iterable of strs :return: the combined text or None if no text results after combining

Source code in dataimporter/emu/views/utils.py
113
114
115
116
117
118
119
120
121
122
123
124
def combine_text(lines: Iterable[str]) -> Optional[str]:
    """
    Combines several lines of text together into a str and returns it. If the text
    strips down to the empty string, None is returned. Only single \n characters are
    inserted between lines even if there are multiple new lines between text in the
    lines parameter.

    :param lines: a number of lines of text in an iterable of strs
    :return: the combined text or None if no text results after combining
    """
    text = '\n'.join(filter(None, (line.strip() for line in lines)))
    return text if text else None

emu_date(date, time) cached

Given the date and time from a pair of EMu date and time fields, returns the ISO formatted datetime.

Parameters:

Name Type Description Default
date str

an EMu date string

required
time str

an EMu time string

required

Returns:

Type Description
Optional[str]

None if either the date or the time are missing, otherwise

Source code in dataimporter/emu/views/utils.py
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
@lru_cache(maxsize=65536)
def emu_date(date: str, time: str) -> Optional[str]:
    """
    Given the date and time from a pair of EMu date and time fields, returns the ISO
    formatted datetime.

    :param date: an EMu date string
    :param time: an EMu time string
    :return: None if either the date or the time are missing, otherwise
    """
    if not date or not time:
        return None

    try:
        # parse the string and then replace the timezone
        return parse_datetime(f'{date} {time}').replace(tzinfo=tz.UTC).isoformat()
    except ValueError:
        return None

is_valid_guid(record)

Checks if the record has a valid GUID. If it does, returns True, else returns False.

Parameters:

Name Type Description Default
record SourceRecord

the record to check

required

Returns:

Type Description
bool

True if the record has a valid GUID, False if not

Source code in dataimporter/emu/views/utils.py
70
71
72
73
74
75
76
77
78
def is_valid_guid(record: SourceRecord) -> bool:
    """
    Checks if the record has a valid GUID. If it does, returns True, else returns False.

    :param record: the record to check
    :return: True if the record has a valid GUID, False if not
    """
    guid = record.get_first_value('AdmGUIDPreferredValue')
    return guid is not None and GUID_REGEX.match(guid)

is_web_published(record)

Checks if the record should be published on the Data Portal. If it should, returns True, else False.

Parameters:

Name Type Description Default
record SourceRecord

the record to check

required

Returns:

Type Description
bool

True if the record can be published on the Portal, False if not

Source code in dataimporter/emu/views/utils.py
59
60
61
62
63
64
65
66
67
def is_web_published(record: SourceRecord) -> bool:
    """
    Checks if the record should be published on the Data Portal. If it should, returns
    True, else False.

    :param record: the record to check
    :return: True if the record can be published on the Portal, False if not
    """
    return record.get_first_value('AdmPublishWebNoPasswordFlag', lower=True) == 'y'

orientation_requires_swap(record)

Determines whether the image represented by the given record object requires its width and height values to be swapped based on the EXIF orientation tag. If the tag isn't found or the tag indicates no width/height swap is needed, then False is returned. If the orientation tag is in the range 5-8 then a swap is required and True is returned.

Parameters:

Name Type Description Default
record SourceRecord

the SourceRecord object

required

Returns:

Type Description
bool

True if the width and height need to be swapped, False if not

Source code in dataimporter/emu/views/utils.py
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
def orientation_requires_swap(record: SourceRecord) -> bool:
    """
    Determines whether the image represented by the given record object requires its
    width and height values to be swapped based on the EXIF orientation tag. If the tag
    isn't found or the tag indicates no width/height swap is needed, then False is
    returned. If the orientation tag is in the range 5-8 then a swap is required and
    True is returned.

    :param record: the SourceRecord object
    :return: True if the width and height need to be swapped, False if not
    """
    exif_tags = record.get_all_values('ExiTag', reduce=False, clean=False)
    exif_tag_values = record.get_all_values('ExiValue', reduce=False, clean=False)
    if exif_tags and exif_tag_values:
        try:
            # the orientation tag is 274 (0x0112), so look up the index of that in
            # the tags tuple and then the value will be at the same index in the
            # values tuple. Because EMu, the value can be either a number or the
            # text version, e.g. "7" or "Rotate 270 CW"
            orientation = exif_tag_values[exif_tags.index('274')]
            # if the orientation tag has one of the values in the swap set, return True
            return orientation.lower() in EXIF_ORIENTATION_SWAP_VALUES
        except ValueError:
            # the orientation tag wasn't present in the exif data, fall through
            pass

    return False

translate_collection_code(department) cached

Given a department from an EMu record, return the short code version we display on the Data Portal in the collectionCode field.

Parameters:

Name Type Description Default
department str

the department name

required

Returns:

Type Description
Optional[str]

the short code version of the department, or None if no match is found

Source code in dataimporter/emu/views/utils.py
101
102
103
104
105
106
107
108
109
110
@lru_cache
def translate_collection_code(department: str) -> Optional[str]:
    """
    Given a department from an EMu record, return the short code version we display on
    the Data Portal in the collectionCode field.

    :param department: the department name
    :return: the short code version of the department, or None if no match is found
    """
    return DEPARTMENT_COLLECTION_CODES.get(department)