def _set_submitted(self, ret): ret.lodgement_number = '%s-%s' % (str(ret.licence.licence_type.pk).zfill(LICENCE_TYPE_NUM_CHARS), str(ret.pk).zfill(LODGEMENT_NUMBER_NUM_CHARS)) ret.lodgement_date = datetime.date.today() if is_officer(self.request.user): ret.proxy_customer = self.request.user # assume that all the amendment requests has been solved. pending_amendments = ret.pending_amendments_qs if pending_amendments: pending_amendments.update(status='amended') ret.status = 'amended' else: ret.status = 'submitted' ret.save() message = 'Return successfully submitted.' # update next return in line's status to become the new current return next_ret = Return.objects.filter(licence=ret.licence, status='future').order_by('due_date').first() if next_ret is not None: next_ret.status = 'current' next_ret.save() message += ' The next return for this licence can now be entered and is due on {}.'. \ format(next_ret.due_date.strftime(DATE_FORMAT)) return_submitted.send(sender=self.__class__, ret=ret) messages.success(self.request, message)
def _set_submitted(self, ret): ret.lodgement_number = "%s-%s" % ( str(ret.licence.licence_type.pk).zfill(LICENCE_TYPE_NUM_CHARS), str(ret.pk).zfill(LODGEMENT_NUMBER_NUM_CHARS), ) ret.lodgement_date = datetime.date.today() if is_officer(self.request.user): ret.proxy_customer = self.request.user ret.status = "submitted" ret.save() message = "Return successfully submitted." # update next return in line's status to become the new current return next_ret = Return.objects.filter(licence=ret.licence, status="future").order_by("due_date").first() if next_ret is not None: next_ret.status = "current" next_ret.save() message += " The next return for this licence can now be entered and is due on {}.".format( next_ret.due_date.strftime(DATE_FORMAT) ) return_submitted.send(sender=self.__class__, ret=ret) messages.success(self.request, message)
def post(self, request, *args, **kwargs): context = self.get_context_data() ret = context['return'] if 'upload' in request.POST: form = UploadSpreadsheetForm(request.POST, request.FILES) if form.is_valid(): temp_file_dir = tempfile.mkdtemp(dir=settings.MEDIA_ROOT) try: data = form.cleaned_data.get('spreadsheet_file') path = default_storage.save(os.path.join(temp_file_dir, str(data)), ContentFile(data.read())) workbook = excel.load_workbook_content(path) for table in context['tables']: worksheet = excel.get_sheet(workbook, table.get('title')) \ or excel.get_sheet(workbook, table.get('name')) if worksheet is not None: table_data = excel.TableData(worksheet) schema = Schema(ret.return_type.get_schema_by_name(table.get('name'))) validated_rows = list(schema.rows_validator(table_data.rows_by_col_header_it())) table['data'] = validated_rows else: messages.warning(request, 'Missing worksheet ' + table.get('name')) finally: shutil.rmtree(temp_file_dir) elif 'draft' in request.POST or 'draft_continue' in request.POST: _create_return_data_from_post_data(ret, context['tables'], request.POST) if is_officer(request.user): ret.proxy_customer = request.user ret.status = 'draft' ret.save() messages.warning(request, 'Return saved as draft.') # redirect or reshow page depending on whether save or save/continue was clicked if 'draft' in request.POST: return redirect('home') else: for table in context['tables']: table['data'] = _get_validated_rows_from_post(ret, table.get('name'), request.POST) elif 'lodge' in request.POST: if _is_post_data_valid(ret, context['tables'], request.POST): _create_return_data_from_post_data(ret, context['tables'], request.POST) ret.lodgement_number = '%s-%s' % (str(ret.licence.licence_type.pk).zfill(LICENCE_TYPE_NUM_CHARS), str(ret.pk).zfill(LODGEMENT_NUMBER_NUM_CHARS)) ret.lodgement_date = date.today() if is_officer(request.user): ret.proxy_customer = request.user ret.status = 'submitted' ret.save() message = 'Return successfully submitted.' # update next return in line's status to become the new current return next_ret = Return.objects.filter(licence=ret.licence, status='future').order_by('due_date').first() if next_ret is not None: next_ret.status = 'current' next_ret.save() message += ' The next return for this licence can now be entered and is due on {}.'.\ format(next_ret.due_date.strftime(DATE_FORMAT)) return_submitted.send(sender=self.__class__, ret=ret) messages.success(request, message) return redirect('home') else: for table in context['tables']: table['data'] = _get_validated_rows_from_post(ret, table.get('name'), request.POST) if len(table['data']) == 0: messages.warning(request, "You must enter data for {}".format(table.get('name'))) return render(request, self.template_name, context)