def send_email_to_resource(application):

    person_data = get_person_from_web(application.person.uri)
    resource_data = get_resource_from_web(application.resource.uri)

    person_name = get_person_name(person_data)
    resource_name = resource_data.get('name', None)
    slots = generate_slots(application)

    unit_email = resource_data.get('unit_email_address', None)
    unit_name = resource_data.get('unit_name', 'Ukjent enhet')
    org_name = get_organisation_name(application)

    if application.type == 'strotime':
        to_email = unit_email
        topic = u'Strøtime tildelt'
        message = render_template(
            'email_strotime_granted_unit.txt',
            resource_name=resource_name,
            unit_name=unit_name,
            person_name=person_name,
            slots=slots
        )

    message = None
    topic = None
    to_email = None
    if application.type == 'single':
        to_email = unit_email
        topic = u'Søknad om engangslån for %s' % unit_name
        message = render_template(
            'email_single_applied_unit.txt',
            resource_name=resource_name,
            unit_name=unit_name,
            person_name=person_name,
            slots=slots,
            organisation_name=org_name
        )

    if application.type == 'repeating':
        to_email = TK_EMAIL
        topic = u'Søknad om fast lån for %s' % unit_name
        message = render_template(
            'email_repeating_applied_unit.txt',
            resource_name=resource_name,
            unit_name=unit_name,
            person_name=person_name,
            slots=slots,
            organisation_name=org_name
        )

    if not to_email or not topic or not message:
        return

    send_email_task.delay(
        topic,
        u'*****@*****.**',
        [to_email],
        message
    )
def send_email_to_applicant(application):

    person_data = get_person_from_web(application.person.uri)
    resource_data = get_resource_from_web(application.resource.uri)

    person_email = person_data.get('email_address')
    resource_name = resource_data.get('name', None)

    message = None
    topic = None

    if application.type == 'single':
        topic = u'Søknad om engangslån'
        message = render_template(
            'email_application_created.txt',
            resource_name=resource_name
        )

    if application.type == 'repeating':
        topic = u'Søknad om fast lån'
        message = render_template(
            'email_application_created.txt',
            resource_name=resource_name
        )

    if not person_email or not topic or not message:
        return

    send_email_task.delay(
        topic,
        u'*****@*****.**',
        [person_email],
        message
    )
def send_email_strotime_granted(application):
    personJSON = get_person_from_web(application.person.uri)
    resourceJSON = get_resource_from_web(application.resource.uri)
    person_name = ""
    if personJSON["first_name"] and personJSON["last_name"]:
        person_name = personJSON["first_name"] + " " + personJSON["last_name"]
    if personJSON["email_address"]:
        application_status = format_application_status_for_email(application.status)

        resource_name = ""
        if resourceJSON["name"]:
            resource_name = resourceJSON["name"]

        slots = []
        for slot in application.slots:
            slots.append(format_slot_for_email(slot))
        message = render_template(
            "email_strotime_granted.txt",
            resource_name=resource_name,
            application_status=application_status,
            slots=slots,
            person_name=person_name,
        )
        resource_email = personJSON["email_address"]
        if message is not None and resource_email is not None:
            send_email_task.delay(u"Strøtime tildelt", u"*****@*****.**", [resource_email], message)
def send_email_application_processed(application):
    email_address, message = render_email_template(application)
    if email_address and message is not None:
        send_email_task.delay(u'Søknad om lån av lokale',
                              u'*****@*****.**',
                              email_address,
                              message)

    return message
    def send_email_to_affected_applications(self, arrangement,
                                            affected_applications,
                                            message):
        arrangement_slots = []
        for arrangement_slot in arrangement.slots:
            arrangement_slots.append(format_slot_for_email(arrangement_slot))

        for affected_application in affected_applications:
            if affected_application.slots:
                resource_details = get_resource_from_web(affected_application.resource.uri)
                resource_name = resource_details['name']

                person_details = get_person_from_web(affected_application.person.uri)
                email_address = person_details['email_address']

                org_name = None
                if affected_application.organisation is not None:
                    org_details = get_organisation_from_web(affected_application.organisation.uri)
                    org_name = org_details.get('name')

                application_time = affected_application.application_time.strftime("%Y.%m.%d %H:%M:00")

                slots = []
                # Just create human readable text out of slots
                for slot in affected_application.slots:
                    if affected_application.get_type() == "repeating":
                        slots.append(format_repeating_slot_for_email(slot))
                    else:
                        slots.append(format_slot_for_email(slot))

                msg = render_template("email_arrangement_notification.txt",
                                      org_name=org_name,
                                      resource_name=resource_name,
                                      application_time=application_time,
                                      slots=slots,
                                      arrangement_slots=arrangement_slots,
                                      message=message)

                if email_address and msg is not None:
                    send_email_task.delay(u'Arrangement',
                                          u'*****@*****.**',
                                          [email_address],
                                           msg)