def get(self, request, *args, **kwargs): delete_app_session_data(request.session) set_app_session_data(request.session, 'temp_files_dir', tempfile.mkdtemp(dir=settings.MEDIA_ROOT)) if is_customer(request.user): set_app_session_data(request.session, 'customer_pk', request.user.pk) return redirect('applications:select_licence_type', *args, **kwargs) else: return redirect('applications:create_select_customer')
def post(self, request, *args, **kwargs): if 'select' in request.POST: utils.set_app_session_data(request.session, 'customer_pk', request.POST.get('customer')) elif 'create' in request.POST: create_customer_form = EmailUserForm(request.POST, email_required=False) if create_customer_form.is_valid(): customer = create_customer_form.save() utils.set_app_session_data(request.session, 'customer_pk', customer.id) else: context = {'create_customer_form': create_customer_form} return render(request, self.template_name, context) return redirect('wl_applications:select_licence_type', *args, **kwargs)
def get(self, request, *args, **kwargs): try: utils.delete_app_session_data(request.session) except Exception as e: messages.warning(request, 'There was a problem deleting session data: %s' % e) utils.set_app_session_data(request.session, 'temp_files_dir', tempfile.mkdtemp(dir=settings.MEDIA_ROOT)) if is_customer(request.user): utils.set_app_session_data(request.session, 'customer_pk', request.user.pk) return redirect('wl_applications:select_licence_type', *args, **kwargs) else: return redirect('wl_applications:create_select_customer')
def get(self, request, *args, **kwargs): try: utils.delete_app_session_data(request.session) except Exception as e: messages.warning(request, 'There was a problem deleting session data: %s' % e) temp_files_dir = tempfile.mkdtemp(dir=settings.MEDIA_ROOT) utils.set_app_session_data(request.session, 'temp_files_dir', temp_files_dir) application = get_object_or_404(Application, pk=args[1]) if len(args) > 1 else None if application is not None: utils.set_app_session_data(request.session, 'customer_pk', application.applicant_profile.user.pk) utils.set_app_session_data(request.session, 'profile_pk', application.applicant_profile.pk) utils.set_app_session_data(request.session, 'data', application.data) # copy document files into temp_files_dir for document in application.documents.all(): shutil.copyfile(document.file.path, os.path.join(temp_files_dir, document.name)) if application.hard_copy is not None: shutil.copyfile(application.hard_copy.file.path, os.path.join(temp_files_dir, application.hard_copy.name)) utils.set_app_session_data(request.session, 'application_document', application.hard_copy.name) return redirect('wl_applications:enter_details', *args, **kwargs)
def post(self, request, *args, **kwargs): try: applicant = utils.determine_applicant(request) except utils.SessionDataMissingException as e: messages.error(request, six.text_type(e)) return redirect('wl_applications:create_select_customer') licence_type = WildlifeLicenceType.objects.get(code_slug=args[0]) if 'select' in request.POST: profile_selection_form = ProfileSelectionForm(request.POST, user=applicant) if profile_selection_form.is_valid(): utils.set_app_session_data(request.session, 'profile_pk', profile_selection_form.cleaned_data.get('profile').id) else: return render(request, self.template_name, {'licence_type': licence_type, 'profile_selection_form': profile_selection_form, 'profile_creation_form': ProfileForm(), 'address_form': AddressForm()}) elif 'create' in request.POST: profile_form = ProfileForm(request.POST) address_form = AddressForm(request.POST) if profile_form.is_valid() and address_form.is_valid(): profile = profile_form.save(commit=False) profile.postal_address = address_form.save() profile.save() utils.set_app_session_data(request.session, 'profile_pk', profile.id) else: return render(request, self.template_name, {'licence_type': licence_type, 'profile_selection_form': ProfileSelectionForm(user=request.user), 'profile_creation_form': profile_form, 'address_form': address_form}) return redirect('wl_applications:enter_details', *args)
def get(self, request, *args, **kwargs): delete_app_session_data(request.session) temp_files_dir = tempfile.mkdtemp(dir=settings.MEDIA_ROOT) set_app_session_data(request.session, 'temp_files_dir', temp_files_dir) application = get_object_or_404(Application, pk=args[1]) if len(args) > 1 else None if application is not None: set_app_session_data(request.session, 'customer_pk', application.applicant_profile.user.pk) set_app_session_data(request.session, 'profile_pk', application.applicant_profile.pk) set_app_session_data(request.session, 'data', application.data) # copy document files into temp_files_dir for document in application.documents.all(): shutil.copyfile(document.file.path, os.path.join(temp_files_dir, document.name)) if application.hard_copy is not None: shutil.copyfile(application.hard_copy.file.path, os.path.join(temp_files_dir, application.hard_copy.name)) return redirect('applications:enter_details', *args, **kwargs)
def get(self, request, *args, **kwargs): delete_app_session_data(request.session) previous_application = get_object_or_404(Application, licence=args[0]) # check if there is already a renewal, otherwise create one try: application = Application.objects.get(previous_application=previous_application) if application.customer_status == 'under_review': messages.warning(request, 'A renewal for this licence has already been lodged and is awaiting review.') return redirect('dashboard:home') except Application.DoesNotExist: application = clone_application_for_renewal(previous_application) set_app_session_data(request.session, 'customer_pk', application.applicant_profile.user.pk) set_app_session_data(request.session, 'profile_pk', application.applicant_profile.pk) set_app_session_data(request.session, 'data', application.data) set_app_session_data(request.session, 'temp_files_dir', tempfile.mkdtemp(dir=settings.MEDIA_ROOT)) return redirect('applications:enter_details', application.licence_type.code, application.pk, **kwargs)
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)
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)