Utilities Package

Submodules

APIs

class seed.utils.api.APIBypassCSRFMiddleware

Bases: object

This middleware turns off CSRF protection for API clients.

It must come before CsrfViewMiddleware in settings.MIDDLEWARE_CLASSES.

process_view(request, *args, **kwargs)

If this request is an API request, bypass CSRF protection.

seed.utils.api.api_endpoint(fn)

Decorator function to mark a view as allowed to authenticate via API key.

Decorator must be used before login_required or has_perm to set request.user for those decorators.

seed.utils.api.api_endpoint_class(fn)

Decorator function to mark a view as allowed to authenticate via API key.

Decorator must be used before login_required or has_perm to set request.user for those decorators.

seed.utils.api.clean_api_regex(url)

Given a django-style url regex pattern, strip it down to a human-readable url.

TODO: If pks ever appear in the url, this will need to account for that.

seed.utils.api.drf_api_endpoint(fn)

Decorator to register a Django Rest Framework view with the list of API endpoints. Marks it with is_api_endpoint = True as well as appending it to the global endpoints list.

seed.utils.api.format_api_docstring(docstring)

Cleans up a python method docstring for human consumption.

seed.utils.api.get_all_urls(urllist, prefix='')

Recursive generator that traverses entire tree of URLs, starting with urllist, yielding a tuple of (url_pattern, view_function) for each one.

seed.utils.api.get_api_endpoints()

Examines all views and returns those with is_api_endpoint set to true (done by the @api_endpoint decorator).

..:todo: this isn’t particularly expensive now, but if the number of URLs grows a lot, it may be worth caching this somewhere.

seed.utils.api.get_api_request_user(request)

Determines if this is an API request and returns the corresponding user if so.

Buildings

seed.utils.buildings.get_buildings_for_user_count(user)

returns the number of buildings in a user’s orgs

seed.utils.buildings.get_columns(org_id, all_fields=False)

Get default columns, to be overridden in future

Returns:

title: HTML presented title of column
sort_column: semantic name used by js and for searching DB
class: HTML CSS class for row td elements
title_class: HTML CSS class for column td elements
type: 'string', 'number', 'date'
min, max: the django filter key e.g. gross_floor_area__gte
field_type: assessor, pm, or compliance (currently not used)
sortable: determines if the column is sortable
checked: initial state of "edit columns" modal
static: True if option can be toggle (ID is false because it is
    always needed to link to the building detail page)
link: signifies that the cell's data should link to a building detail
    page
seed.utils.buildings.get_search_query(user, params)
seed.utils.buildings.get_source_type(import_file, source_type='')

Used for converting ImportFile source_type into an int.

seed.utils.buildings.serialize_building_snapshot(b, pm_cb, building)

returns a dict that’s safe to JSON serialize

Constants

Mappings

seed.utils.mapping.get_mappable_columns(exclude_fields=None)

Get a list of all the columns we’re able to map to that are fields in the database already

seed.utils.mapping.get_mappable_types(exclude_fields=None)

Like get_mappable_columns, but with type information.

Organizations

seed.utils.organizations.create_organization(user, org_name='', *args, **kwargs)

Helper script to create a user/org relationship from scratch.

Parameters:
  • user – user inst.
  • org_name – str, name of Organization we’d like to create.
  • kwargs ((optional)) – ‘role’, int; ‘status’, str.

Projects

seed.utils.projects.copy_buildings(source_project, target_project, buildings, select_all, search_params, user)

copies buildings from source project to target project

Parameters:
  • source_project_slug – str, a slug to get a Project inst.
  • target_project_slug – str, a slug to get a Project inst.
  • buildings – list, list of source_facility_id as str to get BuildingSnapshot inst.
  • select_all – bool, if the select all checkbox was checked. i.e. should we transfer all buildings in a project or just the buildings in the list
Search_params:

dict, params needed to generate a queryset of buildings, with keys (q, other_params, project_slug)

User:

User inst., django user instance needed for select all queryset

seed.utils.projects.delete_matching_buildings(project, buildings, select_all, search_params, user)

deletes buildings in a project that match search search params

Parameters:
  • project_slug – str, a slug to get a Project inst.
  • buildings – list, list of source_facility_id as str to get BuildingSnapshot inst.
  • select_all – bool, if the select all checkbox was checked. i.e. should we transfer all buildings in a project or just the buildings in the list
Search_params:

dict, params needed to generate a queryset of buildings, with keys (q, other_params, project_slug)

User:

User inst., django user instance needed for select all queryset

seed.utils.projects.get_projects(building, organization)

return an JSON friendly list of the building’s projects

Parameters:
  • building – the BuildingSnapshot inst.
  • organization – the Organization inst.
Returns:

list of projects

seed.utils.projects.get_transfer_buildings(source_project, target_project, buildings, select_all, search_params, user)

generates move or copy buildings queryset

Parameters:
  • source_project_slug – str, a slug to get a Project inst.
  • target_project_slug – str, a slug to get a Project inst.
  • buildings – list, list of source_facility_id as str to get BuildingSnapshot inst.
  • select_all – bool, if the select all checkbox was checked. i.e. should we transfer all buildings in a project or just the buildings in the list
Search_params:

dict, params needed to generate a queryset of buildings, with keys (q, other_params, project_slug)

User:

User inst., django user instance needed for select all queryset

Rtype Queryset:

a django queryset of buildings to move or copy

seed.utils.projects.move_buildings(source_project, target_project, buildings, select_all, search_params, user)

moves buildings from source project to target project

Parameters:
  • source_project_slug – str, a slug to get a Project inst.
  • target_project_slug – str, a slug to get a Project inst.
  • buildings – list, list of source_facility_id as str to get BuildingSnapshot inst.
  • select_all – bool, if the select all checkbox was checked. i.e. should we transfer all buildings in a project or just the buildings in the list
Search_params:

dict, params needed to generate a queryset of buildings, with keys (q, other_params, project_slug)

User:

User inst., django user instance needed for select all queryset

seed.utils.projects.transfer_buildings(source_project_slug, target_project_slug, buildings, select_all, search_params, user, copy_flag=False)

copies or moves buildings from one project to another

Parameters:
  • source_project_slug – str, a slug to get a Project inst.
  • target_project_slug – str, a slug to get a Project inst.
  • buildings – list, list of source_facility_id as str to get BuildingSnapshot inst.
  • select_all – bool, if the select all checkbox was checked. i.e. should we transfer all buildings in a project or just the buildings in the list
Search_params:

dict, params needed to generate a queryset of buildings, with keys (q, other_params, project_slug)

User:

User inst., django user instance needed for select all queryset and to update the projects’ “last changed” and “last changed by”

Copy_flag:

bool, True - copy buildings, False - move buildings

Time

seed.utils.time.convert_datestr(datestr)

Converts dates like 12/31/2010 into datetime objects.

seed.utils.time.convert_to_js_timestamp(timestamp)

converts a django/python datetime object to milliseconds since epoch

seed.utils.time.parse_datetime(maybe_datetime)

Process a datetime value that may be None, timestamp, strftime.