vendredi 24 juin 2016

Implement chunked upload for django's model field


The Technical Problem: realize such a model field that would upload file by chunks. Details: I know about https://github.com/blueimp/jQuery-File-Upload and I'm going to use it. As I undestand Django's architecture: SomeModelField class has formfield function, where we can define 'form_class', that point to SomeFormField. Defined SomeFormField has attribute widget, that could contain SomeWidget. SomeWidget could have Media class that could have js variable, which contains tuple of linked scripts. urls.py,pointing to some View function view function Suppose following sources(aren't complete) in accordance with my current understanding: SomeModelField Class class ChunkedFileField(models.FileField): def __init__(self, *args, **kwargs): super(ChunkedFileField, self).__init__(*args, **kwargs) description = "Chunked file" __metaclass__ = models.SubfieldBase def __init__(self, *args, **kwargs): kwargs['max_length'] = len(UPLOAD_TO) + 1 + FILE_NAME_MAX_LENGTH super(ChunkedFileField, self).__init__(*args, **kwargs) def formfield(self, form_class=None, choices_form_class=None, **kwargs): defaults = {'form_class': ChunkedFileFormField} defaults.update(kwargs) return super(ChunkedFileField, self).formfield(**defaults) SomeFormField Class class ChunkedFileFormField(forms.FileField): widget = ChunkedFileUploadWidget def __init__(self, *args, **kwargs): print 'ChunkedUploadFileField' super(ChunkedFileFormField, self).__init__(*args, **kwargs) SomeWidget Class class ChunkedFileUploadWidget(ClearableFileInput): def __init__(self, attrs=None): super(ChunkedFileUploadWidget, self).__init__(attrs) self.attrs['id'] = 'chunked_upload' class Media: js = ( 'js/cu.js', 'js/jquery.js', 'js/jquery.ui.widget.js', 'js/jquery.iframe-transport.js', 'js/jquery.fileupload.js', 'js/spark-md5.js', 'js/ksb_django_fields.js', ) Question: Am I on right way of constructing chunked uploading for model's field? I'm don't understand, how can I solve such situation: If I have form with set of field, one of which is my ChunkedFileFormField, POST request for others field should happen only once, but for my Chunked Field POST request should happen for chunked quantity times. What is the optimal way to realize my problem? P.S.: if it possible with article links, code examples or git sources references.

Aucun commentaire:

Enregistrer un commentaire