Skip to content

Commit

Permalink
save document to collection
Browse files Browse the repository at this point in the history
  • Loading branch information
Sandravaphilips committed Jul 17, 2024
1 parent 2d5fbb5 commit 44c5651
Show file tree
Hide file tree
Showing 9 changed files with 181 additions and 48 deletions.
4 changes: 4 additions & 0 deletions peachjam/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
CaseNumber,
CitationLink,
CitationProcessing,
Collection,
CoreDocument,
Court,
CourtClass,
Expand Down Expand Up @@ -69,6 +70,7 @@
PeachJamSettings,
Predicate,
Relationship,
SavedDocument,
SourceFile,
Taxonomy,
UserProfile,
Expand Down Expand Up @@ -1177,6 +1179,8 @@ class AttorneyAdmin(ImportExportMixin, admin.ModelAdmin):
CourtClass,
AttachedFileNature,
CitationProcessing,
Collection,
SavedDocument,
]
)
admin.site.register(PeachJamSettings, PeachJamSettingsAdmin)
Expand Down
57 changes: 50 additions & 7 deletions peachjam/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,21 @@

from django import forms
from django.conf import settings
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User
from django.core.files import File
from django.core.mail import send_mail
from django.http import QueryDict
from django.template.loader import render_to_string
from django.utils.translation import gettext as _

from peachjam.models import AttachedFiles, CoreDocument, SourceFile, pj_settings
from peachjam.models import (
AttachedFiles,
Collection,
CoreDocument,
SavedDocument,
SourceFile,
UserProfile,
pj_settings,
)
from peachjam.plugins import plugins
from peachjam.storage import clean_filename

Expand Down Expand Up @@ -235,9 +241,46 @@ def send_email(self):
)


class SignUpForm(UserCreationForm):
email = forms.EmailField()
class SaveDocumentForm(forms.ModelForm):
collection = forms.ModelChoiceField(queryset=Collection.objects, required=False)
user_profile = forms.ModelChoiceField(
queryset=UserProfile.objects, widget=forms.HiddenInput()
)
document = forms.ModelChoiceField(
queryset=CoreDocument.objects, widget=forms.HiddenInput()
)

class Meta:
model = User
fields = ["username", "email", "password1", "password2"]
model = SavedDocument
fields = "__all__"

def __init__(self, *args, document=None, user_profile=None, **kwargs):
super().__init__(*args, **kwargs)
if document is not None and user_profile is not None:
self.document = document
self.user_profile = user_profile
self.fields["document"].initial = self.document
self.fields["user_profile"].initial = self.user_profile

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


class CollectionForm(forms.ModelForm):
name = forms.CharField(max_length=255, required=True)
user_profile = forms.ModelChoiceField(
queryset=UserProfile.objects, widget=forms.HiddenInput()
)

class Meta:
model = Collection
fields = "__all__"

def __init__(self, *args, user_profile=None, **kwargs):
super().__init__(*args, **kwargs)
if user_profile is not None:
self.user_profile = user_profile
self.fields["user_profile"].initial = self.user_profile

def save(self, commit=True):
return super().save()
4 changes: 1 addition & 3 deletions peachjam/models/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,7 @@ class SavedDocument(models.Model):
related_name="saved_documents",
)
collection = models.ForeignKey(
Collection,
on_delete=models.CASCADE,
verbose_name=_("collection"),
Collection, on_delete=models.CASCADE, verbose_name=_("collection"), null=True
)

