Skip to content

Commit

Permalink
used one modal to save document
Browse files Browse the repository at this point in the history
  • Loading branch information
Sandravaphilips committed Jul 24, 2024
1 parent 5f5a1fa commit 1d8fc1f
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 27 deletions.
7 changes: 4 additions & 3 deletions peachjam/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,7 @@ def send_email(self):

class SaveDocumentForm(forms.ModelForm):
folder = forms.ModelChoiceField(queryset=Folder.objects, required=False)
new_folder = forms.CharField(max_length=255, required=False)
user_profile = forms.ModelChoiceField(
queryset=UserProfile.objects, widget=forms.HiddenInput()
)
Expand All @@ -284,7 +285,7 @@ class SaveDocumentForm(forms.ModelForm):

class Meta:
model = SavedDocument
fields = "__all__"
fields = ["user_profile", "document", "folder", "new_folder"]

def __init__(self, *args, document=None, user_profile=None, **kwargs):
super().__init__(*args, **kwargs)
Expand All @@ -294,8 +295,8 @@ def __init__(self, *args, document=None, user_profile=None, **kwargs):
self.fields["document"].initial = self.document
self.fields["user_profile"].initial = self.user_profile

# def save(self, commit=True):
# return super().save()
def save(self, commit=True):
return super().save()


class FolderForm(forms.ModelForm):
Expand Down
7 changes: 0 additions & 7 deletions peachjam/templates/peachjam/_document_detail_toolbar.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,6 @@
{% endif %}
</button>
<div id="saveDocument" class="modal fade">{% include "peachjam/save_document.html" %}</div>
<div class="modal fade"
id="addFolder"
tabindex="-1"
aria-labelledby="addFolder"
aria-hidden="true">
{% include "peachjam/folders_list.html" %}
</div>
</div>
{% if document.source_file %}
<div class="btn-group dropdown-center ms-2">
Expand Down
1 change: 1 addition & 0 deletions peachjam/templates/peachjam/_header.html
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
<a class="dropdown-item" href="{% url 'admin:index' %}">{% trans 'Admin' %}</a>
{% endif %}
<a class="dropdown-item" href="{% url 'account_logout' %}">{% trans 'Logout' %}</a>
<a class="dropdown-item" href="">{% trans 'Dashboard' %}</a>
</li>
</ul>
</li>
Expand Down
37 changes: 22 additions & 15 deletions peachjam/templates/peachjam/save_document.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,6 @@
aria-label="Close"></button>
</div>
<div class="modal-body">
{% if updated %}
<div class="alert alert-success">
{{ document }} has been saved
{% if saved_document.folder %}to the {{ saved_document.folder }} folder.{% endif %}
</div>
{% endif %}
{% if deleted %}<div class="alert alert-danger">{{ document }} has been deleted successfully</div>{% endif %}
<form id="save_document-form"
hx-post="{% url 'save_document' %}"
hx-target="#saveDocument">
Expand All @@ -38,13 +31,29 @@
</option>
{% endfor %}
</select>
<button type="button"
class="btn btn-link ps-0"
data-bs-toggle="collapse"
data-bs-target="#addFolder"
aria-expanded="false"
aria-controls="addFolder">
<i class="bi bi-plus-circle"></i> New folder
</button>
<div class="collapse" id="addFolder">
<label class="form-label"
for="{{ save_document_form.new_folder.id_for_label }}">Give your new folder a name:</label>
<input class="form-control"
id="{{ save_document_form.new_folder.id_for_label }}"
name="{{ save_document_form.new_folder.name }}"
type="text"/>
</div>
</form>
</div>
<div class="modal-footer d-flex justify-content-between">
<button class="btn btn-danger"
type="button"
data-bs-dismiss="modal"
hx-target="#saveDocument"
{% if not saved_document %} disabled{% endif %}
hx-include="#save_document-form"
hx-delete="{% url 'save_document' %}?id={{ saved_document.id }}"
hx-confirm="Are you sure you want to unsave this document?">
Expand All @@ -53,13 +62,11 @@
<div>
<button type="button"
class="btn btn-outline-secondary btn-shrink-sm ms-2"
data-bs-toggle="modal"
data-bs-target="#addFolder"
hx-get="{% url 'folders' %}"
hx-target="#addFolder">
New Folder
</button>
<button class="btn btn-primary ms-1" type="submit" form="save_document-form">Done</button>
data-bs-dismiss="modal">Cancel</button>
<button class="btn btn-primary ms-1"
type="submit"
form="save_document-form"
data-bs-dismiss="modal">Save</button>
</div>
</div>
</div>
Expand Down
11 changes: 9 additions & 2 deletions peachjam/views/folder.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from django.views.generic import TemplateView

from peachjam.forms import FolderForm, SaveDocumentForm
from peachjam.models import CoreDocument, SavedDocument, UserProfile
from peachjam.models import CoreDocument, Folder, SavedDocument, UserProfile

User = get_user_model()

Expand Down Expand Up @@ -55,7 +55,14 @@ def post(self, request: HttpRequest):
form = SaveDocumentForm(post_data, instance=instance)
context = self.get_context_data()
if form.is_valid():
instance = form.save()
if post_data["new_folder"]:
instance = form.save(commit=False)
instance.folder = Folder.objects.get_or_create(
name=post_data["new_folder"], user_profile=instance.user_profile
)[0]
instance.save()
else:
instance = form.save()
form = SaveDocumentForm(
document=instance.document,
user_profile=instance.user_profile,
Expand Down

0 comments on commit 1d8fc1f

Please sign in to comment.