Esempio n. 1
0
def get_organisation_name(application):
    if application.organisation:
        organisation_data = get_organisation_from_web(
            application.organisation.uri
        )
        return organisation_data.get('name', None)
    return None
def render_email_template(application, status=None):
    resource_details = get_resource_from_web(application.resource.uri)
    requested_resource_details = get_resource_from_web(
        application.requested_resource.uri)
    org_name = None
    org_email = None
    org_local_email = None
    if application.organisation is not None:
        org_details = get_organisation_from_web(application.organisation.uri)
        org_name = org_details.get('name')
        org_email = org_details.get('email_address', None)
        org_local_email = org_details.get('local_email_address', None)

    email_address = None
    if application.person is not None:
        person_details = get_person_from_web(application.person.uri)
        email_address = person_details.get("email_address", None)

    resource_name = resource_details['name']
    requested_resource_name = requested_resource_details['name']
    application_time = application.application_time.strftime("%Y.%m.%d %H:%M")

    # Override status if given, used for previewing template
    if status is not None:
        application_status = format_application_status_for_email(status)
    else:
        application_status = format_application_status_for_email(
            application.status)

    application_type = application.get_type()
    requested_slots = format_slots_for_email(application.requested_slots,
                                             application_type)
    slots = format_slots_for_email(application.slots, application_type)

    requested_period = get_period_from_slots(application.requested_slots, application_type)
    period = get_period_from_slots(application.slots, application_type)

    changed_period = not is_period_equal(period, requested_period)
    changed_time = not is_slots_equal(slots, requested_slots) or changed_period;

    resource_documents = resource_details.get("documents", [])

    message = render_template("email_applications_processed.txt",
                              org_name=org_name,
                              resource_name=resource_name,
                              requested_resource_name=requested_resource_name,
                              application_time=application_time,
                              message=application.message,
                              application_status=application_status,
                              resource_documents=resource_documents,
                              slots=slots,
                              requested_slots=requested_slots,
                              changed_period=changed_period,
                              changed_time=changed_time,
                              period=period,
                              requested_period=requested_period)

    return [email_address, org_email, org_local_email], message
 def get_applicant(self, slot):
     application = slot['application']
     if application.organisation is not None:
         org = get_organisation_from_web(application.organisation.uri)
         return (org.get('name'), org.get('email_address'))
     if application.person is not None:
         person = get_person_from_web(application.person.uri)
         name = u'{}, {}'.format(person.get('last_name', ''),
                                 person.get('first_name', ''))
         return (name, person.get('email_address'))
     return None
def get_display_name(application):

    if application.organisation:
        try:
            org = get_organisation_from_web(application.organisation.uri)
            return org.get('name')
        except werkzeug.exceptions.NotFound:
            return u'Ad-hoc aktør'


    user = get_user(request.cookies)
    show_person = user is not None and has_role(user, 'flod_saksbehandlere')
    if show_person:
        person = get_person_from_web(application.person.uri)
        return '%s, %s' % (person.get('last_name', ''), person.get('first_name', ''))
    return "Privatperson"
    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)
