From 7485d6d75aec0b52b955fd4c5a910be4b45c2cc4 Mon Sep 17 00:00:00 2001 From: Adam Hill Date: Tue, 9 Mar 2021 21:34:50 -0500 Subject: [PATCH] Initial first thoughts about how file uploading might work. --- django_unicorn/static/js/element.js | 10 ++++++++++ example/coffee/models.py | 8 +++++++- example/unicorn/components/js.py | 9 +++++++++ example/unicorn/forms.py | 11 +++++++++++ example/unicorn/templates/unicorn/js.html | 8 ++++++++ 5 files changed, 45 insertions(+), 1 deletion(-) diff --git a/django_unicorn/static/js/element.js b/django_unicorn/static/js/element.js index a8bdb38f..50ca60de 100644 --- a/django_unicorn/static/js/element.js +++ b/django_unicorn/static/js/element.js @@ -36,6 +36,7 @@ export class Element { this.key = null; this.events = []; this.errors = []; + this.file = {}; if (!this.el.attributes) { return; @@ -62,6 +63,15 @@ export class Element { this[key].debounceTime = attribute.modifiers.debounce ? parseInt(attribute.modifiers.debounce, 10) || -1 : -1; + + if (this.el.type === "file") { + this.file = { + accept: this.el.accept, + multiple: this.el.multiple, + }; + + // console.log("this.file", this.file); + } } else if (attribute.isDb) { this.db.name = attribute.value; } else if (attribute.isPK) { diff --git a/example/coffee/models.py b/example/coffee/models.py index 0aaefb97..926f416f 100644 --- a/example/coffee/models.py +++ b/example/coffee/models.py @@ -1,6 +1,6 @@ import uuid -from django.db.models import SET_NULL, ForeignKey, Model +from django.db.models import SET_NULL, FileField, ForeignKey, Model from django.db.models.fields import ( CharField, DateField, @@ -27,3 +27,9 @@ class Flavor(Model): def __str__(self): return self.name + + +class Document(Model): + description = CharField(max_length=255, blank=True) + document = FileField() + uploaded_at = DateTimeField(auto_now_add=True) diff --git a/example/unicorn/components/js.py b/example/unicorn/components/js.py index c595bfbc..97d1256c 100644 --- a/example/unicorn/components/js.py +++ b/example/unicorn/components/js.py @@ -2,8 +2,14 @@ from django_unicorn.components import UnicornView +from ..forms import DocumentForm + class JsView(UnicornView): + form_class = DocumentForm + + document = "" + states = ( "Alabama", "Alaska", @@ -27,5 +33,8 @@ def select_state(self, val, idx): print("select_state called idx", idx) self.selected_state = val + def upload_file(self): + print("UPLOAD") + class Meta: javascript_excludes = ("states",) diff --git a/example/unicorn/forms.py b/example/unicorn/forms.py index eac9b29e..0b24a5a4 100644 --- a/example/unicorn/forms.py +++ b/example/unicorn/forms.py @@ -1,7 +1,18 @@ from django import forms +from example.coffee.models import Document + class ValidationForm(forms.Form): text = forms.CharField(min_length=3, max_length=10) date_time = forms.DateTimeField() number = forms.IntegerField() + + +class DocumentForm(forms.ModelForm): + class Meta: + model = Document + fields = ( + "description", + "document", + ) diff --git a/example/unicorn/templates/unicorn/js.html b/example/unicorn/templates/unicorn/js.html index a9299d48..3e04217f 100644 --- a/example/unicorn/templates/unicorn/js.html +++ b/example/unicorn/templates/unicorn/js.html @@ -54,4 +54,12 @@

Select2

select2_datetime: {{ select2_datetime }} + +
+
+ + + +
+
\ No newline at end of file