Skip to content

Commit

Permalink
adds recaptcha; allows management of savedoc feature
Browse files Browse the repository at this point in the history
  • Loading branch information
actlikewill committed Jul 30, 2024
1 parent 26232db commit 4bb2dd4
Show file tree
Hide file tree
Showing 11 changed files with 111 additions and 52 deletions.
11 changes: 11 additions & 0 deletions peachjam/forms.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import copy

from allauth.account.forms import LoginForm, SignupForm
from django import forms
from django.conf import settings
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 django_recaptcha.fields import ReCaptchaField
from django_recaptcha.widgets import ReCaptchaV2Invisible

from peachjam.models import (
AttachedFiles,
Expand Down Expand Up @@ -326,3 +329,11 @@ def __init__(self, *args, user_profile=None, **kwargs):

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


class PeachjamSignupForm(SignupForm):
captcha = ReCaptchaField(widget=ReCaptchaV2Invisible)


class PeachjamLoginForm(LoginForm):
captcha = ReCaptchaField(widget=ReCaptchaV2Invisible)
2 changes: 1 addition & 1 deletion peachjam/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
from .citations import *
from .core_document_model import *
from .external_document import *
from .folder import *
from .gazette import *
from .generic_document import *
from .ingestors import *
from .journals_books import *
from .judgment import *
from .profile import *
from .relationships import *
from .save_document import *
from .settings import *
from .taxonomies import *
File renamed without changes.
5 changes: 5 additions & 0 deletions peachjam/models/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,11 @@ class PeachJamSettings(SingletonModel):
blank=True,
help_text=_("Matomo site ID (e.g. 2)"),
)
allow_save_documents = models.BooleanField(
verbose_name=_("allow save documents"),
default=False,
help_text=_("Allow documents to be saved."),
)

class Meta:
verbose_name = verbose_name_plural = _("site settings")
Expand Down
13 changes: 13 additions & 0 deletions peachjam/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
"martor",
"corsheaders",
"django_htmx",
"django_recaptcha",
]

