Пример #1
0
    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)

        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 = get_object_or_404(WildlifeLicenceType, code=args[0])
        application.correctness_disclaimer = request.POST.get('correctnessDisclaimer', '') == 'on'
        application.further_information_disclaimer = request.POST.get('furtherInfoDisclaimer', '') == 'on'
        application.applicant_profile = get_object_or_404(Profile, pk=request.session.get('application').get('profile'))
        application.lodgement_sequence += 1
        application.lodgement_date = datetime.now().date()
        if application.customer_status == 'amendment_required':
            # this is a 're-lodged' application after some amendment were required.
            # from this point we assume that all the amendments have been amended.
            AmendmentRequest.objects.filter(application=application).filter(status='requested').update(status='amended')
            application.review_status = 'amended'
            application.processing_status = 'ready_for_action'
        else:
            application.processing_status = 'new'
        application.customer_status = 'under_review'

        if len(application.lodgement_number) == 0:
            application.save(no_revision=True)
            application.lodgement_number = str(application.id).zfill(9)

        application.save(version_user=request.user, version_comment='Details Modified')

        # if attached files were saved temporarily, add each to application as part of a Document
        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_data')):
                    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)

                messages.success(request, 'The application was successfully lodged.')
            except Exception as e:
                messages.error(request, 'There was a problem creating the application: %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)
        else:
            messages.success(request, 'The application was successfully lodged.')

        delete_application_session_data(request.session)

        return redirect('dashboard:home')
Пример #2
0
    def get(self, request, *args, **kwargs):
        # if we've arrived at the licence type selection page and there is hangover application data left in the session, delete it
        delete_application_session_data(request.session)

        request.session['application'] = {}

        context = {'licence_types': dict(
            [(licence_type.code, licence_type.name) for licence_type in WildlifeLicenceType.objects.all()])}

        return render(request, self.template_name, context)
Пример #3
0
    def get(self, request, *args, **kwargs):
        delete_application_session_data(request.session)

        application = get_object_or_404(Application, pk=args[1]) if len(args) > 1 else None
        if application is not None and 'application' not in request.session:
            request.session['application'] = {}
            request.session['application']['profile'] = application.applicant_profile.id
            request.session['application']['data'] = application.data
            request.session.modified = True

        return redirect('applications:enter_details', *args, **kwargs)
Пример #4
0
    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)