コード例 #1
0
    def post(self, request, *args, **kwargs):
        try:
            application = utils.get_session_application(self.request.session)
        except Exception as e:
            messages.error(self.request, str(e))
            return redirect('wl_applications:new_application')

        application.data = utils.create_data_from_form(
            application.licence_type.application_schema, request.POST,
            request.FILES)

        for f in request.FILES:
            if f == 'application_document':
                if application.hard_copy is None:
                    application.hard_copy = Document.objects.create(
                        name='hard_copy')
                application.hard_copy.file = request.FILES[f]
                application.hard_copy.save()
            else:
                # for legacy applications, need to check if there's a document where file is
                # named by the file name rather than the form field name
                try:
                    document = application.documents.get(
                        name=str(request.FILES[f]))
                except Document.DoesNotExist:
                    document = application.documents.get_or_create(name=f)[0]

                document.name = f
                document.file = request.FILES[f]

                document.save()

        application.save()

        if 'draft' in request.POST or 'draft_continue' in request.POST:
            application.customer_status = 'draft'

            # The processing status should only be set to 'draft' if the application hasn't been lodged
            if not application.lodgement_number:
                application.processing_status = 'draft'

            application.save()

            messages.warning(request, 'The application was saved to draft.')

            if 'draft' in request.POST:
                utils.delete_session_application(request.session)
                return redirect('wl_dashboard:home')
            else:
                return redirect('wl_applications:enter_details')
        else:
            return redirect('wl_applications:preview')
コード例 #2
0
ファイル: entry.py プロジェクト: rayrayndwiga/ledger
    def post(self, request, *args, **kwargs):
        try:
            application = utils.get_session_application(self.request.session)
        except Exception as e:
            messages.error(self.request, e.message)
            return redirect("wl_applications:new_application")

        application.data = utils.create_data_from_form(
            application.licence_type.application_schema, request.POST, request.FILES
        )

        for f in request.FILES:
            if f == "application_document":
                if application.hard_copy is None:
                    application.hard_copy = Document.objects.create(name="hard_copy")
                application.hard_copy.file = request.FILES[f]
                application.hard_copy.save()
            else:
                # for legacy applications, need to check if there's a document where file is
                # named by the file name rather than the form field name
                try:
                    document = application.documents.get(name=str(request.FILES[f]))
                except Document.DoesNotExist:
                    document = application.documents.get_or_create(name=f)[0]

                document.name = f
                document.file = request.FILES[f]

                document.save()

        application.save()

        if "draft" in request.POST or "draft_continue" in request.POST:
            application.customer_status = "draft"

            if application.processing_status != "renewal":
                application.processing_status = "draft"

            application.save()

            messages.warning(request, "The application was saved to draft.")

            if "draft" in request.POST:
                utils.delete_session_application(request.session)
                return redirect("wl_dashboard:home")
            else:
                return redirect("wl_applications:enter_details")
        else:
            return redirect("wl_applications:preview")
コード例 #3
0
ファイル: entry.py プロジェクト: wilsonc86/ledger
    def post(self, request, *args, **kwargs):
        application = utils.get_session_application(self.request.session)

        application.data = utils.create_data_from_form(application.licence_type.application_schema,
                                                       request.POST, request.FILES)

        for f in request.FILES:
            if f == 'application_document':
                if application.hard_copy is None:
                    application.hard_copy = Document.objects.create(name='hard_copy')
                application.hard_copy.file = request.FILES[f]
                application.hard_copy.save()
            else:
                document = Document.objects.create(name=f, file=request.FILES[f])
                document.save()
                # for legacy applications, need to check if there's a document where file is
                # named by the file name rather than the form field name
                try:
                    old_document = application.documents.get(name=str(request.FILES[f]))
                except Document.DoesNotExist:
                    old_document = application.documents.filter(name=f).first()

                if old_document is not None:
                    application.documents.remove(old_document)

                application.documents.add(document)

        application.save()

        if 'draft' in request.POST or 'draft_continue' in request.POST:
            application.customer_status = 'draft'

            # The processing status should only be set to 'draft' if the application hasn't been lodged
            if not application.lodgement_number:
                application.processing_status = 'draft'

            application.save()

            messages.warning(request, 'The application was saved to draft.')

            if 'draft' in request.POST:
                utils.delete_session_application(request.session)
                return redirect('wl_dashboard:home')
            else:
                return redirect('wl_applications:enter_details')
        else:
            return redirect('wl_applications:preview')