MIDDLEWARE = [
Expand Down Expand Up @@ -160,6 +161,11 @@
LOGIN_URL = "account_login"
LOGIN_REDIRECT_URL = "home_page"
ACCOUNT_EMAIL_VERIFICATION = "mandatory"
ACCOUNT_AUTHENTICATION_METHOD = "email"
ACCOUNT_FORMS = {
"signup": "peachjam.forms.PeachjamSignupForm",
"login": "peachjam.forms.PeachjamLoginForm",
}

# social logins
SOCIALACCOUNT_PROVIDERS = {
Expand All @@ -176,6 +182,13 @@

SOCIALACCOUNT_ADAPTER = "peachjam.auth.SocialAccountAdapter"

# Recaptcha
RECAPTCHA_PUBLIC_KEY = os.environ.get("RECAPTCHA_PUBLIC_KEY", "")
RECAPTCHA_PRIVATE_KEY = os.environ.get("RECAPTCHA_PRIVATE_KEY", "")
if DEBUG:
SILENCED_SYSTEM_CHECKS = ["django_recaptcha.recaptcha_test_key_error"]


if DEBUG:
INSTALLED_APPS.append("debug_toolbar")
INSTALLED_APPS.append("django_extensions")
Expand Down
8 changes: 8 additions & 0 deletions peachjam/templates/account/login.html
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ <h1>{% trans 'Log in' %}</h1>
</div>
{% endif %}
</div>
<div class="mb-4">
{{ form.captcha }}
{% if form.captcha.errors %}
<div class="text-danger">
{% for error in form.captcha.errors %}<p class="m-1">{{ error }}</p>{% endfor %}
</div>
{% endif %}
</div>
<div class="mb-4">
<button class="btn btn-primary px-4" type="submit">{% trans 'Log in' %}</button>
</div>
Expand Down
12 changes: 10 additions & 2 deletions peachjam/templates/account/signup.html
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,16 @@ <h1>{% trans 'Sign up' %}</h1>
</div>
{% endif %}
</div>
<div class="my-2">
<button class="btn btn-primary px-4 mb-3" type="submit">{% trans 'Sign up' %}</button>
<div class="mb-4">
{{ form.captcha }}
{% if form.captcha.errors %}
<div class="text-danger">
{% for error in form.captcha.errors %}<p class="m-1">{{ error }}</p>{% endfor %}
</div>
{% endif %}
</div>
<div>
<button class="btn btn-primary px-4" type="submit">{% trans 'Sign up' %}</button>
</div>
</form>
</div>
Expand Down
94 changes: 48 additions & 46 deletions peachjam/templates/peachjam/_document_detail_toolbar.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,56 +10,58 @@
{% endblock %}
{% block download-btn %}
<div>
{% if request.user.is_authenticated %}
<button id="saveDocumentButton"
class="btn {% if saved %}btn-primary{% else %}btn-outline-primary{% endif %} btn-shrink-sm ms-2"
type="button"
data-bs-toggle="modal"
data-bs-target="#saveDocument"
aria-expanded="false"
hx-post="{% url 'save_document' %}"
hx-target="#saveDocument"
hx-include="#save_document-form">
{% if saved %}
Document saved <i class="bi bi-check-circle"></i>
{% else %}
{% if show_save_doc_button %}
{% if request.user.is_authenticated %}
<button id="saveDocumentButton"
class="btn {% if saved %}btn-primary{% else %}btn-outline-primary{% endif %} btn-shrink-sm ms-2"
type="button"
data-bs-toggle="modal"
data-bs-target="#saveDocument"
aria-expanded="false"
hx-post="{% url 'save_document' %}"
hx-target="#saveDocument"
hx-include="#save_document-form">
{% if saved %}
Document saved <i class="bi bi-check-circle"></i>
{% else %}
Save document
{% endif %}
</button>
<div id="saveDocument" class="modal fade" tabindex="-1" aria-hidden="true">
{% include "peachjam/save_document.html" %}
</div>
{% else %}
<button class="btn btn-outline-primary ms-2"
data-bs-toggle="modal"
type="button"
data-bs-target="#loginprompt">
Save document
{% endif %}
</button>
<div id="saveDocument" class="modal fade" tabindex="-1" aria-hidden="true">
{% include "peachjam/save_document.html" %}
</div>
{% else %}
<button class="btn btn-outline-primary ms-2"
data-bs-toggle="modal"
type="button"
data-bs-target="#loginprompt">
Save document
</button>
<div id="loginprompt" class="modal fade">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Login</h5>
<button type="button"
class="btn-close"
data-bs-dismiss="modal"
aria-label="Close"></button>
</div>
<div class="modal-body">
<p>
<strong>You are not logged in.</strong>
</p>
<p>To save a document you need to be logged in.</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<a href="{% url 'account_login' %}?next={{ document.get_absolute_url }}"
class="btn btn-primary">Login</a>
</button>
<div id="loginprompt" class="modal fade">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Login</h5>
<button type="button"
class="btn-close"
data-bs-dismiss="modal"
aria-label="Close"></button>
</div>
<div class="modal-body">
<p>
<strong>You are not logged in.</strong>
</p>
<p>To save a document you need to be logged in.</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<a href="{% url 'account_login' %}?next={{ document.get_absolute_url }}"
class="btn btn-primary">Login</a>
</div>
</div>
</div>
</div>
</div>
{% endif %}
{% endif %}
</div>
{% if document.source_file %}
Expand Down
2 changes: 1 addition & 1 deletion peachjam/views/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
from .document_problem import *
from .documents import *
from .external_document import *
from .folder import *
from .gazette import *
from .generic_document import *
from .home import *
Expand All @@ -20,6 +19,7 @@
from .place import *
from .pocketlaw import *
from .robots import *
from .save_document import *
from .taxonomy import *
from .terms_of_use import *
from .widgets import *
9 changes: 9 additions & 0 deletions peachjam/views/generic_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
Relationship,
SavedDocument,
UserProfile,
pj_settings,
)
from peachjam_api.serializers import (
CitationLinkSerializer,
Expand Down Expand Up @@ -242,6 +243,13 @@ def get_object(self, *args, **kwargs):
self.model, expression_frbr_uri=add_slash(self.kwargs.get("frbr_uri"))
)

def show_save_doc_button(self):
if pj_settings().allow_save_documents and self.request.user.has_perm(
"peachjam.add_saveddocument"
):
return True
return False

def get_context_data(self, **kwargs):
context = super().get_context_data(
document_diffs_url=self.document_diffs_url, **kwargs
Expand Down Expand Up @@ -289,6 +297,7 @@ def get_context_data(self, **kwargs):
context["documents_citing_current_doc"] = self.fetch_citation_docs(
doc.work.works_citing_current_work()
)
context["show_save_doc_button"] = self.show_save_doc_button()
if self.request.user.is_authenticated:
user_profile = UserProfile.objects.filter(user=self.request.user).first()
instance = SavedDocument.objects.filter(
Expand Down
7 changes: 5 additions & 2 deletions peachjam/views/folder.py → peachjam/views/save_document.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from django.contrib.auth import get_user_model
from django.contrib.auth.mixins import PermissionRequiredMixin
from django.http import HttpRequest
from django.shortcuts import get_object_or_404
from django.views.generic import TemplateView
Expand All @@ -9,7 +10,8 @@
User = get_user_model()


class CreateFolderView(TemplateView):
class CreateFolderView(PermissionRequiredMixin, TemplateView):
permission_required = "peachjam.add_folder"
template_name = "peachjam/folders_list.html"

def post(self, request: HttpRequest):
Expand All @@ -36,7 +38,8 @@ def get(self, *args, **kwargs):
return self.render_to_response({"folder_form": form})


class SaveDocumentView(TemplateView):
class SaveDocumentView(PermissionRequiredMixin, TemplateView):
permission_required = "peachjam.add_saveddocument"
template_name = "peachjam/save_document.html"

def get_context_data(self, *args, **kwargs):
Expand Down

0 comments on commit 4bb2dd4

Please sign in to comment.