def create_ws_journalpost(application, resource, document, ws_sak):
    person = get_person_from_web(application.person.uri)
    organisation = get_organisation_from_web(application.organisation.uri)

    adressat = WSAdressat(fornavn=person['first_name'] or '',
                          etternavn=person['last_name'] or '',
                          adresse1=person['address_line'] or '',
                          adresse2='',
                          postnr=person['postal_code'] or '',
                          sted=person['postal_city'] or '',
                          foedselsnr=person['national_identity_number'] or '',
                          orgnr=organisation['org_number'] or '')

    dok_tittel = u'Søknad godkjent: {} for lokale {}'.format(
        organisation['name'], resource['name'])
    dokument = WSDokument(dokument_tittel=dok_tittel,
                          fil_innhold=b64encode(document.encode('utf-8')))
    tittel2 = u' '.join((adressat.fornavn, adressat.etternavn))

    return WSJournalpost(adressat1=adressat,
                         saksbehandler=ws_sak['Saksansvarlig'],
                         tittel1=ws_sak['Tittel1'],
                         tittel2=tittel2,
                         dokument=dokument)
    def put(self, application_id):
        data = request.get_json()

        application = self.get_object_by_id(application_id)
        ensure(PUT, application)

        status = data.get("status", None)
        resource_uri = data["resource"]["uri"]
        application_message = data.get("message", "")
        to_be_invoiced = data.get("to_be_invoiced", None)
        invoice_amount = data.get("invoice_amount") or None
        if invoice_amount is not None and type(invoice_amount) is not int \
                and not invoice_amount.isdigit():
            abort(400, __error__=[u'Fakturabeløp må være heltall'])
        resource = get_resource_for_uri(resource_uri)
        if not resource:
            abort(404)
        resource_details = get_resource_from_web(application.resource.uri)
        if not resource_details:
            abort(404)

        settings = SettingsResource().get()

        user = get_user(request.cookies)

        # Check that the resource allows the type of application
        if application.get_type() == "single" and not resource.single_booking_allowed:
            abort(403, __error__=[u'Det er ikke mulig å behandle en søknad om engangslån fordi type utlån er deaktivert for lokalet'])

        if application.get_type() == "repeating" and (not resource.repeating_booking_allowed or \
                                                              (resource.repeating_booking_allowed and not settings["repeating_booking_allowed"] and not has_role(user,
                                                                                                                                                                 'flod_saksbehandlere'))):
            abort(403, __error__=[u'Det er ikke mulig å behandle en søknad om fast lån fordi type utlån er deaktivert for lokalet'])

        # The application might have been moved to a different resource.
        application.resource = resource
        if status == "Pending":
            application.amenities = application.requested_amenities
            application.accessibility = application.requested_accessibility
            application.equipment = application.requested_equipment
            application.suitability = application.requested_suitability
            application.facilitators = application.requested_facilitators
        else:
            application.amenities = data.get('amenities')
            application.accessibility = data.get('accessibility')
            application.equipment = data.get('equipment')
            application.suitability = data.get('suitability')
            application.facilitators = data.get('facilitators')

        slot_data = data.get("slots", None)
        if status == "Granted" and not slot_data:
            abort(403, __error__=[u'Tildelt tid mangler'])

        if application.get_type() == "single":
            slots = [parse_single_slot(slot, application) for slot in slot_data]
            application.single_slots = slots
        elif application.get_type() == "repeating":
            slots = [parse_repeating_slot(slot, application) for slot in slot_data]
            application.repeating_slots = slots

        # Check if there are any conflicts
        for slot in slots:
            if application.get_type() == "single":
                start_date = slot.start_time.date()
                end_date = slot.end_time.date()
                week_day = slot.start_time.isoweekday()
                start_time = slot.start_time.time()
                end_time = slot.end_time.time()
            elif application.get_type() == "repeating":
                start_date = slot.start_date
                end_date = slot.end_date
                week_day = slot.week_day
                start_time = slot.start_time
                end_time = slot.end_time

            if application.get_type() == "single":
                self.validate_end_date_of_slot(settings["single_booking_enddate"], end_date.isoformat(), u'engangslån')
            elif application.get_type() == "repeating":
                self.validate_end_date_of_slot(settings["repeating_booking_enddate"], end_date.isoformat(), u'fast lån')

            self.validate_start_and_end_times(start_date, end_date, start_time, end_time)
            if application.is_arrangement or status == "Denied":
                # NOTE: Arrangements trumph all other applications. If we reject an application, we dont nede to
                # check if the time is available...
                pass
            else:
                if self.is_conflict_slot_time(resource, start_date, end_date, week_day, start_time, end_time) or \
                        self.is_conflict_rammetid(resource, start_date, end_date, week_day, start_time, end_time):
                    abort(
                        400,
                        __error__=[u'Tiden du har søkt på er ikke tilgjengelig']
                    )

            if status != "Denied":
                if self.is_conflict_blocked_time(resource, start_date, end_date, week_day, start_time, end_time):
                    abort(
                        400,
                        __error__=[u'Tiden du har søkt på er blokkert']
                    )

        if status == "Granted" or status == "Denied" or (status == "Pending" and len(slots) == 0):
            application.status = status
        else:
            application.status = "Processing"

        application.message = application_message
        application.invoice_amount = invoice_amount
        application.to_be_invoiced = to_be_invoiced
        application.comment = data.get('comment')

        current_app.db_session.add(application)
        current_app.db_session.commit()
        current_app.db_session.refresh(application)

        document = ''
        if status == "Granted" or status == "Denied":
            document = send_email_application_processed(application)

        # Create erv code
        if invoice_amount is not None and status == "Granted":
            invoice_amount = int(invoice_amount)
            org_number = None
            if application.organisation is not None:
                organisation = get_organisation_from_web(
                    application.organisation.uri)
                org_number = organisation.get('org_number', None)

            if org_number is None:
                person = get_person_from_web(application.person.uri)
                erv_code = erv_person(application, invoice_amount,
                                      person['national_identity_number'])
            else:
                erv_code = erv_organisation(application, invoice_amount,
                                            org_number)

            print 'erv: "{}"'.format(erv_code)
            # Skip sending mail for now
            # send_erv_code_mail("{},".format(erv_code))

        application_dict = {}
        if application.get_type() == "single":
            application_dict = marshal(application, single_application_fields)
        elif application.get_type() == "repeating":
            # Authentication is not implemented for all resources yet, so we
            # skip creating sak/journalpost if no auth_token exists
            if status == 'Granted' and 'auth_token' in request.cookies:
                arkiver(application, document)

            application_dict = marshal(application, repeating_application_fields)

        # Include emails in response, if requested
        if data.get("include_emails", False):
            _, granted_message = render_email_template(application, "Granted")
            _, denied_message = render_email_template(application, "Denied")
            application_dict['emails'] = dict(
                granted_message=granted_message,
                denied_message=denied_message
            )

        return application_dict