def edit_type(jwt, type_id): try: categories = Category.query.order_by(Category.id).all() type = Type.query.get(type_id) if type is None: abort(404) except exc.SQLAlchemyError: abort(422) data = type.format() form = TypeForm(request.form) # Add category list to form form.category.choices = [(category.id, category.name) for category in categories] form.category.default = type.category_id form.process() return jsonify({ 'success': True, 'form': render_template('forms/edit_type.html', form=form, type=data) })
def add_type(request, id, template='type.html', redirect='../edit/'): if request.method == 'POST': form = TypeForm(request.POST) if form.is_valid(): # Do form processing here... return HttpResponseRedirect(redirect) else: form = TypeForm() return render_to_response(template, {'form': form})
def new_type(): """ Add a new instrument type """ form = TypeForm(request.form) if request.method == 'POST' and form.validate(): # save the type save_changes('type', Type(), form, new=True) flash('Instrument type created successfully!') return redirect('/') return render_template('new_type.html', form=form)
def type(request): if request.POST: form = TypeForm(request.POST) if form.is_valid(): form.save() return HttpResponseRedirect('/type/') else: form = TypeForm() types = Type.objects.all() title = "Device Types" url = "type" return render(request, 'add.html', locals())
def get_context_data(self, **kwargs): if 'type_id' in kwargs: pass else: if self.request.method == 'POST': type_form = TypeForm(self.request.POST) if type_form.is_valid(): pass else: type_form = TypeForm() return {'type_form': type_form}
def edit_type_submission(jwt, type_id): try: categories = Category.query.order_by(Category.id).all() except exc.SQLAlchemyError: abort(422) form = TypeForm() # Add category list to form form.category.choices = [(category.id, category.name) for category in categories] if not form.validate_on_submit(): return jsonify({'success': False}) error = False try: type = Type.query.get(type_id) if type is None: abort(404) type.name = request.form['name'] type.description = request.form['description'] type.category_id = int(request.form['category']) db.session.commit() # Save images as file = request.files['image'] if file: filename = upload_image(file, consts.LEAD_TYPE, type_id) type.image = filename db.session.commit() except exc.SQLAlchemyError: error = True db.session.rollback() finally: db.session.close() if not error: flash('Type {} was successfully updated!'.format( request.form['name'])) else: flash('An error occurred. Type {} could not be updated.'.format( request.form['name'])) return jsonify({'success': not error})
def edittype(request, type_id): types = get_object_or_404(Type, pk=type_id) t = "Edit" if request.POST: form = TypeForm(request.POST, instance=types) if form.is_valid(): form.save() return HttpResponseRedirect('/type/') else: form = TypeForm(instance=types) types = Type.objects.all() title = "Edit Device Type" url = "type" return render_to_response("edit.html", { 'form': form, 't': t, }, context_instance=RequestContext(request, locals()))
def create_type(jwt): try: categories = Category.query.order_by(Category.id).all() except exc.SQLAlchemyError: abort(422) form = TypeForm(request.form) # Add category list to form form.category.choices = [(category.id, category.name) for category in categories] return jsonify({ 'success': True, 'form': render_template('forms/new_type.html', form=form) })
def types(request): _types = Type.objects.all() forms=[] if request.method == "POST": for t in _types: prefix = "%s" % t.id f = TypeForm(request.POST, request.FILES, prefix=prefix, instance=t) if f.is_valid(): f.save() forms.append(f) f = TypeForm(request.POST, request.FILES) if f.is_valid(): f.save() forms.append(f) forms.append(TypeForm()) else: for t in _types: prefix = "%s" % t.id forms.append(TypeForm(prefix=prefix, instance=t)) forms.append(TypeForm()) return render_to_response('manager_types.html', {'forms': forms}, RequestContext(request))
def deleteItem(request): form1 = TypeForm({}) form2 = VenderForm({}) form3 = ColorForm({}) if request.method == 'POST': form1 = TypeForm(request.POST) form2 = VenderForm(request.POST) form3 = ColorForm(request.POST) success1 = '' success2 = '' success3 = '' inventorys = None if form1.is_valid(): cd = form1.cleaned_data name = cd['TypeName'] try: type = Type.objects.get(TypeName = name) inventorys = Inventory.objects.filter(Type__TypeName__contains=name) if (inventorys.count()==0): type.delete() success1 = '类型删除成功' else: success1 = '库存中有该类型的数据,无法删除!' except: success1 = '类型不存在!无需再删除!' form1 = TypeForm() form2 = VenderForm() form3 = ColorForm() return render_to_response('itemDelete.html', {'form1': form1,'form2': form2,'form3': form3,'success1': success1}, context_instance = RequestContext(request)) if form2.is_valid(): cd = form2.cleaned_data name = cd['VenderName'] try: vender = Vender.objects.get(VenderName = name) inventorys = Inventory.objects.filter(Vender__VenderName__contains=name) if (inventorys.count()==0): vender.delete() success2 = '厂家删除成功' else: success2 = '库存中有该厂家的数据,无法删除!' except: success2 = '厂家不存在!无需再删除!' form1 = TypeForm() form2 = VenderForm() form3 = ColorForm() return render_to_response('itemDelete.html', {'form1': form1,'form2': form2,'form3': form3,'success2': success2}, context_instance = RequestContext(request)) if form3.is_valid(): cd = form3.cleaned_data name = cd['ColorName'] try: color = Color.objects.get(ColorName = name) inventorys = Inventory.objects.filter(Color__ColorName__contains=name) if (inventorys.count()==0): color.delete() success3 = '颜色删除成功' else: success3 = '库存中有该颜色的数据,无法删除!' except: success3 = '颜色不存在!无需再删除!' form1 = TypeForm() form2 = VenderForm() form3 = ColorForm() return render_to_response('itemDelete.html', {'form1': form1,'form2': form2,'form3': form3,'success3': success3}, context_instance = RequestContext(request)) else: form1 = TypeForm() form2 = VenderForm() form3 = ColorForm() return render_to_response('itemDelete.html', {'form1': form1,'form2': form2,'form3': form3}, context_instance = RequestContext(request))
def addItem(request): form1 = TypeForm({}) form2 = VenderForm({}) form3 = ColorForm({}) if request.method == 'POST': form1 = TypeForm(request.POST) form2 = VenderForm(request.POST) form3 = ColorForm(request.POST) success1 = '' success2 = '' success3 = '' inventorys = None if form1.is_valid(): cd = form1.cleaned_data type = Type() type.TypeName = cd['TypeName'] inventorys = Type.objects.filter(TypeName__contains=type.TypeName) if (inventorys.count()==0): type.save() success1 = '类型添加成功' else: success1 = '类型已存在!无需再添加!' form1 = TypeForm() form2 = VenderForm() form3 = ColorForm() return render_to_response('itemAdd.html', {'form1': form1,'form2': form2,'form3': form3,'success1': success1}, context_instance = RequestContext(request)) if form2.is_valid(): cd = form2.cleaned_data vender = Vender() vender.VenderName = cd['VenderName'] inventorys = Vender.objects.filter(VenderName__contains=vender.VenderName) if (inventorys.count()==0): vender.save() success2 = '厂家添加成功' else: success2 = '厂家已存在!无需再添加!' form1 = TypeForm() form2 = VenderForm() form3 = ColorForm() return render_to_response('itemAdd.html', {'form1': form1,'form2': form2,'form3': form3,'success2': success2}, context_instance = RequestContext(request)) if form3.is_valid(): cd = form3.cleaned_data color = Color() color.ColorName = cd['ColorName'] inventorys = Color.objects.filter(ColorName__contains=color.ColorName) if (inventorys.count()==0): color.save() success3 = '颜色添加成功' else: success3 = '颜色已存在!无需再添加!' form1 = TypeForm() form2 = VenderForm() form3 = ColorForm() return render_to_response('itemAdd.html', {'form1': form1,'form2': form2,'form3': form3,'success3': success3}, context_instance = RequestContext(request)) else: form1 = TypeForm() form2 = VenderForm() form3 = ColorForm() return render_to_response('itemAdd.html', {'form1': form1,'form2': form2,'form3': form3}, context_instance = RequestContext(request))