class ContentFileTestCase(unittest.TestCase): def setUp(self): self.storage_dir = tempfile.mkdtemp() self.storage = FileSystemStorage(self.storage_dir) def tearDown(self): shutil.rmtree(self.storage_dir) def test_content_file_default_name(self): self.assertEqual(ContentFile(b"content").name, None) def test_content_file_custom_name(self): """ Test that the constructor of ContentFile accepts 'name' (#16590). """ name = "I can have a name too!" self.assertEqual(ContentFile(b"content", name=name).name, name) def test_content_file_input_type(self): """ Test that ContentFile can accept both bytes and unicode and that the retrieved content is of the same type. """ self.assertTrue(isinstance(ContentFile(b"content").read(), bytes)) if six.PY3: self.assertTrue(isinstance(ContentFile("español").read(), six.text_type)) else: self.assertTrue(isinstance(ContentFile("español").read(), bytes)) def test_content_saving(self): """ Test that ContentFile can be saved correctly with the filesystem storage, both if it was initialized with string or unicode content""" self.storage.save("bytes.txt", ContentFile(b"content")) self.storage.save("unicode.txt", ContentFile("español"))
class FileLikeObjectTestCase(LiveServerTestCase): """ Test file-like objects (#15644). """ available_apps = [] def setUp(self): self.temp_dir = tempfile.mkdtemp() self.storage = FileSystemStorage(location=self.temp_dir) def tearDown(self): shutil.rmtree(self.temp_dir) def test_urllib_request_urlopen(self): """ Test the File storage API with a file-like object coming from urllib.request.urlopen(). """ file_like_object = urlopen(self.live_server_url + '/') f = File(file_like_object) stored_filename = self.storage.save("remote_file.html", f) remote_file = urlopen(self.live_server_url + '/') with self.storage.open(stored_filename) as stored_file: self.assertEqual(stored_file.read(), remote_file.read())
def get_screencap(url, file_name): """ This func helps monitor our Celery/Selenium/PhantomJS image creation process. We'll use this to coarsely keep tabs on the availability of our "create new archive" service. Returns the image (png) capture NOTE: This func does not permanently store anything. It should only be used for monitoring purposes. TODO: this should replicate everything that happens in proxy_capture, flesh this out. """ # We don't want to mix this monitoring stuff with our user generated media (archives) monitor_storage = FileSystemStorage(location=getattr(settings, 'MONITOR_ROOT')) driver = webdriver.PhantomJS(executable_path=getattr(settings, 'PHANTOMJS_BINARY', 'phantomjs'),) driver.get(url) file_object = BytesIO(driver.get_screenshot_as_png()) file = File(file_object) driver.quit monitor_storage.save(file_name, file)
def test_lazy_base_url_init(self): """ FileSystemStorage.__init__() shouldn't evaluate base_url. """ storage = FileSystemStorage(base_url=reverse_lazy('app:url')) with self.assertRaises(NoReverseMatch): storage.url(storage.base_url)
def getAudio(request, book_id, para_id): #Should be served by nginx-gridfs #response = HttpResponse(mimetype = "audio/x-wav") ''' image = Image.open(os.path.dirname(settings.BASE_DIR) + "/" + "wastore/hindi.jpg") image.save(response, 'png') image = Image.open(settings.BASE_DIR) ''' #response=HttpResponse() path_to_save = "documents/"+str(book_id) + "_" + str(para_id) + "_sound.wav" #path_to_save = str(book_id) + "/chunks/" + str(para_id) + "/AudioFiles/1.wav" print(path_to_save) a = default_storage.open(path_to_save) local_fs = FileSystemStorage(location='/tmp/audio') local_fs.save(a.name,a) file = open("/tmp/audio/documents/"+str(book_id) + "_" + str(para_id) + "_sound.wav", "rb").read() #response['Content-Disposition'] = 'attachment; filename=filename.mp3' #audio = wave.open("/tmp/audio/"+a.name,'r') #--audio.save(response, 'wav') #data=audio.readframes(audio.getnframes()) #print len(data) #print type(data) #print data[9000:9002] #--response.write(data) #response = HttpResponse(content=data, mimetype="audio/x-wav") #print response.content[9000:9002] #audio.close() local_fs.delete(a.name) #return HttpResponse("hello") return HttpResponse(file, mimetype="audio/wav")
class FileStoragePathParsing(SimpleTestCase): def setUp(self): self.storage_dir = tempfile.mkdtemp() self.storage = FileSystemStorage(self.storage_dir) def tearDown(self): shutil.rmtree(self.storage_dir) def test_directory_with_dot(self): """Regression test for #9610. If the directory name contains a dot and the file name doesn't, make sure we still mangle the file name instead of the directory name. """ self.storage.save('dotted.path/test', ContentFile("1")) self.storage.save('dotted.path/test', ContentFile("2")) files = sorted(os.listdir(os.path.join(self.storage_dir, 'dotted.path'))) self.assertFalse(os.path.exists(os.path.join(self.storage_dir, 'dotted_.path'))) self.assertEqual(files[0], 'test') self.assertRegex(files[1], 'test_%s' % FILE_SUFFIX_REGEX) def test_first_character_dot(self): """ File names with a dot as their first character don't have an extension, and the underscore should get added to the end. """ self.storage.save('dotted.path/.test', ContentFile("1")) self.storage.save('dotted.path/.test', ContentFile("2")) files = sorted(os.listdir(os.path.join(self.storage_dir, 'dotted.path'))) self.assertFalse(os.path.exists(os.path.join(self.storage_dir, 'dotted_.path'))) self.assertEqual(files[0], '.test') self.assertRegex(files[1], '.test_%s' % FILE_SUFFIX_REGEX)
def __init__(self, apps=None, *args, **kwargs): # List of locations with static files self.locations = [] # Maps dir paths to an appropriate storage instance self.storages = SortedDict() if not isinstance(settings.STATICFILES_DIRS, (list, tuple)): raise ImproperlyConfigured( "Your STATICFILES_DIRS setting is not a tuple or list; " "perhaps you forgot a trailing comma?") for root in settings.STATICFILES_DIRS: if isinstance(root, (list, tuple)): prefix, root = root else: prefix = '' if settings.STATIC_ROOT and os.path.abspath(settings.STATIC_ROOT) == os.path.abspath(root): raise ImproperlyConfigured( "The STATICFILES_DIRS setting should " "not contain the STATIC_ROOT setting") if (prefix, root) not in self.locations: self.locations.append((prefix, root)) for prefix, root in self.locations: filesystem_storage = FileSystemStorage(location=root) filesystem_storage.prefix = prefix self.storages[root] = filesystem_storage super(FileSystemFinder, self).__init__(*args, **kwargs)
def save(self, page, language, data, change, extra_data=None): if 'delete' in extra_data: return super(ImagePlaceholderNode, self).save( page, language, "", change ) filename = '' if change and data: # the image URL is posted if not changed if type(data) is unicode: return storage = FileSystemStorage() filename = os.path.join( settings.PAGE_UPLOAD_ROOT, 'page_'+str(page.id), self.name + '-' + str(time.time()) ) m = re.search('\.[a-zA-Z]{1,4}$', str(data)) if m is not None: filename += m.group(0).lower() filename = storage.save(filename, data) return super(ImagePlaceholderNode, self).save( page, language, filename, change )
def __init__(self, name, author=None, *args, **kwargs): self.author = author super(FsStorage, self).__init__(name, *args, **kwargs) FileSystemStorage.__init__(self, os.path.join( self.base_dir, self.name)) if name: self.init_project()
def setUpClass(cls): super(PhotoReviewTests, cls).setUpClass() storage = FileSystemStorage() desired_storage_path = join('queued-images', 'pilot.jpg') with open(cls.example_image_filename, 'rb') as f: cls.storage_filename = storage.save(desired_storage_path, f) mkdir_p(TEST_MEDIA_ROOT)
def upload(request): if request.method == 'POST': form = UploadFileForm(request.POST, request.FILES) if form.is_valid(): file=request.FILES['file'] storage = FileSystemStorage( location = '/tmp/django', base_url = '/tmp/django' ) content = request.FILES['file'] name = storage.save(None, content) url = storage.url(name) filetype = magic.from_file(url,mime=True).decode() myreg=re.compile(r'(mp4)|(ogg)|(webm)',re.I) ext=myreg.search(filetype) if ext: newfilename=move_file(url,ext.group(0).lower()) dir=ext.group(0).lower() #data=dir + "/" + str(newfilename) data=dir #Check with the guys what they want try: Video.create(video_id=newfilename, correctness=0, title=form.cleaned_data['title'], description=form.cleaned_data['description'], data=data, date_created=datetime.datetime.now(),video_codec=dir) except Video.DoesNotExist: return HttpResponse("LWT failed") return render(request,'upload.html', {'form':form}) else: form = UploadFileForm() return render (request,'upload.html', {'form': form})
class ThumbnailField: ''' Instances of this class will be used to access data of the generated thumbnails ''' def __init__(self, name): self.name = name self.storage = FileSystemStorage() def path(self): return self.storage.path(self.name) def url(self): return self.storage.url(self.name) def size(self): return self.storage.size(self.name) def is_landscape(self): return self.orientation() == 'landscape' def is_portrait(self): return self.orientation() == 'portrait' def orientation(self): w, h = get_image_dimensions(self.path()) if w > h: return 'landscape' else: return 'portrait'
def ck_upload(request, upload_folder): error_message = 'Unable to upload' upload_location = os.path.join(settings.MEDIA_ROOT, upload_folder) upload_url = urlparse.urljoin(settings.MEDIA_URL, upload_folder) if request.method == "POST": filesystem = FileSystemStorage(location=upload_location, base_url=upload_url) try: uploaded_file = request.FILES['upload'] saved_file = filesystem.save(uploaded_file.name, uploaded_file) saved_url = filesystem.url(saved_file) except Exception: return HttpResponse(error_message) try: callback = request.GET['CKEditorFuncNum'] except KeyError: callback = '' else: return HttpResponse(error_message) context = { 'callback': callback, 'saved_url': saved_url, } return direct_to_template(request, 'ckeditor/upload_result.html', context)
class VariationField(object): """Instances of this class will be used to access data of the generated variations.""" def __init__(self, name): """ :param name: str """ self.name = name self.storage = FileSystemStorage() @property def path(self): """Return the abs. path of the image file.""" return self.storage.path(self.name) @property def url(self): """Return the url of the image file.""" return self.storage.url(self.name) @property def size(self): """Return the size of the image file, reported by os.stat().""" return self.storage.size(self.name)
def __init__(self, **kwargs): options = kwargs.get("options", None) location = kwargs.get("location", None) base_url = kwargs.get("base_url", None) # TODO if django >= 1.6: file_permissions_mode ThumborStorage.__init__(self, options) FileSystemStorage.__init__(self, location=location, base_url=base_url)
class Command(collectstatic.Command): help = "Collects static files from apps and other locations into "\ "`VERSIONED_STATIC_ROOT` with a versioned filename. (Used in "\ "conjunction with the {% versionedstatic %} template tag.)" def __init__(self, *args, **kwargs): super(Command, self).__init__(*args, **kwargs) # Force storage to be a filesystem storage for VERSIONED_STATIC_ROOT. self.storage = FileSystemStorage(location=settings.VERSIONED_STATIC_ROOT) try: self.storage.path('') except NotImplementedError: self.local = False else: self.local = True def copy_file(self, path, prefixed_path, source_storage, **kwargs): version = get_file_version(path, source_storage) prefixed_path = get_versioned_path(prefixed_path, version) return super(Command, self).copy_file(path, prefixed_path, source_storage, **kwargs) def link_file(self, path, prefixed_path, source_storage, **kwargs): version = get_file_version(path, source_storage) prefixed_path = get_versioned_path(prefixed_path, version) return super(Command, self).link_file(path, prefixed_path, source_storage, **kwargs)
def new_image(request,menu_code="default"): menu,created=Menu.objects.get_or_create(code=menu_code) print request.REQUEST.items() frame = request.REQUEST['frame'] try: original_name = request.REQUEST['original_name'] except KeyError: original_name = frame original_name = original_name.replace("/","_"); image_dir=menu.img_dir(); print image_dir image=request.FILES['image'] storage = FileSystemStorage(image_dir); path = storage.save(os.path.join(image_dir,original_name),image); base_pose = request.REQUEST.get('base_pose','') object_pose = request.REQUEST.get('object_pose','') mi=MenuItem(menu=menu, image_name=original_name,base_pose=base_pose,object_pose=object_pose); mi.save(); return HttpResponse("ItemID: %d",mi.id)
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_n
class FileStoragePathParsing(unittest.TestCase): def setUp(self): self.storage_dir = tempfile.mkdtemp() self.storage = FileSystemStorage(self.storage_dir) def tearDown(self): shutil.rmtree(self.storage_dir) def test_directory_with_dot(self): """Regression test for #9610. If the directory name contains a dot and the file name doesn't, make sure we still mangle the file name instead of the directory name. """ self.storage.save('dotted.path/test', ContentFile(b"1")) self.storage.save('dotted.path/test', ContentFile(b"2")) self.assertFalse(os.path.exists(os.path.join(self.storage_dir, 'dotted_.path'))) self.assertTrue(os.path.exists(os.path.join(self.storage_dir, 'dotted.path/test'))) self.assertTrue(os.path.exists(os.path.join(self.storage_dir, 'dotted.path/test_1'))) def test_first_character_dot(self): """ File names with a dot as their first character don't have an extension, and the underscore should get added to the end. """ self.storage.save('dotted.path/.test', ContentFile(b"1")) self.storage.save('dotted.path/.test', ContentFile(b"2")) self.assertTrue(os.path.exists(os.path.join(self.storage_dir, 'dotted.path/.test'))) self.assertTrue(os.path.exists(os.path.join(self.storage_dir, 'dotted.path/.test_1')))
class ThumbnailField: ''' Instances of this class will be used to access data of the generated thumbnails ''' def __init__(self, name): self.name = name self.storage = FileSystemStorage() @property def path(self): return self.storage.path(self.name) @property def url(self): return self.storage.url(self.name) @property def size(self): return self.storage.size(self.name) @property def height(self): im = Image.open(self.path) return im.size[1] @property def width(self): im = Image.open(self.path) return im.size[0]
def handle_mock(self, app, url_map, output_prefix, base_context, **other_options): app_list = [app] if app else settings.INSTALLED_APPS if [a for a in app_list if a not in settings.INSTALLED_APPS]: raise CommandError('Invalid app specified. Only installed apps may ' 'be used.') if url_map and not os.path.exists(url_map): raise CommandError('No such URL map at that path.') if base_context and not os.path.exists(base_context): raise CommandError('No such URL map at that path.') if '..' in output_prefix.split('/') or output_prefix.startswith('/'): raise CommandError('Treachery! No root paths or parent navigation ' 'when specifying an output prefix, you clever ' 'devil.') compiler.DjangoJadeCompiler.preempt_url_patterns( json.load(open(url_map)) if url_map else {}) for app in app_list: self.handle_compile(app) compiler_obj = compiler.DjangoJadeCompiler( app, base_context=(json.load(open(base_context)) if base_context else {})) for tmpl_data in compiler_obj.find_compilable_jade_templates(standalone=False): logger.debug('Template data: %s', tmpl_data) html = compiler_obj.mock(**tmpl_data) faux_file = ContentFile(html) html_path = os.path.join(output_prefix, tmpl_data['template_path'], '%s.html' % tmpl_data['base_file_name']) logger.info('Saving HTML file %s', html_path) storage_obj = FileSystemStorage(location=settings.STATIC_ROOT) storage_obj.save(html_path, faux_file)
def upload(request): t = Template(SUCCESS_TEMPLATE) if hasattr(settings, "CK_FILE_PATH"): ck_file_path = settings.CK_FILE_PATH else: ck_file_path = "" FILE_PATH = os.path.join(settings.MEDIA_ROOT, ck_file_path) FILE_URL = urlparse.urljoin(settings.MEDIA_URL, ck_file_path) if request.method == "POST": filesystem = FileSystemStorage(location=FILE_PATH, base_url=FILE_URL) try: uploaded_file = request.FILES["upload"] filename = filesystem.save(uploaded_file.name, uploaded_file) ck_filename = filesystem.url(filename) except MultiValueDictKeyError: return HttpResponse(t.render()) try: callback = request.GET["CKEditorFuncNum"] except: callback = "" else: return HttpResponse(t.render()) rc = RequestContext(request, {"callback": callback, "filename": ck_filename}) return HttpResponse(t.render(rc))
def enqueue_image(person, user, image_url): r = requests.get( image_url, headers={ 'User-Agent': USER_AGENT, }, stream=True ) if not r.status_code == 200: message = "HTTP status code {0} when downloading {1}" raise Exception(message.format(r.status_code, image_url)) storage = FileSystemStorage() suggested_filename = \ 'queued_image/{d.year}/{d.month:02x}/{d.day:02x}/ci-upload'.format( d=date.today() ) storage_filename = storage.save(suggested_filename, r.raw) QueuedImage.objects.create( why_allowed=QueuedImage.OTHER, justification_for_use="Downloaded from {0}".format(image_url), decision=QueuedImage.UNDECIDED, image=storage_filename, person_id=person.id, user=user )
def do_upload_image(request,session,form,uploaded_file): user = request.user; print form.cleaned_data print uploaded_file submission_rt=os.path.join(settings.DATASETS_ROOT,session.code); id = session.mthit_set.count()+1; rand_id=str(uuid.uuid4())+"-"+str(id) print dir(uploaded_file) original_name =uploaded_file.name storage = FileSystemStorage(submission_rt); fname=storage.save(os.path.join(submission_rt,rand_id+".jpg"),uploaded_file); params="image_url=/frames/"+session.code+"/"+rand_id+".jpg&frame="+rand_id+"&original_name="+original_name print params hit=mturk.models.MTHit(session=session,ext_hitid=rand_id,int_hitid=id,parameters=params); hit.save(); if form.cleaned_data["submit_for_annotation"]: (activated,msg)=mturk.views.activate_hit(session,hit); else: activated=False msg="submission disabled" return HttpResponse("done. Activated %d, %s" % (activated,msg))
class FileLikeObjectTestCase(LiveServerBase): """ Test file-like objects (#15644). """ def setUp(self): self.temp_dir = tempfile.mkdtemp() self.storage = FileSystemStorage(location=self.temp_dir) def tearDown(self): shutil.rmtree(self.temp_dir) def test_urllib2_urlopen(self): """ Test the File storage API with a file like object coming from urllib2.urlopen() """ file_like_object = self.urlopen("/example_view/") f = File(file_like_object) stored_filename = self.storage.save("remote_file.html", f) remote_file = self.urlopen("/example_view/") with self.storage.open(stored_filename) as stored_file: self.assertEqual(stored_file.read(), remote_file.read())
def post(self, request, *args, **kwargs): msg_subject = request.POST.get('subject') message_body = request.POST.get('temp_message') sender = request.user.first_name + "|" + request.user.email to = get_email_list(request.POST.get('to')) cc = get_email_list(request.POST.get('cc')) bcc = get_email_list(request.POST.get('bcc')) bcc.append(request.user.email) files = request.FILES.getlist('attachments') _files = [] for f in files: fs = FileSystemStorage() fs.location = join(settings.MEDIA_ROOT, "ipcaccounts/vouchers/email") filename = fs.save(f.name, f) _files.append(join(fs.location, filename)) uploaded_file_url = fs.url(filename) stage_classic_notification("IPC Vouchers", sender, to, cc, msg_subject, message_body, _files) _dispatch_status = True if _dispatch_status: account_id = request.POST.get('account_id') account_master = RCOAccountsMaster.objects.get(id=account_id) account_master.email_sent = True account_master.save() return render(request, 'ipcaccounts/confirm.html') else: return render(request, 'ipcaccounts/error.html')
def upload(request): # The assumption here is that jQuery File Upload # has been configured to send files one at a time. # If multiple files can be uploaded simultaneously, # 'file' may be a list of files. try: uid = request.POST['upload-hash'] in_file = request.FILES['file'] if request.FILES else None file_name, extension = os.path.splitext(in_file.name) file_storage = FileSystemStorage(location=os.path.join(settings.CACHE_ROOT, "mixes")) cache_file = file_storage.save("%s%s" % (uid, extension), ContentFile(in_file.read())) response = '' try: create_waveform_task.delay(in_file=os.path.join(file_storage.base_location, cache_file), uid=uid) except: logger.debug("Unable to connect to celery") response = 'Unable to connect to waveform generation task, there may be a delay in getting your mix online' file_dict = { 'response': response, 'size': in_file.size, 'uid': uid } return UploadResponse(request, file_dict) except Exception, ex: logger.exception(ex.message) raise
def do_upload_video(request,session,form,uploaded_file): user = request.user; print form.cleaned_data print uploaded_file submission_rt=os.path.join(settings.DATASETS_ROOT,session.code); id = session.mthit_set.count()+1; rand_id=str(uuid.uuid4())+"-"+str(id) print dir(uploaded_file) original_name =uploaded_file.name storage = FileSystemStorage(submission_rt); fname=storage.save(os.path.join(submission_rt,rand_id+".flv"),uploaded_file); params="video=/frames/"+session.code+"/"+rand_id+".flv&original_name="+original_name print params hit=mturk.models.MTHit(session=session,ext_hitid=rand_id,int_hitid=id,parameters=params); hit.save(); if form.cleaned_data["submit_for_annotation"]: (activated,id)=mturk.views.activate_hit(session,hit); else: activated=False return HttpResponse("done. Activated %d" % activated) #challenge=form.cleaned_data['challenge'] #challenge = get_object_or_404(Challenge,name=challenge_name); """
def list(self, ignore_patterns): re_patterns = [] for pattern in ignore_patterns: if pattern.startswith('*'): re_pattern = re.compile(pattern[1:].replace('.', '\\.') + '$') elif pattern.endswith('*'): re_pattern = re.compile('^' + pattern[:-1].replace('.', '\\.')) else: re_pattern = re.compile(pattern) if re_pattern is not None: re_patterns.append(re_pattern) for helio_static_dir in self.helio_static_dirs: storage = FileSystemStorage(location=helio_static_dir) for static_file in listdir(helio_static_dir): yield (static_file, storage) for component_base_dir in self.component_base_directories: for (dir_root, dir_list, file_list) in walk_component_base_dir(component_base_dir): for static_file in file_list: if should_ignore_file(static_file, re_patterns): continue full_static_path = join(dir_root, static_file) res = re.search(r'/(.*)/static', full_static_path) if res is None: continue storage = FileSystemStorage(location=dir_root) storage.prefix = res.group(1) yield (static_file, storage)
def upload_files(request): files = request.FILES.getlist('images') for image_file in files: if image_file.content_type not in [u'image/jpeg', u'image/gif', u'image/pjpeg', u'image/png']: continue instance = Picture.objects.create(owner=request.user) data = {} file_info = [] if image_file.__class__ is InMemoryUploadedFile: file_info.append(image_file.field_name) file_info.append(image_file.name) file_info.append(image_file.content_type) file_info.append(image_file.size) file_info.append(image_file.charset) #the actual data of the image, read into a string data['data'] = image_file.read() async_save_in_memory.delay(data, file_info, instance.pk) else: storage = FileSystemStorage() path = storage.save(u'uploaded/%s' % image_file.name, image_file) async_save_temporary.delay(instance.pk, path, image_file.name) return redirect(reverse('my_picture'))
class EventWizard(PermissionRequired, SensibleBackWizardMixin, SessionWizardView): permission_required = 'orga.create_events' file_storage = FileSystemStorage(location=Path(settings.MEDIA_ROOT) / 'new_event') form_list = [ ('initial', EventWizardInitialForm), ('basics', EventWizardBasicsForm), ('timeline', EventWizardTimelineForm), ('display', EventWizardDisplayForm), ('copy', EventWizardCopyForm), ] condition_dict = {'copy': condition_copy} def get_template_names(self): return f'orga/event/wizard/{self.steps.current}.html' @context def has_organiser(self): return ( self.request.user.teams.filter(can_create_events=True).exists() or self.request.user.is_administrator) @context def url_placeholder(self): return f'https://{self.request.host}/' @context def organiser(self): return (self.get_cleaned_data_for_step('initial').get('organiser') if self.steps.current != 'initial' else None) def render(self, form=None, **kwargs): if self.steps.current != 'initial': fdata = self.get_cleaned_data_for_step('initial') if fdata is None: return self.render_goto_step('initial') if self.steps.current == 'timeline': fdata = self.get_cleaned_data_for_step('basics') year = now().year % 100 if fdata and not str(year) in fdata['slug'] and not str( year + 1) in fdata['slug']: messages.warning( self.request, str( _('Please consider including your event\'s year in the slug, e.g. myevent{number}.' )).format(number=year)) if self.steps.current == 'display': fdata = self.get_cleaned_data_for_step('timeline') if fdata and fdata.get('date_to') < now().date(): messages.warning( self.request, _('Did you really mean to make your event take place in the past?' )) return super().render(form, **kwargs) def get_form_kwargs(self, step=None): kwargs = {'user': self.request.user} if step != 'initial': fdata = self.get_cleaned_data_for_step('initial') kwargs.update(fdata or dict()) return kwargs @transaction.atomic() def done(self, form_list, *args, **kwargs): steps = { step: self.get_cleaned_data_for_step(step) for step in ('initial', 'basics', 'timeline', 'display', 'copy') } with scopes_disabled(): event = Event.objects.create( organiser=steps['initial']['organiser'], locale_array=','.join(steps['initial']['locales']), name=steps['basics']['name'], slug=steps['basics']['slug'], timezone=steps['basics']['timezone'], email=steps['basics']['email'], locale=steps['basics']['locale'], primary_color=steps['display']['primary_color'], logo=steps['display']['logo'], date_from=steps['timeline']['date_from'], date_to=steps['timeline']['date_to'], ) with scope(event=event): deadline = steps['timeline'].get('deadline') if deadline: zone = timezone(event.timezone) event.cfp.deadline = zone.localize( deadline.replace(tzinfo=None)) event.cfp.save() for setting in [ 'custom_domain', 'display_header_data', 'show_on_dashboard' ]: value = steps['display'].get(setting) if value: event.settings.set(setting, value) has_control_rights = self.request.user.teams.filter( organiser=event.organiser, all_events=True, can_change_event_settings=True, can_change_submissions=True, ).exists() if not has_control_rights: t = Team.objects.create( organiser=event.organiser, name=_(f'Team {event.name}'), can_change_event_settings=True, can_change_submissions=True, ) t.members.add(self.request.user) t.limit_events.add(event) logdata = {} for f in form_list: logdata.update({k: v for k, v in f.cleaned_data.items()}) with scope(event=event): event.log_action('pretalx.event.create', person=self.request.user, data=logdata, orga=True) if steps['copy'] and steps['copy']['copy_from_event']: event.copy_data_from(steps['copy']['copy_from_event']) return redirect(event.orga_urls.base + '?congratulations')
def __init__(self): # type: () -> None super().__init__(FileSystemStorage())
def setUp(self): self.storage_dir = tempfile.mkdtemp() self.storage = FileSystemStorage(self.storage_dir)
def test_file_upload_directory_default_permissions(self): self.storage = FileSystemStorage(self.storage_dir) name = self.storage.save("the_directory/the_file", ContentFile("data")) dir_mode = os.stat(os.path.dirname(self.storage.path(name)))[0] & 0o777 self.assertEqual(dir_mode, 0o777 & ~self.umask)
def test_file_upload_default_permissions(self): self.storage = FileSystemStorage(self.storage_dir) fname = self.storage.save("some_file", ContentFile("data")) mode = os.stat(self.storage.path(fname))[0] & 0o777 self.assertEqual(mode, 0o666 & ~self.umask)
def test_file_upload_permissions(self): self.storage = FileSystemStorage(self.storage_dir) name = self.storage.save("the_file", ContentFile("data")) actual_mode = os.stat(self.storage.path(name))[0] & 0o777 self.assertEqual(actual_mode, 0o654)
def setUp(self): self.storage_dir = tempfile.mkdtemp() self.storage = FileSystemStorage(self.storage_dir) self.thread = threading.Thread(target=self.save_file, args=['conflict'])
def Upload(request): if request.method == 'POST': uploaded_file = request.FILES['dataset'] arr = uploaded_file.name.split('.', 1) fName = arr[0] extension = arr[1] fullName = fName + '.' + extension # Validating the uploaded file if extension == 'csv': fs = FileSystemStorage() file_path1 = os.path.join(settings.MEDIA_ROOT, 'original/' + fullName) file_path2 = os.path.join(settings.MEDIA_ROOT, 'processed/' + fullName) if os.path.exists(file_path1 and file_path2): os.remove(file_path1) os.remove(file_path2) plt.clf() chart_path = os.path.join(settings.MEDIA_ROOT, 'static/charts/') boxplot_chart_path = os.path.join(settings.MEDIA_ROOT, 'static/boxplotcharts/') if os.path.exists(chart_path): shutil.rmtree(chart_path, ignore_errors=False, onerror=handleRemoveReadonly) os.makedirs(chart_path) else: os.makedirs(chart_path) if os.path.exists(boxplot_chart_path): shutil.rmtree(boxplot_chart_path, ignore_errors=False, onerror=handleRemoveReadonly) os.makedirs(boxplot_chart_path) else: os.makedirs(boxplot_chart_path) fs.save('processed/' + fullName, uploaded_file) fs.save('original/' + fullName, uploaded_file) df = pd.read_csv(os.path.join(settings.MEDIA_ROOT, 'processed/' + fName + '.csv'), encoding='mbcs') df = df.replace(to_replace="?", value="nan") df.to_csv(os.path.join(settings.MEDIA_ROOT, 'processed/' + fName + '.csv'), index=False) context = Overview(fName) context['status'] = 'Success' context['message'] = 'Dataset Uploaded Successfully' return render(request, 'index.html', context) else: context = { 'fName': fName, 'status': 'Error', 'message': 'Please upload .csv files' } return render(request, 'Upload.html', context) return render(request, 'Upload.html')
def setUp(self): self.old_perms = settings.FILE_UPLOAD_PERMISSIONS settings.FILE_UPLOAD_PERMISSIONS = 0666 self.storage_dir = tempfile.mkdtemp() self.storage = FileSystemStorage(self.storage_dir)
def cargaexcel(request): if request.method == 'POST': uploaded_file = request.FILES['document'] fs = FileSystemStorage() name = fs.save(uploaded_file.name, uploaded_file) xls = pd.ExcelFile(BASE_DIR + fs.url(name)) hojas = xls.sheet_names print(len(hojas)) dc1 = Document() def mat(abcd): pri = 0 val = 0 aux = [] for i in range(15): aux.append([]) for j in range(5): aux[i].append(None) for i in range(len(abcd)): sec = 0 val = 0 for j in range(len(abcd[i])): if type(abcd[i][j]) is str: val += 1 aux[pri][sec] = abcd[i][j] sec += 1 else: if type(abcd[i][j]) is int: abcd[i][j] = str(abcd[i][j]) val += 1 aux[pri][sec] = abcd[i][j] sec += 1 else: if type(abcd[i][j]) is float: if abcd[i][j]/abcd[i][j]==1: abcd[i][j] = str(abcd[i][j]) val += 1 aux[pri][sec] = abcd[i][j] sec += 1 if val > 0: pri += 1 return aux aux=0 for cont in range(len(hojas)-1): cont += 1 df = xls.parse(hojas[cont]) fich = df.__array__() real = mat(fich); fich = real print(fich) tit = fich[0][0] anio = str(fich[4][1]) nota1 = str(fich[10][1]) nota2 = str(fich[10][2]) nota3 = str(fich[10][3]) promedio = str(fich[11][1]) dc1.add_heading(tit, 0) dc1.add_heading(fich[1][0], 2) p = dc1.add_paragraph() # p.add_run('\n') tbl1 = dc1.add_table(rows=0, cols=2) fila = tbl1.add_row().cells fila[0].text = fich[2][0] fila[1].text = fich[2][1] # p.add_run(fich[2][0] + ' \t').bold = True # p.add_run(fich[2][1]) fila1 = tbl1.add_row().cells fila1[0].text = fich[3][0] fila1[1].text = fich[3][1] # p.add_run('\n\n') # p.add_run(fich[3][0] + ' \t').bold = True # p.add_run(fich[3][1]) fila2 = tbl1.add_row().cells fila2[0].text = fich[4][0] fila2[1].text = anio # p.add_run('\n\n') # p.add_run(fich[4][0] + ' \t').bold = True # p.add_run(anio) dc1.add_heading(fich[5][0], 2) p1 = dc1.add_paragraph() # p1.add_run('\n') tbl2 = dc1.add_table(rows=0, cols=2) fila = tbl2.add_row().cells fila[0].text = fich[6][0] fila[1].text = fich[6][1] # p1.add_run(fich[6][0] + ' \t').bold = True # p1.add_run(fich[6][1]) fila1 = tbl2.add_row().cells fila1[0].text = fich[7][0] fila1[1].text = fich[7][1] # p1.add_run('\n\n') # p1.add_run(fich[7][0] + ' \t').bold = True # p1.add_run(fich[7][1]) dc1.add_heading(fich[8][0], 2) p2 = dc1.add_paragraph() # p2.add_run('\n') tbl3 = dc1.add_table(rows=0, cols=4) fila = tbl3.add_row().cells fila[0].text = fich[9][0] fila[1].text = fich[9][1] # p2.add_run(fich[9][0] + ' \t').bold = True # p2.add_run(fich[9][1]) fila1 = tbl3.add_row().cells fila1[0].text = fich[10][0] fila1[1].text = nota1 fila1[2].text = nota2 fila1[3].text = nota3 # p2.add_run('\n\n') # p2.add_run(fich[10][0] + ' \t').bold = True # p2.add_run(nota1+ ' \t'+nota2+ ' \t'+nota3+ ' \t') fila2 = tbl3.add_row().cells fila2[0].text = fich[11][0] fila2[1].text = promedio # p2.add_run('\n\n') # p2.add_run(fich[11][0] + ' \t').bold = True # p2.add_run(promedio) dc1.add_heading(fich[12][0], 2) p3 = dc1.add_paragraph() tbl4 = dc1.add_table(rows=0, cols=2) fila = tbl4.add_row().cells fila[0].text = fich[13][0] fila[1].text = fich[13][1] # p3.add_run('\n') # p3.add_run(fich[13][0] + ' \t').bold = True # p3.add_run(fich[13][1]) fila1 = tbl4.add_row().cells fila1[0].text = fich[14][0] fila1[1].text = fich[14][1] # p3.add_run('\n\n') # p3.add_run(fich[14][0] + ' \t').bold = True # p3.add_run(fich[14][1]) # dc1.add_picture('C:/Users/Angel/Desktop/prueba/excel/imgplot.png') """ nombimg = 'img' + str(aux) + '.jpg' num = int(nota) nomb = str(fich[2][1]) nombre = ("", nomb, "") posicion_y = np.arange(3) unidad = (0, num, 0) plt.barh(posicion_y, unidad, align="center") plt.yticks(posicion_y, nombre) plt.xlabel("NOTA") plt.title("NOTAS") plt.savefig(BASE_DIR+'/media/'+nombimg) dc1.add_picture(BASE_DIR+'/media/'+nombimg) os.remove(BASE_DIR+'/media/'+nombimg) """ dc1.add_page_break() nombre_archivo = "Proyecto.docx" response = HttpResponse(content_type="application/msword") contenido = "attachment; filename= {0}".format(nombre_archivo) response["Content-Disposition"] = contenido dc1.save(response) return response #return render(request, 'word/index.html', {'url':fs.url(name)}) return render(request, 'word/index.html')
def setUp(self): self.temp_dir = tempfile.mkdtemp() self.storage = FileSystemStorage(location=self.temp_dir)
def create(self, request, *args, **kwargs): serializer = self.serializer_class(data=request.data) if serializer.is_valid(raise_exception=True): file_name = serializer.validated_data['products_file'].name fs = FileSystemStorage('media/import_export/') file_extension = file_name.split('.')[1].lower() filename = fs.save( file_name.split('.')[0].lower() + '_' + str(str(uuid.uuid4())[-4:]) + '.' + file_extension, serializer.validated_data.get('products_file')) file_path = MEDIA_URL + 'import_export/' + filename cwd = os.getcwd() full_path = f"{cwd}/{file_path}" if file_extension == 'xls' or file_extension == 'xlsx': try: book = xlrd.open_workbook(full_path) sheet_obj = book.sheet_by_index(0) max_rows = sheet_obj.nrows products_list = list() for rows_count in range(1, max_rows): if len(str(sheet_obj.cell_value(rows_count, 0))) == 2: digits = 2 elif len(str(sheet_obj.cell_value(rows_count, 0))) == 4: digits = 4 elif len(str(sheet_obj.cell_value(rows_count, 0))) == 6: digits = 6 elif len(str(sheet_obj.cell_value(rows_count, 0))) == 8: digits = 8 else: digits = None products_list.append( ProductMaster(hs_code=str( sheet_obj.cell_value(rows_count, 0)), description=sheet_obj.cell_value( rows_count, 1), digits=digits, created_by=self.request.user)) ProductMaster.objects.bulk_create(products_list) return Response( {'msg': 'Product file gets successfully uploaded'}, status=status.HTTP_200_OK) except xlrd.biffh.XLRDError: return Response( { 'error': 'Could not process the uploaded file at this moment' }, status=status.HTTP_400_BAD_REQUEST) else: return Response( { 'error': f"Only Files with 'xls' and 'xlsx are supported" }, status=status.HTTP_400_BAD_REQUEST) else: return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
class SubmitWizard(EventPageMixin, SensibleBackWizardMixin, NamedUrlSessionWizardView): form_list = FORMS condition_dict = {'questions': show_questions_page, 'user': show_user_page} file_storage = FileSystemStorage( os.path.join(settings.MEDIA_ROOT, 'avatars')) def dispatch(self, request, *args, **kwargs): if not request.event.cfp.is_open: messages.error(request, phrases.cfp.submissions_closed) return redirect( reverse('cfp:event.start', kwargs={'event': request.event.slug})) return super().dispatch(request, *args, **kwargs) def get_form_kwargs(self, step=None): kwargs = super().get_form_kwargs(step) if step in ['info', 'profile', 'questions']: kwargs['event'] = self.request.event if step == 'profile': user_data = self.get_cleaned_data_for_step('user') or dict() if user_data and user_data.get('user_id'): kwargs['user'] = User.objects.filter( pk=user_data['user_id']).first() if not kwargs.get('user') and self.request.user.is_authenticated: kwargs['user'] = self.request.user user = kwargs.get('user') kwargs['name'] = user.name if user else user_data.get( 'register_name') kwargs['read_only'] = False kwargs['essential_only'] = True if step == 'questions': kwargs['target'] = '' kwargs['track'] = (self.get_cleaned_data_for_step('info') or dict()).get('track') return kwargs def get_form_initial(self, step): initial = super().get_form_initial(step) if step == 'info': for field, model in (('submission_type', SubmissionType), ('track', Track)): request_value = self.request.GET.get(field) if request_value: with suppress(AttributeError, TypeError): pk = int(request_value.split('-')) obj = model.objects.filter(event=self.request.event, pk=pk).first() if obj: initial[field] = obj return initial def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) step = kwargs.get('step') form = kwargs.get('form') step_list = [] phase = 'done' for stp, form_class in self.get_form_list().items(): if stp == step or isinstance(form, form_class): phase = 'current' step_list.append({ 'url': self.get_step_url(stp), 'phase': phase, 'label': FORM_DATA[stp]['label'], 'icon': FORM_DATA[stp]['icon'], }) if phase == 'current': phase = 'todo' step_list.append({ 'phase': 'todo', 'label': _('Done!'), 'icon': 'check' }) context['step_list'] = step_list if step == 'profile': if hasattr(self.request.user, 'email'): email = self.request.user.email else: data = self.get_cleaned_data_for_step('user') or dict() email = data.get('register_email', '') context['gravatar_parameter'] = User( email=email).gravatar_parameter return context def get_template_names(self): return f'cfp/event/submission_{self.steps.current}.html' def get_prefix(self, request, *args, **kwargs): return super().get_prefix(request, *args, ** kwargs) + ':' + kwargs.get('tmpid') def get_step_url(self, step): return reverse( self.url_name, kwargs={ 'step': step, 'tmpid': self.kwargs.get('tmpid'), 'event': self.kwargs.get('event'), }, ) def _handle_question_answer(self, sub, qid, value, user=None): question = self.request.event.questions.filter(pk=qid).first() if not question: return if question.target == QuestionTarget.SUBMISSION: answer = Answer(question=question, submission=sub) elif question.target == QuestionTarget.SPEAKER: answer = Answer(question=question, person=user) if question.variant == QuestionVariant.MULTIPLE: answstr = ', '.join([str(o) for o in value]) answer.save() if value: answer.answer = answstr answer.options.add(*value) elif question.variant == QuestionVariant.CHOICES: answer.save() if value: answer.options.add(value) answer.answer = value.answer else: answer.answer = value or '' if answer.answer is not None: answer.save() def done(self, form_list, **kwargs): form_dict = kwargs.get('form_dict') if self.request.user.is_authenticated: user = self.request.user else: uid = form_dict['user'].save() user = User.objects.filter(pk=uid).first() if not user or not user.is_active: raise ValidationError( _('There was an error when logging in. Please contact the organiser for further help.' ), ) form_dict['info'].instance.event = self.request.event form_dict['info'].save() form_dict['info'].instance.speakers.add(user) sub = form_dict['info'].instance form_dict['profile'].user = user form_dict['profile'].save() if 'questions' in form_dict: for k, value in form_dict['questions'].cleaned_data.items(): qid = k.split('_')[1] self._handle_question_answer(sub, qid, value, user=user) try: sub.event.ack_template.to_mail( user=user, event=self.request.event, context=template_context_from_submission(sub), skip_queue=True, locale=user.locale, submission=sub, full_submission_content=True, ) if self.request.event.settings.mail_on_new_submission: MailTemplate( event=self.request.event, subject=_('New submission!').format( event=self.request.event.slug), text=self.request.event.settings.mail_text_new_submission, ).to_mail( user=self.request.event.email, event=self.request.event, context=template_context_from_submission(sub), skip_queue=True, locale=self.request.event.locale, ) additional_speaker = form_dict['info'].cleaned_data.get( 'additional_speaker').strip() if additional_speaker: sub.send_invite(to=[additional_speaker], _from=user) except SendMailException as exception: logging.getLogger('').warning(str(exception)) messages.warning(self.request, phrases.cfp.submission_email_fail) sub.log_action('pretalx.submission.create', person=user) messages.success(self.request, phrases.cfp.submission_success) login(self.request, user, backend='django.contrib.auth.backends.ModelBackend') return redirect( reverse('cfp:event.user.submissions', kwargs={'event': self.request.event.slug}))
def save(self): try: image = self.validated_data['image'] title = self.validated_data['title'] if len(title) < MIN_TITLE_LENGTH: raise serializers.ValidationError({ "response": "Enter a title longer than " + str(MIN_TITLE_LENGTH) + " characters." }) body = self.validated_data['body'] if len(body) < MIN_BODY_LENGTH: raise serializers.ValidationError({ "response": "Enter a body longer than " + str(MIN_BODY_LENGTH) + " characters." }) blog_post = BlogPost( author=self.validated_data['author'], title=title, body=body, image=image, ) url = os.path.join(settings.TEMP, str(image)) storage = FileSystemStorage(location=url) with storage.open('', 'wb+') as destination: for chunk in image.chunks(): destination.write(chunk) destination.close() if sys.getsizeof(image.file) > IMAGE_SIZE_MAX_BYTES: os.remove(url) raise serializers.ValidationError({ "response": "That image is too large. Images must be less than 2 MB. Try a different image." }) img = cv2.imread(url) dimensions = img.shape # gives: (height, width, ?) aspect_ratio = dimensions[1] / dimensions[0] # divide w / h if aspect_ratio < 1: os.remove(url) raise serializers.ValidationError({ "response": "Image height must not exceed image width. Try a different image." }) os.remove(url) blog_post.save() return blog_post except KeyError: raise serializers.ValidationError({ "response": "You must have a title, some content, and an image." })
def test_get_files(yarn_dir): storage = FileSystemStorage(location=str(yarn_dir)) files = get_files(storage, match_patterns='*') assert any([True for _ in files])
import uuid from django.db import models from django.contrib.postgres.fields import JSONField from django.core.files.storage import FileSystemStorage # Create your models here. FS = FileSystemStorage(location='') class EventBasic(models.Model): uiqueidEVENTS = models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True) title = models.CharField(max_length=50) startDate = models.DateField(max_length=10) endDate = models.DateField(max_length=10) registrationDeadline = models.DateField(max_length=10) description = models.TextField(max_length=500) banner = models.ImageField(storage=FS) audienceType = JSONField() venue = JSONField() venueCoordinate = JSONField() region = JSONField() maxAudience = JSONField() cuurency = JSONField() regisTrationFee = models.FloatField(max_length=10) def __str__(self): return self.title
def post(self, request): severity_color = "" uploaded_poc_url = "" vuln_name = request.POST.get("vuln_name") severity = request.POST.get("vuln_severity") vuln_url = request.POST.get("vuln_instance") description = request.POST.get("vuln_description") solution = request.POST.get("vuln_solution") reference = request.POST.get("vuln_reference") scan_id = request.POST.get("scan_id") project_uu_id = request.POST.get("project_id") project_id = (ProjectDb.objects.filter( uu_id=project_uu_id).values("id").get()["id"]) pentest_type = request.POST.get("pentest_type") poc = request.FILES.get("poc", False) poc_description = request.POST.get("poc_description") date_time = datetime.now() vuln_id = uuid.uuid4() fs = FileSystemStorage() if poc is not False: filename = fs.save(poc.name, poc) uploaded_poc_url = fs.url(filename) if severity == "High": severity_color = "danger" elif severity == "Medium": severity_color = "warning" elif severity == "Low": severity_color = "info" dump_data = PentestScanResultsDb( vuln_id=vuln_id, vuln_name=vuln_name, severity_color=severity_color, severity=severity, vuln_url=vuln_url, description=description, solution=solution, reference=reference, scan_id=scan_id, pentest_type=pentest_type, vuln_status="Open", project_id=project_id, Poc_Img=uploaded_poc_url, poc_description=poc_description, ) dump_data.save() all_scan_data = PentestScanResultsDb.objects.filter(scan_id=scan_id) total_vuln = len(all_scan_data) total_high = len(all_scan_data.filter(severity="High")) total_medium = len(all_scan_data.filter(severity="Medium")) total_low = len(all_scan_data.filter(severity="Low")) PentestScanDb.objects.filter(scan_id=scan_id).update( date_time=date_time, total_vul=total_vuln, high_vul=total_high, medium_vul=total_medium, low_vul=total_low, ) messages.success(request, "Pentest Vulnerability Created") return HttpResponseRedirect(reverse("pentest:list_scan"))
def storage(self): return FileSystemStorage(location=self.chunks_dir)
class EmptyModel(models.Model): def __unicode__(self): return "Primary key = %s" % self.id class EmptyModelAdmin(admin.ModelAdmin): def queryset(self, request): return super(EmptyModelAdmin, self).queryset(request).filter(pk__gt=1) class OldSubscriberAdmin(admin.ModelAdmin): actions = None temp_storage = FileSystemStorage(tempfile.mkdtemp()) UPLOAD_TO = os.path.join(temp_storage.location, 'test_upload') class Gallery(models.Model): name = models.CharField(max_length=100) class Picture(models.Model): name = models.CharField(max_length=100) image = models.FileField(storage=temp_storage, upload_to='test_upload') gallery = models.ForeignKey(Gallery, related_name="pictures") class PictureInline(admin.TabularInline): model = Picture
def popular(): pass class TattooManager(models.Manager): def new(): pass def popular(): pass BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) TATTOO_IMG_PATH = os.path.join(MEDIA_ROOT, "tattooimg") tattoofs = FileSystemStorage(TATTOO_IMG_PATH) class Studio(models.Model): objects = StudioManager() name = models.CharField(default="", max_length=255) rating = models.IntegerField(default=0) lat = models.FloatField(default=0) lng = models.FloatField(default=0) temporary_price = models.FloatField(default=-1) colored_price = models.FloatField(default=-1) temp_col_price = models.FloatField(default=-1) regular_price = models.FloatField(default=10) class Tattoo(models.Model):
def development_tracker_response(request): if request.method == "POST" and len(request.FILES) != 0: if request.FILES['file1']: myfile = request.FILES['file1'] fs = FileSystemStorage() if fs.exists(request.user.username + "1.png"): fs.delete(request.user.username + "1.png") filename = fs.save(request.user.username + "1.png", myfile) uploaded_file_url = fs.url(filename) if myfile.name[len(myfile.name) - 3:len(myfile.name)] != "png": return render(request, 'label/qgis_support_app.html', {'is_not_file_valid': True}) if request.FILES['file2']: myfile = request.FILES['file2'] fs = FileSystemStorage() if fs.exists(request.user.username + "2.png"): fs.delete(request.user.username + "2.png") filename = fs.save(request.user.username + "2.png", myfile) uploaded_file_url = fs.url(filename) if myfile.name[len(myfile.name) - 3:len(myfile.name)] != "png": return render(request, 'label/development_tracker_app.html', {'is_not_file_valid': True}) if (request.POST.get('type') == "road"): ps.save_image( Unet_model_Road, graph_road, os.path.join(settings.MEDIA_ROOT, request.user.username + "1.png"), os.path.join(settings.MEDIA_ROOT, request.user.username + "1_mask.png")) ps.save_image( Unet_model_Road, graph_road, os.path.join(settings.MEDIA_ROOT, request.user.username + "2.png"), os.path.join(settings.MEDIA_ROOT, request.user.username + "2_mask.png")) difference = ps.difference_analyzier( os.path.join(settings.MEDIA_ROOT, request.user.username + "1_mask.png"), os.path.join(settings.MEDIA_ROOT, request.user.username + "2_mask.png")) if (request.POST.get('type') == "building"): ps.save_image( Unet_model_Building, graph_building, os.path.join(settings.MEDIA_ROOT, request.user.username + "1.png"), os.path.join(settings.MEDIA_ROOT, request.user.username + "1_mask.png")) ps.save_image( Unet_model_Building, graph_building, os.path.join(settings.MEDIA_ROOT, request.user.username + "2.png"), os.path.join(settings.MEDIA_ROOT, request.user.username + "2_mask.png")) difference = ps.difference_analyzier( os.path.join(settings.MEDIA_ROOT, request.user.username + "1_mask.png"), os.path.join(settings.MEDIA_ROOT, request.user.username + "2_mask.png")) if (request.POST.get('type') == "car"): ps.save_image( Unet_model_Car, graph_car, os.path.join(settings.MEDIA_ROOT, request.user.username + "1.png"), os.path.join(settings.MEDIA_ROOT, request.user.username + "1_mask.png")) ps.save_image( Unet_model_Car, graph_car, os.path.join(settings.MEDIA_ROOT, request.user.username + "2.png"), os.path.join(settings.MEDIA_ROOT, request.user.username + "2_mask.png")) difference = ps.difference_analyzier( os.path.join(settings.MEDIA_ROOT, request.user.username + "1_mask.png"), os.path.join(settings.MEDIA_ROOT, request.user.username + "2_mask.png")) return render( request, "label/development_tracker_response.html", { 'image_url1': "/media/" + request.user.username + "1_mask.png", 'image_url2': "/media/" + request.user.username + "2_mask.png", 'difference': difference }) return redirect(request.META.get('HTTP_REFERER'))
def home(request): if request.user.is_authenticated: if request.method == 'POST' and request.FILES.get('sourcefile', False): time_stamp = datetime.datetime.now().strftime('%Y-%m-%d_%H-%M-%S') source_file = request.FILES['sourcefile'] fs = FileSystemStorage() filename = fs.save(source_file.name, source_file) uploaded_file_url = fs.url(filename) filepath = os.path.join(settings.MEDIA_ROOT, filename) img = Image.open(filepath) img_width = img.size[0] img_height = img.size[1] (img_ldpi, img_mdpi, img_hdpi, img_xhdpi, img_xxhdpi, img_xxxhdpi) = get_images(img, img_width, img_height) (zip_ldpi, zip_mdpi, zip_hdpi, zip_xhdpi, zip_xxhdpi, zip_xxxhdpi) = get_folder_names() main_save_path = os.path.join(settings.MEDIA_ROOT, time_stamp) ldpi_save_path = os.path.join(main_save_path, zip_ldpi) mdpi_save_path = os.path.join(main_save_path, zip_mdpi) hdpi_save_path = os.path.join(main_save_path, zip_hdpi) xhdpi_save_path = os.path.join(main_save_path, zip_xhdpi) xxhdpi_save_path = os.path.join(main_save_path, zip_xxhdpi) xxxhdpi_save_path = os.path.join(main_save_path, zip_xxxhdpi) os.makedirs(ldpi_save_path, exist_ok=True) os.makedirs(mdpi_save_path, exist_ok=True) os.makedirs(hdpi_save_path, exist_ok=True) os.makedirs(xhdpi_save_path, exist_ok=True) os.makedirs(xxhdpi_save_path, exist_ok=True) os.makedirs(xxxhdpi_save_path, exist_ok=True) img_ldpi.save(os.path.join(ldpi_save_path, filename)) img_mdpi.save(os.path.join(mdpi_save_path, filename)) img_hdpi.save(os.path.join(hdpi_save_path, filename)) img_xhdpi.save(os.path.join(xhdpi_save_path, filename)) img_xxhdpi.save(os.path.join(xxhdpi_save_path, filename)) img_xxxhdpi.save(os.path.join(xxxhdpi_save_path, filename)) s = io.BytesIO() zf = zipfile.ZipFile(s, 'w') zf.write( os.path.join(ldpi_save_path, filename), os.path.join(os.path.join(ZIP_MAIN_NAME, zip_ldpi), filename)) zf.write( os.path.join(mdpi_save_path, filename), os.path.join(os.path.join(ZIP_MAIN_NAME, zip_mdpi), filename)) zf.write( os.path.join(hdpi_save_path, filename), os.path.join(os.path.join(ZIP_MAIN_NAME, zip_hdpi), filename)) zf.write( os.path.join(xhdpi_save_path, filename), os.path.join(os.path.join(ZIP_MAIN_NAME, zip_xhdpi), filename)) zf.write( os.path.join(xxhdpi_save_path, filename), os.path.join(os.path.join(ZIP_MAIN_NAME, zip_xxhdpi), filename)) zf.write( os.path.join(xxxhdpi_save_path, filename), os.path.join(os.path.join(ZIP_MAIN_NAME, zip_xxxhdpi), filename)) zf.close() shutil.rmtree(main_save_path) resp = HttpResponse(s.getvalue(), content_type="application/x-zip-compressed") resp[ 'Content-Disposition'] = 'attachment; filename=%s' % ZIP_FILENAME return resp return render(request, 'home.html') return render(request, 'home.html')
def store_image(image): fs = FileSystemStorage() file = fs.save(image.name, image) file_url = fs.url(file) return file_url
def index(request): if (request.method == 'POST' and request.FILES['resume']): resume = request.FILES['resume'] # print('resume',type(resume),resume.name) file_Storage = FileSystemStorage() file_Name = file_Storage.save(resume.name, resume) uploaded_file_url = file_Storage.url(file_Name) # print(uploaded_file_url,type(resume),resume.name) if ('.pdf' in str(resume)): # print('It is a pdf') email_address_pdf = extract_email_addresses_pdf(resume) # print('email_address_from_pdf',email_address_pdf) name_pdf = extract_name_pdf(resume) # print('primary name possibilities',name_pdf[0]) # print('secondary name possibilities',name_pdf[1]) phone_numbers_pdf = extract_phone_numbers_pdf(resume) print(phone_numbers_pdf.replace('-', ''), 'contact no.') # print('linkedin url',name_pdf[2]) #extract_num_lines_pdf(resume) text_char_count_pdf = extract_text_characters_count(resume) # print('text_char_count_pdf',text_char_count_pdf) font_font_size = extract_font_font_size_pdf(resume) # print('font_font_size_pdf',font_font_size) table_image_count = extract_table_image_count_pdf(resume) # print(table_image_count) response = HttpResponse(content_type='text/csv') response[ 'Content-Disposition'] = 'attachment;filename="resume_details.csv"' writer = csv.writer(response) writer.writerow([ 'First row', 'Primary possibilities for name', 'Secondary possibilities for name', 'Email Address', 'Phone Number', 'LinkedIn Link', 'Number of text characters per page', 'Font details', 'Number of tables', 'Number of images' ]) writer.writerow([ 'Second row', name_pdf[0], name_pdf[1], email_address_pdf, 'Phone number: ' + phone_numbers_pdf.replace('-', ''), name_pdf[2], text_char_count_pdf, font_font_size, table_image_count[0], table_image_count[1] ]) return response elif ('.docx' in str(resume)): # print('It is a doc') doc = docx.Document(resume) celltext = '' rowsum = 0 for table in doc.tables: for row in table.rows: rowsum += 1 for cell in row.cells: celltext += cell.text num_lines_doc = 0 num_lines_doc = document_num_lines(resume, celltext) num_lines_doc += rowsum # print('number of lines in word document',num_lines_doc) phone_number_doc = extract_phone_numbers_doc(resume, celltext) phone_number_final = '' if (len(phone_number_doc[0]) > 0): phone_number_final = 'Phone number' + phone_number_doc[0] else: phone_number_final = 'Phone number' + phone_number_doc[1] # print('phone_number_doc',phone_number_final.replace('-','')) email_address_doc = extract_email_addresses_docs(resume, celltext) # print('linkedin',email_address_doc[1]) email_address_final = email_address_doc[0] # print('email_address_docs',email_address_final) name_doc = extract_name(resume, celltext) name_doc_final = [] for i in range(len(name_doc)): if (len(name_doc[1]) > 0): name_doc_final.append(name_doc[1]) elif (len(name_doc[0]) > 0 or len(name_doc[2]) > 0): if (name_doc[0] not in name_doc_final): name_doc_final.append(name_doc[0]) if (name_doc[2] not in name_doc_final): name_doc_final.append(name_doc[2]) final_name = set(name_doc_final) # print('name_Doc',final_name) font_doc = font_type_and_font_size_extraction_doc(resume, celltext) # print(font_doc) table_count_doc, image_count_doc = extract_table_image_count( resume, celltext) # print('table count_doc,image count_doc respectively',table_count_doc,image_count_doc) response = HttpResponse(content_type='text/csv') response[ 'Content-Disposition'] = 'attachment;filename="resume_details.csv"' writer = csv.writer(response) writer.writerow([ 'Primary possibilities for name', 'Secondary possibilities for name', 'Email Address', 'Phone Number', 'LinkedIn Link', 'Number of text lines', 'Font details', 'Number of tables', 'Number of images' ]) writer.writerow([ final_name, final_name, email_address_final, phone_number_final.replace('-', ''), email_address_doc[1], num_lines_doc, font_doc, table_count_doc, image_count_doc ]) return response return render(request, 'resume_reader_app/index.html')
def view_name(request): file_name = "" file_numberOfSections = "" file_DllChar = "" dll_types = [] dll_dict = {} file_ImageVersion = "" compileTimeInHex = "" compileTime = "" dt_object = "" virtual_memory = 0 physical_memory = 0 fileSize = "" severity = "" section_count = "" version = "" numberOfDll = 0 dict_func = "" virtualMem = 0 physicalMem = 0 # LOGGIN DEBUG # logger.info(file_path) logger = logging.getLogger('testlogger') # micosoft_func == a list of dataframes # a temporary solution for categorizing microsoft_func = pd.read_html( 'https://docs.microsoft.com/en-us/windows/win32/api/_security/#functions' ) SnI_df = microsoft_func[1] # CSV download # https://docs.djangoproject.com/en/3.1/howto/outputting-csv/ # For some reason, I can't use the bottom comment # if requst.method == 'POST' and 'CSV' in request.POST: if request.method == 'POST' and "myfile" not in request.FILES: response = HttpResponse(content_type='text/csv') response['Content-Disposition'] = 'attachment; filename="report.csv"' writer = csv.writer(response) # Insert features here for file # writer.writerow(["first row", "some", "value"]) # writer.writerow(["second row", "some", "value"]) # read data from txt file and write to csv file with open("temp.txt", "r") as reader: line = reader.readline() while line != '': params = line.split("$") logger.info(params[0] + "---" + params[1]) x = params[0] y = "some" z = params[1] writer.writerow([x, y, z]) line = reader.readline() return response elif request.method == 'POST' and 'myfile' in request.FILES: # open temp.txt and erase temp data file = open("temp.txt", "r+") file.truncate(0) file.close() file = request.FILES['myfile'] fs = FileSystemStorage() file_save = fs.save(file.name, file) # comment out this code below to enable storing file in firebase storage # storage.child("files/" + file.name).put("media/" + file.name) fileSize = str(file.size) print("Uploaded file name: " + file.name) print("Uploaded file size: " + fileSize) # Test pefile file_name = file.name file_path = os.path.join(BASE_DIR, 'media/' + file.name) putty = pefile.PE(file_path) # FILE_HEADER machine = putty.FILE_HEADER.Machine timeDateStamp = putty.FILE_HEADER.TimeDateStamp section_count = putty.FILE_HEADER.NumberOfSections characteristic = putty.FILE_HEADER.Characteristics # OPTIONAL_HEADER magic = putty.OPTIONAL_HEADER.Magic dll = putty.OPTIONAL_HEADER.DllCharacteristics version = putty.OPTIONAL_HEADER.MajorImageVersion addressOfEntry = putty.OPTIONAL_HEADER.Magic severity = "HIGH" for entry in putty.DIRECTORY_ENTRY_IMPORT: dll_types.append(entry.dll.decode('utf-8')) dll_dict['dll_types'] = dll_types ##------------------- All possible value needed to return to homepage ------------------## numberOfDll = getNumberOfDllUsed(dll_dict['dll_types']) ##------------------- Get info from sections in exe file ------------------## virtualMem, physicalMem = getVirtualSize(file_path) # ------- Get Compile Time ----------- compileTimeInHex = getTimeDateStamp(file_path) # convert hex to decimal string compileTimeInDec = int(compileTimeInHex, 16) logger.info(compileTimeInDec) compileTime = datetime.fromtimestamp(compileTimeInDec).strftime( '%Y-%m-%d %H:%M:%S') logger.info(compileTime) # -------- Function Collecting ---------- # returns a dictionary of dlls as keys with values being a list of functions called with their category. # Ex. 'Kernal.DLL': [(function_Names, Category)] # def func_collect is near the top dict_func = func_collect(file_path, SnI_df) # pprint.pprint(dict_func) ##### ---------------- store all data in temp text file to export to csv file -------- ######### file1 = open("temp.txt", "a") L = [ "File_name$" + file_name + "\n", "Number of Sections$" + str(section_count) + "\n", "CompileTimeInHex$" + str(compileTimeInHex) + "\n", "CompileTime$" + compileTime + "\n", "virtual _memory$" + str(virtual_memory) + "\n", "physical_memory$" + str(physical_memory) + "\n", "file_size$" + str(fileSize) + "\n", "severity$" + severity + "\n", "file_version$" + str(version) + "\n", 'NumberOfDlls$' + str(numberOfDll) + "\n", ] # \n is placed to indicate EOL (End of Line) file1.writelines(L) file1.close() ##### ----------------end-------- ######### # messages.success(request, "File upload in Firebase Storage successful") # return render(request, "fileUpload/home.html",{"FileName":file_name,"NumberOfSection":file_numberOfSections,"DllChar":file_DllChar,"ImageVersion":file_ImageVersion}) return render( request, "fileUpload/home.html", { "CompileTimeInHex": compileTimeInHex, "CompileTime": compileTime, "FileName": file_name, "virtual_memory": virtual_memory, "physical_memory": physical_memory, "file_size": fileSize, "severity": severity, "file_section_count": section_count, "file_version": version, 'NumberOfDlls': numberOfDll, 'FunctionCalls': dict_func, 'VirtualMem': virtualMem, 'PhysicalMem': physicalMem })
"contact": "*****@*****.**" }) DEFAULT_BADGE_IMAGE = getattr( settings, 'BADGER_DEFAULT_BADGE_IMAGE', "%s/fixtures/default-badge.png" % dirname(__file__)) # Set up a file system for badge uploads that can be kept separate from the # rest of /media if necessary. Lots of hackery to ensure sensible defaults. UPLOADS_ROOT = getattr( settings, 'BADGER_UPLOADS_ROOT', os.path.join(getattr(settings, 'MEDIA_ROOT', 'media/'), 'uploads')) UPLOADS_URL = getattr( settings, 'BADGER_UPLOADS_URL', urljoin(getattr(settings, 'MEDIA_URL', '/media/'), 'uploads/')) BADGE_UPLOADS_FS = FileSystemStorage(location=UPLOADS_ROOT, base_url=UPLOADS_URL) TIME_ZONE_OFFSET = getattr(settings, "TIME_ZONE_OFFSET", timedelta(0)) class TZOffset(tzinfo): """TZOffset""" def __init__(self, offset): self.offset = offset def utcoffset(self, dt): return self.offset def tzname(self, dt): return settings.TIME_ZONE
def submit_file_content(request): the_file = request.FILES['file'] # error throws up here. fs = FileSystemStorage() filename = fs.save(the_file.name, the_file) uploaded_file_url = fs.url(filename) return HttpResponse(uploaded_file_url)
from intrinsic.evaluation import evaluate_decomposition for decomposition_id in decomposition_ids: update_kwargs = evaluate_decomposition( decomposition_id, delete_failed_open=delete_failed_open, **evaluate_kwargs) IntrinsicImagesDecomposition.objects \ .filter(id=decomposition_id) \ .update(**update_kwargs) from django.core.files.storage import FileSystemStorage INTRINSIC_STORAGE = FileSystemStorage( location=settings.MEDIA_ROOT, base_url=settings.MEDIA_URL, ) @shared_task def upload_intrinsic_file(name): from django.core.files.base import ContentFile from django.core.files.storage import default_storage default_storage.save(name, ContentFile(INTRINSIC_STORAGE.open(name).read())) INTRINSIC_STORAGE.delete(name) @shared_task def export_dataset_photo_task(photo_id, out_dir): photo = Photo.objects.filter(id=photo_id).select_related(
def update_doctor(request, Docid): if request.method == 'POST': usr = user.objects.filter(user_id=Docid) # upload profile,ssn pictures and save paths to database fs = FileSystemStorage() if 'Profile_picture' in request.FILES: profile_picture_file = request.FILES['Profile_picture'] ppf_name = fs.save(profile_picture_file.name, profile_picture_file) Pro_pic = '/hospital' + fs.url(ppf_name) usr.update(Profile_picture=Pro_pic) if 'SSN_Picture' in request.FILES: ssn_picture_file = request.FILES['SSN_Picture'] spf_name = fs.save(ssn_picture_file.name, ssn_picture_file) SSN_Pic = '/hospital' + fs.url(spf_name) usr.update(SSN_Picture=SSN_Pic) usr.update(first_name=request.POST['first_name']) usr.update(middle_name=request.POST['middle_name']) usr.update(last_name=request.POST['last_name']) usr.update(country=request.POST['country']) usr.update(state=request.POST['state']) usr.update(city=request.POST['city']) usr.update(street=request.POST['street']) usr.update(zip_code=request.POST['zip_code']) usr.update(phone_number=request.POST['phone_number']) usr.update(home_phone_number=request.POST['home_phone_number']) usr.update(work_phone_number=request.POST['work_phone_number']) usr.update(Date_of_birth=request.POST['Date_of_birth']) usr.update(Child_num=request.POST['Child_num']) usr.update(email_1=request.POST['email_1']) usr.update(email_2=request.POST['email_2']) usr.update(Nationality=request.POST['Nationality']) usr.update(Jop_place=request.POST['Jop_place']) usr.update(Job_name=request.POST['Job_name']) usr.update(Job_organization=request.POST['Job_organization']) usr.update(Ssn=request.POST['Ssn']) ssn = request.POST['Ssn'] ssn_id = ssn[7:14] usr.update(Ssn_id=ssn_id) genderMessage = gender(request.POST['gender']) maritalStatusMessage = maritalStatus(request.POST['marital_status']) if genderMessage == 'error': return HttpResponseRedirect('/hospital/updateHospitalDoctor/') elif maritalStatusMessage == 'error': return HttpResponseRedirect('/hospital/updateHospitalDoctor/') else: usr.update(gender=genderMessage) usr.update(marital_status=maritalStatusMessage) usr.update(User_type=2) ######################################################### # form 2 doct = doctor.objects.filter(Doc_id=Docid) doct.update(Registration_num=request.POST['Registration_num']) doct.update(Specialization=request.POST['Specialization']) doct.update(Registration_date=request.POST['Registration_date']) doct.update(licence=request.POST['licence']) doct.update(Last_date_payment=request.POST['Last_date_payment']) doct.update(End_date=request.POST['End_date']) doct.update(Specialization_type=request.POST['Specialization_type']) doct.update(University=request.POST['University']) doct.update(Work_place=request.POST['Work_place']) doct.update(Sub_syndicate=request.POST['Sub_syndicate']) doct.update(General_secretary=request.POST['General_secretary']) doct.update(Working_till=request.POST['Working_till']) doct.update(Graduation_year=request.POST['Graduation_year']) ######################################################## return HttpResponseRedirect('/hospital/Index/') else: hospitaldata = hospital.objects.get( h_id=request.session['hospital_id']) doctorData = doctor.objects.get(Doc_id=Docid) userData = user.objects.get(user_id=Docid) context = { 'hospital': hospitaldata, 'hos_id': hospitaldata.h_id, 'doctorData': doctorData, 'userData': userData, } return render(request, 'updateHospitalDoctor.html', context)
was opened. """ def __init__(self, *args, **kwargs): self.was_opened = False super().__init__(*args, **kwargs) def open(self): self.was_opened = True super().open() class TestImageField(ImageField): attr_class = TestImageFieldFile # Set up a temp directory for file storage. temp_storage_dir = tempfile.mkdtemp() temp_storage = FileSystemStorage(temp_storage_dir) temp_upload_to_dir = os.path.join(temp_storage.location, 'tests') class Person(models.Model): """ Model that defines an ImageField with no dimension fields. """ name = models.CharField(max_length=50) mugshot = TestImageField(storage=temp_storage, upload_to='tests') class AbstractPersonWithHeight(models.Model): """ Abstract model that defines an ImageField with only one dimension field to make sure the dimension update is correctly run on concrete subclass instance post-initialization. """