Flask Uploads Photo class - python

Trying to write a website that will allow users to upload photos, and so far, I've been using the Flask-Uploads library (docs here)
Used in the example in the docs is a class Photo that seems fairly critical:
rec = Photo(filename=filename, user=g.user.id)
rec.store()
...
photo = Photo.load(id)
Problem being that the name Photo doesn't exist in flask.ext.uploads and I'm not sure where to upload it from.
Has anyone else experienced the same issue?

Photo is a model class that you'd need to define. If you take a look at the example application you'll see a class named Post.
post = Post(title=title, caption=caption, filename=filename)
post.id = unique_id()
post.store()
This particular example was made using Flask-CouchDB, but you can use any data store you'd like. You'll just need to replace the lines that save the Photo or Post with however you save references to uploaded files in your application.

Related

Django; What is preferable way to upload some images?

I've been working on Django project. Currently, user can upload only one image but now I want to change it so user can upload some images.
models.py is like this.
class Entry(models.Model):
photo = models.ImageField(...)
I am thinking of just adding photo2 and photo3 inside Entry model. But I'm wondering if there's a better way to do it. I don't want to delete images that are already uploaded. Anyone who could give me tips? Also, I don't like the file upload form's design and some people use just button-like form. I also want to know how to create button-like form.
Create new model called 'Photo'.
Create relationship from 'Entry' to 'Photo'.
If you want to use photos that related with Entry record, you need to use select_related() function
Also you can find button-like upload form here.

Django can StdImageField replace default variation?

I have existing Django-based project with some files uploaded.
I need to add a feature to automatically resize new uploaded files to some resolution (200x200). I found a nice library django-stdimage that does what I need.
But on upload it stores original file with its original resolution. And existing code works with the original file instead of resized one.
class Product(models.Model):
name = models.CharField(verbose_name=_('Name'), max_length=64)
image = StdImageField(upload_to='product_images/', verbose_name=_('Image'), blank=True, null=True,
variations={'default': (200, 200)})
I would like to save processed files by the same name as original file. I do not need original file by the way.
I do not want to change all the code where it works with image field - there are complex DRF serializers, some views, forms, templates, etc.
So I would like to get new resized image as before by using myproduct.image - in templates for example.
Is it possible to do without subclassing StdImageField ?
I opened a ticket on the StdImage's page and received an answer..
No, I'm sorry, but nether would this work, nor would I advice you to
do such a thing. It would add implicit behavior. That's something you
might want to avoid long term.
You can always overwrite the field tho and implement your own
behavior. The StdImage code base should be good guidance to implement
your own behavior.

How to do a user image upload - Django

I really need a lot of help with this and I dont know where to start since I am new to Django so excuse my ignorance.
I'll explain. I am building kind of a image uploading-based page. The images are grouped into albums. I created the album model and the image model which belongs to an album and it has its foreign key. But I dont know how to do the upload itself. I thought I just had to handle the data taken from the template in the views.py file and kind of upload it; but I am getting confused with some things I saw about a form... I dont know. I have no idea.
So its simple: How do I upload the image and register a new row in the database, submitting all the additional data (like pic description) the module requires? Please help with this.
Looks like you are confuse about django forms. If you define the forms.py and simply do {{ form }} will render all the field in html so you don't need to worry about defining in html form field. or Don't use form... Define all the field by yourself (!mportant Client side validation is must )
Write the views like save the file into Image Table that's it. If you need anyhelp here . Happy to assist you ..

How to handle uploaded files in webapp2

Google appengine's webapp2 has a very cryptic documentation regarding the handling of uploaded files.
Uploaded files are available as cgi.FieldStorage (see the cgi module) instances directly in request.POST.
I have a form which makes a POST request of JSON files which I want to store in an NDB.JsonProperty.
Can anyone offer a short example of how do I read the file from the request object?
You can use enctype="multipart/form-data" in your form, and then get file content by using in your handler:
raw_file = self.request.get('field_name')
Then, pass raw_file as input to your model's property.
Google's document just sucks. I've spent about two hours experimenting with webapp2's request object and finally figures out a way to do this.
Check https://stackoverflow.com/a/30969728/2310396.
The basic code snippets is here:
class UploadHandler(BaseHandler):
def post(self):
attachments = self.request.POST.getall('attachments')
_attachments = [{'content': f.file.read(),
'filename': f.filename} for f in attachments]
We use self.request.POST.getall('attachments') instead of self.request.POST.get('attachments'), since they may be multiple input field in HTML forms with the same name, so if you just use self.request.POST.get('attachments'), you'll only get one of them.
Instead of using the solution described in How does cgi.FieldStorage store files?, I used enctype="multipart/form-data" in the form, and
in the handler method for the post I accessed the files via:
file_content = self.request.POST.multi['myfieldname'].file.read()
it worked!

Django Temporary Images

Hi I have this type of model
class Post(models.Model):
title = models.CharField()
image = models.ImageField()
# other fields
For creating a new post I would a flow like this:
in the new post page 1 the user choose the photo and upload it
(submit)
in the new post page 2 the user can see the photo and fill other fields like 'title'
My question is how to upload a photo and display it in the next page, without creating a new Post object with the image field. Should I save the image in a temporany directory? In this case, how can I keep the reference and use it in the second page?
Thanks.
There are couple of solutions
Define another model to just hold images, and add may be OneToOneField in your Post model. On submitting page1, create instance of this image model and put it in page2 as image field.
With html5 file API, you can refer to local file in image field. So you can show image on page2 using local client side path w/o storing it at server. refer: View image selected from file-system on client-side before upload?
First read the relevant doc so you understant what happens with uploaded files:
https://docs.djangoproject.com/en/dev/topics/http/file-uploads/
The simplest solution IMHO would be to
1/ save the uploaded file in some temporary directory - just make sure you can serve the content one way or another (either directly thru the front webserver, or thru a custom view if you want more control on who can access this content),
2/ pass the temporary file path and url to the "next" view one way or another (hidden form fields, session...).
I use to have table to store images (any files in fact :)). Each image is linked to object (FK) and has status - Draft, Approved, Deleted. So I don't use temporary directory, just change status in table of images.

Categories

Resources