akvo.utils package

class akvo.utils.DjangoModel(table_name, app, model_name)

Bases: tuple

app

Alias for field number 1

model_name

Alias for field number 2

table_name

Alias for field number 0

class akvo.utils.HttpResponseNoContent(content=b'', *args, **kwargs)[source]

Bases: HttpResponse

status_code = 204
class akvo.utils.ObjectReaderProxy(real)[source]

Bases: object

Proxy object to wrap other object for read only purpose.

Main use case is for wrapping models to be used in the presentation layer. Additional property/method can be added to encapsulate presentation logic in sub-classes.

akvo.utils.check_auth_groups(group_names)[source]
akvo.utils.codelist_choices(codelist, show_code=True)[source]

Based on a model from the codelists app, returns a list of tuples with the available choices.

Parameters:
  • codelist – Codelist from codelists store

  • show_code – Show the code (e.g. ‘1 - ..’) in front of the name, True by default

Returns:

List of tuples with available choices, tuples in the form of (code, name)

akvo.utils.codelist_has_value(model, value, version='2.03')[source]

Check if value exists

akvo.utils.codelist_name(model, instance, field, version='2.03')[source]

Looks up the name of a codelist, returns the field value if the lookup fails :param model: Model from codelists app :param instance: Instance from model :param field: String of the lookup field (e.g. ‘type’) :param version: String of version (optional) :return: String of the codelist instance

akvo.utils.codelist_value(model, instance, field, version='2.03')[source]

Looks up the value of a codelist :param model: Model from codelists app :param instance: Instance from model :param field: String of the lookup field (e.g. ‘type’) :param version: String of version (optional) :return: String of the codelist instance

akvo.utils.custom_get_or_create_country(iso_code, country=None)[source]

add the missing fields to a skeleton country object from the admin or create a new one with the given iso_code if it doesn’t already exist

akvo.utils.ensure_decimal(value)[source]
akvo.utils.file_from_zip_archive(zip, file_name)[source]

Return a file from a zip archive :param zip: zip file or file name :param file_name: name of the file to retrieve from the archive :return: the file or None

akvo.utils.filter_query_string(qs)[source]

Takes a QueryDict and returns a string that can be prepended to paginated links. Since pagination is handled outside of this function we pop the page item.

akvo.utils.get_country(*args, **kwargs)[source]

Stub function since one of the migrations imports this function

akvo.utils.get_placeholder_thumbnail(file_, geometry_string, **options)[source]

Return a place holder url for the given geometry string

akvo.utils.get_project_for_object(Project, obj)[source]

Return the Project to which an object is associated.

akvo.utils.get_report_thumbnail(file_)[source]

Helper function to guarantee the same settings for existing and new thumbs of project update photos

akvo.utils.get_sha1_hash(s)[source]

return the sha1 hash of the string you call with

akvo.utils.log_project_changes(user, project, related_obj, data, action)[source]

Logs all changes to Django’s LogEntry model.

akvo.utils.make_safe_timezone_aware_date(d)[source]

If datetime is naive and is an invalid datetime of current timezone, then assume datetime is in UTC and convert it to a datetime of current timezone.

akvo.utils.maybe_decimal(value)[source]
akvo.utils.model_and_instance_based_filename(object_name, pk, field_name, img_name)[source]

Create a file name for an image based on the model name, the current object’s pk, the field name of the model and the current date and time

akvo.utils.pagination(page, object_list, objects_per_page)[source]
akvo.utils.right_now_in_akvo()[source]

Calculate the numbers used in the “Right now in Akvo” box on the home page.

akvo.utils.rsr_image_path(instance, file_name, path_template='db/project/%s/%s')[source]

Use to set ImageField upload_to attribute. Create path for image storing. When a new object instance is created we save in MEDIA_ROOT/db/project/temp/img_name.ext first and then immediately call save on the ImageFieldFile when the object instance has been saved to the db, so the path changes to MEDIA_ROOT/db/project/org.pk/img_name.ext. Modify path by supplying a path_tempate string

akvo.utils.rsr_send_mail(to_list, subject='templates/email/test_subject.txt', message='templates/email/test_message.txt', subject_context=None, msg_context=None, html_message=None, attachments=None)[source]
Send template driven email.

to_list is a list of email addresses subject and message are templates for use as email subject and message body subject_context and msg_context are dicts used when renedering the respective templates html_message is the HTML template for use as message body

settings.RSR_DOMAIN is added to both contexts as current_site, defaulting to ‘akvo.org’ if undefined

akvo.utils.rsr_send_mail_to_users(users, subject='test/test_subject.txt', message='test/test_message.txt', subject_context=None, msg_context=None)[source]

Send mail to many users supplied through a queryset

akvo.utils.rsr_show_keywords(instance)[source]
akvo.utils.save_image(img, name, field_name)[source]
akvo.utils.send_mail_with_attachments(subject, message, from_email, recipient_list, fail_silently=False, auth_user=None, auth_password=None, connection=None, html_message=None, attachments=None)[source]

Extension of django.core.main.send_mail to allow the inclusion of attachments

Easy wrapper for sending a single message to a recipient list. All members of the recipient list will see the other recipients in the ‘To’ field.

If auth_user is None, the EMAIL_HOST_USER setting is used. If auth_password is None, the EMAIL_HOST_PASSWORD setting is used.

Note: The API for this method is frozen. New code wanting to extend the functionality should use the EmailMessage class directly.

attachments must be a list of dicts of the form

{‘filename’: <file name>, ‘content’: <attachment data>, ‘mimetype’: mime type}

akvo.utils.send_user_invitation(email, user, invited_user, employment=None, project=None)[source]
akvo.utils.single_period_dates(name)[source]
akvo.utils.to_bool(obj) bool[source]

Converts a given object to a boolean

akvo.utils.to_gmt(dt)[source]
akvo.utils.who_am_i()[source]

introspecting function returning the name of the function where who_am_i is called

akvo.utils.who_is_parent()[source]

introspecting function returning the name of the caller of the function where who_is_parent is called

Submodules