class Meta:
Expand Down
33 changes: 25 additions & 8 deletions peachjam/templates/peachjam/_document_detail_toolbar.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,31 @@
{% endif %}
{% endblock %}
{% block download-btn %}
<button class="btn btn-outline-primary btn-shrink-sm ms-2"
data-bs-toggle="modal"
data-bs-target="#documentProblemModal"
hx-get="/collections/"
hx-target="#documentDetailModal"
hx-trigger="click">
Save document
</button>
<div class="dropdown">
<button class="btn btn-outline-primary btn-shrink-sm ms-2 dropdown-toggle"
type="button"
data-bs-toggle="dropdown"
aria-expanded="false"
hx-post="{% url 'save_document' %}"
hx-target="#saveDocument"
hx-include="#collections-form">
Save document
</button>
<div id="saveDocument" class="dropdown-menu p-2">{% include "peachjam/save_document.html" %}</div>
<div class="modal fade"
id="addCollection"
tabindex="-1"
aria-labelledby="addCollection"
aria-hidden="true">
<div class="modal-dialog" id="testing">
<div class="modal-content">
<div class="modal-header">
<p class="h5 modal-title">Add new collection</p>
</div>
</div>
</div>
</div>
</div>
{% if document.source_file %}
<div class="btn-group dropdown-center ms-2">
<a href="{% url 'document_source' document.expression_frbr_uri|strip_first_character %}"
Expand Down
16 changes: 6 additions & 10 deletions peachjam/templates/peachjam/collections_list.html
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
{% load static i18n %}
<div class="modal-header">
<h5 id="SaveDocumentModalTitle" class="modal-title">{% trans 'Where do you want to save your document?' %}</h5>
<button type="button"
class="btn-close"
data-bs-dismiss="modal"
aria-label="Close"/>
</div>
<div class="modal-body">
<div id="SaveDocumentModal" data-vue-component="SaveDocumentModal"></div>
<div class="modal-dialog" id="testing">
<div class="modal-content">
<div class="modal-header">
<p class="h5 modal-title">Add new collection</p>
</div>
</div>
</div>
35 changes: 35 additions & 0 deletions peachjam/templates/peachjam/save_document.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<form id="collections-form" hx-post="{% url 'save_document' %}">
{% if new %}
<h4>Document Saved</h4>
{% else %}
<h2 class="h4">Choose Collection</h2>
{% endif %}
<p class="h6">{{ saved_document }}</p>
{{ save_document_form.document }}
{{ save_document_form.user_profile }}
<label class="form-label"
for="{{ save_document_form.collection.id_for_label }}">
{{ save_document_form.collection.label }}
</label>
<select id="{{ save_document_form.collection.id_for_label }}"
class="form-select"
name="{{ save_document_form.collection.name }}">
{% for value, name in save_document_form.fields.collection.choices %}
<option value="{{ value }}"
{% if save_document_form.fields.collection.value == x %} selected{% endif %}>
{{ name }}
</option>
{% endfor %}
</select>
<div class="d-inline-flex justify-content-between">
<div>
<button type="button"
class="btn btn-outline-secondary btn-shrink-sm ms-2"
data-bs-toggle="modal"
data-bs-target="#addCollection">
Add collection
</button>
</div>
<button class="btn btn-primary" type="submit">Done</button>
</div>
</form>
4 changes: 2 additions & 2 deletions peachjam/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
ArticleYearArchiveView,
AuthorDetailView,
BookListView,
CollectionListView,
CourtClassDetailView,
CourtClassMonthView,
CourtClassYearView,
Expand Down Expand Up @@ -66,6 +65,7 @@
PlaceDetailView,
PocketLawResources,
RobotsView,
SaveDocumentView,
TaxonomyDetailView,
TaxonomyFirstLevelView,
TaxonomyListView,
Expand All @@ -91,7 +91,7 @@
AboutPageView.as_view(),
name="about",
),
path("collections/", CollectionListView.as_view(), name="collections"),
path("save-document/", SaveDocumentView.as_view(), name="save_document"),
# listing views
path(
"authors/<slug:code>/",
Expand Down
62 changes: 45 additions & 17 deletions peachjam/views/collection.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,57 @@
from typing import Any

from django.contrib.auth import get_user_model
from django.db.models.query import QuerySet
from django.shortcuts import render
from django.views.generic import ListView
from django.http import HttpRequest
from django.shortcuts import get_object_or_404
from django.views.generic import TemplateView

from peachjam.models import Collection
from peachjam.forms import CollectionForm, SaveDocumentForm
from peachjam.models import SavedDocument, UserProfile

User = get_user_model()


class CollectionListView(ListView):
model = Collection
class CreateCollectionView(TemplateView):
template_name = "peachjam/collections_list.html"
context_object_name = "collections"
navbar_link = "collections"
paginate_by = 10

def get_queryset(self) -> QuerySet[Any]:
# print(super().get_queryset().filter(user=User(self.request.user.id)))
return super().get_queryset().filter(user=User(self.request.user.id))
def post(self, request: HttpRequest):
post_data = request.POST
user_profile = get_object_or_404(UserProfile, user=self.request.user)
form = CollectionForm(post_data, user_profile=user_profile)
context = self.get_context_data()
if form.is_valid():
instance = form.save()
return self.render_to_response(
{
"saved_document": instance,
"add_new_collection_form": form,
**context,
}
)

return self.render_to_response({"add_new_collection_form": form, **context})


class SaveDocumentView(TemplateView):
template_name = "peachjam/save_document.html"

def get_context_data(self, *args, **kwargs):
context = super().get_context_data(*args, **kwargs)
context = super().get_context_data(**kwargs)
user_profile = get_object_or_404(UserProfile, user=self.request.user)
collections = user_profile.collections.all()
context["collections"] = collections
return context

def render_collections(self, request):
return render(request, "peachjam/collections_list.html")
def post(self, request: HttpRequest):
post_data = request.POST
instance = SavedDocument.objects.filter(
document=post_data.get("document"),
user_profile=post_data.get("user_profile"),
).first()
form = SaveDocumentForm(post_data, instance=instance)
context = self.get_context_data()
if form.is_valid():
instance = form.save()
return self.render_to_response(
{"saved_document": instance, "save_document_form": form, **context}
)

return self.render_to_response({"save_document_form": form, **context})
14 changes: 13 additions & 1 deletion peachjam/views/generic_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from django.views.generic import DetailView, ListView, View
from lxml import html

from peachjam.forms import BaseDocumentFilterForm
from peachjam.forms import BaseDocumentFilterForm, SaveDocumentForm
from peachjam.helpers import add_slash, get_language, lowercase_alphabet
from peachjam.models import (
Author,
Expand All @@ -16,6 +16,8 @@
LegalInstrument,
Predicate,
Relationship,
SavedDocument,
UserProfile,
)
from peachjam_api.serializers import (
CitationLinkSerializer,
Expand Down Expand Up @@ -187,6 +189,16 @@ def get_context_data(self, **kwargs):
context["documents_citing_current_doc"] = self.fetch_citation_docs(
doc.work.works_citing_current_work()
)
user_profile = UserProfile.objects.filter(user=self.request.user).first()
instance = SavedDocument.objects.filter(
document=self.get_object(), user_profile=user_profile
).first()
context["save_document_form"] = SaveDocumentForm(
instance=instance,
document=self.get_object(),
user_profile=user_profile,
initial={"document": self.get_object(), "user_profile": user_profile},
)

return context

Expand Down

0 comments on commit 44c5651

Please sign in to comment.