How to get the files uploaded in InMemoryUploadedFile django - python

I am developing an application in django 2.1 in which I must upload an undetermined number of audios through a modal and then pass the information to the view from which the modal is launched.
However, these audios should not be stored in the database until the main view form is filled out. Then I thought about these solutions:
First I thought about saving it as a session attribute but the contents of a FileField is not JSON serializable which did not work.
Second I thought about the LocalStorage property but if the files exceed the size I will have problems.
Third I thought about getting the file path and then create the audio but as I was reading is a bad practice and can only be obtained if the file is created on the disk, that is, if it is in TemporaryUploadedFile but my files should weigh less than one 1MB
For which I have the option that all files that are loaded with a size smaller than 2.5MB are stored in InMemoryUploadedFile but I do not know how to obtain them. Does anyone know how this is done? or how else can I save a list of temporary audios?

InMemoryUploadedFile is a wrapper around a file object. You can access the file object using the file attribute.
file_in_memory # <InMemoryUploadedFile: xxx (xxx/xxx)>
file_object = file_in_memory.file

ClientSide :
var file = document.getElementById('file');
const formData = new FormData();
formData.append('file', file[0]);
fetch('api/upload_file/',{
method:'POST',
body:formData
}).then(res => console.log(res);
.catch(error => console.log(error);
On Server Side:
InMemoryUploadedFile can be accessed as:
file = request.FILES.get('file')

Related

How to selectively process parts of a large dataset without reloading the page in Django?

I have an app which allows the user to upload a large datafile, process its contents into a python object (not Django model), and then present a summary of the contents to the user. In other words, the big data is present in the view and summarised to the template.
The user then selects which of the content sections to save to the database, and submits the form to do so.
I'm wrestling with how to pass the python object to the AJAX-called function without having to do all the processing again?
I've used AJAX in the past and have read the answers to suggestions for not reloading pages etc, but none of them have involved passing large objects from within a view.
# retrieve the file
storage = TemporaryParseStorage.objects.get(id=item_id)
# open the file from memory
f = open(storage.file.path, "r")
file_text = f.read()
# Parse the file:
parser = Parser()
# Process its contents to create the object - I want to store this
# object and call its member functions based on a button click in the template
objectIWantToKeep = parser.parseModel(file_text)
# Builds tree for preview
tree = build_tree_from_model(storage, model)
context = {
'storage': storage,
'model_name': model.name(),
'tree': tree
}
return render(request, 'main/upload_check.html', context)

Access to file uploaded with FileField Django

I'm trying to create a Django form with a filefield to upload a pdf based on a model.
#models.py
class ProductionRequest(models.Model):
...
contract_file = models.FileField('Contract file (PDF)', upload_to='contracts/')
...
I can upload the file and save it in my object, but when I try to show the file in my template with this code
{{ prod_req.contract_file }}
it only show me the path to the file
"contracts/file_uploaded.pdf". How can I make a link to download (or open in a new tab ) the file ?
Plus : when I try to open the file from Django admin, with this link
Contract file (PDF) : Currently: contracts/file_uploaded.pdf
I don't show me the file, i got this :
Page not found (404) Request URL: http://127.0.0.1:8000/admin/appname/productionrequest/1/change/contracts/file_uploaded.pdf/change/
How can I access to my file ?
It works just like a Python file. So, the way you have just refers to a file object.
To get the text, you need to use f.read(). So, in your template, instead of
{{ prod_req.contract_file }}
use
{{ prod_req.contract_file.read }}
BUT, note that calling read moves the cursor to the end of the file. The first time you call read on a file, it returns all the text. The second time, it returns nothing. This is only a problem if you need the text twice, but in that case, store the text directly on the context object.
Let me know if anything was unclear.

How to split Flask Main file in multiple files and share Variable

I have read alot about this but I just don't seem to figure it out... I should use Blueprint for this but the problem I am having right now is that I do not know how to pass my variable from my main file in my second file.
As an example :
/app
/runserver.py
/app
init.py
main.py
second.py
Now I do have a dictionairy in my main that I fill. And I want to use it in my second file to adjust it etc. How will I be able to do this? Since I tried to import the files and tried:
import main
dictMain = main.dictFromMain
I thought this would be enough since I read it on different question on Stack Overflow but it doesn't seem to work!
EDIT: To sketch the problem further
More background : I am making a client - server application, the client is receiving and sending data from the server. But there is a difference is the data the client is sending. On one hand you have files and paramters which I want to 'capture' with my second file with ReST. And on the other hand I got a incomming stream which I 'capture' in my main file.
Example second file:
#app.route('/uploads/', methods = ['GET', 'POST'])
def get_files():
if request.method == 'GET':
sendDict = []
for element in ctxList:
for fileCtx in element['file']:
d = { 'id' : element['id'], 'file': [ {'name': fileCtx['name'], 'uri' : fileCtx['uri'], 'path' : fileCtx['path'] } ] }
sendDict.append(d)
jsonString = jsonify(ctx=sendDict)
return jsonString
But this code uses a dictionairy from my first file (the dict ctxList) I have no idea to get it out of my first file. I used to get a error when I did : ctxList = mainFile.ctxList that the module did not have this variable, but now I am getting a error that the first file does not know the URL structure ( /uploads/ from the second file).

json to geoDjango model

I am building a database using Django, geodjango and postgresql of field data. The data includes lats and lons. One of the tasks I have is to ingest data that has already been collected. I would like to use .json file to define the metadata and write some code to batch process some json files.
What I have so far is, a model:
class deployment(models.Model):
'''
#brief This is the abstract deployment class.
'''
startPosition=models.PointField()
startTimeStamp=models.DateTimeField()
endTimeStamp=models.DateTimeField()
missionAim=models.TextField()
minDepth=models.FloatField() # IT seems there is no double in Django
maxDepth=models.FloatField()
class auvDeployment(deployment):
'''
#brief AUV meta data
'''
#==================================================#
# StartPosition : <point>
# distanceCovered : <double>
# startTimeStamp : <dateTime>
# endTimeStamp : <dateTime>
# transectShape : <>
# missionAim : <Text>
# minDepth : <double>
# maxDepth : <double>
#--------------------------------------------------#
# Maybe need to add unique AUV fields here later when
# we have more deployments
#==================================================#
transectShape=models.PolygonField()
distanceCovered=models.FloatField()
And I function I want to use to ingest the data
#staticmethod
def importDeploymentFromFile(file):
'''
#brief This function reads in a metadta file that includes campaign information. Destinction between deployment types is made on the fine name. <type><deployment>.<supported text> auvdeployment.json
#param file The file that holds the metata data. formats include .json todo:-> .xml .yaml
'''
catamiWebPortal.logging.info("Importing metadata from " + file)
fileName, fileExtension = os.path.splitext(file)
if fileExtension == '.json':
if os.path.basename(fileName.upper()) == 'AUVDEPLOYMENT':
catamiWebPortal.logging.info("Found valid deployment file")
data = json.load(open(file))
Model = auvDeployment(**data)
Model.save()
And the file I am trying to read in this
{
"id":1,
"startTimeStamp":"2011-09-09 13:20:00",
"endTimeStamp":"2011-10-19 14:23:54",
"missionAim":"for fun times, call luke",
"minDepth":10.0,
"maxDepth":20.0,
"startPosition":{{"type": "PointField", "coordinates": [ 5.000000, 23.000000 ] }},
"distanceCovered":20.0
}
The error that I am getting is this
TypeError: cannot set auvDeployment GeometryProxy with value of type: <type 'dict'>
If I remove the geo types from the model and file. It will read the file and populate the database table.
I would appreciate any advice one how I am parse the datafile with the geotypes.
Thanks
Okay the solution is as follows. The file format is not the geoJSON file format, it's the geos format. The .json file should be as follows.
{
"id": 1,
"startTimeStamp": "2011-10-19 10:23:54",
"endTimeStamp":"2011-10-19 14:23:54",
"missionAim": "for fun times, call luke",
"minDepth":10.0,
"maxDepth":20.0,
"startPosition":"POINT(-23.15 113.12)",
"distanceCovered":20,
"transectShape":"POLYGON((-23.15 113.12, -23.53 113.34, -23.67 112.9, -23.25 112.82, -23.15 113.12))"
}
Not the StartPosition syntax has changed.
A quick fix would be to use the GEOs API in geoDjango to change the startPosition field from geoJson format to a GEOSGeometry object before you save the model. This should allow it to pass validation.
Include the GEOSGeometry function from Django with:
from django.contrib.gis.geos import GEOSGeometry
...
Model = auvDeployment(**data)
Model.startPosition = GEOSGeometry(str(Model.startPosition))
Model.save()
The GEOS API cant construct objects from a GeoJSON format, as long as you make it a string first. As it stands, you are loading it as a dictionary type instead of a string.
I suggest you use the default command for loading fixtures: loaddata
python manage.py loaddata path/to/myfixture.json ...
The structure of your json would have to be slighty adjusted, but you could make a simple dumpdata to see how the structure should look like.

how can i set the key 'blob-key' about BlobStore?

I tried to use the jquery plugin "uploadify" to upload multiple files to My App in Google App-Engine, and then save them with blobstore, but it failed. I traced the code into get_uploads, it seems field.type_options is empty, and of course does not have 'blob-key'. Where does the key 'blob-key' come from?
the code like this:
def upload(request):
for blob in blogstorehelper.get_uploads(request, 'Filedata'):
file = File()
file.blobref = blob
file.save()
return ……
but, blogstorehelper.get_uploads(request, 'Filedata') is always empty. In fact, the request has contained the uploaded file(I print the request). I debugged into the blogstorehelper.get_uploads, and found that field.type_options is empty. who can tell me why? thank you! here is the source about get_uploads: http://appengine-cookbook.appspot.com/recipe/blobstore-get_uploads-helper-function-for-django-request/?id=ahJhcHBlbmdpbmUtY29va2Jvb2tyjwELEgtSZWNpcGVJbmRleCI4YWhKaGNIQmxibWRwYm1VdFkyOXZhMkp2YjJ0eUZBc1NDRU5oZEdWbmIzSjVJZ1pFYW1GdVoyOE0MCxIGUmVjaXBlIjphaEpoY0hCbGJtZHBibVV0WTI5dmEySnZiMnR5RkFzU0NFTmhkR1ZuYjNKNUlnWkVhbUZ1WjI4TTIxDA

Categories

Resources