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

File uploading #167

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions django_unicorn/static/js/element.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export class Element {
this.key = null;
this.events = [];
this.errors = [];
this.file = {};

if (!this.el.attributes) {
return;
Expand All @@ -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) {
Expand Down
8 changes: 7 additions & 1 deletion example/coffee/models.py
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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)
9 changes: 9 additions & 0 deletions example/unicorn/components/js.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,14 @@

from django_unicorn.components import UnicornView

from ..forms import DocumentForm


class JsView(UnicornView):
form_class = DocumentForm

document = ""

states = (
"Alabama",
"Alaska",
Expand All @@ -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",)
11 changes: 11 additions & 0 deletions example/unicorn/forms.py
Original file line number Diff line number Diff line change
@@ -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",
)
8 changes: 8 additions & 0 deletions example/unicorn/templates/unicorn/js.html
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,12 @@ <h2>Select2</h2>
select2_datetime: {{ select2_datetime }}
</div>
</div>

<div>
<form wire:submit.prevent="upload_file">

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this meant to be unicorn:submit?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes! This would need to be unicorn:submit once this is all working.

<input id="blob" type="file" u:model="document" />

<button type="submit">Upload</button>
</form>
</div>
</div>