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_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_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 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 filter_applications(applications, cookies): """Filter applications based on user roles""" is_superuser = verify_superuser_auth_token( request.cookies.get('auth_token')) if is_superuser: # No filter for superuser return applications user = get_user(request.cookies) if user is None: abort(401) if has_role(user, 'flod_saksbehandlere'): # No filter for flod_saksbehandlere return applications if has_role(user, 'flod_brukere'): # Administratosr should only see applications for resources they own remote_resource_ids = [c['resource_id'] for c in user.get('credentials', []) if c['id'].startswith('CAN_EDIT_FACILITY_')] # Need to map the ids in the credentials which uses remote ids # to the local ones used in booking. uris = ["/facilities/%s" % i for i in remote_resource_ids] res = current_app.db_session.query(Resource).filter(Resource.uri.in_(uris)).all() local_resource_ids = [r.id for r in res] applications = applications.filter( Application.resource_id.in_(local_resource_ids) ) else: # External users should only see their own applications and the applications belonging to their organisations org_ids = [] orgs = get_person_from_web('/persons/%s/organisations/' % user['person_id']) org_uris = [org.get('uri') for org in orgs] if len(org_uris) > 0: res = current_app.db_session.query(Organisation).filter(Organisation.uri.in_(org_uris)).all() org_ids = [o.id for o in res] person_uri = '/persons/{}'.format(user['person_id']) if len(org_ids) == 0: applications = applications.filter( Application.person.has(uri=person_uri) ) else: applications = applications.filter( or_( Application.person.has(uri=person_uri), Application.organisation_id.in_(org_ids) ) ) return applications
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 is_organisation_member(organisation_id, user): if not is_idporten_user(user): return False if organisation_id: org_ids = [] orgs = get_person_from_web('/persons/%s/organisations/' % user['person_id']) org_uris = [org.get('uri') for org in orgs] if len(org_uris) > 0: res = current_app.db_session.query(Organisation).filter(Organisation.uri.in_(org_uris)).all() org_ids = [o.id for o in res] return organisation_id in org_ids return False
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 post(self): data = request.get_json() start_time = data["start_time"] end_time = data["end_time"] if start_time is None or end_time is None: abort(400) strotime_slots = self.get_strotime_slots(start_time, end_time) result = [] for strotime_slot in strotime_slots: personJSON = get_person_from_web(strotime_slot.application.person.uri) resourceJSON = get_resource_from_web(strotime_slot.application.resource.uri) result.append({ 'id': strotime_slot.id, 'start_time': strotime_slot.start_time, 'end_time': strotime_slot.end_time, 'application_id': strotime_slot.application.id, 'resource_name': resourceJSON['name'], 'email_address': personJSON['email_address'], 'person_name': personJSON['first_name'] + " " + personJSON['last_name'] }) return result, 201
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