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"))
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")
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)
class FileStoragePermissions(unittest.TestCase): def setUp(self): self.umask = 0o027 self.old_umask = os.umask(self.umask) self.storage_dir = tempfile.mkdtemp() self.storage = FileSystemStorage(self.storage_dir) def tearDown(self): shutil.rmtree(self.storage_dir) os.umask(self.old_umask) @override_settings(FILE_UPLOAD_PERMISSIONS=0o654) def test_file_upload_permissions(self): name = self.storage.save("the_file", ContentFile("data")) actual_mode = os.stat(self.storage.path(name))[0] & 0o777 self.assertEqual(actual_mode, 0o654) @override_settings(FILE_UPLOAD_PERMISSIONS=None) def test_file_upload_default_permissions(self): fname = self.storage.save("some_file", ContentFile("data")) mode = os.stat(self.storage.path(fname))[0] & 0o777 self.assertEqual(mode, 0o666 & ~self.umask) @override_settings(FILE_UPLOAD_DIRECTORY_PERMISSIONS=0o765) def test_file_upload_directory_permissions(self): 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, 0o765) @override_settings(FILE_UPLOAD_DIRECTORY_PERMISSIONS=None) def test_file_upload_directory_default_permissions(self): 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)
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 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 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 App_SaveUploadedFile(request, f): from django.core.files.storage import FileSystemStorage filedir = os.path.join(settings.MEDIA_ROOT, App_UserFilesDir(request.user)) fs = FileSystemStorage(location=filedir) filename = '{0}_{1}'.format(timezone.now(), f.name) fs.save(filename, f) # print(filename) return filename
def uploadBook(request): # Handle file upload if request.user.is_authenticated(): if request.method == 'POST': form = DocumentForm(request.POST, request.FILES) if form.is_valid(): log = logging.getLogger("wa") log.info("Upload Book :") log.info(request.POST['language']) b = Book(lang = Language.objects.get(langName = request.POST.get("language", "")), author = request.POST.get("author", ""), bookName = request.POST.get("bookName", ""), shouldConcatAudio = True, shouldConcatDigi = True) b.save() user_id = request.user.id uh = UserHistory(user = CustomUser.objects.get(pk = user_id), action = 'up', uploadedBook = b) uh.save() # add points user = CustomUser.objects.get(pk = user_id) user.points = user.points + pointsToAward("up") user.save() newdoc = Document(docfile = request.FILES['docfile']) #newdoc.docfile.save(str(b.id) + "/original/originalBook.pdf", request.FILES['docfile'], save=False) newdoc.docfile.save(str(b.id), request.FILES['docfile'], save=False) a = default_storage.open("documents/"+str(b.id)) local_fs = FileSystemStorage(location='/tmp/pdf') local_fs.save(a.name,a) #b = default_storage.save(str(b.id) + "/original/originalBook.pdf",a) #default_storage.close("documents/"+str(b.id)) log.info((a.name)) mod_path = "/tmp/pdf/"+a.name f = open(mod_path, 'rb') myfile = File(f) new_name =str(b.id) + "/original/originalBook.pdf" default_storage.save(new_name,myfile) os.remove(mod_path) #--TODO--add it to user history #splitBookIntoPages(str(b.id) + "/original/originalBook.pdf") uploadSplitBookIntoGridFS.delay( str(b.id) + "/original/originalBook.pdf", b.id) # Redirect to the document list after POST #delete the file from default storage default_storage.delete("documents/"+ str(b.id)) return HttpResponseRedirect(reverse('wa.views.audioSelection')) else: form = DocumentForm() # A empty, unbound form #langs = Languages.objects.all() # Render list page with the documents and the form langs = Language.objects.all() return render_to_response( 'wa/uploadBook.html', {'form': form,'langs':Language.objects.all()}, context_instance=RequestContext(request) ) else : return render_to_response('/wa')
def getImage(request, book_id): response = HttpResponse(mimetype = "image/jpg") path_to_save = str(book_id) +"/bookThumbnail.png" a = default_storage.open(path_to_save) local_fs = FileSystemStorage(location='/tmp/pdf') local_fs.save(a.name,a) image = Image.open("/tmp/pdf/"+a.name) image.save(response, 'png') local_fs.delete(a.name) return response
def upload(self, files, dest): try: for _file in list(files): path = os.path.join(self.root, dest.replace('/', '', 1)) if not path.startswith(self.root): return {'result': {'success': 'false', 'error': 'Invalid path'}} fs = FileSystemStorage(location=path) fs.save(files.get(_file).name, files.get(_file)) except Exception as e: return {'result': {'success': 'false', 'error': e.message}} return {'result': {'success': 'true', 'error': ''}}
def handle(self, *args, **options): location = self.get_location() file = 'reverse.js' fs = FileSystemStorage(location=location) if fs.exists(file): fs.delete(file) default_urlresolver = urlresolvers.get_resolver(None) content = generate_js(default_urlresolver) fs.save(file, ContentFile(content)) if len(sys.argv) > 1 and sys.argv[1] in ['collectstatic_js_reverse']: self.stdout.write('js-reverse file written to %s' % (location)) # pragma: no cover
def splitBookIntoPages(f_arg, book_id): #print 'splitbook' rxcountpages = re.compile(r"$\s*/Type\s*/Page[/\s]", re.MULTILINE|re.DOTALL) #--TODOJO--Change this depending on your system path sys.path.append('/home/jo/wikiaudia/') #sys.path.append('/home/jo/wikiaudia/wa/') os.environ['DJANGO_SETTINGS_MODULE']='wikiaudia.settings' log = logging.getLogger("wa") #log.info("hiiii") log.info(f_arg) print f_arg print "Before IF" if default_storage.exists(f_arg): print "splitbook file exists" a = default_storage.open(f_arg) local_fs = FileSystemStorage(location='/tmp/pdf') local_fs.save(a.name,a) mod_path = "/tmp/pdf/"+ f_arg print mod_path data = file(mod_path,"rb").read() #log.info(file(mod_path,"rb").size()) no_pages = len(rxcountpages.findall(data)) file_for = mod_path+"[%d]" #--TODOJO--save the image in the path as required. Cuurently just stores as temp[i]. print no_pages continueConversion = True i = 0 _img = Image(filename=file_for) while continueConversion: filen = file_for%i with Image(filename=filen) as img: if img: print type(img) i=i+1 img.save(filename=("temp[%d].png"%i)) else: continueConversion = False with open("temp[%d].png"%i, 'r') as f: myfile = File(f) para = Paragraph(book = Book.objects.get(pk = book_id)) para.save() print "para ID: " + str(para.id) path_to_save = str(book_id) + "/chunks/" + str(para.id) + "/image.png" default_storage.save(path_to_save, myfile) os.remove("temp[%d].png"%i) os.remove(mod_path) else: print "doesn't exist"
def handle(self, *args, **options): if not hasattr(settings, 'STATIC_ROOT') or not settings.STATIC_ROOT: raise ImproperlyConfigured('The collectstatic_js_reverse command needs settings.STATIC_ROOT to be set.') location = os.path.join(settings.STATIC_ROOT, 'django_js_reverse', 'js') file = 'reverse.js' fs = FileSystemStorage(location=location) if fs.exists(file): fs.delete(file) content = urls_js() fs.save(file, ContentFile(content)) if len(sys.argv) > 1 and sys.argv[1] in ['collectstatic_js_reverse']: self.stdout.write('js-reverse file written to %s' % (location)) # pragma: no cover
def setUp(self): test_storage = FileSystemStorage( location=getattr(settings, 'FILE_UPLOAD_TEMP_DIR')) fixtures_storage = FileSystemStorage(location=CHUNKS_ROOT) for filename in fixtures_storage.listdir('.')[1]: test_storage.save( filename, fixtures_storage.open(filename) ) self.seagull = ResumableFile(test_storage, seagull) self.craw = ResumableFile(test_storage, craw) self.storage = test_storage
def handle_noargs(self, **options): # Force django to calculate template_source_loaders try: find_template('notexists') except TemplateDoesNotExist: pass from django.template.loader import template_source_loaders loaders = [] for loader in template_source_loaders: if isinstance(loader, CachedLoader): loaders.extend(loader.loaders) else: loaders.append(loader) paths = set() for loader in loaders: paths.update(list(loader.get_template_sources(''))) templates = list() for path in paths: for root, dirs, files in os.walk(path): for name in files: if not name.startswith(('.', '_')) and name.endswith('.xml'): templates.append(dict({ 'root': path, 'file': os.path.join(root[len(path)+1:], name) })) storage = FileSystemStorage(settings.FEST_TEMPLATES_ROOT) print "Compile templates:" for template in templates: template_file = os.path.join(template['root'], template['file']) filename = template['file'] content = open(template_file) try: tpl = Template(content.read().decode(settings.FILE_CHARSET), template_file=template_file, template_name=filename) tpl.compile() compiled = ContentFile(tpl.template_string) finally: content.close() filename = '%s.js' % filename[:-4] print "%s -> %s" % (template['file'], filename) storage.delete(filename) storage.save(filename, compiled)
class ContentFileStorageTestCase(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_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"))
def upload(request):#ajax upload file to a question or answer """view that handles file upload via Ajax """ # check upload permission result = '' error = '' new_file_name = '' try: #may raise exceptions.PermissionDenied if request.user.is_anonymous(): msg = _('Sorry, anonymous users cannot upload files') raise exceptions.PermissionDenied(msg) request.user.assert_can_upload_file() # check file type f = request.FILES['file-upload'] file_extension = os.path.splitext(f.name)[1].lower() if not file_extension in settings.ASKBOT_ALLOWED_UPLOAD_FILE_TYPES: file_types = "', '".join(settings.ASKBOT_ALLOWED_UPLOAD_FILE_TYPES) msg = _("allowed file types are '%(file_types)s'") % \ {'file_types': file_types} raise exceptions.PermissionDenied(msg) # generate new file name new_file_name = str( time.time() ).replace( '.', str(random.randint(0,100000)) ) + file_extension file_storage = FileSystemStorage( location = settings.ASKBOT_FILE_UPLOAD_DIR, base_url = reverse('uploaded_file', kwargs = {'path':''}), ) # use default storage to store file file_storage.save(new_file_name, f) # check file size # byte size = file_storage.size(new_file_name) if size > settings.ASKBOT_MAX_UPLOAD_FILE_SIZE: file_storage.delete(new_file_name) msg = _("maximum upload file size is %(file_size)sK") % \ {'file_size': settings.ASKBOT_MAX_UPLOAD_FILE_SIZE} raise exceptions.PermissionDenied(msg) except exceptions.PermissionDenied, e: error = unicode(e)
def avatar_view(request): if request.method == 'GET': form = UploadFileForm() return render(request, 'avatar.html', {'form': form}) elif request.method == 'POST': form = UploadFileForm(request.POST, request.FILES) if form.is_valid(): myfile = request.FILES['user_file'] fs = FileSystemStorage(location='social_network/static/img/') myfile.name = request.user.username + '-' + myfile.name fs.save(myfile.name, myfile) return redirect(reverse('user_page')) return render(request, 'avatar.html', {'form': form})
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 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 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 )
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 getParagraph(request, book_id): response = HttpResponse(mimetype = "image/jpg") ''' image = Image.open(os.path.dirname(settings.BASE_DIR) + "/" + "wastore/" + book_id + "/" + "frontcover.jpg") image.save(response, 'png') ''' b = Book.objects.get(pk = book_id) para = Paragraph.objects.filter(book = b, audioReadBy__isnull = True)[0] #Should be server by nginx-gridfs path_to_save = str(book_id) + "/chunks/" + str(para.id) + "/image.png" a = default_storage.open(path_to_save) local_fs = FileSystemStorage(location='/tmp/pdf') local_fs.save(a.name,a) image = Image.open("/tmp/pdf/"+a.name) image.save(response, 'png') return response
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)
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 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'))
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)
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})
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 adminstudentsupdate(request): try: if request.method == 'POST': dict_keys = dict(json.loads(request.body)) fullname = dict_keys.get('fullname') hometown = dict_keys.get('hometown') birthday = dict_keys.get('birthday') school = dict_keys.get('school') graduationtime = dict_keys.get('graduationtime') face = dict_keys.get('face') email = dict_keys.get('email') phone = dict_keys.get('phone') # coursecode = dict_keys.get('coursecode') # studentcode = dict_keys.get('studentcode') accountcode = dict_keys.get('accountcode') changebirthday = dict_keys.get('changebirthday') changegraduationtime = dict_keys.get('changegraduationtime') namefile = dict_keys.get('namefile') changeimage = dict_keys.get('changeimage') idrow = dict_keys.get('idrow') if changeimage: Student(pk=idrow).deleteimage() formatd, imgstr = face.split(';base64,') ext = formatd.split('/')[-1] data = ContentFile(base64.b64decode(imgstr), name=namefile) link = "student/%02d/%02d/%02d" % (datetime.today().year, datetime.today().month, datetime.today().day) fs = FileSystemStorage( location=os.path.join(settings.MEDIA_ROOT, link), base_url=os.path.join(settings.MEDIA_ROOT, link)) filename = fs.save(data.name, data) if changebirthday and changegraduationtime: resbirthday = datetime.strptime(birthday, "%Y-%m-%dT%H:%M:%S.%fZ") strbirthday = '%02d-%02d-%02d' % ( resbirthday.year, resbirthday.month, resbirthday.day) resgraduationtime = datetime.strptime( graduationtime, "%Y-%m-%dT%H:%M:%S.%fZ") strgraduationtime = '%02d-%02d-%02d' % ( resgraduationtime.year, resgraduationtime.month, resgraduationtime.day) student = Student.objects.filter(pk=idrow).first() student.fullname = fullname student.hometown = hometown student.birthday = strbirthday student.school = school student.graduationtime = strgraduationtime student.face = os.path.join(link, filename) student.email = email student.phone = phone student.accountcode = accountcode student.saveupdate() elif changebirthday: resbirthday = datetime.strptime(birthday, "%Y-%m-%dT%H:%M:%S.%fZ") strbirthday = '%02d-%02d-%02d' % ( resbirthday.year, resbirthday.month, resbirthday.day) student = Student.objects.filter(pk=idrow).first() student.fullname = fullname student.hometown = hometown student.birthday = strbirthday student.school = school student.face = os.path.join(link, filename) student.email = email student.phone = phone student.accountcode = accountcode student.saveupdate() elif changegraduationtime: resgraduationtime = datetime.strptime( graduationtime, "%Y-%m-%dT%H:%M:%S.%fZ") strgraduationtime = '%02d-%02d-%02d' % ( resgraduationtime.year, resgraduationtime.month, resgraduationtime.day) student = Student.objects.filter(pk=idrow).first() student.fullname = fullname student.hometown = hometown student.school = school student.graduationtime = strgraduationtime student.face = os.path.join(link, filename) student.email = email student.phone = phone student.accountcode = accountcode student.saveupdate() elif not changebirthday and not changegraduationtime: student = Student.objects.filter(pk=idrow).first() student.fullname = fullname student.hometown = hometown student.school = school student.face = os.path.join(link, filename) student.email = email student.phone = phone student.accountcode = accountcode student.saveupdate() if not changeimage: if changebirthday and changegraduationtime: resbirthday = datetime.strptime(birthday, "%Y-%m-%dT%H:%M:%S.%fZ") strbirthday = '%02d-%02d-%02d' % ( resbirthday.year, resbirthday.month, resbirthday.day) resgraduationtime = datetime.strptime( graduationtime, "%Y-%m-%dT%H:%M:%S.%fZ") strgraduationtime = '%02d-%02d-%02d' % ( resgraduationtime.year, resgraduationtime.month, resgraduationtime.day) student = Student.objects.filter(pk=idrow).first() student.fullname = fullname student.hometown = hometown student.birthday = strbirthday student.school = school student.graduationtime = strgraduationtime student.email = email student.phone = phone student.accountcode = accountcode student.save() elif changebirthday: resbirthday = datetime.strptime(birthday, "%Y-%m-%dT%H:%M:%S.%fZ") strbirthday = '%02d-%02d-%02d' % ( resbirthday.year, resbirthday.month, resbirthday.day) student = Student.objects.filter(pk=idrow).first() student.fullname = fullname student.hometown = hometown student.birthday = strbirthday student.school = school student.email = email student.phone = phone student.accountcode = accountcode student.save() elif changegraduationtime: resgraduationtime = datetime.strptime( graduationtime, "%Y-%m-%dT%H:%M:%S.%fZ") strgraduationtime = '%02d-%02d-%02d' % ( resgraduationtime.year, resgraduationtime.month, resgraduationtime.day) student = Student.objects.filter(pk=idrow).first() student.fullname = fullname student.hometown = hometown student.school = school student.graduationtime = strgraduationtime student.email = email student.phone = phone student.accountcode = accountcode student.save() elif not changebirthday and not changegraduationtime: student = Student.objects.filter(pk=idrow).first() student.fullname = fullname student.hometown = hometown student.school = school student.email = email student.phone = phone student.accountcode = accountcode student.save() return HttpResponse(content="", content_type='application/json', status=200) except Exception: pass
class ResumableFile(object): """A resumable file controls getting pieces of a file""" upload_root = getattr(settings, 'UPLOAD_ROOT', None) name_template = "part_%(resumableChunkNumber)04d.tmp" def __init__(self, user, kwargs): self.kwargs = kwargs self.user = user if not self.upload_root: raise ImproperlyConfigured('You must set UPLOAD_ROOT in settings') self.storage = FileSystemStorage(location=self.upload_dir) @property def chunk_exists(self): """Checks if the requested chunk exists.""" name = self.name_template % self.kwargs if not self.storage.exists(name): return False chunk_size = int(self.kwargs.get('resumableCurrentChunkSize')) return self.storage.size(name) == chunk_size def chunk_names(self): """Iterates over all stored chunks and yields their names.""" try: return sorted(self.storage.listdir('')[1]) except OSError: return [] def chunks(self): """Yield the contents of every chunk, FileSystemStorage.save compatible""" for name in self.chunk_names(): try: yield self.storage.open(name).read() except AttributeError: raise IOError("Couldn't read {}".format(name)) def delete_chunks(self): """Remove every chunk (once complete)""" return [self.storage.delete(chunk) for chunk in self.chunk_names()] @property def filename(self): """Gets the filename.""" filename = self.kwargs.get('resumableFilename') if '/' in filename: raise Exception('Invalid filename') return filename @property def upload_dir(self): """Gets the directory to save chunks to""" directory = os.path.join(self.upload_root, str(self.user.pk), self.filename) # When orginal uploads have been deleted, re-upload them. if os.path.islink(directory) and not os.path.exists(os.readlink(directory)): try: os.unlink(directory) except FileNotFoundError: # Catch when a different thread cleaned up just as we were about to. pass # This might be a file, or a directory, or a symbolic link at this point. return directory @property def is_complete(self): """Checks if all chunks are allready stored.""" return os.path.isfile(self.upload_dir) or \ self.kwargs['resumableTotalSize'] == self.size def process_chunk(self, _file): """Process the chunks of the given file""" if not self.chunk_exists: try: self.storage.save(self.name_template % self.kwargs, _file) except AttributeError: pass # Error saving file except (IOError, OSError) as err: raise IOError("Tried to save: {}, {}, {}".format(self.name_template, self.kwargs, self.name_template % self.kwargs)) #pass # Existing file in the way def save_to(self, new_dir): """When saving all the chunks to a new directory""" filename = os.path.join(new_dir, self.filename) lock_file = filename + '.lock' if os.path.islink(self.upload_dir): # This was previously uploaded and we can relink it. if not os.path.exists(filename): linkto = os.readlink(self.upload_dir) os.symlink(linkto, filename) return if os.path.isfile(lock_file): return with open(lock_file, "w") as fhl: fhl.write("1") # Actually save the file using storage storage = FileSystemStorage(location=new_dir) storage.save(self.filename, self) # Delete all the chunks after use self.delete_chunks() # Sometimes there's arace condition if people double-click if os.path.isdir(self.upload_dir) and not os.listdir(self.upload_dir): os.rmdir(self.upload_dir) # Create a symlink for tracking and re-user if os.path.isfile(filename): os.symlink(filename, self.upload_dir) if os.path.isfile(lock_file): os.unlink(lock_file) @property def size(self): """Gets chunks size.""" size = 0 for chunk in self.chunk_names(): size += self.storage.size(chunk) return size @property def started(self): """Return the first modified datetime""" return self.get_times()[0] @property def ended(self): """Return the last modified datetime""" return self.get_times()[-1] def get_times(self): """Return a list of modified datetimes""" upload = self.upload_dir files = [] if os.path.isdir(upload): files = [os.path.join(upload, f) for f in os.listdir(upload)] elif os.path.exists(upload): files = [upload] return [fromtimestamp(os.path.getmtime(f)) for f in files]
def visual_rec_create_classifier2(request): unknown_error = 0 classifier_name_taken = 0 files_not_zip = False negative_class = None class_names = [] short_descriptons = [] long_descriptions = [] more_info_links = [] training_sets = [] training_data_dict = {} try: if request.method == 'POST': classifier_name = request.POST.get('classifier_name', 'bez_nazwy').replace(' ', '_') for i in range(1, 10): class_names.append(request.POST.get(f'class_name_{i}')) short_descriptons.append(request.POST.get(f'short_description_{i}')) long_descriptions.append(request.POST.get(f'long_description_{i}')) more_info_links.append(request.POST.get(f'more_info_link_{i}')) training_sets.append(request.FILES.get(f'training_set_{i}')) negative_class = request.FILES.get('negative_class') # filter out the Nones gotten by POST.get class_names_final = list(filter(lambda item: item is not None, class_names)) short_descriptons_final = list(filter(lambda item: item is not None, short_descriptons)) long_descriptons_final = list(filter(lambda item: item is not None, long_descriptions)) more_info_links_final = list(filter(lambda item: item is not None, more_info_links)) training_sets_final = list(filter(lambda item: item is not None, training_sets)) # check if files are .zip files files_not_zip = any([1 for file in training_sets_final if not file.name.endswith(".zip")]) # create a folder for each class if files_not_zip is False: if not os.path.isdir(f"../wip/classifiers/{classifier_name}"): os.mkdir(f"../wip/classifiers/{classifier_name}") os.chdir(f"../wip/classifiers") fs = FileSystemStorage(location=os.path.join(os.getcwd(), classifier_name)) # save each class model to a separate .txt file in class's folder for i, class_id in enumerate(class_names_final): file = open(os.path.join(classifier_name, f"{class_id}.txt"), "w") file.write(f"class_name->{class_names_final[i]}\nshort_description->{short_descriptons_final[i]}\n" f"long_description->{long_descriptons_final[i]}\n" f"more_info_link->{more_info_links_final[i]}") file.close() fs.save(f"{class_names_final[i]}.zip", training_sets_final[i]) # creates a dictionary of training data for i, class_name in enumerate(class_names_final): training_data_dict[f"{class_name}_positive_examples"] = training_sets_final[i] response = create_classifier(training_data_dict=training_data_dict, classifier_name=classifier_name, negative_class=negative_class) # create a storage for new data for each class in classifier's folder os.chdir(f"../pictures/new_training_set") if not os.path.isdir(os.path.join(os.getcwd(), classifier_name)): os.mkdir(os.path.join(os.getcwd(), classifier_name)) for class_name in class_names_final: os.mkdir(os.path.join(os.getcwd(), classifier_name, class_name)) else: classifier_name_taken = 1 return render(request, "datainput/classificator_creation_result.html", {'unknown_error': unknown_error, 'classifier_name_taken': classifier_name_taken, 'files_not_zip': files_not_zip, 'api_error': 0, 'response': None }) # if response is a string, there was an ApiException- otherwise creation was successful if isinstance(response, dict): return render(request, "datainput/classificator_creation_result.html", {'response': response, 'api_error': 0, 'unknown_error': 0, 'files_not_zip': 0, 'classifier_name_taken': 0}) else: return render(request, "datainput/classificator_creation_result.html", {'response': response, 'api_error': 1, 'unknown_error': 0, 'files_not_zip': 0, 'classifier_name_taken': 0}) else: classifier_name_taken = 1 except: unknown_error = 1 return render(request, "datainput/classificator_creation_result.html", {'unknown_error': unknown_error, 'classifier_name_taken': classifier_name_taken, 'files_not_zip': files_not_zip, 'api_error': 0, 'response': None })
def edit_student_save(request): if request.method != "POST": return HttpResponse("<h2>Method Not Allowed</h2>") else: student_id = request.session.get("student_id") if student_id == None: return HttpResponseRedirect(reverse("manage_student")) form = EditStudentForm(request.POST, request.FILES) if form.is_valid(): first_name = form.cleaned_data["first_name"] last_name = form.cleaned_data["last_name"] username = form.cleaned_data["username"] email = form.cleaned_data["email"] address = form.cleaned_data["address"] session_year_id = form.cleaned_data["session_year_id"] course_id = form.cleaned_data["course"] sex = form.cleaned_data["sex"] if request.FILES.get('profile_pic', False): profile_pic = request.FILES['profile_pic'] fs = FileSystemStorage() filename = fs.save(profile_pic.name, profile_pic) profile_pic_url = fs.url(filename) else: profile_pic_url = None try: user = CustomUser.objects.get(id=student_id) user.first_name = first_name user.last_name = last_name user.username = username user.email = email user.save() student = Students.objects.get(admin=student_id) student.address = address session_year = SessionYearModel.object.get(id=session_year_id) student.session_year_id = session_year student.gender = sex course = Courses.objects.get(id=course_id) student.course_id = course if profile_pic_url != None: student.profile_pic = profile_pic_url student.save() del request.session['student_id'] messages.success(request, "Successfully Edited Student") return HttpResponseRedirect( reverse("edit_student", kwargs={"student_id": student_id})) except: messages.error(request, "Failed to Edit Student") return HttpResponseRedirect( reverse("edit_student", kwargs={"student_id": student_id})) else: form = EditStudentForm(request.POST) student = Students.objects.get(admin=student_id) return render(request, "hod_template/edit_student_template.html", { "form": form, "id": student_id, "username": student.admin.username })
def bulk_upload(request): if not request.user.is_authenticated: return render(request, 'students/login_form.html') else: template = 'students/bulk_upload.html' global html_msg, uploaded_file_url, sheet, num_students, database_loc if request.method == 'POST' and 'submit_file' in request.POST: myfile = request.FILES['myfile'] fs = FileSystemStorage( location=os.path.join(settings.MEDIA_ROOT, 'uploads'), base_url=os.path.join(settings.MEDIA_URL, 'uploads')) filename = fs.save(myfile.name, myfile) uploaded_file_url = fs.url(filename) database_loc = os.path.join(settings.MEDIA_ROOT, 'uploads', filename) return render(request, template, {'uploaded_file_url': uploaded_file_url}) if "next" in request.POST: workbook = xlrd.open_workbook(database_loc) sheet = workbook.sheet_by_index(0) num_students = sheet.nrows - 1 html_msg = "You are about to Create %d New Student Profiles" % num_students context = { 'uploaded_file_url': uploaded_file_url, 'msg': html_msg, } return render(request, template, context) if "execute" in request.POST: objects = [] for i in range(num_students): i += 1 row = sheet.row_values(i) admno = row[0] gender = row[1] classcode = row[2] if len(classcode) == 2: grade = classcode[0] class_designation = classcode[1] else: grade = classcode[:2] class_designation = classcode[2] fullname = row[3] first = fullname.split()[1:] last = fullname.split()[0] first_names = "" for n in first: first_names = first_names + n + " " first_names = first_names[:-1] objects.append( Student(student_admno=admno, student_gender=gender, student_classcode=classcode, student_grade=grade, student_class_designation=class_designation, student_fullname=fullname, student_firstname=first_names, student_lastname=last)) Student.objects.bulk_create(objects) html_msg_1 = "%d New Student Profiles Created" % num_students context = { 'uploaded_file_url': uploaded_file_url, 'msg': html_msg, 'msg_1': html_msg_1, } return render(request, template, context) return render( request, template, )
def post(request): # POST KE SEND MISHE, MAN URL E POSTO TOYE TELEGRAMO INSTA MIKHAM! VASE SAVE user = request.user if 'telegram' in request.POST: request.session['telegram_id'] = request.POST['telegram-id'] telegram_id = request.session.get('telegram_id', None) if 'insta' in request.POST: request.session['insta_id'] = request.POST['insta-un'] request.session['insta_pw'] = request.POST['insta-pw'] insta_id = request.session.get('insta_id', None) if 'insta-remove' in request.POST: request.session['insta_id'] = None if 'tele-remove' in request.POST: request.session['telegram_id'] = None # try: # twitter_api = get_twitter_api(request) # # user = twitter_api.me() # except tweepy.TweepError: # return render(request, 'twitter_auth/vpn.html') if 'post' in request.POST: form = PostForm(request.POST, request.FILES) if form.is_valid(): post = form.save(commit=False) post.published_date = timezone.now() post.creator = request.user post.save() if post.media: # Post with media file_address = "%s/%s" % (settings.MEDIA_ROOT, post.media) fs = FileSystemStorage() filename = fs.save(post.media.name, post.media) uploaded_file_url = fs.url(filename) host = request.get_host() uploaded_file_url = host + uploaded_file_url if telegram_id != None: try: telegram_send_message(text=post.text, id=telegram_id, file_address=file_address, file_url=uploaded_file_url) except: pass # telegram_send_message(uploaded_file_url, text=post.text, id=request.session['telegram_id'], file_address=file_address) if insta_id != None: try: instagram_send_message(request, text=post.text, file=file_address) except: pass if 'twitter' in request.session != None: pass # twitter_api.update_with_media(filename=file_address, status=post.text) # os.remove(file_address) else: # Post only with text if telegram_id != None: try: telegram_send_message(text=post.text, id=telegram_id) except: pass if 'twitter' in request.session: pass # twitter_api.update_status(status=post.text) else: print('invalid form') post = Post(text='') else: form = PostForm() post = Post(text='') # return render(request, 'twitter_auth/info.html', {'user': user, 'post': post, 'form': form, 'teleid': request.session['telegram_id']}) return render( request, 'twitter_auth/info.html', { 'post': post, 'form': form, 'teleid': telegram_id, 'instaid': insta_id, 'user': user })
def store_image(image): fs=FileSystemStorage() file=fs.save(image.name,image) file_url=fs.url(file) return file_url
def add_news(request): categories = Categories.objects.all() subcategories = SubCategories.objects.all() try: if request.method == 'POST' and request.FILES['article_picture']: #print ('Form data has been sent suffesfully... ') category_id = request.POST.get('article_category') subcategory_id = request.POST.get('article_subcategory') article_name = request.POST.get('article_name') article_short = request.POST.get('article_short') article_author = request.POST.get('article_author') article_date = utc.to('US/Eastern').format('MM/DD/YYYY | HH:mm:ss A') #article_picture = request.POST.get('article_picture') #article_category = request.POST.get('article_category') article_body = request.POST.get('article_body') if article_name == "" or article_short == "" or article_body == "": errors = "All fields are required" return render(request, 'backend/pages/errors/errors.html', {'errors': errors}) else: try: article_percentage = 100 article_status = 'success' image_file = request.FILES.get('article_picture') fs = FileSystemStorage() filename = fs.save(image_file.name, image_file) image_url = fs.url(filename) if str(image_file.content_type).startswith("image"): print(image_file.size) if image_file.size < 5000000: success = "News post has been created succesfully" imageUpload = "Image uploaded succesfully " + image_file.name category_name = Categories.objects.get(pk=category_id).category_name subcategory_name = SubCategories.objects.get(pk=subcategory_id).subcategory_name send_form = News( article_name=article_name, article_short=article_short, article_body=article_body, article_category=category_name, article_subcategory=subcategory_name, article_date=article_date, article_picture_name=filename, article_picture_url=image_url, article_author=article_author, article_percentage=article_percentage, article_status=article_status, article_live=True, article_image_size=image_file.size, ) send_form.save() return render(request, 'backend/pages/list/success.html', {'success': success, 'imageUpload': imageUpload}) else: print('Image size greater than 5MB') errorsImage = "You file is not supported excedd the limit of 5MB. File size: " + str(humanbytes(image_file.size)) return render(request, 'backend/pages/list/errorsImage.html', {'errorsImage': errorsImage}) else: fs = FileSystemStorage() fs.delete(filename) errorsImage = "You file is not supported only image files. " + image_file.content_type.split('/')[-1].upper() + " invalid." return render(request, 'backend/pages/errors/errorsImage.html', {'errorsImage': errorsImage}) except: errorsImage = "Please select an Image" + image_file.size return render(request, 'backend/pages/errors/errorsImage.html', {'errorsImage': errorsImage}) return render(request, 'backend/pages/list/add.html', {'categories': categories, 'subcategories': subcategories}) except: errorsImage = "Please select an Image" return render(request, 'backend/pages/errors/errorsImage.html', {'errorsImage': errorsImage})
def index(request): if 'user_id' not in request.session: return redirect(settings.BASE_URL) else: # project add code Start if request.method == "POST": update = request.POST.get('update') submit = request.POST.get('submit') project_id = request.POST['project_id'] upload_file = request.FILES.get('project_map_value', False) # pro_map=request.POST['pro_map'] today = datetime.now() val = today.strftime("_%d_%m_%Y_%H_%M_%S_%f") if upload_file is not False: filename = upload_file.name splitedfilename = filename.split(".") length_of_filename = len(splitedfilename) file_extention = splitedfilename[length_of_filename - 1] upload_file_name = str(val) + "." + file_extention fs = FileSystemStorage() fs.save(upload_file_name, upload_file) im = Image.open(upload_file) width = im.size[0] pro_map = project_id + str(val) + "." + file_extention height = im.size[1] else: if request.POST['pro_map_old'] != "": upload_file_name = request.POST['pro_map_old'] width = request.POST['width_old'] height = request.POST['height_old'] else: upload_file_name = "" width = 0 height = 0 project_name = request.POST.getlist('project_name_project') ids = str(project_name).split("'") project_name = ids[1] project_country = request.POST['project_country'] lat = request.POST['lat'] long = request.POST['long'] project_city = request.POST['project_city'] if project_city is None: project_city = "" if project_country is None: project_country = "" if upload_file_name is None: upload_file = "" if request.POST["add_project_id"] == "": entry_user = AddProject(project_name=project_name, project_id=project_id, project_country=project_country, project_city=project_city, width=width, height=height, lat=lat, long=long, date=datetime.now(), upload_map=upload_file_name, create_by=request.session['user_id']) entry_user.save() data_id = AddProject.objects.get(project_id=project_id) # return HttpResponse(data_id.id) current_project_id = str(data_id.id) if request.session['user_type'] == "Sub Admin": entry_project = ProjectMap( project_id=current_project_id, user_type=request.session['user_type'], user_id=request.session['user_id'], date=datetime.now()) entry_project.save() return redirect(settings.BASE_URL + 'dashboard/' + str(current_project_id)) else: if AddProject.objects.filter( id=request.POST["add_project_id"]).exists(): AddProject.objects.filter( id=request.POST["add_project_id"]).update( project_name=project_name, project_country=project_country, project_city=project_city, width=width, height=height, lat=lat, long=long, upload_map=upload_file_name, date=datetime.now()) # ProjectMap.objects.filter(project_id=request.POST["add_project_id"]).update(project=project_name) # project add code End user_info = UserRegistration.objects.get(id=request.session["user_id"]) project_data = AddProject.objects.all().order_by("project_name") return render( request, "project/index.html", { "BASE_URL": settings.BASE_URL, "user_info": user_info, "project_data": project_data, "user_type": request.session["user_type"], "temp_user_type": request.session["temp_user_type"] })
def get_file(myfile): fs = FileSystemStorage() filename = fs.save(myfile.name, myfile) return filename
def add_student_save(request): if request.method != "POST": messages.error(request, "Invalid Method") return redirect('add_student') else: form = AddStudentForm(request.POST, request.FILES) if form.is_valid(): first_name = form.cleaned_data['first_name'] last_name = form.cleaned_data['last_name'] username = form.cleaned_data['username'] email = form.cleaned_data['email'] password = form.cleaned_data['password'] myconfirm_password = form.cleaned_data['myconfirm_password'] address = form.cleaned_data['address'] session_year_id = form.cleaned_data['session_year_id'] course_id = form.cleaned_data['course_id'] gender = form.cleaned_data['gender'] # Getting Profile Pic first # First Check whether the file is selected or not # Upload only if file is selected if len(request.FILES) != 0: profile_pic = request.FILES['profile_pic'] fs = FileSystemStorage() filename = fs.save(profile_pic.name, profile_pic) profile_pic_url = fs.url(filename) else: profile_pic_url = None try: user = CustomUser.objects.create_user(username=username, password=password, email=email, first_name=first_name, last_name=last_name, user_type=3) user.students.address = address user.students.myconfirm_password = myconfirm_password course_obj = Courses.objects.get(id=course_id) user.students.course_id = course_obj session_year_obj = SessionYearModel.objects.get( id=session_year_id) user.students.session_year_id = session_year_obj user.students.gender = gender user.students.profile_pic = profile_pic_url user.save() messages.success(request, "Student Added Successfully!") #Send Email send_mail( 'Your application is approved!', f'Hi {user.username}, here is your login id ({user.email}) and Password ({user.students.myconfirm_password}), thank you for registering in Rajarshi College.', settings.EMAIL_HOST_USER, [user.email], fail_silently=False, ) return redirect('add_student') except: messages.error(request, "Failed to Add Student!") return redirect('add_student') else: return redirect('add_student')
def register_realtor(request): if request.method == 'POST': first_name=request.POST['first_name'] last_name=request.POST['last_name'] username=request.POST['username'] email=request.POST['email'] phone=request.POST['phone'] password=request.POST['password'] password2=request.POST['password2'] photo=request.FILES['photo'] pattern="[a-z]+[A-Z]+[a-z|A-Z|@|#|0-9]+[a-z|A-Z|@|#|0-9]+[a-z|A-Z|@|#|0-9]+[a-z|A-Z|@|#|0-9]+[a-z|A-Z|@|#|0-9]+[a-z|A-Z|@|#|0-9]+" if password == password2: if match(pattern,password): if CustomUser.objects.filter(username=username).exists(): messages.error(request,'usernmae exsits') return redirect('register_realtor') else: if CustomUser.objects.filter(email=email).exists(): messages.error(request,'email exsits') return redirect('register_realtor') else: fs=FileSystemStorage() name=fs.save(photo.name,photo) user=CustomUser.objects.create_user(username=username,password=password,first_name=first_name,last_name=last_name,email=email,phone=phone) user.is_active=False user.is_realtor=True user.save() user_r=Realtor(name=first_name+last_name,email=email,phone=phone,photo=photo.name,user_id=user.id) #auth.login(request,user) #messages.success(request,'you are now logged in') #return redirect('index') user_r.save() current_site = get_current_site(request) email_subject = 'Activate Your Account' message_tosend = render_to_string('accounts/activate_account.html', { 'user': user, 'domain': current_site.domain, 'uid': urlsafe_base64_encode(force_bytes(user.id)), 'token': account_activation_token.make_token(user), }) send_mail( email_subject, message_tosend,'*****@*****.**',[email], fail_silently=False ) messages.success(request,'check your email for activating your account') return redirect('login_r') else: messages.error(request,'password must contain atleast one uppercase and one digit') return redirect('register_realtor') else: messages.error(request,'password not match') return redirect('register_realtor') else: return render(request,'accounts/register_realtor.html')
def task(request, task_category=None, task_id=-1, tasks_olimpic=None): current_task = get_object_or_404(Task, pk=task_id) current_user = request.user tries = None user_full_success = "" # If user login if current_user.id is not None: tries = TaskComplete.objects.filter( user=SimpleUser.objects.get(pk=current_user.id), task=task_id).order_by('-time_upload')[:15] tries_success = TaskComplete.objects.filter( user=SimpleUser.objects.get(pk=current_user.id), task=task_id, user_success=True) # If user didn't done this task if tries_success.__len__() == 0: user_full_success = False # Else else: user_full_success = True if request.method == 'POST': filename = '' # Create object of Filesystem and input its location fs = FileSystemStorage( location="Contester/media/uploads/tasks/usertaskfile") upload_file = "uploads/tasks/usertaskfile/" # If user upload the script by file input if request.FILES.__len__() != 0: myfile = request.FILES['file'] filename = fs.save(myfile.name, myfile) upload_file += filename # If user didn't upload the script by file input but write it in code editor textarea elif request.POST['code'] != "": filename = get_random_string(length=32) + ".py" file = open(fs.location + "\\" + filename, 'w+t') code = request.POST['code'] upload_file += filename file.write(code) file.close() # If all above is empty else: empty = True if tasks_olimpic is not None: task_olimpic = Olimpic.objects.get(id=tasks_olimpic) date = task_olimpic.time_olimpic olimpic_started = False if date > timezone.now( ) and task_olimpic.start_olimpic == True: olimpic_started = True return render( request, "Contester/task.html", { "task": current_task, "tries": tries, "empty": empty, "user_full_success": user_full_success, "tasks_olimpic": tasks_olimpic, "olimpic_started": olimpic_started }) else: return render( request, "Contester/task.html", { "task": current_task, "tries": tries, "empty": empty, "user_full_success": user_full_success }) # check file compilation success = True error = "" # If task input variables exist if current_task.task_txt != "": # Copy all input variables in line to bytes-like string with open(current_task.task_txt.path) as fp: line = fp.readline() while line: d = line.split(" ") c = bytes() for letter in d: c += bytes(letter + '\n', encoding='utf-8') # Create two Popen to interact with script and save output in answer p = Popen(['python', fs.path(filename), 'f'], stdout=PIPE, stdin=PIPE, stderr=STDOUT) file_stdout = p.communicate(input=c)[0] answer_user = str(file_stdout.decode()) p = Popen(['python', current_task.task_file.path, 'f'], stdout=PIPE, stdin=PIPE, stderr=STDOUT) start = time.time() file_stdout = p.communicate(input=c)[0] finish = time.time() - start answer_server = str(file_stdout.decode()) # If answers is equal if answer_server == answer_user: success = True # If answer contains Compilation error elif answer_user.find("Error") > 0: success = False error = "Compilation error" break # If answer's time compilation is more than 1 second elif finish > 1: success = False error = "Time error" break # If answers is wrong else: success = False error = "Wrong answer" break line = fp.readline() # If task input variables not exist else: # Create two Popen to interact with script and save output in answer p = Popen(['python', fs.path(filename), 'f'], stdout=PIPE, stdin=PIPE, stderr=STDOUT) file_stdout = p.communicate()[0] answer_user = str(file_stdout.decode()) p = Popen(['python', current_task.task_file.path, 'f'], stdout=PIPE, stdin=PIPE, stderr=STDOUT) start = time.time() file_stdout = p.communicate()[0] finish = time.time() - start answer_server = str(file_stdout.decode()) if answer_server == answer_user: success = True elif answer_user.find("Error") > 0: success = False error = "Compilation error" elif finish > 1: success = False error = "Time error" else: success = False error = "Wrong answer" # Save the answer task_complete = TaskComplete( task=Task.objects.get(pk=task_id), user=SimpleUser.objects.get(id=request.POST["userid"]), user_success=success, error_type=error, user_file=upload_file) # if task is olimpic if tasks_olimpic is not None: task_olimpic = Olimpic.objects.get(id=tasks_olimpic) date = task_olimpic.time_olimpic # if time of olimpic is not over and it's started if date > timezone.now() and task_olimpic.start_olimpic: task_complete.save() # if user success then we save the result in OlimpicComplete # and when olimpic is over we show it in leaderbord if task_complete.user_success: olimpic_complete = OlimpicComplete.objects.filter( task_olimpic=task_olimpic, user=SimpleUser.objects.get(id=request.user.id)) # if olimpic result is not exits we create new one if olimpic_complete.count() < 1: tasks_by_olimpic = Task.objects.filter( task_olimpic_category=task_olimpic, task_speed_run=True) tasks_sum = tasks_by_olimpic.count() progress = 0 for task_item in tasks_by_olimpic: query = TaskComplete.objects.filter( user=SimpleUser.objects.get( id=current_user.id), user_success=True, task=task_item.id) if query.count() > 0: progress += 1 progress = int((progress / tasks_sum) * 100) olimic_new = OlimpicComplete( task_olimpic=task_olimpic, user=SimpleUser.objects.get(id=request.user.id), time_complete=timezone.now(), grade=progress) olimic_new.save() # else we change existing olimpic results else: olimpic_complete_change = None for olimpic_complete_item in olimpic_complete: olimpic_complete_change = olimpic_complete_item tasks_by_olimpic = Task.objects.filter( task_olimpic_category=task_olimpic, task_speed_run=True) tasks_sum = tasks_by_olimpic.count() progress = 0 for task_item in tasks_by_olimpic: query = TaskComplete.objects.filter( user=SimpleUser.objects.get( id=current_user.id), user_success=True, task=task_item.id) if query.count() > 0: progress += 1 progress = int((progress / tasks_sum) * 100) olimpic_complete_change.grade = progress olimpic_complete_change.time_complete = timezone.now() olimpic_complete_change.save() else: task_complete.save() # if task is olimpic if tasks_olimpic is not None: task_olimpic = Olimpic.objects.get(id=tasks_olimpic) date = task_olimpic.time_olimpic olimpic_started = False # if olimpic is started then we show upload form if date > timezone.now() and task_olimpic.start_olimpic: olimpic_started = True return render( request, "Contester/task.html", { "task": current_task, "tries": tries, "user_full_success": user_full_success, "tasks_olimpic": tasks_olimpic, "olimpic_started": olimpic_started }) else: return render( request, "Contester/task.html", { "task": current_task, "tries": tries, "user_full_success": user_full_success })
def monpos(request, *args, **kwargs): up_files,site,deactive,ant,rec, year, gnss_list=[],[],[],[],[],[],[] # select = "select antenna from antennas order by antenna asc" select = "select antenas from antenna order by antenas asc" antennas=sqlutga(select,'') for antenna in antennas: ant.append(antenna[0]) select = "select receiver from receiver order by receiver asc" receivers=sqlutga(select,'') for receiver in receivers: rec.append(receiver[0]) select = "select distinct (year) from ref_rinex" years=sqlutga(select,'') for yr in years: year.append(yr[0]) year.sort() select = "select site from ref_coord_cartesian where status=%s" sites=sqlutga(select,['mgl']) for st in sites: site.append(st[0]) site.sort() context= { 'ant':ant, 'rec':rec, 'year':year, 'site':site, } if request.method == 'POST': os.system('rm /Project/django/monpos/media/*') suljee = request.POST.getlist('suljee') height = request.POST.getlist('height[]') receiver = request.POST.getlist('receiver[]') antenna = request.POST.getlist('antenna[]') mail = request.POST.getlist('mail') antenna = request.POST.getlist('antenna[]') add_gnss=request.POST.getlist('gnss') gnss_list=['1']+add_gnss unuudur=date.today() gnss_list=[''.join(gnss_list)] directory=randomString() messages=(mail,gnss_list, antenna, receiver, height, suljee,directory) for count,up_file in enumerate(request.FILES.getlist('myfile[]')): if str(up_file).endswith("o") != True and str(up_file).endswith("O") != True: return render(request, 'index.html', {'messages': "Та зөвхөн статик хэмжилтийн файл хуулах ёстой."}) else: fs = FileSystemStorage() filename = fs.save(up_file.name, up_file) ant=antenna[count][0:16].strip() dome=antenna[count][-4:].strip() header(filename,height[count],ant,dome,receiver[count],directory) up_files.append(filename) sql_cmd=sql = "Insert into user_upload (mail, gnss_option, file_name, ant_type, rec_type, height, suljee, upload_date, directory) values (%s, %s, %s, %s, %s, %s, %s, %s, %s) " zam='python3 /home/administrator/soft/sd_usno_def.py '+ str(directory) + ' ' + str(gnss_list[0]) + ' ' + str(mail[0])+ ' '+str(suljee[0]) sqlinsert(sql_cmd,[str(mail),str(gnss_list), str(up_files), str(antenna), str(receiver), str(height), str(suljee), str(unuudur), str(zam)]) for aa in up_files: os.system('/home/administrator/soft/gfzrnx_lx -finp /Project/django/monpos/media/'+ aa + ' -fout /home/administrator/mong/'+directory+'/rinex/::RX2:: -crux /Project/django/monpos/logs/monpos_header'+directory+'.txt -chk -f -q -smp 30 -split 86400 -vo 2') #os.system('cp -rf /home/administrator/github/monpos/gamit/media/*.*o ) os.chdir('/home/administrator/mong/'+directory) os.system('chmod 777 -Rf /home/administrator/mong/'+directory) gnss_list=' '.join(map(str, gnss_list)) mail=' '.join(map(str, mail)) suljee=' '.join(map(str, suljee)) onlyfiles = [f for f in os.listdir('/home/administrator/mong/'+directory+'/rinex') if f.endswith('o')] listfiles=[] for file in onlyfiles: rinexday=file[4:7] rinexyear=2000+int(file[9:11]) odruud=str(rinexday) for file2 in onlyfiles: rinexday2=file2[4:7] rinexyear2=2000+int(file2[9:11]) odruud2=str(rinexday2) if not odruud in odruud2: return render(request, 'monpos.html', {'messages':'Таны илгээсэн RINEX өгөгдлүүд дотор UTC-ийн тооллоор 1 өдөр хэмжигдээгүй өгөгдөл агуулагдсан тул боловсруулах боломжгүй байна.'}) exit() # listfiles.append[odruud] rinexheader=file[0:4] rinex_date = datetime.strptime(str(rinexyear) +" "+str(rinexday),'%Y %j') curD = datetime.now() diff=(curD-rinex_date).days if int(diff)<2: return render(request, 'monpos.html', {'messages':'Таны илгээсэн RINEX өгөгдлийг боловсруулахад шаардлагатай засварын мэдээллүүд үүсээгүй байна. Дэлгэрэнгүйг https://www.igs.org/products/ хуудаснаас танилцана уу.'}) exit() elif rinexheader in site: return render(request, 'monpos.html', {'messages':'Та Монгол Улсын GNSS-ийн байнгын ажиллагаатай станцын сүлжээний өгөгдлийг боловсруулах шаардлагагүй.'}) exit() elif int(diff)<13 and len(gnss_list)>1: return render(request, 'monpos.html', {'messages':'Өнөөдрийн байдлаар MONPOS систем таны '+str(file)+' өгөгдлийн GPS-ээс бусад дагуулын мэдээг боловсруулах боломжгүй байна. Дэлгэрэнгүйг https://www.igs.org/products/ хуудаснаас танилцана уу.'}) exit() elif rinexheader in ['XXXX','xxxx']: return render(request, 'monpos.html', {'messages':'Таны илгээсэн RINEX өгөгдлийн толгойн мэдээлэл бүрэн бус байна. Дэлгэрэнгүйг https://www.igs.org/formats-and-standards/ хуудаснаас танилцана уу.'}) exit() for i in ['autcln.cmd', 'itrf08.apr','lfile.apr','process.defaults', 'pole.usno', 'sestbl.', 'sites.defaults','sittbl.','station.info','ut1.usno']: os.system('cp /home/administrator/gg/tables/'+str(i) +' tables') try: #os.system('python3 /home/administrator/soft/sd_usno_def.py '+ str(directory) + ' ' + str(gnss_list[0]) + ' ' + str(mail[0])+ ' '+str(suljee[0])) p = subprocess.Popen(['python3', '/home/administrator/soft/sd_usno_def.py', directory, gnss_list, mail, suljee], stdout=subprocess.PIPE, stderr=subprocess.PIPE) except OSError as ex: return (ex.errno, "", ex.strerror) return render(request, 'monpos.html', {'messages':'Таны өгөгдлийг хүлээн авлаа. '+ str(mail) +' хаяг руу үр дүнг хүргүүлнэ.'}) #del request.session #return render(request, 'index.html', {'messages':messages}) return render(request, "monpos.html",context)
def giriskalite(request): mac = request.user_agent.os.family grup = request.user.grup birim = request.user.birim #Test.objects.all().delete() #Test sonuçlarını silmek için fullname = request.user.first_name + ' ' + request.user.last_name if request.method == 'POST': if request.POST.dict()['tur'] == 'basinc': veris = json.loads(request.POST.dict()['veri']) for veri in veris: t = Test(tur='basinc', seri_no=veri[0], acma=veri[1], kapatma=veri[2], kabul_durumu=veri[3], testi_yapan=fullname) t.save(force_insert=True) elif request.POST.dict()['tur'] == 'manometre': veris = json.loads(request.POST.dict()['veri']) for veri in veris: t = Test(tur='manometre', seri_no=veri[0], okunan_deger=veri[1], kabul_durumu=veri[2], testi_yapan=fullname) t.save(force_insert=True) elif request.POST.dict()['tur'] == 'altnipel': print(request.POST) kontrolResult = nipelSeriNoKontrol(request) if kontrolResult == True: if request.FILES: upload_file = request.FILES['file'] fs = FileSystemStorage() fs.save(upload_file.name, upload_file) next_lot_no = getNextLotNo(request.POST.dict()['tur']) t = Test( tur='altnipel', lot_no=next_lot_no, pdf_ismi=request.POST.get('pdf_ismi'), baslangic_seri_no=request.POST.get('baslangic_seri_no'), bitis_seri_no=request.POST.get('bitis_seri_no'), kabul_durumu=request.POST.get('kabulAlt'), testi_yapan=fullname) t.save(force_insert=True) messages.success(request, 'Alt nipel testi başarıyla kaydedildi.') elif request.POST.dict()['tur'] == 'ustnipel': print(request.POST) kontrolResult = nipelSeriNoKontrol(request) if kontrolResult == True: if request.FILES: upload_file = request.FILES['file'] fs = FileSystemStorage() fs.save(upload_file.name, upload_file) next_lot_no = getNextLotNo(request.POST.dict()['tur']) t = Test( tur='ustnipel', lot_no=next_lot_no, pdf_ismi=request.POST.get('pdf_ismi'), baslangic_seri_no=request.POST.get('baslangic_seri_no'), bitis_seri_no=request.POST.get('bitis_seri_no'), kabul_durumu=request.POST.get('kabulUst'), testi_yapan=fullname) t.save(force_insert=True) messages.success(request, 'Üst nipel testi başarıyla kaydedildi.') elif request.POST.dict()['tur'] == 'bakirmembran': print(request.POST) next_lot_no = getNextLotNo(request.POST.get('test_tur')) if request.FILES: upload_file = request.FILES['file'] fs = FileSystemStorage() fs.save(upload_file.name, upload_file) t = Test(tur=request.POST.get('test_tur'), lot_no=next_lot_no, pdf_ismi=request.POST.get('pdf_ismi'), test_basinci=request.POST.get('test_basinci'), patlama_basinci=request.POST.get('patlama_basinci'), kabul_durumu=request.POST.get('kabulBak'), testi_yapan=fullname) t.save(force_insert=True) if (request.POST.get('test_tur') == 'bakirmembran'): messages.success(request, 'Bakır membran testi başarıyla kaydedildi.') else: messages.success( request, 'Emniyet ventili testi başarıyla kaydedildi.') """ elif request.POST.get('tur') == 'emniyet': print(request.POST) if request.FILES: upload_file = request.FILES['file'] fs = FileSystemStorage() fs.save(upload_file.name,upload_file) next_lot_no = getNextLotNo( request.POST.dict()['tur']) t = Test(tur='emniyet',lot_no =next_lot_no, pdf_ismi = request.POST.get('pdf_ismi') ,test_basinci = request.POST.get('test_basinci'), patlama_basinci = request.POST.get('patlama_basinci'),kabul_durumu = request.POST.get('kabulEmn'),testi_yapan = fullname) t.save(force_insert=True) messages.success(request,'Emniyet ventili testi başarıyla kaydedildi.') """ return render(request, 'giris-kalite-kontrol.html', { 'mac': mac, 'grup': grup, 'birim': birim, 'server': server })
def assessment_creator(self, serializer): """Method responsible for creating new assessments Raises: MultiValueDictKeyError -- Raised when not enough arguments have been passed Returns: HttpResponse -- Whether resource has been successfully created or not """ try: user = self.request.user name = self.request.POST.get("name", "") course_id = self.request.POST.get("course_id", "") description = self.request.POST.get("description", "") additional_help = self.request.POST.get("additional_help", "") deadline = self.request.POST.get('deadline', "") max_time = self.request.POST.get('max_time', 0) dynamic_input = self.request.POST["dynamicInput"] static_input = self.request.POST["staticInput"] languages = [x for x in json.loads( self.request.POST.get('languages', ""))] selected_language = self.request.POST.get('selected_language', "") num_of_static = int(self.request.POST.get('numOfStatic', "")) if static_input == "true": for i in range(int(num_of_static)): if 'inputFile'+str(i) not in self.request.FILES or 'outputFile'+str(i) not in self.request.FILES: raise MultiValueDictKeyError except MultiValueDictKeyError: return HttpResponseBadRequest("Looks like you have an empty field or an unknown file type.") except Exception as e: logger.error(e) return HttpResponseBadRequest("Unexpected error.") if name == "" or description == "": return HttpResponseBadRequest("Looks like you have an empty field.") if not user.is_staff: return HttpResponseForbidden("You are not allowed to create assessments") course = Course.objects.get(pk=course_id) assessment = Assessment( name=name, description=description, additional_help=additional_help, course=course, languages=languages, deadline=deadline, static_input=json.loads(static_input), dynamic_input=json.loads(dynamic_input), num_of_static=num_of_static, max_time=max_time) assessment.save() if static_input == "true": for i in range(int(num_of_static)): os.makedirs(os.path.join(settings.MEDIA_ROOT, str(assessment.id), 'static_inputs'), exist_ok=True) os.makedirs(os.path.join(settings.MEDIA_ROOT, str(assessment.id), 'expected_static_outputs'), exist_ok=True) fs = FileSystemStorage(location=os.path.join( settings.MEDIA_ROOT, str(assessment.id), 'static_inputs')) static_file = self.request.FILES['inputFile'+str(i)] fs.save(str(i), static_file) fs = FileSystemStorage(location=os.path.join( settings.MEDIA_ROOT, str(assessment.id), 'expected_static_outputs')) static_file = self.request.FILES['outputFile'+str(i)] fs.save(str(i), static_file) if dynamic_input == "true": resource_file = self.request.FILES['resource'] resource = Resource( filename=resource_file.name, status="start", assessment=assessment, language=selected_language) resource.save() os.makedirs(os.path.join(settings.MEDIA_ROOT, str(assessment.id), 'model_solutions', str(resource.id)), exist_ok=True) fs = FileSystemStorage(location=os.path.join( settings.MEDIA_ROOT, str(assessment.id), 'model_solutions', str(resource.id))) fs.save(resource_file.name, resource_file) input_generator_file = self.request.FILES['input_generator'] input_generator = InputGenerator( filename=input_generator_file.name, assessment=assessment, language=selected_language) input_generator.save() os.makedirs(os.path.join(settings.MEDIA_ROOT, str(assessment.id), 'input_generators', str(input_generator.id)), exist_ok=True) fs = FileSystemStorage(location=os.path.join( settings.MEDIA_ROOT, str(assessment.id), 'input_generators', str(input_generator.id))) fs.save(input_generator_file.name, input_generator_file) assessment.input_generator = input_generator assessment.resource = resource assessment.save() return Response(assessment.id, 201)
class FileManager(object): current_user = None user_directory = None user_trash = None user_storage = None trash_storage = None def __init__(self, user=None): self.current_user = user if self.current_user != None: self.user_directory = settings.MEDIA_ROOT + "/" + self.current_user.user_id self.user_trash = settings.TRASH_ROOT + "/" + self.current_user.user_id # For security, limit access to user directory only self.user_storage = FileSystemStorage(location=self.user_directory) self.trash_storage = FileSystemStorage(location=self.user_trash) else: # Handle none logged in user user_storage = None self.update_path(None) def set_share_path(self, path): self.user_directory = settings.MEDIA_ROOT + "/" + self.current_user.user_id self.user_trash = settings.TRASH_ROOT + "/" + self.current_user.user_id self.user_storage = FileSystemStorage(location=self.user_directory) self.trash_storage = FileSystemStorage(location=self.user_trash) if os.path.isfile(self.user_storage.path(path)): return 1 # File else: self.update_path(path) return 0 # Directory def update_path(self, path): if path is None or len(path) == 0: self.path = '' self.abspath = self.user_storage.path(self.user_directory) else: self.path = self.clean_path(path) self.abspath = os.path.join( self.user_storage.path(self.user_directory), self.path) self.location = self.abspath # self.url = os.path.join(settings.MEDIA_URL, self.path) def clean_path(self, path): # replace backslash with slash path = path.replace('\\', '/') # remove leading and trailing slashes path = '/'.join([i for i in path.split('/') if i]) return path def get_breadcrumbs(self): breadcrumbs = [{ 'label': 'fm-home', 'path': '', }] parts = [e for e in self.path.split('/') if e] path = '' for part in parts: path = os.path.join(path, part) breadcrumbs.append({ 'label': part, 'path': path, }) return breadcrumbs def update_context_data(self, context): context.update({ 'path': self.path, 'breadcrumbs': self.get_breadcrumbs(), }) def file_details(self, file_path): file_path = self.user_storage.path(file_path) filename = smart_str(os.path.split(file_path)[1]) # Extract filemame if os.path.isfile(file_path): return { 'md5checksum': md5_checksum(file_path), 'directory': os.path.dirname(file_path), 'filename': filename, 'filesize': file_size_formatted(self.user_storage.size(file_path)), 'filedate': self.user_storage.get_modified_time(file_path), 'fileurl': file_path, } else: return {} def directory_list(self, include_files=True): listing = [] directories, files = self.user_storage.listdir(self.location) def _helper(name, filetype): return { 'filepath': os.path.join(self.path, name), 'filetype': filetype, 'filename': name, 'filedate': self.user_storage.get_modified_time( os.path.join(self.path, name)), 'filesize': file_size_formatted( self.user_storage.size(os.path.join(self.path, name))), } for directoryname in directories: listing.append(_helper(directoryname, 'directory')) if include_files: mimetypes.init() for filename in files: guessed_mime = mimetypes.guess_type(filename)[0] if (guessed_mime == None): file_mime = "unknown" else: file_mime = str(guessed_mime) listing.append(_helper(filename, file_mime)) return listing def trash_list(self): listing = [] directories, files = self.trash_storage.listdir(self.user_trash) def _helper(name, filetype): return { 'filepath': self.trash_storage.get_valid_name(name), 'filetype': filetype, 'filename': name, 'filedate': self.trash_storage.get_modified_time( os.path.join(self.user_trash, name)), 'filesize': file_size_formatted( self.trash_storage.size(os.path.join( self.user_trash, name))), } for directoryname in directories: listing.append(_helper(directoryname, 'directory')) mimetypes.init() for filename in files: guessed_mime = mimetypes.guess_type(filename)[0] if (guessed_mime == None): file_mime = "unknown" else: file_mime = str(guessed_mime) listing.append(_helper(filename, file_mime)) return listing def delete_item(self, item_path): res_path = item_path i = 0 try: delete_path = os.path.join( self.user_storage.path(self.user_directory), item_path) while os.path.exists( self.trash_storage.path(os.path.basename(res_path))): i = i + 1 res_path = str(i) + "_" + os.path.basename(item_path) new_delete_path = os.path.join( self.user_storage.path(self.user_directory), res_path) os.rename(delete_path, new_delete_path) delete_path = new_delete_path except Exception: return HttpResponseNotFound("File not found") # Move to trash shutil.move(delete_path, self.trash_storage.path(self.user_trash)) # Unshare item if it is shared if ShareUrl.objects.filter(owner=self.current_user, path=item_path).exists(): share_url = get_object_or_404(ShareUrl, owner=self.current_user, path=item_path) share_url.delete() return JsonResponse({'result': 0}) def purge_item(self, item_path): try: delete_path = os.path.join( self.trash_storage.path(self.user_trash), item_path) except Exception: return HttpResponseNotFound("File not found") # Permanantly delete file total_size = 0 if os.path.isdir(delete_path): # Get the size of the file for path, dirs, files in os.walk(delete_path): for f in files: fp = os.path.join(path, f) total_size += os.path.getsize(fp) for fold in dirs: total_size += 4096 shutil.rmtree(delete_path, ignore_errors=True) # Delete selected directory # Update Quota user_db = get_object_or_404(User, pk=self.current_user.user_id) user_db.used_quota = user_db.used_quota - int(total_size) user_db.save() else: total_size = os.path.getsize(delete_path) os.remove(delete_path) # Delete file # Update Quota user_db = get_object_or_404(User, pk=self.current_user.user_id) user_db.used_quota = user_db.used_quota - int(total_size) user_db.save() return JsonResponse({'result': 0}) def upload_file(self, file_data): filename = self.user_storage.get_valid_name(file_data.name) upload_path = os.path.join(self.user_storage.path(self.location), filename) # Check if user has sufficient space if file_data.size > self.current_user.get_remaining_quota(): # Insufficent space return False else: if os.path.exists(self.user_storage.path(upload_path)): # Overwrite existing file and remove from quota user_db = get_object_or_404(User, pk=self.current_user.user_id) user_db.used_quota = user_db.used_quota - int( os.path.getsize(self.user_storage.path(upload_path))) user_db.save() os.remove(self.user_storage.path(upload_path)) try: # Set max_length as a safety precaution to not go over-quota self.user_storage.save( upload_path, file_data, max_length=self.current_user.get_remaining_quota()) user_db = get_object_or_404(User, pk=self.current_user.user_id) # Update Quota user_db.used_quota = user_db.used_quota + int(file_data.size) user_db.save() return True except Exception as ex: # Upload failed. Not enough space return False def empty_trash(self): # Delete trsah folder and recreate it delete_path = self.user_trash # Get the size of the trash total_size = 0 for path, dirs, files in os.walk(delete_path): for f in files: fp = os.path.join(path, f) total_size += os.path.getsize(fp) for fold in dirs: total_size += 4096 shutil.rmtree(delete_path, ignore_errors=True) # Delete selected directory os.mkdir(delete_path) # Update Quota user_db = get_object_or_404(User, pk=self.current_user.user_id) user_db.used_quota = user_db.used_quota - int(total_size) user_db.save() return JsonResponse({'result': 0}) def restore_item(self, item_path): res_path = item_path i = 0 try: restore_path = os.path.join( self.trash_storage.path(self.user_trash), item_path) user_dir_path = os.path.join( self.user_storage.path(self.user_directory)) # Rename if item already exists while os.path.exists(self.user_storage.path(res_path)): i = i + 1 res_path = str(i) + "_" + item_path new_restore_path = os.path.join( self.trash_storage.path(self.user_trash), res_path) os.rename(restore_path, new_restore_path) restore_path = new_restore_path except Exception: return HttpResponseNotFound("File not found") # Move item back to user directory res_location = shutil.move(restore_path, user_dir_path) return JsonResponse({ 'result': 0, 'location': res_location.replace(self.user_storage.path(""), "") }) def download_file(self, filename): download_path = '' try: download_path = os.path.join( self.user_storage.path(self.user_directory), filename) except Exception: return HttpResponseNotFound("File not found") if os.path.isdir(download_path): # Cannot download directory return HttpResponseForbidden("Not allowed") else: file_wrapper = FileWrapper(open(download_path, 'rb')) file_mimetype = mimetypes.guess_type(download_path)[0] response = HttpResponse(file_wrapper, content_type=file_mimetype) response['X-Sendfile'] = download_path response['Content-Length'] = self.user_storage.size(download_path) # Extract filename only response[ 'Content-Disposition'] = 'attachment; filename=%s' % smart_str( os.path.split(filename)[1]) return response def create_directory(self, dir_name): """ Create directory by creating temp file in the directory FileStorageAPI will create the directory while saving the empty temp file then delete the temp file leaving behind the empty new directory """ new_directory = os.path.join(self.location, dir_name) if os.path.exists(self.user_storage.path(new_directory)): # Directory already exists return False else: new_path = self.user_storage.path(new_directory) temp_file = os.path.join(new_path, '.tmp') self.user_storage.save(temp_file, ContentFile('')) self.user_storage.delete(temp_file) user_db = get_object_or_404(User, pk=self.current_user.user_id) # Update Quota - 4KB per directory user_db.used_quota = user_db.used_quota + int(4096) user_db.save() return True def move(self, old_path, new_path): """ Moves a given file to the provided destination """ current_path = os.path.join( self.user_storage.path(self.user_directory), old_path) move_path = os.path.join(self.user_storage.path(self.user_directory), new_path) if current_path == move_path: return JsonResponse({ "result": 1, "message": "Cannot move '" + current_path.replace(self.user_storage.path(""), "") + "' into itself" }) # Error try: shutil.move(current_path, move_path) if ShareUrl.objects.filter(owner=self.current_user, path=old_path).exists(): share_url = get_object_or_404(ShareUrl, owner=self.current_user, path=old_path) # Remove leading slash new_url_path = move_path.replace( self.user_storage.path(""), "") + "/" + os.path.basename( os.path.normpath(old_path)) while new_url_path.startswith("/"): new_url_path = new_url_path[1:] share_url.path = new_url_path share_url.save() except shutil.Error as ex: # Strip user directory location and return error return JsonResponse({ "result": 1, "message": str(ex).replace(self.user_storage.path(""), "") }) # Some sort of error return JsonResponse({"result": 0, "message": "success"}) # Success def rename(self, file_path, new_name): try: rename_path = os.path.join( self.user_storage.path(self.user_directory), file_path) except Exception: # File not found return False new_name_path = os.path.join(self.user_storage.path(rename_path), "../") new_name_path = os.path.join(self.user_storage.path(new_name_path), new_name) if os.path.exists(new_name_path): return False else: os.rename(rename_path, new_name_path) # Update links if necessary if ShareUrl.objects.filter(owner=self.current_user, path=file_path).exists(): share_url = get_object_or_404(ShareUrl, owner=self.current_user, path=file_path) # Remove leading slash new_url_path = new_name_path.replace( self.user_storage.path(""), "") while new_url_path.startswith("/"): new_url_path = new_url_path[1:] share_url.path = new_url_path share_url.save() return True def file_search(self, search_item): file_list = [] # Recursively search through user directory for filename in Path(self.user_storage.path("")).glob('**/' + search_item): file_list.append( str(filename).replace( self.user_storage.path(""), "")) # Remove absolute path to home directory return JsonResponse({ 'result': 0, 'files': file_list, 'search_query': search_item })
def index(request): contex = { 'form': UploadFileForm, } if request.method == 'POST' and request.FILES['image']: MyUploadFileForm = UploadFileForm(request.POST) print(MyUploadFileForm) watermark_message = MyUploadFileForm.cleaned_data['message'] myfile = request.FILES['image'] mainImage = myfile watermark_message_option = MyUploadFileForm.cleaned_data['option'] fs = FileSystemStorage() filename = fs.save(myfile.name, myfile) upload_file_url = fs.url(filename) print("file url" + upload_file_url) if watermark_message_option == 'visible': print('under visible') if watermark_message is not None: myfile = Visible(myfile, watermark_message) myfile.watermark() print('not none') contex = { 'myfile': upload_file_url, } try: watermark_file = request.FILES['watermark_image'] if watermark_file is not None: print("watermark file") fs1 = FileSystemStorage() filename1 = fs1.save(watermark_file.name, watermark_file) upload_file_url1 = fs1.url(filename1) print("upload_file_url1" + upload_file_url1) vi = VisibleImage() vi.imageMark(watermark_file, mainImage) except Exception: print("Error") else: watermark_file = None if watermark_message_option == 'invisible': try: watermark = request.FILES['watermark_image'] fs1 = FileSystemStorage() filename1 = fs1.save(watermark.name, watermark) upload_file_url1 = fs1.url(filename1) print("upload_file_url1" + upload_file_url1) if watermark is not None: watermark_url = embedding(myfile, watermark) contex = { 'myfile': watermark_url, } return render(request, "watermark/watermark.html", contex) except Exception: Invisible().encrypt(watermark_message, myfile) print("gjjjjjjjjjjjjh") contex = {'myfile': upload_file_url} return render(request, "watermark/watermark.html", contex) print("end") return render(request, "watermark/watermark.html", contex) return render(request, "watermark/index.html", contex)
def appstudentupdate(request): try: if request.method == 'POST': dict_keys = dict(json.loads(request.body)) username = dict_keys.get('username') image = dict_keys.get('image') fullname = dict_keys.get('fullname') email = dict_keys.get('email') password = dict_keys.get('password') namefile = dict_keys.get('namefile') changepass = dict_keys.get('changepass') changeimage = dict_keys.get('changeimage') idrow = dict_keys.get('idrow') if changepass and not changeimage: acc = Account.objects.filter(pk=idrow).first() acc.name = username acc.info.fullname = fullname acc.info.email = email acc.password = pwd_context.hash(password) acc.saveupdate() if changeimage and not changepass: Account(pk=idrow).deleteimage() formatd, imgstr = image.split(';base64,') ext = formatd.split('/')[-1] data = ContentFile(base64.b64decode(imgstr), name=namefile) link = "account/%02d/%02d/%02d" % (datetime.today().year, datetime.today().month, datetime.today().day) fs = FileSystemStorage( location=os.path.join(settings.MEDIA_ROOT, link), base_url=os.path.join(settings.MEDIA_ROOT, link)) filename = fs.save(data.name, data) acc = Account.objects.filter(pk=idrow).first() acc.name = username acc.info.fullname = fullname acc.info.email = email acc.image = os.path.join(link, filename) acc.saveupdate() if changepass and changeimage: Account(pk=idrow).deleteimage() formatd, imgstr = image.split(';base64,') ext = formatd.split('/')[-1] data = ContentFile(base64.b64decode(imgstr), name=namefile) link = "account/%02d/%02d/%02d" % (datetime.today().year, datetime.today().month, datetime.today().day) fs = FileSystemStorage( location=os.path.join(settings.MEDIA_ROOT, link), base_url=os.path.join(settings.MEDIA_ROOT, link)) filename = fs.save(data.name, data) acc = Account.objects.filter(pk=idrow).first() acc.name = username acc.info.fullname = fullname acc.info.email = email acc.password = pwd_context.hash(password) acc.image = os.path.join(link, filename) acc.saveupdate() if not changepass and not changeimage: acc = Account.objects.filter(pk=idrow).first() acc.name = username acc.info.fullname = fullname acc.info.email = email acc.saveupdate() return HttpResponse(content="", content_type='application/json', status=200) except Exception: pass
def index(request): if request.method == 'POST' and request.FILES['file']: upload_file = request.FILES['file'] extension = os.path.splitext(upload_file.name)[1] rename = datetime.datetime.now().strftime( "%Y_%m_%d %H_%M_%S") + extension fss = FileSystemStorage() filename = fss.save(rename, upload_file) file = File(file=rename) file.save() upload_file_path = fss.path(filename) md5 = trail.hash_value(upload_file_path) f = upload_file_path fl = open(f, "rb") s = fl.read(5) byte_range = bytearray(s) date_header, timestamp = struct.unpack('>BL', byte_range) print(date_header) if (date_header != 77): print(" Not an EXE file") else: fl.seek(60) s = fl.read(4) header_offset = struct.unpack("<L", s)[0] fl.seek(header_offset + 4) s = fl.read(2) machine = struct.unpack("<H", s)[0] if machine == IMAGE_FILE_MACHINE_I386: print(" Image Type = IA-32 (32-bit x86)") fp = open('PE Analysis.txt', 'a') fp.write("Image Type = IA-32 (32-bit x86)") fp.write('\n\n') fp.close() elif machine == IMAGE_FILE_MACHINE_IA64: print(" Image Type = IA-64 (Itanium)") fp = open('PE Analysis.txt', 'a') fp.write("Image Type = IA-64 (Itanium)") fp.write('\n\n') fp.close() elif machine == IMAGE_FILE_MACHINE_AMD64: print(" Image Type = AMD64 (64-bit x86)") fp = open('PE Analysis.txt', 'a') fp.write("Image Type = AMD64 (64-bit x86)") fp.write('\n\n') fp.close() else: print(" Unknown architecture") print('\n File Size = ' + trail.file_size(f)) print('\n Last Modified Date = %s' % time.ctime(os.path.getmtime(f))) print('\n Created Date = %s' % time.ctime(os.path.getctime(f))) fp = open('PE Analysis.txt', 'a') fp.write('File Size = ' + trail.file_size(f)) fp.write('\n\nLast Modified Date: %s' % time.ctime(os.path.getmtime(f))) fp.write('\n\nCreated Date: %s' % time.ctime(os.path.getctime(f))) fp.write('\n') fp.write('\n') fp.close() fl.close() virustotal = trail.VT_Request( 'eca6223108ae025265a09a1723ddc2d7396c31ab41f720428f9a9fc7bd4cb95e', md5, upload_file_path) if (isinstance(virustotal, str)): output = virustotal else: md = virustotal[0] sha1 = virustotal[1] sha256 = virustotal[2] positives = virustotal[3] total = virustotal[4] return render( request, 'file/hello.html', { 'upload_file_path': upload_file_path, 'md5': virustotal[0], 'sha1': virustotal[1], 'sha256': virustotal[2], 'positives': virustotal[3], 'total': virustotal[4] }) else: return render(request, 'file/index.html')
def upload(self, request, **kwargs): """ Upload a File\n Params: - file: file object - force: bool (optional) - whether rewrite existing file and Document """ session_id = kwargs.get('pk') file_ = request.FILES.dict().get('file') if session_id and file_: try: storage = FileSystemStorage( location=os.path.join(settings.MEDIA_ROOT, settings. FILEBROWSER_DIRECTORY, session_id)) if storage.exists(file_.name): if request.POST.get('force') == 'true': storage.delete(file_.name) file_tasks = Task.objects\ .filter(metadata__session_id=session_id)\ .filter(metadata__file_name=file_.name) for file_task in file_tasks: if file_task.metadata.get( 'file_name') == file_.name: purge_task(file_task.id) Document.objects\ .filter(upload_session_id=session_id, name=file_.name)\ .delete() else: raise APIException('Already exists') stored_file_name = storage.save(file_.name, file_.file) required_locators = [ 'date', 'party', 'term', 'geoentity', 'currency', 'citation', 'definition', 'duration' ] linked_tasks = [{ 'task_name': 'Locate', 'locate': required_locators, 'parse': 'paragraphs', 'do_delete': False, 'metadata': { 'session_id': session_id, 'file_name': file_.name }, 'user_id': request.user.id }] document_type = UploadSession.objects.get( pk=session_id).project.type # if Document type specified if document_type: for app_name in custom_apps: module_str = 'apps.%s.tasks' % app_name module = sys.modules.get(module_str) if hasattr(module, 'DetectFieldValues'): linked_tasks.append({ 'task_name': 'DetectFieldValues', 'module_name': module_str, 'do_not_write': False, 'metadata': { 'session_id': session_id, 'file_name': file_.name }, 'user_id': request.user.id }) call_task(task_name='LoadDocuments', source_path=os.path.join(session_id, stored_file_name), user_id=request.user.id, metadata={ 'session_id': session_id, 'file_name': file_.name }, linked_tasks=linked_tasks) except Exception as e: raise APIException(str(e)) else: raise ValidationError( 'Provide session_id and file in request data.') return Response('Loaded')
def save_image(filename, image): fs = FileSystemStorage() fs.save(filename, image)
def add_application_form_submission(request): if request.session.has_key('username'): if request.method == 'POST': if 'attachment' in request.FILES: print("in if") attachment = request.FILES['attachment'] fs = FileSystemStorage() ext = os.path.splitext(attachment.name)[1] ext = ext.lower() print(ext) date = datetime.now() result = '%s%s%s%s%s%s_%s' % ( date.year, date.month, date.day, date.hour, date.minute, date.second, os.urandom(10).hex()) print(result) filename = fs.save(result + ext, attachment) upload_file_url = fs.url(filename) else: print("in else") upload_file_url = 'null' project_title = request.POST['project_title'] project_url = request.POST['project_url'] project_size = request.POST['project_size'] freelance_account = request.POST['freelance_account'] invited_by_client = request.POST['invited_by_client'] client_location = request.POST['client_location'] project_description = request.POST['project_description'] project_type = request.POST['payment_method'] project_amount = request.POST['project_amount'] bde_user = User.objects.get(user_email=request.session['username']) if invited_by_client == '1': new_job_application = Contract( project_title=project_title, project_url=project_url, project_size_id=project_size, freelance_account_id=freelance_account, invited_by_client=invited_by_client, project_attachment=upload_file_url, client_location=client_location, project_desc=project_description, contract_type=project_type, contract_price=project_amount, bidder_account_id=bde_user.id, lead_generated='1') new_job_application.save() lead_generated = LeadGeneratedContract( lead_contract_id=new_job_application.id, bidder_account_id=bde_user.id) lead_generated.save() return redirect('/bde/lead_generated_jobs/') else: new_job_application = Contract( project_title=project_title, project_url=project_url, project_size_id=project_size, freelance_account_id=freelance_account, invited_by_client=invited_by_client, project_attachment=upload_file_url, client_location=client_location, project_desc=project_description, contract_type=project_type, contract_price=project_amount, bidder_account_id=bde_user.id) new_job_application.save() return redirect('/bde/applied_jobs') else: return redirect('/bde/add_application') else: return redirect("/")
def edit_contract_info_form_submission(request): if request.session.has_key('username'): selected_contract = Contract.objects.get( id=request.POST['contract_id']) if 'attachment' in request.FILES: print("in if") attachment = request.FILES['attachment'] fs = FileSystemStorage() ext = os.path.splitext(attachment.name)[1] ext = ext.lower() print(ext) date = datetime.now() result = '%s%s%s%s%s%s_%s' % (date.year, date.month, date.day, date.hour, date.minute, date.second, os.urandom(10).hex()) print(result) filename = fs.save(result + ext, attachment) upload_file_url = fs.url(filename) selected_contract.project_url = upload_file_url selected_contract.project_title = request.POST['project_title'] selected_contract.project_url = request.POST['project_url'] selected_contract.project_size_id = request.POST['project_size'] selected_contract.freelance_account_id = request.POST[ 'freelance_account'] selected_contract.client_location = request.POST['client_location'] selected_contract.project_desc = request.POST['project_description'] selected_contract.contract_type = request.POST['payment_method'] selected_contract.contract_price = request.POST['project_amount'] bde_user = User.objects.get(user_email=request.session['username']) if selected_contract.invited_by_client == '1' and request.POST[ 'invited_by_client'] == '0': lead_generated_contract = LeadGeneratedContract.objects.get( lead_contract_id=selected_contract.id) lead_generated_contract.delete() selected_contract.lead_generated = '0' selected_contract.invited_by_client = request.POST[ 'invited_by_client'] # selected_contract.save() elif selected_contract.invited_by_client == '0' and request.POST[ 'invited_by_client'] == '1': lead_generated_contract = LeadGeneratedContract( lead_contract_id=selected_contract.id, bidder_account_id=bde_user.id) lead_generated_contract.save() selected_contract.lead_generated = '1' selected_contract.invited_by_client = request.POST[ 'invited_by_client'] # selected_contract.save() selected_contract.save() filter_string = request.session['bde_selectedFreelancer'] if filter_string == 'all': bde_user = User.objects.get(user_email=request.session['username']) jobs_list = Contract.objects.filter( bidder_account_id=bde_user.id, contract_status='0').order_by('-created_at') freelance_account = FreelanceAccount.objects.all() else: bde_user = User.objects.get(user_email=request.session['username']) jobs_list = Contract.objects.filter( bidder_account_id=bde_user.id, freelance_account_id=filter_string, contract_status='0').order_by('-created_at') freelance_account = FreelanceAccount.objects.all() return redirect( "/bde/applied_jobs", { 'jobs_lists': jobs_list, 'freelance_accounts': freelance_account, 'selected_option': filter_string }) else: return redirect("/")
def doc_change(request): idnew = request.POST['idnew'] refpath = request.POST['refp'] idprefix = request.POST['idprefix'] fcat = request.POST['fcat'] refdate = request.POST['refd'] file_refno = request.POST['refn'] files = request.FILES['updoc'] ann_ex = TAapplicationfiles.objects.filter(refpath=refpath, user_id=request.user.id, refid=idprefix).first() remove(ann_ex.filepath) if ann_ex.relation == 'parent': newpath = refpath + '/' + fcat else: newpath = refpath[-2:] + '/' + fcat + '/' + refpath print(newpath, 'pathhhhhhhhhhhh') print(files.name, 'myfile') fn = files.name ext = fn[-4:] print(ext, 'ext') extension = ext curr_path = "/" + str(request.user.id) + "/" + idprefix + newpath + "/" curr_path = curr_path.replace('/', '\\') new_path = os.path.join(settings.MEDIA_ROOT + curr_path) print(new_path, 'newpath') fs = FileSystemStorage(location=new_path, base_url=new_path) # unique_filename = str(uuid.uuid4()) # filename = fs.save(unique_filename, files) filename = fs.save(files.name, files) uploaded_file_url = fs.path(filename) print(uploaded_file_url, 'url') bufferSize = 64 * 1024 passw = "#EX\xc8\xd5\xbfI{\xa2$\x05(\xd5\x18\xbf\xc0\x85)\x10nc\x94\x02)j\xdf\xcb\xc4\x94\x9d(\x9e" with open(uploaded_file_url, "rb") as fIn: with open(uploaded_file_url + ".aes", "wb") as fOut: pyAesCrypt.encryptStream(fIn, fOut, passw, bufferSize) # refpath=idprefix+newpath+ "/"+files.name remove(uploaded_file_url) ann_ex.filepath = uploaded_file_url + ".aes" ann_ex.refdate = refdate ann_ex.file_refno = file_refno # taf = TAapplicationfiles(filecategory=fcat, filepath=uploaded_file_url+".aes",ext=extension,user_id=request.user.id,refid=idprefix,refpath=refpath,refdate=refdate,file_refno=file_refno,relation='child') taff = ann_ex.save() form = fileUploadForm taf = TAapplicationfiles.objects.filter( user_id=request.user.id).order_by('refpath') idg = idgenerationmodel.objects.filter(user_id=request.user.id, idprefix=idprefix).first() print(idnew, refpath, fcat, 'nnnnnnnnnnnnnnnnnn') return render(request, 'applicant/view_all_doc.html', { 'form': form, 'details': taf, 'idg': idg, 'idprefix': idprefix })
def create_call(self): pipeline_id = self.generate_id() FS = FileSystemStorage() FS.location = os.path.join(MEDIA_ROOT, pipeline_id) os.system("mkdir " + FS.location) out_dir = FS.location mirfile = self.cleaned_data.get("mirfile") utrfile = self.cleaned_data.get("utrfile") program_list = [] param_list = [] if self.cleaned_data.get('psRobot'): program_list.append("PSROBOT") param_list.append(self.cleaned_data.get("psRobot_par")) if self.cleaned_data.get('tapir_fasta'): program_list.append("TAPIR_FASTA") param_list.append(self.cleaned_data.get("tapir_fasta_par")) if self.cleaned_data.get('tapir_RNA'): program_list.append("TAPIR_HYBRID") param_list.append(self.cleaned_data.get('tapir_RNA_par')) program_string = ":".join(program_list) if mirfile: file_to_update = mirfile uploaded_file = str(file_to_update) mirfile = FS.save(uploaded_file, file_to_update) else: mirtext = self.cleaned_data.get("mirtext") content = ContentFile(mirtext) mirfile = FS.fileUpload.save("mirs.fa", content) FS.save() if utrfile: file_to_update = utrfile uploaded_file = str(file_to_update) utrfile = FS.save(uploaded_file, file_to_update) elif self.cleaned_data.get("utrtext"): utrtext = self.cleaned_data.get("utrtext") content = ContentFile(utrtext) utrfile = FS.fileUpload.save('utrs.fa', content) FS.save() else: utrfile = self.cleaned_data.get('utrchoice') name = pipeline_id + '_mirconstarget' configuration = { 'pipeline_id': pipeline_id, 'out_dir': out_dir, 'name': name, "mirna_file": mirfile, "utr_file": utrfile, "program_string": program_string, #"parameter_string": '":"'.join(param_list), "parameter_string": "\"'" + " : ".join(param_list) + "'\"", 'type': 'miRNAconstarget' } configuration_file_path = os.path.join(out_dir, 'conf.json') import json with open(configuration_file_path, 'w') as conf_file: json.dump(configuration, conf_file, indent=True) JobStatus.objects.create( job_name=name, pipeline_key=pipeline_id, job_status="not launched", start_time=datetime.datetime.now(), #finish_time=datetime.time(0, 0), all_files=[mirfile, utrfile], modules_files="", pipeline_type="mirconstarget", ) if QSUB: return 'qsub -v c="{configuration_file_path}" -N {job_name} {sh}'.format( configuration_file_path=configuration_file_path, job_name=name, sh=os.path.join( os.path.dirname(BASE_DIR) + '/core/bash_scripts/run_qsub.sh')), pipeline_id else: return '{sh} {configuration_file_path}'.format( configuration_file_path=configuration_file_path, sh=os.path.join( os.path.dirname(BASE_DIR) + '/core/bash_scripts/run.sh')), pipeline_id
def doc_add(request): idprefix = request.POST['idprefix'] print(idprefix, 'xxxxxxxxxxxxx') if request.POST: filecategory = request.POST['filecategory'] refdate = request.POST['refdate'] file_refno = request.POST['file_refno'] print(filecategory, refdate, 'fc') files = request.FILES['files'] if filecategory == 'TOT': newpath = 'Annexure 1.1/' + filecategory refpath = 'Annexure 1.1' elif filecategory == 'Brief_Desc': newpath = 'Annexure 2/' + filecategory refpath = 'Annexure 2' elif filecategory == 'Cont_TR': newpath = 'Annexure 4/' + filecategory refpath = 'Annexure 4' elif filecategory == 'PH': newpath = 'Annexure 4.1/' + filecategory refpath = 'Annexure 4.1' elif filecategory == 'Tech_Spec': newpath = 'Annexure 4.2/' + filecategory refpath = 'Annexure 4.2' elif filecategory == 'DAL_MDI': newpath = 'Annexure 4.3/' + filecategory refpath = 'Annexure 4.3' elif filecategory == 'BOM': newpath = 'Annexure 4.4/' + filecategory refpath = 'Annexure 4.4' elif filecategory == 'SOP_ACBS': newpath = 'Annexure 4.5/' + filecategory refpath = 'Annexure 4.5' elif filecategory == 'Pro_Doc': newpath = 'Annexure 4.6/' + filecategory refpath = 'Annexure 4.6' elif filecategory == 'QTS': newpath = 'Annexure 4.7/' + filecategory refpath = 'Annexure 4.7' elif filecategory == 'QTR': newpath = 'Annexure 4.8/' + filecategory refpath = 'Annexure 4.8' elif filecategory == 'Comp_TR': newpath = 'Annexure 4.9/' + filecategory refpath = 'Annexure 4.9' elif filecategory == 'COD': newpath = 'Annexure 4.10/' + filecategory refpath = 'Annexure 4.10' elif filecategory == 'FER': newpath = 'Annexure 4.11/' + filecategory refpath = 'Annexure 4.11' elif filecategory == 'Per_Fb': newpath = 'Annexure 4.12/' + filecategory refpath = 'Annexure 4.12' elif filecategory == 'PC': newpath = 'Annexure 4.13/' + filecategory refpath = 'Annexure 4.13' elif filecategory == 'Drawings': newpath = 'Annexure 5/' + filecategory refpath = 'Annexure 5' elif filecategory == 'TA_Data_Sheet': newpath = 'Annexure 6/' + filecategory refpath = 'Annexure 6' i = 1 # for f in files: print(files.name, 'myfile') fn = files.name ext = fn[-4:] print(ext, 'ext') extension = ext curr_path = "/" + str(request.user.id) + "/" + idprefix + newpath + "/" curr_path = curr_path.replace('/', '\\') new_path = os.path.join(settings.MEDIA_ROOT + curr_path) print(new_path, 'newpath') fs = FileSystemStorage(location=new_path, base_url=new_path) # unique_filename = str(uuid.uuid4()) # filename = fs.save(unique_filename, files) filename = fs.save(files.name, files) uploaded_file_url = fs.path(filename) print(uploaded_file_url, 'url') bufferSize = 64 * 1024 passw = "#EX\xc8\xd5\xbfI{\xa2$\x05(\xd5\x18\xbf\xc0\x85)\x10nc\x94\x02)j\xdf\xcb\xc4\x94\x9d(\x9e" with open(uploaded_file_url, "rb") as fIn: with open(uploaded_file_url + ".aes", "wb") as fOut: pyAesCrypt.encryptStream(fIn, fOut, passw, bufferSize) # refpath=idprefix+newpath+ "/"+files.name i += 1 remove(uploaded_file_url) taf = TAapplicationfiles(filecategory=filecategory, filepath=uploaded_file_url + ".aes", ext=extension, user_id=request.user.id, refid=idprefix, refpath=refpath, refdate=refdate, file_refno=file_refno, relation='parent') taff = taf.save() print('idprefixxx') print(idprefix, 'idprefix') form = fileUploadForm taf = TAapplicationfiles.objects.filter( user_id=request.user.id, refid=idprefix).order_by('refpath') idg = idgenerationmodel.objects.filter(user_id=request.user.id, idprefix=idprefix).first() # get_refpath=TAapplicationfiles.objects.filter(user_id=request.user.id,refid=idprefix).values('refpath').order_by('refpath') # print(get_refpath,'get_refpath') # for anex_name in get_refpath: # anexture_name = anex_name['refpath'] # print(anexture_name,'taff') # comments = commentsmodel(name=anexture_name,idprefix=idprefix,user_id=request.user.id) # commentsdb=comments.save() return render(request, 'applicant/view_all_doc.html', { 'form': form, 'details': taf, 'idg': idg, 'idprefix': idprefix }) else: print('idprefixxx') print(idprefix, 'idprefix') form = fileUploadForm taf = TAapplicationfiles.objects.filter( user_id=request.user.id).order_by('refpath') return render(request, 'applicant/view_all_doc.html', { 'form': form, 'details': taf, 'idprefix': idprefix })
def generatepdf(request): if request.POST: # dal_mdi_files = request.FILES.getlist('dal_mdi_file') # for f in dal_mdi_files: # print(f.name,'myfile') # fn=f.name # ext=fn[-4:] # print(ext,'ext') # extension=ext # curr_path = "/"+str(request.user.id)+ "/DAL_MDI/" # curr_path=curr_path.replace('/','\\') # new_path = os.path.join(settings.MEDIA_ROOT + curr_path) # print(new_path,'newpath') # fs = FileSystemStorage(location=new_path , base_url = new_path ) # unique_filename = str(uuid.uuid4()) # filename = fs.save(unique_filename, f) # uploaded_file_url = fs.path(filename) # print(uploaded_file_url,'url') # bufferSize = 64 * 1024 # passw = "#EX\xc8\xd5\xbfI{\xa2$\x05(\xd5\x18\xbf\xc0\x85)\x10nc\x94\x02)j\xdf\xcb\xc4\x94\x9d(\x9e" # with open(uploaded_file_url, "rb") as fIn: # with open(uploaded_file_url+".aes", "wb") as fOut: # pyAesCrypt.encryptStream(fIn, fOut, passw, bufferSize) # remove(uploaded_file_url) # taf = TAapplicationfiles(filecategory='DAL_MDI', filepath=uploaded_file_url+".aes",ext=extension,user_id=request.user.id) # taff=taf.save() # bom_files = request.FILES.getlist('bom_file') # for f in bom_files: # print(f.name,'myfile') # fn=f.name # ext=fn[-4:] # print(ext,'ext') # extension=ext # curr_path = "/"+str(request.user.id)+ "/BOM/" # curr_path=curr_path.replace('/','\\') # new_path = os.path.join(settings.MEDIA_ROOT + curr_path) # print(new_path,'newpath') # fs = FileSystemStorage(location=new_path , base_url = new_path ) # unique_filename = str(uuid.uuid4()) # filename = fs.save(unique_filename, f) # uploaded_file_url = fs.path(filename) # print(uploaded_file_url,'url') # bufferSize = 64 * 1024 # passw = "#EX\xc8\xd5\xbfI{\xa2$\x05(\xd5\x18\xbf\xc0\x85)\x10nc\x94\x02)j\xdf\xcb\xc4\x94\x9d(\x9e" # with open(uploaded_file_url, "rb") as fIn: # with open(uploaded_file_url+".aes", "wb") as fOut: # pyAesCrypt.encryptStream(fIn, fOut, passw, bufferSize) # remove(uploaded_file_url) # taf = TAapplicationfiles(filecategory='BOM', filepath=uploaded_file_url+".aes",ext=extension,user_id=request.user.id) # taff=taf.save() # sop_acbs_files = request.FILES.getlist('sop_acbs_file') # for f in sop_acbs_files: # print(f.name,'myfile') # fn=f.name # ext=fn[-4:] # print(ext,'ext') # extension=ext # curr_path = "/"+str(request.user.id)+ "/SOP_ACBS/" # curr_path=curr_path.replace('/','\\') # new_path = os.path.join(settings.MEDIA_ROOT + curr_path) # print(new_path,'newpath') # fs = FileSystemStorage(location=new_path , base_url = new_path ) # unique_filename = str(uuid.uuid4()) # filename = fs.save(unique_filename, f) # uploaded_file_url = fs.path(filename) # print(uploaded_file_url,'url') # bufferSize = 64 * 1024 # passw = "#EX\xc8\xd5\xbfI{\xa2$\x05(\xd5\x18\xbf\xc0\x85)\x10nc\x94\x02)j\xdf\xcb\xc4\x94\x9d(\x9e" # with open(uploaded_file_url, "rb") as fIn: # with open(uploaded_file_url+".aes", "wb") as fOut: # pyAesCrypt.encryptStream(fIn, fOut, passw, bufferSize) # remove(uploaded_file_url) # taf = TAapplicationfiles(filecategory='SOP_ACBS', filepath=uploaded_file_url+".aes",ext=extension,user_id=request.user.id) # taff=taf.save() form = TAapplicationForm(request.POST, request.FILES) if form.is_valid(): idprefix = request.POST['idprefix'] taf = idgenerationmodel.objects.get(user_id=request.user.id, idprefix=idprefix) print(idprefix, 'iiiiiiiiiiiid') # current Date date_joined = datetime.now() formatted_datetime = date_joined.strftime("%Y-%m-%d") taapp = form.save(commit=False) taapp.user = request.user taapp.submitted_date = formatted_datetime taapp.file_in_name = "RD" taapp.rcma = taf.rcma taapp.idprefix = idprefix taapp.save() submitted_date = datetime.now() print("taapp", taapp, taapp.id, taapp.rcma, submitted_date) tastatus = statusmodel(TAA_id=taapp.id, RCMA=taapp.rcma, status="RCMA_RD_Received", RCMA_RD_Received=submitted_date, user_id=request.user.id) tastatusdb = tastatus.save() messages.success(request, 'TA Application Submitted Successfully!') myfile = request.FILES['sign'] curr_path = "/" + str(request.user.id) curr_path = curr_path.replace('/', '\\') new_path = os.path.join(settings.MEDIA_ROOT + curr_path) print(new_path, 'newpath') fs = FileSystemStorage(location=new_path, base_url=new_path) # unique_filename = str(uuid.uuid4()) filename = fs.save(str(request.user.id) + ".jpg", myfile) uploaded_file_url = fs.path(filename) print(uploaded_file_url, 'url') template = get_template('applicant/newtypeapprovalpdf.html') context = { 'firmname': request.POST['firmname'], 'addr1': request.POST['addr1'], 'addr2': request.POST['addr2'], 'tot': request.POST['tot'], 'item_name': request.POST['item_name'], 'part_no': request.POST['part_no'], 'desc': request.POST['desc'], 'spec': request.POST['spec'], 'dal_mdi': request.POST['dal_mdi'], 'bom': request.POST['bom'], 'sop_acbs': request.POST['sop_acbs'], 'pc': request.POST['pc'], 'tre': request.POST['tre'], 'otheritems': request.POST['otheritems'], 'sign': uploaded_file_url, 'designation': request.POST['designation'], 'date': formatted_datetime, 'addr': request.POST['addr'] } html = template.render(context) curr_path = "/" + str( request.user.id) + "/" + idprefix + "Annexure 1/TAapplication/" curr_path = curr_path.replace('/', '\\') new_path = os.path.join(settings.MEDIA_ROOT + curr_path) os.makedirs(new_path) result = open(new_path + "TAapplication.pdf", 'wb') pdf = pisa.pisaDocument(BytesIO(html.encode("ISO-8859-1")), result, link_callback=link_callback) result.close() taf = TAapplicationfiles(filecategory='TAapplication', filepath=new_path + "TAapplication.pdf", ext=".pdf", user_id=request.user.id, refid=idprefix, refpath='Annexure 1') taff = taf.save() remove(uploaded_file_url) return render(request, 'applicant/newtypeapproval.html') else: print('ddddddd') return render(request, 'applicant/newtypeapproval.html')
def import_file(request): if request.method == 'POST' and request.FILES['myfile']: myfile = request.FILES['myfile'] fs = FileSystemStorage() BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) filename = fs.save(myfile.name, myfile) uploaded_file_url = fs.url(filename) reader = csv.reader(open(BASE_DIR + uploaded_file_url), quotechar='"', delimiter=',') idd = [] Sfinal = [] to = [] unique = [] rat = [] count = 0 for row in reader: if (row[0] != "PRODUCT NAME"): material_name = row[4] productCode = row[1] try: raws = raw.objects.get(material_name=material_name) Raw_id = raws.id except raw.DoesNotExist: raws = None messages.info(request, 'No Such Raw Material' + row[4], extra_tags='alert') pass if (raws != None): t = [] t.append(count) t.append(row[1]) to.append(t) r = cost() r.Ki = count r.product_name = row[0] r.product_code = row[1] r.factory_name = row[2] r.pack_size = row[3] r.rawmultiplier = row[6] r.wastage = row[8] r.overall_wastage = row[43] r.margin_per = row[45] r.margin_amount = row[46] r.mrp_per = row[48] r.mrp_price = row[49] r.Raw_id = Raw_id r.rt_id = Raw_id rat.append(r) i = 10 j = 38 while j > i: pack = {} multi = {} if len(row[i]) > 0: pack["material_name"] = row[i] multi = row[i + 1] raws = raw.objects.get_or_create( material_name=pack["material_name"], defaults={ 'mode_id': 1, 'material_type': "PACKAGING MATERIAL", 'material_name': pack["material_name"], 'purchase_name': "NoN", 'purchase_date': str("2018-1-1"), 'factory_wise_bifercation': str(0), 'ex_factory_price': str(0), 'supplier_name': "NON", 'hsn_code': str(0), 'gst': str(0), 'price_after_gst': str(0), 'freight': str(0), 'cost_price': str(0), 'transpoter': "NON" }) ro = raw.objects.get( material_name=pack["material_name"]) print("chala") uu = ro.id idd.append(uu) Sfinal.append(multi) i += 3 if len(idd) > 0: packingMaterials = idd packingMultipliars = Sfinal else: packingMaterials = 0 if packingMaterials != 0: for i in range(len(packingMaterials)): packId = packingMaterials[i] multiVal = packingMultipliars[i] o = [] o.append(count) o.append(packId) o.append(packId) o.append(multiVal) unique.append(o) count += 1 idd = [] Sfinal = [] z = [] cost.objects.bulk_create(rat) for i in to: for j in unique: if i[0] == j[0]: e = cost.objects.get(product_code=i[1]) ee = e.id k = costpack() k.cost_id = ee k.multipliar = j[3] k.Raw_id = j[2] k.packing_id = j[2] z.append(k) costpack.objects.bulk_create(z) return redirect('/manufacturing_cost_master')
def edit_student_save(request): if request.method != "POST": return HttpResponse("Invalid Method!") else: student_id = request.session.get('student_id') if student_id == None: return redirect('/manage_student') form = EditStudentForm(request.POST, request.FILES) if form.is_valid(): email = form.cleaned_data['email'] username = form.cleaned_data['username'] first_name = form.cleaned_data['first_name'] last_name = form.cleaned_data['last_name'] address = form.cleaned_data['address'] course_id = form.cleaned_data['course_id'] gender = form.cleaned_data['gender'] session_year_id = form.cleaned_data['session_year_id'] # Getting Profile Pic first # First Check whether the file is selected or not # Upload only if file is selected if len(request.FILES) != 0: profile_pic = request.FILES['profile_pic'] fs = FileSystemStorage() filename = fs.save(profile_pic.name, profile_pic) profile_pic_url = fs.url(filename) else: profile_pic_url = None try: # First Update into Custom User Model user = CustomUser.objects.get(id=student_id) user.first_name = first_name user.last_name = last_name user.email = email user.username = username user.save() # Then Update Students Table student_model = Students.objects.get(admin=student_id) student_model.address = address course = Courses.objects.get(id=course_id) student_model.course_id = course session_year_obj = SessionYearModel.objects.get( id=session_year_id) student_model.session_year_id = session_year_obj student_model.gender = gender if profile_pic_url != None: student_model.profile_pic = profile_pic_url student_model.save() # Delete student_id SESSION after the data is updated del request.session['student_id'] messages.success(request, "Student Updated Successfully!") return redirect('/edit_student/' + student_id) except: messages.success(request, "Failed to Uupdate Student.") return redirect('/edit_student/' + student_id) else: return redirect('/edit_student/' + student_id)