def edit_project(request, id): project = get_object_or_404(Project, pk=id) if request.user == project.created_by or request.user.is_superuser: RewardTierFormSet = inlineformset_factory( parent_model=Project, model=RewardTier, fields=('minimum_amount', 'description'), extra=0, can_delete=project.is_draft, ) if request.method == 'POST': form = ProjectForm(request.POST, files=request.FILES, instance=project) reward_tier_formset = RewardTierFormSet(request.POST, instance=project) if form.is_valid() and reward_tier_formset.is_valid(): publish = request.POST.get('publish', None) if project.status == Project.STATUS_DRAFT and publish == '1': form.instance.publish() form.save() reward_tier_formset.save() else: form = ProjectForm(instance=project) reward_tier_formset = RewardTierFormSet(instance=project) return render(request, 'app/project/edit.html', context={ 'form': form, 'reward_tier_formset': reward_tier_formset, 'project': project, }) else: raise PermissionDenied()
def edit_view(request, slug): instance = get_object_or_404(Request, slug=slug) category = None if request.method == 'POST': type = request.POST['type'] if type: category = get_object_or_404(RequestCategory, slug=type) form = ProjectForm(request.POST or None, instance=instance) if form.is_valid(): project = form.save(commit=False) project.created_by = request.user if category: project.type = category project.save() for id in request.POST.getlist('skills'): tag = SkillTag.objects.get(pk=id) if tag: project.skills.add(tag) project.save() messages.add_message(request, messages.SUCCESS, 'Your Request has been updated!') return HttpResponseRedirect("/projects/" + str(project.slug) + "/") else: print form.errors else: form = ProjectForm() return render(request=request, template_name='app/editProject.html', context={'form': form, 'instance': instance})
def add_gc(request): if not request.user.has_perm('app.add_gc'): request.session['msg'] = u'你没有权限进行此操作!' request.session['type'] = 'warning' return HttpResponseRedirect('/') if request.method == 'POST': form = ProjectForm(request.POST) if form.is_valid(): f = form.save(commit=False) f.user = request.user f.save() request.session['msg'] = u'成功创建工程!' try: temp = Template.objects.get(name=f.temp) titems = Titem.objects.filter(template=temp.pk) for t in titems: iitem = Iitem(gc=f, key=t.key, order=t.order, parent=t.parent) iitem.save() except Exception, e: request.session['msg'] += e.message request.session['type'] = 'success' return HttpResponseRediret('/') else: request.session['msg'] = u'创建工程失败!请检查信息是否准确填写,或是联系管理员。' request.session['type'] = 'danger' # return HttpResponse(form.errors) return HttpResponseRedirect('/')
def post(self, request): data = json.loads(request.body) form = ProjectForm(data) if form.is_valid(): new_project = form.save() response = serializers.serialize("json", [new_project]) else: response = json.dumps({'errors': form.errors}) return HttpResponse(response, content_type="application/json")
def create_project(request): if not request.user.is_authenticated(): return render(request, '/login.html') else: form = ProjectForm(request.POST or None, request.FILES or None) if form.is_valid(): project = form.save(commit=False) project.user = request.user project.save() jobs = Job.objects.filter(project=project) return render(request, 'app/project_detail.html', {'title': 'Create New Project', 'project': project, 'user': request.user, 'jobs' : jobs}) context = {"form": form,} return render(request, 'app/create_project.html', context)
def start_project(request): RewardTierFormSet = inlineformset_factory( parent_model=Project, model=RewardTier, fields=('minimum_amount', 'description'), extra=1, ) if request.method == 'POST': form = ProjectForm(request.POST, request.FILES) if form.is_valid(): form.instance.created_by = request.user project = form.save() reward_tier_formset = RewardTierFormSet(request.POST, request.FILES, instance=project) if reward_tier_formset.is_valid(): reward_tier_formset.save() return redirect(reverse('view_project', args=[form.instance.id])) else: form = ProjectForm() reward_tier_formset = RewardTierFormSet() return render(request, 'app/project/start.html', context={'form': form, 'reward_tier_formset': reward_tier_formset})
def create_project(request): if not request.user.is_authenticated(): return render(request, '/login.html') else: form = ProjectForm(request.POST or None, request.FILES or None) if form.is_valid(): project = form.save(commit=False) project.user = request.user project.save() jobs = Job.objects.filter(project=project) return render( request, 'app/project_detail.html', { 'title': 'Create New Project', 'project': project, 'user': request.user, 'jobs': jobs }) context = { "form": form, } return render(request, 'app/create_project.html', context)