def update_stock_item(request, item_id = None): member = getMemberByUser(request.user) parent_type = util.get_parent(member.member_type) parents = Member.objects.filter(member_type = parent_type) parent_items = [] for parent in parents: parent_items.extend(Item.objects.filter(member = parent)) if request.method == 'POST': form = RegistStockItemForm(request.POST) if form.is_valid(): name = form.cleaned_data['name'] price = form.cleaned_data['price'] description = form.cleaned_data['description'] count = form.cleaned_data['count'] parent_item = None if parent_type is not None : try: parent_item = Item.objects.get(id=form.cleaned_data['parent_item_id']) except ValueError: return request_stock_error(request) except ObjectDoesNotExist: return request_stock_error(request) if parent_item.member.member_type != parent_type: return request_stock_error(request) now = datetime.datetime.now() item = Item( name = name, price = price, description = description, count = count, parent_item = parent_item, member = member, created=now, ) if item_id is not None : item.id = item_id else : try: if parent_item is not None: current_item = Item.objects.get(parent_item=parent_item) item.id = current_item.id except ObjectDoesNotExist: item.id = None item.save() return HttpResponseRedirect('/manage/') else: variables = RequestContext(request, { 'form': form, 'parent_items': parent_items, 'parent_type': parent_type, }) return render_to_response('update_stock_item.html', variables, context_instance=RequestContext(request) ) else : form = None if item_id is None: form = RegistStockItemForm() else : stock_item = get_object_or_404(Item, id=item_id) if stock_item.parent_item is None: parent_item_id = "" else: parent_item_id = stock_item.parent_item.id form = RegistStockItemForm({ 'name': stock_item.name, 'price': stock_item.price, 'description': stock_item.description, 'count': stock_item.count, 'parent_item_id': parent_item_id, }) variables = RequestContext(request, { 'form': form, 'parent_items': parent_items, 'parent_type': parent_type, }) return render_to_response('update_stock_item.html', variables, context_instance=RequestContext(request) )
def update_selling_item(request, selling_item_id = None) : member = getMemberByUser(request.user) wholesalers = Member.objects.filter(member_type = "wholesaler") wholesaler_items = [] for wholesaler in wholesalers: wholesaler_items.extend(Item.objects.filter(member = wholesaler)) if member is None or member.member_type == "consumer": return HttpResponseRedirect('/') if request.method == 'POST': form = RegistItemForm(request.POST) if form.is_valid(): desc_head = form.cleaned_data['desc_head'] selling_count = form.cleaned_data['selling_count'] name = form.cleaned_data['name'] price = form.cleaned_data['price'] description = form.cleaned_data['description'] item_count = form.cleaned_data['item_count'] parent_item_id = form.cleaned_data['parent_item_id'] parent_item = None try: parent_item = Item.objects.get(id=parent_item_id) except ObjectDoesNotExist: return request_stock_error(request) item = Item( name = name, price = price, description = description, count = item_count, parent_item = parent_item, member = member ) selling_item = SellingItem( desc_head = desc_head, count = selling_count, member = member ) if selling_item_id is not None: selling_items = SellingItem.objects.filter(id=selling_item_id) filtered_selling_item = None if (len(selling_items) == 0): return HttpResponseRedirect('/') else: filtered_selling_item = selling_items[0] if filtered_selling_item.member.id != selling_item.member.id : return HttpResponseRedirect('/') else : item.id = filtered_selling_item.item.id selling_item.id = filtered_selling_item.id else : try: current_item = Item.objects.get(parent_item=parent_item) item.id = current_item.id current_selling_item = SellingItem.objects.get(item=current_item) selling_item.id = current_selling_item.id except ObjectDoesNotExist: item.id = None now = datetime.datetime.now() item.created = now selling_item.created = now item.save() selling_item.item = item selling_item.save() return HttpResponseRedirect('/manage/') else: variables = RequestContext(request, { 'form': form, 'wholesaler_items': wholesaler_items, }) return render_to_response('update_selling_item.html', variables, context_instance=RequestContext(request) ) else : form = None if selling_item_id is not None: selling_items = SellingItem.objects.filter(id=selling_item_id) if len(selling_items) == 0: return HttpResponseRedirect('/') selling_item = selling_items[0] form = RegistItemForm({ 'desc_head': selling_item.desc_head, 'selling_count': selling_item.count, 'name': selling_item.item.name, 'price': selling_item.item.price, 'description': selling_item.item.description, 'item_count': selling_item.item.count, 'parent_item_id': selling_item.item.parent_item.id }) else : form = RegistItemForm() variables = RequestContext(request, { 'form': form, 'wholesaler_items': wholesaler_items, }) return render_to_response('update_selling_item.html', variables, context_instance=RequestContext(request) )