Skip to content

Commit

Permalink
Add "import images" as a method for creating a new deck
Browse files Browse the repository at this point in the history
  • Loading branch information
sheepman4267 committed Oct 24, 2024
1 parent 33e16ce commit a27245e
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 5 deletions.
37 changes: 35 additions & 2 deletions OpenShow/slides/editor/forms.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from django.forms import Form, ModelForm, IntegerField, ChoiceField, Select
from django.forms import Form, ModelForm, IntegerField, ChoiceField, Select, ClearableFileInput, FileField, CharField
from django.urls import reverse_lazy
from ..models import Show, Theme, SlideElement
from ..models import Show, Theme, SlideElement, Deck


# class SimpleShowForm(ModelForm):
Expand Down Expand Up @@ -53,3 +53,36 @@ class Meta:
def save(self, commit=True):
self.instance.body = self.cleaned_data['body'].replace('\n', '<br>')
return super(EditSlideElementTextForm, self).save()


class MultipleFileInput(ClearableFileInput):
allow_multiple_selected = True


class MultipleFileField(FileField):
def __init__(self, *args, **kwargs):
kwargs.setdefault("widget", MultipleFileInput())
super().__init__(*args, **kwargs)

def clean(self, data, initial=None):
single_file_clean = super().clean
if isinstance(data, (list, tuple)):
result = [single_file_clean(d, initial) for d in data]
else:
result = single_file_clean(data, initial)
return result


class DeckFromImagesForm(ModelForm):
class Meta:
model = Deck
fields = [
'name',
'default_transition',
'default_transition_duration',
'default_auto_advance',
'default_auto_advance_duration',
'theme',
]
files = MultipleFileField()
image_css_class = CharField()
9 changes: 9 additions & 0 deletions OpenShow/slides/editor/templates/editor/import_deck.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{% extends 'core/base.html' %}

{% block content %}
<form method="post" action="{% url 'import-deck-from-images' %}" enctype="multipart/form-data">
{% csrf_token %}
{{ form }}
<input type="submit">
</form>
{% endblock %}
3 changes: 2 additions & 1 deletion OpenShow/slides/editor/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
SlideElementUpdateTextView, SlideElementUpdateCSSClassView, SlideElementUpdateImageView, \
SlideElementUpdateVideoView, ChangeSlideElementOrderView, SlideElementUpdateMediaObjectView
from slides.editor.views.deck import DeckCreateView, DeckEditorView, DeckDeleteView, push_deck_cues, \
push_deck_slide_text, pull_aoml_text
push_deck_slide_text, pull_aoml_text, DeckFromImagesView
from slides.editor.views.theme import ThemeUpdateView, ThemeCreateView, ThemeDeleteView
from slides.editor.views.utils import generate_lorem
from slides.editor.views.transition import TransitionEditorView, TransitionCreateView, TransitionKeyframeCreateView, \
Expand Down Expand Up @@ -43,6 +43,7 @@
path('deck/<int:pk>/push-cues', push_deck_cues, name='push-deck-cues'),
path('deck/<int:pk>/push-text', push_deck_slide_text, name='push-deck-text'),
path('deck/<int:pk>/pull-aoml', pull_aoml_text, name='pull-aoml-text'),
path('deck/import/images', DeckFromImagesView.as_view(), name='import-deck-from-images'),
path('theme/new', ThemeCreateView.as_view(), name='new-theme'),
path('theme/<int:pk>', ThemeUpdateView.as_view(), name='edit-theme'),
path('theme/<int:pk>/delete', ThemeDeleteView.as_view(), name='delete-theme'),
Expand Down
33 changes: 31 additions & 2 deletions OpenShow/slides/editor/views/deck.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from django.views.generic import CreateView, UpdateView, DeleteView
from django.views.generic import CreateView, UpdateView, DeleteView, FormView
from django.urls import reverse_lazy
from slides.models import Deck, Slide
from slides.models import Deck, Slide, SlideElement
from django.db import transaction
from django.shortcuts import get_object_or_404, HttpResponseRedirect
import slides.aoml_parser as aoml
from slides.editor.forms import DeckFromImagesForm


class DeckCreateView(CreateView):
Expand Down Expand Up @@ -75,3 +76,31 @@ def pull_aoml_text(request, pk):
deck.slide_text_markup = deck.pull_aoml()
deck.save()
return HttpResponseRedirect(deck.get_absolute_url())


class DeckFromImagesView(FormView):
form_class = DeckFromImagesForm
template_name = 'editor/snippets/hx-simple_create_form.html'
extra_context = {
'previous_page': 'slides-index',
'action': 'import-deck-from-images',
'object_type': 'Deck From Many Images',
}

def form_valid(self, form):
files = form.cleaned_data['files']
print(files)
print('^^FILES')
form.save()
for image in files:
new_slide = Slide(deck=form.instance)
new_slide.save()
new_slide_element = SlideElement(
css_class=form.cleaned_data["image_css_class"],
order=0,
slide=new_slide,
image=image,
body="",
)
new_slide_element.save()
return HttpResponseRedirect(reverse_lazy('edit-deck', kwargs={'pk': form.instance.pk}))
3 changes: 3 additions & 0 deletions OpenShow/slides/templates/slides/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ <h2>Decks</h2>
<button hx-get="{% url 'new-deck' %}" hx-swap="outerHTML">
New Deck
</button>
<button hx-get="{% url 'import-deck-from-images' %}" hx-swap="outerHTML">
Import Images
</button>
<ul>
{% for deck in deck_list %}
<li>
Expand Down

0 comments on commit a27245e

Please sign in to comment.