def post(self, soknad_id):

        if soknad_id is not None:
            soknad = SoknadRepo.find_by_id(soknad_id)
        else:
            return SakApi.create_error_response_for_iframe(body_status=400, body_error='Missing object id')

        ensure(MANAGE, SoknadAction(soknad, SoknadStateMachine.t_last_opp_saksvedlegg))

        validator = VedleggValidator(request.form).validate_post_fields()
        if validator.has_errors():
            return SakApi.create_error_response_for_iframe(body_status=400, body_error=validator.errors)

        file = request.files.get('upload_file')
        if file is None:
            current_app.logger.warn('Missing required file: document')
            return SakApi.create_error_response_for_iframe(body_status=400, body_error=u'Dokument er påkrevd.')

        extension = os.path.splitext(file.filename)[1]

        if file.mimetype not in VALID_MIME_TYPES or extension not in VALID_EXTENSIONS:
            current_app.logger.warn('Invalid mimetype: %s', file.mimetype)
            return SakApi.create_error_response_for_iframe(body_status=400, body_error=u'Ugyldig filtype.')

        filnavn = uuid_with_ext(file.filename)

        target_path = generate_dir_path(DOC_PATH, get_rel_saksvedlegg_path(soknad))

        backend = get_backend(file, filename=filnavn, path=target_path)
        backend.save()

        saksvedlegg = Vedlegg()
        user = get_user_from_auth()

        saksvedlegg.filnavn = file.filename
        saksvedlegg.file_ref = filnavn
        saksvedlegg.beskrivelse = request.form.get('tittel')
        saksvedlegg.user_id = user['id']
        saksvedlegg.vedlagtdato = datetime.now()

        soknad.saksvedlegg.append(saksvedlegg)


        if soknad.saksbehandler_id:
            # Arkivering
            # ###########

            organisation = get_organisation(soknad.organisation_id, request.cookies)
            person = get_person(soknad.person_id, request.cookies)
            try:
                save_journalpost_for_saksvedlegg(soknad, organisation, person, saksvedlegg)
            except InvalidArkivExtensionError as e:
                return SakApi.create_error_response_for_iframe(body_status=400, body_error=e.message)

        current_app.db_session.commit()

        return Response(response=json.dumps(marshal(saksvedlegg, saksvedlegg_fields)), status=201,
                        content_type='text/html')
    def post(self):

        rapport_id = request.form.get('rapport_id')
        soknad_id = request.form.get('soknad_id')

        if not (rapport_id is None):
            soknad_or_rapport = RapportRepo.find_by_id(rapport_id)
            soknad_id = soknad_or_rapport.soknad_id
        elif not (soknad_id is None):
            soknad_or_rapport = SoknadRepo.find_by_id(soknad_id)
        else:
            return SakApi.create_error_response_for_iframe(body_status=400, body_error='Missing object id')

        ensure(PUT, soknad_or_rapport)

        file = request.files.get('upload_file')
        if file is None:
            current_app.logger.warn('Missing required file: document')
            return SakApi.create_error_response_for_iframe(body_status=400, body_error=u'Dokument er påkrevd.')

        extension = os.path.splitext(file.filename)[1]

        if file.mimetype not in VALID_MIME_TYPES or extension not in VALID_EXTENSIONS:
            current_app.logger.warn('Invalid mimetype: %s', file.mimetype)
            return SakApi.create_error_response_for_iframe(body_status=400, body_error=u'Ugyldig filtype.')

        filnavn = uuid_with_ext(file.filename)

        target_path = generate_dir_path(DOC_PATH, get_rel_vedlegg_path(soknad_id));

        backend = get_backend(file, filename=filnavn, path=target_path)
        backend.save()

        vedlegg = Vedlegg()

        user = get_user_from_auth()
        vedlegg.filnavn = file.filename
        vedlegg.file_ref = filnavn
        vedlegg.user_id = user['id']
        vedlegg.vedlagtdato = datetime.now()

        soknad_or_rapport.vedlegg.append(vedlegg)

        current_app.db_session.commit()

        return Response(response=json.dumps(marshal(vedlegg, vedlegg_fields)),
                        status=201,
                        content_type='text/html')
Example #3
0
    def post(self, **kwargs):
        facility_id = request.form.get('facilityId')
        if facility_id is None:
            abort(400)

        facility = current_app.db_session.query(Facility).get(facility_id)
        if facility is None:
            abort(404)

        errors = {}

        title = request.form.get('title')
        if title is None or len(title.strip()) == 0:
            current_app.logger.warn('Missing required parameter: title')
            errors['title'] = u'Tittel er påkrevd'
        elif len(title) > 50:
            errors['title'] = u'Tittel kan ikke være lengre enn 50 tegn'

        file = request.files.get('document')
        if file is None:
            current_app.logger.warn('Missing required file: document')
            errors['file'] = u'Dokument er påkrevd'
        elif file.mimetype not in FacilityDocumentListResource.valid_mimetypes:
            current_app.logger.warn('Invalid mimetype: %s', file.mimetype)
            errors['document'] = (u'Ugyldig filtype. Gyldige filtyper er '
                                  u'PDF/DOC/DOCX')

        if len(errors) != 0:
            abort_with_msg(errors)

        backend = get_backend(file, filename=uuid_with_ext(file.filename),
                              path=os.environ.get('DOCUMENTS_PATH', '/tmp'))
        backend.save()

        document = Document(facility, url=backend.get_url('documents'),
                            title=title,
                            filename=backend.filename,
                            storage_backend=backend.name)
        current_app.db_session.add(document)
        current_app.db_session.commit()

        current_app.db_session.refresh(facility)

        return Response(response=json.dumps(marshal(document, document_fields)), status=201, content_type=content_type_from_request())