コード例 #4
0
ファイル: entry.py プロジェクト: serge-gaia/ledger
    def post(self, request, *args, **kwargs):
        licence_type = WildlifeLicenceType.objects.get(code_slug=self.args[0])

        utils.rename_filename_doubleups(request.POST, request.FILES)

        utils.set_app_session_data(request.session, 'data', utils.create_data_from_form(licence_type.application_schema,
                                                                                        request.POST, request.FILES))

        temp_files_dir = utils.get_app_session_data(request.session, 'temp_files_dir')

        if 'draft' in request.POST or 'draft_continue' in request.POST:
            if len(args) > 1:
                application = get_object_or_404(Application, pk=args[1])
            else:
                application = Application()

            if is_officer(request.user):
                application.proxy_applicant = request.user

            application.data = utils.get_app_session_data(request.session, 'data')
            application.licence_type = WildlifeLicenceType.objects.get(code_slug=args[0])
            application.applicant_profile = get_object_or_404(Profile,
                                                              pk=utils.get_app_session_data(request.session, 'profile_pk'))
            application.customer_status = 'draft'

            if application.processing_status != 'renewal':
                application.processing_status = 'draft'

            application.save(version_user=application.applicant_profile.user)

            application.documents.clear()

            # need to create documents from all the existing files that haven't been replaced
            # (saved in temp_files_dir) as well as any new ones
            try:
                for filename in utils.get_all_filenames_from_application_data(licence_type.application_schema,
                                                                              utils.get_app_session_data(request.session, 'data')):

                    # need to be sure file is in tmp directory (as it could be a freshly attached file)
                    if os.path.exists(os.path.join(temp_files_dir, filename)):
                        document = Document.objects.create(name=filename)
                        with open(os.path.join(temp_files_dir, filename), 'rb') as doc_file:
                            document.file.save(filename, File(doc_file), save=True)
                            application.documents.add(document)
            except Exception as e:
                messages.error(request, 'There was a problem appending applications files: %s' % e)

            for f in request.FILES:
                if f == 'application_document':
                    application.hard_copy = Document.objects.create(name=str(request.FILES[f]), file=request.FILES[f])
                else:
                    application.documents.add(Document.objects.create(name=str(request.FILES[f]), file=request.FILES[f]))

            application.save(no_revision=True)

            messages.warning(request, 'The application was saved to draft.')

            if 'draft' in request.POST:
                try:
                    utils.delete_app_session_data(request.session)
                except Exception as e:
                    messages.warning(request, 'There was a problem deleting session data: %s' % e)

                return redirect('wl_dashboard:home')
            else:
                # if continuing, need to save new files in temp so they can be previewed on enter details screen
                if len(request.FILES) > 0:
                    temp_files_dir = utils.get_app_session_data(request.session, 'temp_files_dir')

                    for f in request.FILES:
                        if f == 'application_document':
                            utils.set_app_session_data(request.session, 'application_document', str(request.FILES[f]))

                        with open(os.path.join(temp_files_dir, str(request.FILES[f])), 'wb+') as destination:
                            for chunk in request.FILES[f].chunks():
                                destination.write(chunk)

                return redirect('wl_applications:enter_details', args[0], application.pk)
        else:
            if len(request.FILES) > 0:
                temp_files_dir = utils.get_app_session_data(request.session, 'temp_files_dir')

                for f in request.FILES:
                    if f == 'application_document':
                        utils.set_app_session_data(request.session, 'application_document', str(request.FILES[f]))

                    with open(os.path.join(temp_files_dir, str(request.FILES[f])), 'wb+') as destination:
                        for chunk in request.FILES[f].chunks():
                            destination.write(chunk)

            return redirect('wl_applications:preview', *args)
