def post(self, organisation_id=None, note_id=None):
        data = request.get_json()

        text = data["text"]

        organisation_data = data.get("organisation", None)
        if organisation_data:
            organisation_uri = organisation_data["uri"]
            organisation = get_organisation_for_uri(organisation_uri)
        else:
            organisation = None

        auth_id = data["auth_id"]

        note = OrganisationInternalNote(organisation, text, auth_id)

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

        return note, 201
    def post(self):
        data = request.get_json()

        is_arrangement = data.get("isArrangement", False)
        person = data.get('person', None)
        if not person or not person.get('uri', None):
            abort(400)
        person_uri = person["uri"]
        text = data["text"]
        facilitation = data["facilitation"]

        resource_uri = data["resource"]["uri"]
        resource = get_resource_for_uri(resource_uri)

        settings = SettingsResource().get()
        user = get_user(request.cookies)

        # Check that the resource allows the type of application
        if not settings["single_booking_allowed"] and not (has_role(user, 'flod_brukere') or has_role(user, 'flod_saksbehandlere')) or \
                not resource.single_booking_allowed:
            abort(403, __error__=[u'Engangslån ikke tillatt'])

        organisation_data = data.get("organisation", None)
        if organisation_data:
            organisation_uri = organisation_data["uri"]
            organisation = get_organisation_for_uri(organisation_uri)
        else:
            organisation = None

        person = get_person_for_uri(person_uri)
        application = Application(
            person,
            organisation,
            text,
            facilitation,
            resource,
            amenities=data.get('amenities'),
            accessibility=data.get('accessibility'),
            equipment=data.get('equipment'),
            suitability=data.get('suitability'),
            facilitators=data.get('facilitators')
        )
        application.is_arrangement = is_arrangement

        slots = [self.parse_slot_request(d) for d in data["slots"]]
        if not slots:
            abort(400, __error__=[u'Tidspunkt mangler'])

        for slot in slots:
            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()

            self.validate_end_date_of_slot(settings["single_booking_enddate"], end_date.isoformat(), u'engangslån')
            self.validate_start_and_end_times(start_date, end_date, start_time, end_time)

            if not is_arrangement:
                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 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']
                )

            application.request_single_slot(slot)

        current_app.db_session.add(application)
        current_app.db_session.commit()
        current_app.db_session.refresh(application)
        send_email_to_resource(application)
        send_email_to_applicant(application)
        return application, 201
    def get(self, organisation_id):

        if "start_date" in request.args and "end_date" in request.args:
            start_date, end_date = self.parse_date_range_from_args(request)
        else:
            abort(404, __error__=["No date or date interval specified."])

        statuses = ["Granted"]

        organisation = get_organisation_for_uri("/organisations/%s" % organisation_id)

        single_booking = current_app.db_session.query(Resource.uri.label("resource_uri"), func.sum(Slot.end_time - Slot.start_time).label("time")) \
            .filter(Application.organisation_id == organisation.id,
                    Slot.application_id == Application.id,
                    Resource.id == Application.resource_id,
                    Application.status.in_(statuses),
                    # Get all slots between start and end date
                    cast(Slot.start_time, Date).between(start_date, end_date),
                    cast(Slot.end_time, Date).between(start_date, end_date),
                    ) \
            .group_by(Resource.uri)

        strotime_booking = current_app.db_session.query(Resource.uri.label("resource_uri"), func.sum(StrotimeSlot.end_time - StrotimeSlot.start_time).label("time")) \
            .filter(Application.organisation_id == organisation.id,
                    StrotimeSlot.application_id == Application.id,
                    Resource.id == Application.resource_id,
                    Application.status.in_(statuses),
                    # Get all slots between start and end date
                    cast(StrotimeSlot.start_time, Date).between(start_date, end_date),
                    cast(StrotimeSlot.end_time, Date).between(start_date, end_date),
                    ) \
            .group_by(Resource.uri)

        repeating_booking = current_app.db_session.query(Resource.uri.label("resource_uri"), RepeatingSlot.start_date, RepeatingSlot.end_date, RepeatingSlot.start_time,
                                                         RepeatingSlot.end_time, RepeatingSlot.week_day) \
            .filter(Application.organisation_id == organisation.id,
                    RepeatingSlot.application_id == Application.id,
                    Resource.id == Application.resource_id,
                    Application.status.in_(statuses),
                    # Get all slots between start and end date
                    cast(RepeatingSlot.start_date, Date) <= end_date,
                    cast(RepeatingSlot.end_date, Date) >= start_date,
                    )

        results = []
        for single in single_booking.all():
            add_hours(results, single)

        for strotime in strotime_booking.all():
            add_hours(results, strotime)

        for repeating in repeating_booking.all():
            start = repeating.start_date
            if start < start_date:
                start = start_date

            end = repeating.end_date
            if end > end_date:
                end = end_date

            weeks = (end - start).days / 7

            starthours = datetime.combine(date.min, repeating.start_time)
            endhours = datetime.combine(date.min, repeating.end_time)

            hours = (endhours - starthours).total_seconds() / 3600.0

            total_hours = hours * weeks

            if ((end == start and start.isoweekday() == repeating.week_day)  # exact day
                or ((end - start).days % 7 != 0  # remaining week
                    # repeating weekday after start and before end. I.E start is tuesday, repeating is wednesday, end is friday
                    and ((start.isoweekday() <= repeating.week_day <= end.isoweekday())
                         # repeating weekday before start and before end, and start is after end. I.E start is friday, repeating is tuesday and end is wednesday
                         or (end.isoweekday() <= start.isoweekday() >= repeating.week_day <= end.isoweekday())))):
                total_hours += hours

            if total_hours > 0:
                item = find_resource(results, repeating.resource_uri)
                item['hours'] += total_hours

        return marshal(results, organisation_statistic_fields)
    def post(self):
        data = request.get_json()

        rammetid_to_application = RammetidToApplication(data.get('umbrella_organisation').get('uri'))
        ensure(POST, rammetid_to_application)

        resource = data.get("resource", None)
        if not resource:
            abort(403, __error__=[u'Ressurs er ikke angitt.'])

        resource = get_resource_for_uri(resource['uri'])
        if not resource:
            abort(404, __error__=[u'Ressursen finnes ikke.'])

        if not resource.repeating_booking_allowed:
            abort(403, __error__=[u'Gjentakende lån ikke tillatt'])

        user = get_user(request.cookies)
        person_uri = '/persons/{}'.format(user['person_id'])
        person = get_person_for_uri(person_uri)
        if not person:
            abort(404, __error__=[u'Personen finnes ikke.'])

        # get umbrella member orgs from organisations backend
        umbrella_member_organisations = get_umbrella_organisation_from_web(data.get('umbrella_organisation').get('uri')).get('organisations', [])

        # get local umbrella organisation object
        umbrella_organisation = get_umbrella_organisation_for_uri(data.get('umbrella_organisation').get('uri'))

        text = "Rammetid fordeling"

        applications = []
        organisations = data.get('organisations', None)
        for organisation in organisations:
            organisation_data = organisations[organisation]
            # have to be superuser if the organisation is not public and not copied to booking already
            organisation = get_organisation_for_uri(organisation_data['uri'], cookies=make_superuser_auth_cookie())

            if not organisation:
                abort(404, __error__=[u'Organisasjonen finnes ikke.'])

            if not any(org.get('uri') == organisation_data['uri'] for org in umbrella_member_organisations):
                abort(403, __error__=[u'Organisasjonen hører ikke til paraplyorganisasjonen'])

            application = Application(person, organisation, text, None, resource)
            application.status = 'Granted'

            slots = organisation_data["slots"]
            for slot_data in slots:

                slot = self.parse_slot(slot_data, application)
                slot_request = self.parse_slot_request(slot_data)

                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

                self.validate_start_and_end_times(start_date, end_date, start_time, end_time)

                if not self.has_matching_rammetid(umbrella_organisation.id, start_date, end_date, week_day, start_time, end_time):
                    abort(400, __error__=[u'Ingen rammetid passer'])

                if self.is_conflict_slot_time(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 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'])

                application.request_repeating_slot(slot_request)
                application.add_repeating_slot(slot)
            applications.append(application)

        for application in applications:
            current_app.db_session.add(application)
            current_app.db_session.commit()
            current_app.db_session.refresh(application)

        return applications, 201
    def post(self):
        data = request.get_json()

        person = data.get('person', None)
        if not person or not person.get('uri', None):
            abort(400)
        person_uri = person["uri"]
        text = data["text"]
        facilitation = data['facilitation']

        resource_uri = data["resource"]["uri"]
        resource = get_resource_for_uri(resource_uri)

        settings = SettingsResource().get()
        user = get_user(request.cookies)

        # Check that the resource allows the type of application
        if (resource.repeating_booking_allowed and not settings["repeating_booking_allowed"] and not has_role(user, 'flod_saksbehandlere')) or \
                (not settings["repeating_booking_allowed"] and not (has_role(user, 'flod_brukere') or has_role(user, 'flod_saksbehandlere'))) or \
                not resource.repeating_booking_allowed:
            abort(403, __error__=[u'Gjentakende lån ikke tillatt'])

        organisation_data = data.get("organisation", None)
        if organisation_data:
            organisation_uri = organisation_data["uri"]
            organisation = get_organisation_for_uri(organisation_uri)
        else:
            organisation = None
        if not organisation:
            abort(
                403,
                __error__=[u'Du kan ikke søke gjentakende lån som privatperson']
            )

        person = get_person_for_uri(person_uri)
        application = Application(
            person,
            organisation,
            text,
            facilitation,
            resource,
            amenities=data.get('amenities'),
            accessibility=data.get('accessibility'),
            equipment=data.get('equipment'),
            suitability=data.get('suitability'),
            facilitators=data.get('facilitators')
        )

        slots = None
        try:
            slots = [self.parse_slot_request(d) for d in data["slots"]]
        except BookingDomainException as e:
            abort(400, __error__=[e.message])

        if not slots:
            abort(400, __error__=[u'Tidspunkt mangler'])

        for slot in slots:
            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

            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 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 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']
                )

            application.request_repeating_slot(slot)

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

        return application, 201