def lever_rapport_action_strategy(soknad, action, data):
    rapport = RapportRepo.find_by_where("soknad_id", soknad.id)
    if len(rapport) != 1:
        abort(400, __error__=['Kan ikke finne rapport'])

    errors = rapport[0].validate_rapport_for_levering()
    if errors:
        abort(400, __error__=errors)

    soknad.status = action.get_end_state().id
    SoknadRepo.save(soknad, autocommit=False)

    # Arkivering
    # ###########
    organisation = get_organisation(soknad.organisation_id, request.cookies)
    person = get_person(soknad.person_id, request.cookies)

    csv_file_content = ExportRapporterResource().get(soknad_id=soknad.id, rapport_id=rapport[0].id).data

    save_journalpost_for_rapport(soknad, rapport[0], organisation, person, csv_file_content,
                                 u"soknad-%s-rapport-%s-%s.pdf" % (soknad.id, rapport[0].id, datetime.now().isoformat()))

    current_app.db_session.commit()

    # Sending mail after save to avoid that mail integration errors make it impossible to perform action.
    # It also makes sense to send mail only when we know that the action is successful
    send_email_to_soker_on_action_lever_rapport(soknad)
    send_email_to_saksbehandler_on_action_lever_rapport(soknad)
def underkjenn_rapport_action_strategy(soknad, action, data):
    rapport = RapportRepo.find_by_where("soknad_id", soknad.id)
    if len(rapport) != 1:
        abort(400, __error__=['Kan ikke finne rapport'])

    if data:
        validator = BaseValidator(data)
        validator.validate_le_max_length("saksbehandler_kommentar", 300)
        if validator.has_errors():
            abort(400, __error__=validator.errors)

        rapport[0].saksbehandler_kommentar = data.get("saksbehandler_kommentar")

        RapportRepo.save(rapport[0])
        # Sending mail after save to avoid that mail integration errors make it impossibv to perform action.
        # It also makes sense to send mail only when we know that the action is successful
        send_email_to_soker_on_action_underkjenn(soknad, data.get("saksbehandler_kommentar"))

    vedtak = soknad.nyeste_fattet_vedtak()
    if vedtak.tilskuddsordning_type == FORSKUDDSUTBETALING:
        soknad.status = SoknadStateMachine.s_tilbakebetaling_kreves.id
    elif vedtak.tilskuddsordning_type == ETTERSKUDDSUTBETALING:
        soknad.status = SoknadStateMachine.s_trukket.id
    SoknadRepo.save(soknad)
    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')