コード例 #5
0
ファイル: entry.py プロジェクト: rockychen-dpaw/ledger
    def post(self, request, *args, **kwargs):
        with open('%s/json/%s.json' % (APPLICATION_SCHEMA_PATH, args[0])) as data_file:
            form_structure = json.load(data_file)

        set_app_session_data(request.session, 'data', create_data_from_form(form_structure, request.POST, request.FILES))

        temp_files_dir = get_app_session_data(request.session, 'temp_files_dir')

        if 'draft' in request.POST or 'draft_continue' in request.POST:
            if len(args) > 1:
                application = get_object_or_404(Application, pk=args[1])
            else:
                application = Application()

            if is_officer(request.user):
                application.proxy_applicant = request.user

            application.data = get_app_session_data(request.session, 'data')
            application.licence_type = WildlifeLicenceType.objects.get(code=args[0])
            application.applicant_profile = get_object_or_404(Profile,
                                                              pk=get_app_session_data(request.session, 'profile_pk'))
            application.customer_status = 'draft'

            if application.processing_status != 'renewal':
                application.processing_status = 'draft'

            application.save(version_user=application.applicant_profile.user)

            # delete all existing documents for this application
            application.documents.all().delete()
            if application.hard_copy is not None:
                application.hard_copy.delete()

            # need to create documents from all the existing files that haven't been replaced
            # (saved in temp_files_dir) as well as any new ones
            try:
                for filename in get_all_filenames_from_application_data(form_structure,
                                                                        get_app_session_data(request.session, 'data')):

                    # need to be sure file is in tmp directory (as it could be a freshly attached file)
                    if os.path.exists(os.path.join(temp_files_dir, filename)):
                        document = Document.objects.create(name=filename)
                        with open(os.path.join(temp_files_dir, filename), 'rb') as doc_file:
                            document.file.save(filename, File(doc_file), save=True)
                            application.documents.add(document)
            except Exception as e:
                messages.error(request, 'There was a problem appending applications files: %s' % e)

            finally:
                try:
                    if temp_files_dir is not None:
                        shutil.rmtree(temp_files_dir)
                except (shutil.Error, OSError) as e:
                    messages.warning(request, 'There was a problem deleting temporary files: %s' % e)

            for f in request.FILES:
                if f == 'application_document':
                    application.hard_copy = Document.objects.create(name=str(request.FILES[f]), file=request.FILES[f])
                else:
                    application.documents.add(Document.objects.create(name=str(request.FILES[f]), file=request.FILES[f]))

            messages.warning(request, 'The application was saved to draft.')

            if 'draft' in request.POST:
                delete_app_session_data(request.session)
                return redirect('dashboard:home')
            else:
                return redirect('applications:enter_details', args[0], application.pk)
        else:
            if len(request.FILES) > 0:
                temp_files_dir = get_app_session_data(request.session, 'temp_files_dir')

                for f in request.FILES:
                    if f == 'application_document':
                        set_app_session_data(request.session, 'application_document', str(request.FILES[f]))

                    with open(os.path.join(temp_files_dir, str(request.FILES[f])), 'wb+') as destination:
                        for chunk in request.FILES[f].chunks():
                            destination.write(chunk)

            return redirect('applications:preview', *args)
コード例 #6
0
ファイル: entry.py プロジェクト: ropable/ledger
    def post(self, request, *args, **kwargs):
        with open('%s/json/%s.json' % (APPLICATION_SCHEMA_PATH, args[0])) as data_file:
            form_structure = json.load(data_file)

        request.session['application']['data'] = create_data_from_form(form_structure, request.POST, request.FILES)
        request.session.modified = True

        if 'draft' in request.POST or 'draft_continue' in request.POST:
            if len(args) > 1:
                application = get_object_or_404(Application, pk=args[1])
            else:
                application = Application()

            application.data = request.session.get('application').get('data')
            application.licence_type = WildlifeLicenceType.objects.get(code=args[0])
            application.applicant_profile = get_object_or_404(Profile,
                                                              pk=request.session.get('application').get('profile'))
            application.customer_status = 'draft'
            application.processing_status = 'draft'
            application.save(version_user=request.user)

            if 'files' in request.session.get('application') and \
                    os.path.exists(request.session.get('application').get('files')):
                try:
                    for filename in get_all_filenames_from_application_data(form_structure,
                                                                            request.session.get('application').get('data')):
                        # need to be sure file is in tmp directory (as it could be a freshly attached file)
                        if os.path.exists(os.path.join(request.session.get('application').get('files'), filename)):
                            document = Document.objects.create(name=filename)
                            with open(os.path.join(request.session.get('application').get('files'), filename),
                                      'rb') as doc_file:
                                document.file.save(filename, File(doc_file), save=True)
                                application.documents.add(document)
                except Exception as e:
                    messages.error(request, 'There was a problem appending applications files: %s' % e)
                finally:
                    try:
                        shutil.rmtree(request.session.get('application').get('files'))
                    except (shutil.Error, OSError) as e:
                        messages.warning(request, 'There was a problem deleting temporary files: %s' % e)

            for f in request.FILES:
                application.documents.add(Document.objects.create(name=f, file=request.FILES[f]))

            messages.warning(request, 'The application was saved to draft.')

            if 'draft' in request.POST:
                delete_application_session_data(request.session)
                return redirect('dashboard:home')
            else:
                return redirect('applications:enter_details', args[0], application.pk)
        else:
            if len(request.FILES) > 0:
                if 'files' not in request.session.get('application'):
                    request.session['application']['files'] = tempfile.mkdtemp()
                    request.session.modified = True
                for f in request.FILES:
                    with open(os.path.join(request.session.get('application').get('files'), str(request.FILES[f])),
                              'wb+') as destination:
                        for chunk in request.FILES[f].chunks():
                            destination.write(chunk)

            return redirect('applications:preview', *args)