def test_storage_dangerous_paths_dir_name(self): file_name = '/tmp/../path' s = FileSystemStorage() msg = "Detected path traversal attempt in '/tmp/..'" with self.assertRaisesMessage(SuspiciousFileOperation, msg): s.get_available_name(file_name) with self.assertRaisesMessage(SuspiciousFileOperation, msg): s.generate_filename(file_name)
def upload2(request): if request.method == 'POST': if not request.FILES: if request.user.is_authenticated: return render(request, 'drodos/home.html', { 'erro': 'no file selected', 'user': request.user }) else: return render(request, 'drodos/index.html', {'erro': 'no file selected'}) else: files = request.FILES.getlist('filesInputId') if request.user.is_authenticated: for f in files: if request.user.profile.currentStorage + f.size > request.user.profile.maxStorage: return HttpResponseRedirect( reverse('drodos:myprofile')) else: fs = FileSystemStorage() filename = fs.save(fs.get_available_name(f.name), f) size = int(fs.size(filename) / 1000) print(str(size) + ' kb') if request.POST.get( 'private') and request.user.profile.premium: storeditem = StoredItem(owner=request.user, title=f.name, fileUrl=filename, description='', private=True) else: storeditem = StoredItem(owner=request.user, title=f.name, fileUrl=filename, description='', private=False) request.user.profile.currentStorage += size request.user.save() storeditem.save() return HttpResponseRedirect(reverse('drodos:files')) else: # unregistered user upload for f in files: if f.size < 1000000: # too big for unregistered fs = FileSystemStorage() filename = fs.save(fs.get_available_name(f.name), f) size = int(fs.size(filename) / 1000) print(str(size) + ' kb') tempitem = StoredItem(owner=None, title=f.name, fileUrl=filename, description='', private=False) tempitem.save() return HttpResponseRedirect(reverse('drodos:index')) else: return HttpResponseRedirect(reverse('drodos:index'))
def predict_image(self, request): image_obj = request.FILES.get('filePath', None) # if user did not upload an image, refresh if image_obj is None: return render(request, 'upload.html') fs = FileSystemStorage() # save the file and get the path image_name = fs.get_available_name(image_obj.name) image_path = fs.save(image_name, image_obj) image_path = fs.url(image_path) full_image_path = os.path.join(os.path.dirname(finder.__file__), 'static', 'media', image_name) # get prediction try: pred_confs, pred_classes = self._predictor.predict(full_image_path, topk=5) except: context = { 'errorMessage': 'There was an error processing your image.\ Make sure it is not a corrupted or image file \ and that the image has no transparency layers.' } return render(request, 'error.html', context) predicted_class = self._classes[pred_classes[0]] # plot confidence scores plot_image_name = fs.get_available_name('plot.png') plot_image_path = os.path.join('media', plot_image_name) full_plot_image_path = os.path.join(os.path.dirname(finder.__file__), 'static', plot_image_path) self._predictor.plot_predictions(pred_confs, pred_classes, full_plot_image_path, topk=5) submitted = True # update upload.html with context context = { 'predictedLabel': predicted_class, 'imagePath': image_path, 'plotImagePath': f'/{plot_image_path}', 'submitted': submitted } return render(request, 'upload.html', context)
def test_storage_dangerous_paths(self): candidates = [ ('/tmp/..', '..'), ('/tmp/.', '.'), ('', ''), ] s = FileSystemStorage() msg = "Could not derive file name from '%s'" for file_name, base_name in candidates: with self.subTest(file_name=file_name): with self.assertRaisesMessage(SuspiciousFileOperation, msg % base_name): s.get_available_name(file_name) with self.assertRaisesMessage(SuspiciousFileOperation, msg % base_name): s.generate_filename(file_name)
def test_storage_dangerous_paths_dir_name(self): candidates = [ ("tmp/../path", "tmp/.."), ("tmp\\..\\path", "tmp/.."), ("/tmp/../path", "/tmp/.."), ("\\tmp\\..\\path", "/tmp/.."), ] s = FileSystemStorage() for file_name, path in candidates: msg = "Detected path traversal attempt in '%s'" % path with self.subTest(file_name=file_name): with self.assertRaisesMessage(SuspiciousFileOperation, msg): s.get_available_name(file_name) with self.assertRaisesMessage(SuspiciousFileOperation, msg): s.generate_filename(file_name)
def add_orders(request, order, order_date, order_datetime): ''' This function is used to add orders to the order history. ''' design = order product = design.product artwork = design.art # This code saves the design image to the folder 'media/order_pics'. image_data = design.design_photo.open() file_storage = FileSystemStorage() file_storage.location = 'media/order_pics' name = file_storage.get_available_name('orderdesign.png') file_storage.save(name, image_data) design_location = 'order_pics/' + name order_history_item_instance = OrderHistoryItem.objects.create( name=artwork.artwork_name + ' | ' + product.product_name, design_photo=design_location, user=request.user, order_date=order_date, order_datetime=order_datetime, status='NP', paid_price=artwork.artwork_price + product.price, quantity=order.quantity, ) return order_history_item_instance
def unzip_microsite(self, zipfilepath): """ handles zips that are microsites, extract and dont import as content items, return a URL """ filepath, filename = os.path.split(zipfilepath) zipname, extension = os.path.splitext(filename) saved_dir = filepath fs = FileSystemStorage(location=saved_dir) if zipname: if fs.exists(zipname): zipname = fs.get_available_name(zipname) saved_dir = saved_dir + '/' + zipname os.makedirs(saved_dir) self.extract_zip(zipfilepath, saved_dir) #need to save both as if user overrides that we return to something in a lower directory we still need to track the root location self.storageLocation = getMediaURL( saved_dir) #saved_dir.replace(MEDIA_ROOT, MEDIA_URL) self.micrositeURL = self.storageLocation #saved_dir.replace(MEDIA_ROOT, MEDIA_URL) for fname in os.listdir(saved_dir): if fname.lower() == 'index.html' or fname.lower() == 'index.htm': self.micrositeURL = os.path.join(getMediaURL(saved_dir), fname) #Delete the zip try: os.remove(zipfilepath) except: logger.warn("Attempted file deletion failed for file: " + zipfilepath) else: logger.warn("Deleted file: " + zipfilepath)
def binary_upload(request): try: filename = request.data['filename'] binary_img = request.data['payload'] except KeyError as e: return Response("filename, and payload shall be set",status.HTTP_400_BAD_REQUEST) dest = getattr(settings, "MEDIA_ROOT", None)+"/uploads/cropped/" # on trouve un nom disponible pour éviter les collisions fs = FileSystemStorage(location=dest) filename = fs.get_available_name(filename) filePath = dest + filename; image_type, image_content = binary_img.split(',', 1) image_type = re.findall('data:image\/(\w+);base64', image_type)[0] with open(filePath, "wb") as f: f.write(base64_decode(image_content)) static_url = getattr(settings, "STATIC_URL", None) domain = request.build_absolute_uri('/')[:-1] file_url = domain+static_url+"medias/uploads/cropped/"+filename img_data = {"url":file_url,"name":filename} return Response(img_data,status.HTTP_200_OK)
def file_upload(request): is_business = False user_id = request.data.get("userId") caption = request.data.get("caption") file = request.FILES["file"] file_name = '' fs = FileSystemStorage() if fs.exists(file.name): name = fs.get_available_name(file.name) fs.save(name, file) file_name = name else: fs.save(file.name, file) file_name = file.name if file.name.endswith('.mp4'): img_data = base64.b64decode(request.data.get("thumb")) new_filename = 'media/thumbnails/' + file_name.replace("mp4", "png") with open(new_filename, 'wb') as f: f.write(img_data) UserDetail.objects.get(user_id=user_id) if UserDetail.isBusiness: is_business = True p = Post(user_id=user_id, caption=caption, content=file_name, likes=0, comments=0, date=datetime.datetime.now().timestamp(), isBusiness=is_business) p.save() trending = Trending(point=0, post_id=p.id) trending.save() return Response(status=HTTP_200_OK)
def upload_handler(request, metadata={}): """ writes file(s) to appropriate spot on disk, collects metadata from the form, calls FileImporter on it """ from datetime import datetime timing_now = datetime.now() timing_string = timing_now.isoformat(' ') new_file = request.FILES['content_file'] # write the file to disk #save_dir = MEDIA_ROOT + UPLOAD_CONTENT_DIR # First check if there is enoght space internally and if not return the path the external HD, # the second check below is then a definative check for any free space save_dir = getSaveLocation(new_file.size, os.path.join(MEDIA_ROOT, UPLOAD_CONTENT_DIR)) if isThereEnoughSpace(new_file.size, save_dir): fs = FileSystemStorage(location=save_dir) if fs.exists(new_file.name): new_file.name = fs.get_available_name(new_file.name) content_file = fs.save(new_file.name, new_file) #return MEDIA_URL + UPLOAD_CONTENT_DIR + new_file.name return getMediaURL(save_dir) + new_file.name else: return None
def post(self, request, format='jpg'): up_file = request.FILES['file'] dest = getattr(settings, "MEDIA_ROOT", None)+"/uploads/" # on supprime les espaces, les caractéres spéciaux etc. via une regexp striped_name = re.sub('[^A-Za-z0-9.]+', '', up_file.name) # on trouve un nom disponible pour éviter les collisions fs = FileSystemStorage(location=dest) available_name = fs.get_available_name(striped_name) filePath = dest + available_name; destination = open(filePath, 'wb+') for chunk in up_file.chunks(): destination.write(chunk) destination.close() max_size = (250,250) self.create_thumbnail(filePath,available_name,max_size) static_url = getattr(settings, "STATIC_URL", None) domain = request.build_absolute_uri('/')[:-1] file_url = domain+static_url+"media/uploads/"+available_name img_data = {"url":file_url,"name":available_name} return Response(img_data, status.HTTP_201_CREATED)
def upload(request): if request.method == 'POST': print(request.FILES) if not request.FILES: if request.user.is_authenticated: return render(request, 'drodos/home.html', { 'erro': 'no file selected', 'user': request.user }) else: return render(request, 'drodos/index.html', {'erro': 'no file selected'}) else: file = request.FILES['file'] fs = FileSystemStorage() filename = fs.save(fs.get_available_name(file.name), file) size = int(fs.size(filename) / 1000) print(str(size) + ' kb') if request.user.is_authenticated: if request.user.profile.currentStorage + size > request.user.profile.maxStorage: # max storage reached fs.delete(filename) return HttpResponseRedirect(reverse('drodos:myprofile')) else: # proceed with upload if request.POST.get( 'private') and request.user.profile.premium: storeditem = StoredItem( owner=request.user, title=request.POST['title'], fileUrl=filename, description=request.POST['desc'], private=True) print('PRIVATE') else: storeditem = StoredItem( owner=request.user, title=request.POST['title'], fileUrl=filename, description=request.POST['desc'], private=False) print('PUBLIC') request.user.profile.currentStorage += size request.user.save() storeditem.save() return HttpResponseRedirect(reverse('drodos:files')) else: # unregistered user upload if size > 1000: # too big for unregistered fs.delete(filename) return HttpResponseRedirect(reverse('drodos:index')) else: tempitem = StoredItem(owner=None, title=request.POST['title'], fileUrl=filename, description=request.POST['desc'], private=False) tempitem.save() return HttpResponseRedirect( reverse('drodos:uploaded', args=(filename, ))) else: return HttpResponseRedirect(reverse('drodos:index'))
def get_video_path(filename, album_pk): fss = FileSystemStorage() year = datetime.datetime.now().year month = datetime.datetime.now().month path = os.path.join("video", str(year), str(month), str(album_pk), filename) return fss.get_available_name(path)
def get_video_path(filename, album_pk): fss = FileSystemStorage() year = datetime.datetime.now().year month = datetime.datetime.now().month path = os.path.join("video", str(year), str(month), str(album_pk), filename) return fss.get_available_name(path)
def get_available_name(self, name, max_length=None): # If the filename already exists, remove it as if it was a true file system if settings.PAGES_EXT_FILE_OVERWRITE_EXISTS: if self.exists(name): self.__log('File ' + name + ' exists, rewrite used.') os.remove(os.path.join(settings.MEDIA_ROOT, name)) else: name = FileSystemStorage.get_available_name(self, name, max_length) return name
def get_available_name(self, name, max_length=None): # If the filename already exists, remove it as if it was a true file system if settings.PAGES_EXT_FILE_OVERWRITE_EXISTS: if self.exists(name): self.__log("File " + name + " exists, rewrite used.") os.remove(os.path.join(settings.MEDIA_ROOT, name)) else: name = FileSystemStorage.get_available_name(self, name, max_length) return name
def cmt(request): if request.method == "POST": uploaded_file = request.FILES['image'] fs = FileSystemStorage() fName = fs.get_available_name(uploaded_file.name) fs.save(fName, uploaded_file) data = extractInfoFromImage("media/{}".format(fName)) print(data) # process return JsonResponse(data)
def index(request): if request.method == 'POST': f = request.FILES['input_file'] fs = FileSystemStorage() filename = fs.get_available_name(f.name, max_length=None) filepath = os.path.join(BASE_DIR,"media", filename) with open(filepath, 'wb+') as destination: for chunk in f.chunks(): destination.write(chunk) return redirect('/file/'+ filename) else: return render(request, 'main/home.html')
def handle_uploaded_file(f: UploadedFile, user_id): # Экземпляр FileSystemStorage fs = FileSystemStorage('fileStore/' + user_id + "/files") # Получаем доступное имя файла name = fs.get_available_name(f.name) # Записываем файл with fs.open(name, 'wb') as destination: for chunk in f.chunks(): destination.write(chunk) return name
def notifications(request): if '_auth_user_id' in request.session: fs = FileSystemStorage('loanboard/files') files=sorted(Path(fs.path('')).iterdir(), key=lambda f: f.stat().st_mtime) notification=0 for fl in files: if fl.name.startswith('New_'): os.rename(fl, fs.path('')+'/'+fs.get_available_name(fl.name[4:]) ) files=filter( lambda a: not a.name.startswith(".") and a.is_file(), sorted(Path(fs.path('')).iterdir(), key=lambda f: f.stat().st_mtime) ) files = paginate(list(files), 13, request) return render(request, 'lbnotifications.html', {'notification': notification, 'files': files}) else: raise Http404('Page does not exist')
def handle_file(self, file): filename = "%s" % file.name.lower().capitalize() fs = FileSystemStorage(location="%s/docs/%s" % (MEDIA_ROOT, self.FILE_FOLDER), file_permissions_mode=0o644) filename = fs.get_available_name(filename) try: filename = fs.save(filename, file) except: return False self.filename = filename self.save() return filename
def uploadPP(request): file_name = '' file = request.FILES["file"] user_id = request.data.get("userId") fs = FileSystemStorage() fs.save(file.name, file) if fs.exists(file.name): name = fs.get_available_name(file.name) fs.save(name, file) file_name = name else: fs.save(file.name, file) file_name = file.name UserDetail.objects.filter(user_id=user_id).update(file_name=file_name) return Response(status=HTTP_200_OK)
def export(test=True): data = gather_data_and_update_flags(test) if data == []: raise Exception(u"%s" % _(u"No data to export!")) elif not data: logger.error(u"%s" % _(u"Error when fetching data - Data empty.")) raise Exception(u"%s" % _(u"Error when fetching data" u" Export cancelled!")) # File name if test: filename = get_valid_filename("test_%s.csv" % date.today()) else: filename = get_valid_filename("%s.csv" % date.today()) mediafilepath = join("invoices/export/", filename) filepath = join(settings.MEDIA_ROOT, mediafilepath) # Ensure that we got an available file name fss = FileSystemStorage() filepath = fss.get_available_name(filepath) # If the file name change, we update the media file path filename = split(filepath)[1] mediafilepath = join("invoices/export/", filename) # Write the file on the FS, and create the Export object if we are not in # test mode with open(filepath, "w") as csvfile: exportwriter = csv.writer(csvfile, delimiter=';', quoting=csv.QUOTE_ALL) # We do this because CSV doesn't support directly Unicode and UTF-8 # http://docs.python.org/2/library/csv.html#examples for row in data: r = [] for field in row: r.append(("%s" % field).strip().encode("utf-8")) exportwriter.writerow(r) if not test: export = Export(date=datetime.today(), file=mediafilepath) export.save() return settings.MEDIA_URL + mediafilepath
def saveDesignCoordinate(design, imageData, art_top, art_left, art_height, art_width, frame_top, frame_left, frame_width, frame_height, frame_border_radius, frame_rotation, text_font, text_top, text_left, text_weight, text_style, text_color, text_size, text): imageData = imageData format, imgstr = imageData.split(';base64,') ext = format.split('/')[-1] imageData = ContentFile(base64.b64decode(imgstr), name='temp.' + ext) fileStorage = FileSystemStorage() fileStorage.location = 'media/design_pics' if design.design_photo.url != '/media/design_pics/defaultDesign.png': storage, path = design.design_photo.storage, design.design_photo.path storage.delete(path) name = fileStorage.get_available_name('design.png') fileStorage.save(name, imageData) design.design_photo = 'design_pics/' + name ##art design.designArtCoordinate.coordinate_top = art_top design.designArtCoordinate.coordinate_left = art_left design.designArtCoordinate.height = art_height design.designArtCoordinate.width = art_width ##artframe design.designArtFrameCoordinate.frame_coordinate_top = frame_top design.designArtFrameCoordinate.frame_coordinate_left = frame_left design.designArtFrameCoordinate.frame_width = frame_width design.designArtFrameCoordinate.frame_height = frame_height design.designArtFrameCoordinate.frame_border_radius = frame_border_radius design.designArtFrameCoordinate.rotation = str(frame_rotation) ##text design.designTextCoordinate.font = text_font design.designTextCoordinate.font_weight = text_weight design.designTextCoordinate.font_style = text_style design.designTextCoordinate.coordinate_top = text_top design.designTextCoordinate.coordinate_left = text_left design.designTextCoordinate.font_color = text_color design.designTextCoordinate.text = text design.designTextCoordinate.font_size = text_size design.designArtCoordinate.save() design.designArtFrameCoordinate.save() design.designTextCoordinate.save() design.save()
def save_file(self, file, path, mode="xb"): assert isinstance(path, Path) # convert to relative if needed if path.is_absolute(): path = path.relative_to(self.root_path) storage = FileSystemStorage(location=self.root_path) try: with storage.open(storage.get_available_name(path), mode) as destination: for chunk in file.chunks(): destination.write(chunk) except OSError as err: raise FileOperationException(detail="{0} {1}".format( FileOperationException.default_detail, err)) return path
def send_updates(request): reg_nos=Loanbeneficiary.objects.all() req_data=Student.objects.filter(reg_no__in=reg_nos) file_name='universityloanoffice/files/exported/emptyfile.xls' sheet = pyexcel.get_sheet(file_name=file_name, name_columns_by_row=0) sheet.row += ['First Name', 'Middle Name', 'Last Name', 'Registration Number', 'Form 4 Index Number', 'Status'] for stdnt in req_data: sheet.row += [ stdnt.first_name, stdnt.middle_name, stdnt.last_name, stdnt.reg_no, stdnt.form_four_index_no, stdnt.status, ] fs = FileSystemStorage('loanboard/files') sheet.save_as("loanboard/files/" + fs.get_available_name("New_exported_sheet.xls") ) return render(request, 'file_sending_response.html')
def save_file_at_spesial_path(filename, obj): fss = FileSystemStorage() year = datetime.datetime.now().year month = datetime.datetime.now().month path = os.path.join("spesial", str(year), str(month), filename) path = fss.get_available_name(path) filepath = os.path.join(settings.MEDIA_ROOT, path) dirpath = os.path.dirname(filepath) if not os.path.exists(dirpath): try: os.makedirs(dirpath) except: pass f = open(filepath, 'wb') f.write(obj.read()) f.close() return path
def handle_file(self, file): prefix = 'Договор с неизвестным клиентом. ' if self.company: prefix = 'Договор с %s. ' % self.company.name filename = prefix + file.name.lower().capitalize() fs = FileSystemStorage(location="%s/docs/%s" % (MEDIA_ROOT, self.FILE_FOLDER), file_permissions_mode=0o644) filename = fs.get_available_name(filename) try: filename = fs.save(filename, file) except Exception: return False self.filename = filename self.save() return filename
def send_beneficiaries(request): req_data=Beneficiary.objects.filter(status="continuous") file_name='loanboard/files/exported/emptyfile.xls' sheet = pyexcel.get_sheet(file_name=file_name, name_columns_by_row=0) sheet.row += ['First Name', 'Middle Name', 'Last Name', 'Registration Number', 'Form 4 Index Number', 'Account Number', 'Bank Name', 'Amount'] for stdnt in req_data: sheet.row += [ stdnt.first_name, stdnt.middle_name, stdnt.last_name, stdnt.reg_no, stdnt.form_four_index_no, stdnt.account_no, stdnt.bank_name, stdnt.accomodation, ] fs = FileSystemStorage('universityloanoffice/files/signing') sheet.save_as( fs.path('')+'/'+fs.get_available_name("New_Sheet_.xls") ) return render(request, 'fl_sending_response.html')
def save_file_at_spesial_path(filename, obj): fss = FileSystemStorage() year = datetime.datetime.now().year month = datetime.datetime.now().month path = os.path.join("spesial", str(year), str(month), filename) path = fss.get_available_name(path) filepath = os.path.join(settings.MEDIA_ROOT, path) dirpath = os.path.dirname(filepath) if not os.path.exists(dirpath): try: os.makedirs(dirpath) except: pass f = open(filepath, 'wb') f.write(obj.read()) f.close() return path
class SaveLocal(Storage): def __init__(self, configs): self.fs = FileSystemStorage() self.sets = configs self.location = self.sets.get('location', lambda name: name) self.name_uuid_len = self.sets.get('name_uuid_len', None) def get_alternative_name(self, file_root, file_ext): return DriverUtils.create_file_name((file_root, file_ext), self.name_uuid_len) def get_available_name(self, name, max_length=None): dir_name, file_name = os.path.split(name) name = DriverUtils.create_file_name(file_name, self.name_uuid_len) name = posixpath.join(dir_name, name) name = self.location(name) name = DriverUtils.clean_name(name) return self.fs.get_available_name(name) def _save(self, name, content): name = self.fs._save(name, content) return name def url(self, name): return self.fs.url(name) def path(self, name): return self.fs.path(name) def delete(self, name): try: os.remove(posixpath.join(settings.MEDIA_ROOT, name)) except OSError as ose: print(f'File "{ose.filename}" does not exist') def download(self, name): file = open(self.path(name), 'rb') filename = posixpath.basename(name) return file, filename def retrieve(self, name): return self.download(name)
def icon_upload_handler(request, metadata={}): """ writes file(s) to appropriate spot on disk, collects metadata from the form, calls FileImporter on it """ from datetime import datetime timing_now = datetime.now() timing_string = timing_now.isoformat(' ') new_file = request.FILES['icon'] # write the file to disk save_dir = getSaveLocation(new_file.size, os.path.join(MEDIA_ROOT, ICONS_DIR)) if isThereEnoughSpace(new_file.size, save_dir): fs = FileSystemStorage(location=save_dir) if fs.exists(new_file.name): new_file.name = fs.get_available_name(new_file.name) icon = fs.save(new_file.name, new_file) #return MEDIA_URL + ICONS_DIR + new_file.name return getMediaURL(save_dir) + new_file.name else: return None
def generate_and_save_preview(self): fs = FileSystemStorage() # Generate a preview image and dump it into a bytes buffer img = PIL.Image.open(fs.open(self.image.name)) img.thumbnail((PREVIEW_WIDTH, PREVIEW_HEIGHT)) # Handle image rotation specified via exif rot, h_flip, v_flip = pil_helper.get_image_rotation(img) if rot: img = img.rotate(rot) buf = io.BytesIO() img.save(buf, 'png') # Save the image buffer to default file storage preview_path = fs.get_available_name(self.image.name) fs.save(preview_path, buf) self.preview = preview_path self.preview_width = img.width self.preview_height = img.height self.save()
def notifications(request): user_role=User.objects.get(id=request.session['_auth_user_id']).role if user_role=='universityloanofficer': fs = FileSystemStorage('universityloanoffice/files/signing') files=sorted(Path(fs.path('')).iterdir(), key=lambda f: f.stat().st_mtime) notification=0 for fl in files: if fl.name.startswith('New_'): os.rename(fl, fs.path('') + '/' + fs.get_available_name(fl.name[4:]) ) files=filter( lambda a: not a.name.startswith(".") and a.is_file(), sorted(Path(fs.path('')).iterdir(), key=lambda f: f.stat().st_mtime) ) files = paginate(list(files), 13, request) templete_data={ 'notification': notification, 'files': files } return render(request, 'uninotifications.html', templete_data) else: raise Http404('Page does not exist')
def import_initial_beneficiaries(request, file_name): fs = FileSystemStorage('universityloanoffice/files/initial') file_path=fs.path('') + '/' + file_name fl=pyexcel.iget_records(file_name=file_path) loanbeneficiary=None for record in fl: if Student.objects.filter(reg_no=record['Registration Number']) and not Loanbeneficiary.objects.filter(reg_no=record['Registration Number']): loanbeneficiary=Loanbeneficiary( reg_no=record['Registration Number'], form_four_index_no=record['F4Index'], status=Student.objects.get(reg_no=record['Registration Number']).status, account_no=record['account number'], bank_name=record['Bank name'], tuition_fee=record['Tuition fee'], accomodation=record['Accommodation'], special_faculty=record['special faculty'], field=record['field'], project_and_research=record['project and reseach'] ) loanbeneficiary.save() if file_name.startswith("New_"): os.rename(file_path, fs.path('') + '/' + fs.get_available_name(file_name[4:]) ) return redirect("/university/inital-import-status")
class ExhibitStorage(FileSystemStorage): """ Assume that incoming paths are of the form <username>/.../... """ def __init__(self, *args, **kwargs): self.__userdata_storage = FileSystemStorage(location=kwargs['location'], base_url=kwargs['base_url']) def _open(self, name, mode='rb'): return self.__userdata_storage._open(chunk_path(name) + name, mode) def _save(self, name, content): chunk = chunk_path(name) fullname = chunk + name if (self.__userdata_storage.exists(fullname)): self.__userdata_storage.delete(fullname) result = self.__userdata_storage._save(fullname, content) return result.partition(chunk)[2] def exists(self, name): return self.__userdata_storage.exists(chunk_path(name) + name) def path(self, name): return self.__userdata_storage.path(chunk_path(name) + name) def size(self, name): return self.__userdata_storage.size(chunk_path(name) + name) def delete(self, name): return self.__userdata_storage.delete(chunk_path(name) + name) def url(self, name): return self.__userdata_storage.url(name) def get_available_name(self, name): return self.__userdata_storage.get_available_name(name) def get_valid_name(self, name): return self.__userdata_storage.get_valid_name(name)
def create(self, request): """ Upload a new file to an import_record. This is a multipart/form upload. --- parameters: - name: import_record description: the ID of the ImportRecord to associate this file with. required: true paramType: body - name: source_type description: the type of file (e.g. 'Portfolio Raw' or 'Assessed Raw') required: false paramType: body - name: source_program_version description: the version of the file as related to the source_type required: false paramType: body - name: file or qqfile description: In-memory file object required: true paramType: Multipart """ if len(request.FILES) == 0: return JsonResponse({ 'success': False, 'message': "Must pass file in as a Multipart/Form post" }) # Fineuploader requires the field to be qqfile it appears... so why not support both? ugh. if 'qqfile' in request.data.keys(): the_file = request.data['qqfile'] else: the_file = request.data['file'] filename = the_file.name path = settings.MEDIA_ROOT + "/uploads/" + filename # Get a unique filename using the get_available_name method in FileSystemStorage s = FileSystemStorage() path = s.get_available_name(path) # verify the directory exists if not os.path.exists(os.path.dirname(path)): os.makedirs(os.path.dirname(path)) # save the file with open(path, 'wb+') as temp_file: for chunk in the_file.chunks(): temp_file.write(chunk) # The s3 stuff needs to be redone someday... delete? if 'S3' in settings.DEFAULT_FILE_STORAGE: os.unlink(path) raise ImproperlyConfigured("Local upload not supported") import_record_pk = request.POST.get('import_record', request.GET.get('import_record')) try: record = ImportRecord.objects.get(pk=import_record_pk) except ImportRecord.DoesNotExist: # clean up the uploaded file os.unlink(path) return JsonResponse({ 'success': False, 'message': "Import Record %s not found" % import_record_pk }) source_type = request.POST.get('source_type', request.GET.get('source_type')) # Add Program & Version fields (empty string if not given) kw_fields = {field: request.POST.get(field, request.GET.get(field, '')) for field in ['source_program', 'source_program_version']} f = ImportFile.objects.create(import_record=record, uploaded_filename=filename, file=path, source_type=source_type, **kw_fields) _log.info("Created ImportFile. kw_fields={} from-PM={}" .format(kw_fields, f.from_portfolio_manager)) return JsonResponse({'success': True, "import_file_id": f.pk})