Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Easier File and Image model fields usage #507

Closed

Conversation

DylannCordel
Copy link
Contributor

With this pull request, when a dev define an Image's foeignKey, he can configure :

  • available mimetypes via the FileMimetypeValidator : takes a list of allowed mime subtypes (e.g: image/jpeg) and/or mimetypes (e.g: image/*)
  • choose which upload method is available when the widget will be displayed (by default : old method is activated (file lookup). the second one is "direct upload" : a simple "browse" button : when we choose a file, ajax upload is performed. Both methods can be enabled together : choose a file from the website ou on the PC)
  • default folder where the file will be uploaded : when direct upload is enabled and the user upload a file, the new file will be linked to the configured folder. It's done by sending a folder "key" which must be a class method of a DefaultFolderGetter child. This method takes the request and must return a folder.

Tests for "dynamic" folder destination and mimetype detections have been added.
If you think this pull request is usefull and mergeable, I'll add:

  • documentation
  • tests for third party models with File/Image foreign keys.
  • direct_upload will also send a "related_field" data of this format : "model_name.field_name" (e.g: 'News.illustration'). direct_upload's view will be able to retrieve validators associated to this field and reject files during the ajax upload and not only when the object (the news) is saved.

Simple usage exemple:

#my_news_app/models.py
# [...] imports and other required things...

class News(NewsboxBase):
    illustration = FilerImageField(
        verbose_name=_('illustration'), 
        default_direct_upload_enabled=True, 
        #add a "browse" button with ajax upload
        default_file_lookup_enabled=False, 
        #remove the file lookup link. The user will only have a browse button to add new files
        direct_upload_folder_key='USER_OWN_FOLDER', 
        #file will be uploaded in a specifc folder owned by the authenticated user
        validators=[FileMimetypeValidator(['image/png', 'image/jpeg'])], 
        #only jpeg and PNG are allowed
        null=True, blank=True)
#my_news_app/utils.py
from filer.utils.folders import DefaultFolderGetter

class MyCustomFolderGetter(DefaultFolderGetter)
    @classmethod
    def USER_OWN_FOLDER(cls, request):
        if not request.user.is_authenticated():
            return None
        folder = Folder.objects.filter(owner=request.user, parent_id=USERS_FOLDER_PK)[0:1]
        if not folder:
            folder = Folder()
            folder.name = request.user.username
            folder.parent_id = USERS_FOLDER_PK
            folder.owner = request.user
            folder.save()
        else:
            folder = folder[0]
        return folder
#project/settings.py

FILER_DEFAULT_FOLDER_GETTER = 'my_news_app.utils.MyCustomFolderGetter'

@coveralls
Copy link

Coverage Status

Coverage decreased (-0.44%) to 66.32% when pulling 0beb3b9 on webu:feature/easier-file-usage into 369e948 on stefanfoulis:develop.

6 similar comments
@coveralls
Copy link

Coverage Status

Coverage decreased (-0.44%) to 66.32% when pulling 0beb3b9 on webu:feature/easier-file-usage into 369e948 on stefanfoulis:develop.

@coveralls
Copy link

Coverage Status

Coverage decreased (-0.44%) to 66.32% when pulling 0beb3b9 on webu:feature/easier-file-usage into 369e948 on stefanfoulis:develop.

@coveralls
Copy link

Coverage Status

Coverage decreased (-0.44%) to 66.32% when pulling 0beb3b9 on webu:feature/easier-file-usage into 369e948 on stefanfoulis:develop.

@coveralls
Copy link

Coverage Status

Coverage decreased (-0.44%) to 66.32% when pulling 0beb3b9 on webu:feature/easier-file-usage into 369e948 on stefanfoulis:develop.

@coveralls
Copy link

Coverage Status

Coverage decreased (-0.44%) to 66.32% when pulling 0beb3b9 on webu:feature/easier-file-usage into 369e948 on stefanfoulis:develop.

@coveralls
Copy link

Coverage Status

Coverage decreased (-0.44%) to 66.32% when pulling 0beb3b9 on webu:feature/easier-file-usage into 369e948 on stefanfoulis:develop.

@coveralls
Copy link

Coverage Status

Coverage decreased (-0.44%) to 66.32% when pulling addac55 on webu:feature/easier-file-usage into 369e948 on stefanfoulis:develop.

5 similar comments
@coveralls
Copy link

Coverage Status

Coverage decreased (-0.44%) to 66.32% when pulling addac55 on webu:feature/easier-file-usage into 369e948 on stefanfoulis:develop.

@coveralls
Copy link

Coverage Status

Coverage decreased (-0.44%) to 66.32% when pulling addac55 on webu:feature/easier-file-usage into 369e948 on stefanfoulis:develop.

@coveralls
Copy link

Coverage Status

Coverage decreased (-0.44%) to 66.32% when pulling addac55 on webu:feature/easier-file-usage into 369e948 on stefanfoulis:develop.

@coveralls
Copy link

Coverage Status

Coverage decreased (-0.44%) to 66.32% when pulling addac55 on webu:feature/easier-file-usage into 369e948 on stefanfoulis:develop.

@coveralls
Copy link

Coverage Status

Coverage decreased (-0.44%) to 66.32% when pulling addac55 on webu:feature/easier-file-usage into 369e948 on stefanfoulis:develop.

@yakky
Copy link
Member

yakky commented Apr 5, 2015

@DylannCordel this is awesome!
Please go ahead

@coveralls
Copy link

Coverage Status

Coverage increased (+2.67%) to 71.5% when pulling cafdec8 on webu:feature/easier-file-usage into 04b4473 on stefanfoulis:develop.

1 similar comment
@coveralls
Copy link

Coverage Status

Coverage increased (+2.67%) to 71.5% when pulling cafdec8 on webu:feature/easier-file-usage into 04b4473 on stefanfoulis:develop.

@DylannCordel
Copy link
Contributor Author

I rebased with the "develop" branch ( @yakky : sorry for noise : I did a pull instead of a rebase and I pushed a real mess... Thx to @ollb for his help about git to clean it)

I added:

  • documentation
  • tests for third party models with File/Image foreign keys. As I test HTML content to check if file_lookup and/or file_choose buttons are available, I added lxml to dependencies of tests.
  • direct_upload will also send a "related_field" data of this format : app_label.model_name.field_name
    (e.g: myapp.News.illustration). direct_upload's view will be able to retrieve validators associated
    to this field and reject files during the ajax upload and not only when the object (the news)
    is saved.

I'd like to add tests for Reversed ForeignKey relations (thirdparty_app has already a ExampleGalleryElement with a FK related to gallery_elements on Example model), but this is not only for this PR : those tests should exist. In fact, I would like to add tests with Selenium because django-file have a great part of his features which need javascript and are currently not tested. I'll open an issue for this precisely.

@yakky yakky added the feature label Jun 14, 2015
@DylannCordel DylannCordel deleted the feature/easier-file-usage branch September 14, 2015 08:44
@DylannCordel DylannCordel restored the feature/easier-file-usage branch September 14, 2015 08:49
@DylannCordel DylannCordel reopened this Sep 14, 2015
@DylannCordel
Copy link
Contributor Author

rebased with develop

@mkoistinen
Copy link
Contributor

@DylannCordel I really like this PR. It looks like we've been making it hard for you to keep up. Sorry about that. Can you rebase one more time and I'll try to get this into a release soon.

@DylannCordel
Copy link
Contributor Author

@mkoistinen okay, I'll do this ASAP.

* mimetype validator (for File's FK and for ajax uploads)
* allow to configure a default folder destination for each filer model field
* Add a direct (ajax) upload with a simple "browse" button and allow to
  disable the default "file lookup" for each filer model fields.
* Add a related_field param for ajax upload to retrieve model's field's
  mimetype validators and reject wrong files at the begening of the upload to
  avoid "upload then reject" for huge files.
JS are only in external files managed by the form
to be compatible with a HTML layout with javascript
added at the bottom of the body (as it is recommended)
@DylannCordel
Copy link
Contributor Author

Code is rebased. I had to throw in trash some part of my work because of the dropzone addition which duplicates a part of the features of this PR (which will be 1 year old soon).

@mkoistinen mkoistinen added this to the 1.3.0 milestone Jun 21, 2016
@mkoistinen
Copy link
Contributor

@DylannCordel we're preparing a release 1.3 now, any chance you could (yet again, sorry!) rebase?

@yakky yakky removed this from the 1.3.0 milestone Apr 9, 2018
@qomhmd
Copy link

qomhmd commented Jul 1, 2021

+1 for this

@stale
Copy link

stale bot commented Jul 28, 2022

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Jul 28, 2022
@stale
Copy link

stale bot commented Aug 30, 2022

This will now be closed due to inactivity, but feel free to reopen it.

@stale stale bot closed this Aug 30, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants