def form_valid(self, form): if self.can_not_send_mess: self.set_message( u'Вы не можете добавлять коментарии не ко своим вопросам', True) return self.form_invalid(form) body = form.cleaned_data['body'] file = form.cleaned_data['file'] new_msg = Chat(question=self.question, admin_name=self.user, body=body, date=now()) new_msg.save() if file: new_file = Files(content_object=new_msg, file=file, name=file.name, size=file.size, date=now()) new_file.save() answers_count = Chat.objects.filter(question=self.question).count() self.question.answers = answers_count self.question.save() self.set_message(u'Ответ успешно добавлен.') return super(QuesChatForm, self).form_valid(form)
def form_valid(self, form): self.pc = self.get_parent_obj() file = form.cleaned_data['file'] new_file = Files(content_object=self.pc, name=file.name, file=file, size=file.size, date=now()) new_file.save() self.set_message(u'Файл успешно добавлен.') return super(AddFileForPcView, self).form_valid(form)
def upload_file(request): header_title = ["文件管理", "上传文件"] title = header_title[-1] if request.method == "POST": form = UploadFileForm(request.POST, request.FILES) if form.is_valid(): is_shared = request.POST.get('is_shared') description = request.POST.get('description') filename = form.cleaned_data['file_name'] email = request.session.get('email') user_obj = CustomUser.objects.get(email=email) files = Files() files.file_name = filename files.description = description files.user = user_obj size, md5 = handle_uploaded_file(request.FILES['file_name'], filename) files.md5 = md5 files.file_size = size # if # files.is_shared if is_shared: files.is_shared = True files.save() return redirect('file_list') else: form = UploadFileForm() return render(request, 'files/upload.html', locals())
def form_valid(self, form): if self.can_not_send_mess: self.set_message(u'Вы не можете добавлять коментарии не ко своим вопросам', True) return self.form_invalid(form) body = form.cleaned_data['body'] file = form.cleaned_data['file'] new_msg = Chat(question=self.question, admin_name=self.user,body=body, date=now()) new_msg.save() if file: new_file = Files(content_object=new_msg, file=file, name=file.name, size=file.size, date=now()) new_file.save() answers_count = Chat.objects.filter(question=self.question).count() self.question.answers = answers_count self.question.save() self.set_message(u'Ответ успешно добавлен.') return super(QuesChatForm, self).form_valid(form)
def post(self, *args, **kwargs): path = self.request.META.get('HTTP_REFERER') context = self.get_context_data(**kwargs) form = context['form'] msg = '修改' if self.object else '添加' invoice = self.get_invoice() files = self.request.FILES.getlist('files') if form.is_valid(): instance = form.save(commit=False) if invoice: instance.type = invoice.type instance.save() print(instance) self.object = instance print('obj', self.object) if files: desc = form.cleaned_data.get('desc', None) from files.models import Files for file in files: f = Files(object=instance, content=file, entry=self.request.user, desc=desc) f.save() if invoice: due_amount = min(invoice.due_amount, instance.amount) assign = Assign.objects.create(invoice=invoice, payment=instance, amount=due_amount, entry=self.request.user) instance.create_comment() msg += '成功' messages.success(self.request, msg) print('ready_sent_msg') self.sent_msg() print('out_sent_msg') # return redirect(path) return JsonResponse({'state': 'ok', 'url': path}) msg += '失败' return HttpResponse( render_to_string(self.template_name, { 'form': form, 'error': msg }))