def add_manufacturer(request, template_name="manage/manufacturers/add_manufacturer.html" ): """Form and logic to add a manufacturer. """ if request.method == "POST": form = ManufacturerDataForm(data=request.POST) if form.is_valid(): new_manufacturer = form.save() return HttpResponseRedirect( reverse("lfs_manufacturer", kwargs={"manufacturer_id": new_manufacturer.id})) else: form = ManufacturerDataForm() return render_to_response( template_name, RequestContext( request, { "form": form, "selectable_manufacturers_inline": selectable_manufacturers_inline(request, 0), "came_from": request.REQUEST.get("came_from", reverse("lfs_manufacturer_dispatcher")), }))
def manufacturer_data_inline(request, manufacturer_id, template_name="manage/manufacturers/manufacturer_data_inline.html"): """Displays the data form of the current manufacturer. """ manufacturer = Manufacturer.objects.get(pk=manufacturer_id) if request.method == "POST": form = ManufacturerDataForm(instance=manufacturer, data=request.POST) else: form = ManufacturerDataForm(instance=manufacturer) return render_to_string(template_name, RequestContext(request, { "manufacturer": manufacturer, "form": form, }))
def update_data(request, manufacturer_id): """Updates data of manufacturer with given manufacturer id. """ manufacturer = Manufacturer.objects.get(pk=manufacturer_id) form = ManufacturerDataForm(instance=manufacturer, data=request.POST, files=request.FILES) if form.is_valid(): manufacturer = form.save() msg = _(u"Manufacturer data has been saved.") else: msg = _(u"Please correct the indicated errors.") # Delete image if request.POST.get("delete_image"): try: manufacturer.image.delete() except OSError as e: logger.error( 'Error while trying to delete manufacturer image: %s' % e) html = ( ("#data", manufacturer_data_inline(request, manufacturer.pk)), ("#selectable-manufacturers", selectable_manufacturers_inline(request, manufacturer_id)), ) result = json.dumps({"html": html, "message": msg}, cls=LazyEncoder) return HttpResponse(result, content_type='application/json')
def manage_manufacturer( request, manufacturer_id, template_name="manage/manufacturers/manufacturer.html"): """The main view to display manufacturers. """ manufacturer = Manufacturer.objects.get(pk=manufacturer_id) categories = [] for category in Category.objects.filter(parent=None): # Checking state checked, klass = _get_category_state(manufacturer, category) categories.append({ "id": category.id, "name": category.name, "checked": checked, "klass": klass, }) data_form = ManufacturerDataForm(instance=manufacturer) return render_to_response( template_name, RequestContext( request, { "categories": categories, "manufacturer_id": manufacturer_id, "selectable_manufacturers_inline": selectable_manufacturers_inline(request, manufacturer_id), "manufacturer_data_inline": manufacturer_data_inline(request, manufacturer_id, data_form), }))
def update_data(request, manufacturer_id): """Updates data of manufacturer with given manufacturer id. """ manufacturer = Manufacturer.objects.get(pk=manufacturer_id) form = ManufacturerDataForm(instance=manufacturer, data=request.POST) if form.is_valid(): form.save() msg = _(u"Manufacturer data has been saved.") html = ( ("#data", manufacturer_data_inline(request, manufacturer_id, form)), ("#selectable-manufacturers", selectable_manufacturers_inline(request, manufacturer_id)), ) result = simplejson.dumps({"html": html, "message": msg}, cls=LazyEncoder) return HttpResponse(result)