def handle(self, *args, **options): workbook = open_workbook(options['inventory_file'][0]) sheet = workbook.sheet_by_name(options['sheet_name'][0]) columns = DEFAULT_COLUMNS for row_index in xrange(len(sheet.col(0))): row = sheet.row(row_index) if row[0].value == LABEL_TRIGGER: columns = [] for cell in row: label = '_'.join(cell.value.lower().split()) label = underscore(label) columns.append(COLUMN_MAP[label] if label in COLUMN_MAP else label) elif row[0].value == DATA_TRIGGER: item_data = {} for index, cell in enumerate(row): if cell.ctype == XL_CELL_DATE: value = xldate_as_tuple(cell.value, workbook.datemode) value = TZ.localize(datetime(*value)) elif cell.ctype in (XL_CELL_TEXT, XL_CELL_NUMBER, XL_CELL_BOOLEAN): value = cell.value else: value = None item_data[columns[index]] = value form = ItemForm(item_data) if form.is_valid(): item = form.instance matching_items = Item.objects.filter( category=item.category, name=item.name, material_number=item.material_number, serial_number=item.serial_number, superceded_by=None) if item.notes.lower().startswith('qty'): numbers = nums.findall(item.notes.lower()) if number: item.qty_at_inventory = numbers[0] else: item.qty_at_inventory = 1 else: item.qty_at_inventory = 1 form.save() if matching_items: matching_items.update(superceded_by=form.instance) else: print('Error row {0}'.format(row_index + 1)) for key, error in form.errors.items(): print(' ==> {0}: {1}'.format(key, repr(error)))
def handle(self, *args, **options): workbook = open_workbook(options['inventory_file'][0]) sheet = workbook.sheet_by_name(options['sheet_name'][0]) columns = DEFAULT_COLUMNS for row_index in xrange(len(sheet.col(0))): row = sheet.row(row_index) if row[0].value == LABEL_TRIGGER: columns = [] for cell in row: label = '_'.join(cell.value.lower().split()) label = underscore(label) columns.append( COLUMN_MAP[label] if label in COLUMN_MAP else label) elif row[0].value == DATA_TRIGGER: item_data = {} for index, cell in enumerate(row): if cell.ctype == XL_CELL_DATE: value = xldate_as_tuple(cell.value, workbook.datemode) value = TZ.localize(datetime(*value)) elif cell.ctype in ( XL_CELL_TEXT, XL_CELL_NUMBER, XL_CELL_BOOLEAN): value = cell.value else: value = None item_data[columns[index]] = value form = ItemForm(item_data) if form.is_valid(): item = form.instance matching_items = Item.objects.filter( category=item.category, name=item.name, material_number=item.material_number, serial_number=item.serial_number, superceded_by=None) if item.notes.lower().startswith('qty'): numbers = nums.findall(item.notes.lower()) if number: item.qty_at_inventory = numbers[0] else: item.qty_at_inventory = 1 else: item.qty_at_inventory = 1 form.save() if matching_items: matching_items.update(superceded_by=form.instance) else: print('Error row {0}'.format(row_index + 1)) for key, error in form.errors.items(): print(' ==> {0}: {1}'.format(key, repr(error)))
def create(request): if request.POST: form = ItemForm(request.POST) if form.is_valid(): form.save() return HttpResponseRedirect('/inventory/all') else: form = ItemForm() args = {} args.update(csrf(request)) args['form'] = form # if request.user.is_authenticated(): args['user'] = request.user return render_to_response('create_item.html', args)
def item_form(request, id=None): if id: item = get_object_or_404(Item, id=id) scenario = 'Update' else: item = Item() scenario = 'Create' if request.POST: form = ItemForm(data=request.POST, instance=item, user=request.user) if form.is_valid(): item = form.save(commit=False) item.save(account_no=form.cleaned_data['account_no'], opening_balance=form.cleaned_data['opening_balance']) if request.is_ajax(): return render(request, 'callback.html', {'obj': ItemSerializer(item).data}) return redirect('/inventory/items/') else: form = ItemForm(instance=item, user=request.user) if request.is_ajax(): base_template = 'modal.html' else: base_template = 'inventory_base.html' return render(request, 'item_form.html', { 'scenario': scenario, 'form': form, 'base_template': base_template, })
def additem(request): if request.method == 'POST': # POST, generate bound form with data from the request form_item = ItemForm(request.POST) # check if it's valid: if form_item.is_valid(): # Insert into DB form_item.save() # redirect to a new URL: return HttpResponseRedirect('/accounts/profile') else: # GET, generate unbound (blank) form form_item = ItemForm() return render(request, 'inventory/add_item.html', {'form_item': form_item})
def item_add(request, container_pk): container = get_object_or_404(Item, pk=container_pk, is_container=True) if request.POST: form = ItemForm(request.POST) if form.is_valid(): item = form.save(commit=False) item.container = container request.session['inventory_item_add_last_category'] = item.category for x in range(0, form.cleaned_data['multiply']): item.pk = None item.save() item.add_journal(request, _('Gegenstand wurde erfasst.')) message_object_created(request, item) if form.cleaned_data['multiply'] > 1: target = container elif item.type == Item.TYPE_CONTAINER: target = item else: target = container return redirect(target) else: form = ItemForm( initial={ 'category': request.session.get('inventory_item_add_last_category') } ) c = { 'container': container, 'form': form } return render(request, 'inventory/item-add.html', get_context_inventory(request, 'inventory-item-add', c))
def item_create(request): if request.method == "POST": form = ItemForm(request.POST, request.FILES) if form.is_valid(): item = form.save() return redirect("item_read", item.pk) else: form = ItemForm() ctx = {"form": form} return render(request, "inventory/item_create.html", ctx)
def item_update(request, pk): item = Item.objects.get(pk=pk) if request.method == "POST": form = ItemForm(request.POST, request.FILES, instance=item) if form.is_valid(): item = form.save() return redirect("item_read", item.pk) else: form = ItemForm(instance=item) ctx = {"form": form} return render(request, "inventory/item_update.html", ctx)
def item_edit(request, item_pk): item = get_object_or_404(Item, pk=item_pk) if request.POST.get('delete'): if item.is_container and item.containing(): messages.error(request, _( u'Der Gegenstand %s ist ein Container und enthält andere Gegenstände. Daher kann dieser Gegenstand nicht gelöscht werden.' % item)) return redirect(item) else: if item.container: target = item.container else: target = None message_object_deleted(request, item) item.delete() if target: return redirect(target) else: return redirect('inventory-homepage') if request.POST: form = ItemForm(request.POST, instance=item) form.fields.pop('multiply') if form.is_valid(): item = form.save() message_object_updated(request, item) item.add_journal(request, _('Gegenstand wurde bearbeitet.')) return redirect(item) else: form = ItemForm(instance=item) form.fields.pop('multiply') c = { 'object': item, 'form': form } return render(request, 'inventory/item-edit.html', get_context_inventory(request, 'inventory-item-edit', c))
def post(self, request): form = ItemForm(request.POST) if form.is_valid: form.save() return redirect('test')