def item_add(request): if request.method == 'POST': form = AddItemForm(request.POST, request.FILES) if form.is_valid(): cd = form.cleaned_data name = request.FILES['file'].name f = request.FILES['file'] m = hashlib.md5() m.update(os.urandom(32)) m.update(name.encode('utf8')) uid = m.hexdigest() dir_path = os.path.join(settings.STATIC_ROOT, 'files', uid) os.mkdir(dir_path) path = os.path.join(dir_path, name) with open(path, 'wb+') as destination: for chunk in f.chunks(): destination.write(chunk) item = Item() item.name = cd['name'] item.description = cd['description'] item.filename = f.name item.size = f.size item.uid = uid item.created = datetime.utcnow().replace(tzinfo=utc) item.updated = datetime.utcnow().replace(tzinfo=utc) item.save() tags = {} if cd['year'] is None: tags['year'] = "" else: tags['year'] = str(cd['year']) tags['people'] = cd['authors'] tags['company'] = cd['company'] tags['type'] = cd['kind'] tags[None] = cd['tags'] fill_tags(item, tags) return HttpResponseRedirect('/item/%d' % item.id) else: form = AddItemForm() # An unbound form return render(request, 'item_add.html', {'form': form, 'edit': False})