-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7d703ab
commit 8959822
Showing
10 changed files
with
322 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
integreat_cms/cms/templates/ajax_contact_form/_contact_form_widget.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
{% load i18n %} | ||
{% load widget_tweaks %} | ||
<div> | ||
<label for="{{ contact_form.title.id_for_label }}"> | ||
{{ contact_form.point_of_contact_for.label }} | ||
</label> | ||
{% render_field contact_form.point_of_contact_for|append_attr:"form:ajax_contact_form" %} | ||
<label for="{{ contact_form.name.id_for_label }}"> | ||
{{ contact_form.name.label }} | ||
</label> | ||
{% render_field contact_form.name|append_attr:"form:ajax_contact_form" %} | ||
<label for="{{ contact_form.email.id_for_label }}"> | ||
{{ contact_form.email.label }} | ||
</label> | ||
{% render_field contact_form.email|append_attr:"form:ajax_contact_form" %} | ||
<label for="{{ contact_form.phone_number.id_for_label }}"> | ||
{{ contact_form.phone_number.label }} | ||
</label> | ||
{% render_field contact_form.phone_number|append_attr:"form:ajax_contact_form" %} | ||
<label for="{{ contact_form.website.id_for_label }}"> | ||
{{ contact_form.website.label }} | ||
</label> | ||
{% render_field contact_form.website|append_attr:"form:ajax_contact_form" %} | ||
<button id="submit-contact-form-button" | ||
name="location" | ||
value="{{ poi_id }}" | ||
form="ajax_contact_form" | ||
data-btn-save-contact-form | ||
class="btn w-full mt-4 mb-2" | ||
data-url="{% url 'create_contact_ajax' region_slug=request.region.slug %}"> | ||
{% translate "Submit" %} | ||
</button> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
integreat_cms/cms/views/contacts/contact_form_ajax_view.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
from __future__ import annotations | ||
|
||
from typing import TYPE_CHECKING | ||
|
||
from django.http import JsonResponse | ||
from django.shortcuts import render | ||
from django.utils.decorators import method_decorator | ||
from django.views.generic import TemplateView | ||
|
||
from ...decorators import permission_required | ||
from ...forms import ContactForm | ||
from ...models import Contact | ||
from .contact_context_mixin import ContactContextMixin | ||
|
||
if TYPE_CHECKING: | ||
from typing import Any | ||
|
||
from django.http import HttpRequest, HttpResponse | ||
|
||
|
||
@method_decorator(permission_required("cms.view_contact"), name="dispatch") | ||
@method_decorator(permission_required("cms.change_contact"), name="post") | ||
class ContactFormAjaxView(TemplateView, ContactContextMixin): | ||
""" | ||
View for the ajax contact widget | ||
""" | ||
|
||
#: Template for ajax POI widget | ||
template = "ajax_contact_form/_contact_form_widget.html" | ||
|
||
def get(self, request: HttpRequest, *args: Any, **kwargs: Any) -> HttpResponse: | ||
r"""Render a contact form widget template | ||
:param request: The current request | ||
:param \*args: The supplied arguments | ||
:param \**kwargs: The supplied keyword arguments | ||
:return: The html template of a POI form | ||
""" | ||
contact_form = ContactForm( | ||
additional_instance_attributes={"region": request.region} | ||
) | ||
|
||
return render( | ||
request, | ||
"ajax_contact_form/_contact_form_widget.html", | ||
{ | ||
**self.get_context_data(**kwargs), | ||
"contact_form": contact_form, | ||
"poi_id": kwargs.get( | ||
"poi_id", | ||
), | ||
}, | ||
) | ||
|
||
def post(self, request: HttpRequest, *args: Any, **kwargs: Any) -> HttpResponse: | ||
r"""Add a new POI to the database | ||
:param request: The current request | ||
:param \*args: The supplied arguments | ||
:param \**kwargs: The supplied keyword arguments | ||
:raises ~django.http.Http404: If no language for the given language slug was found | ||
:return: A status message, either a success or an error message | ||
""" | ||
|
||
contact_instance = Contact.objects.filter(id=None).first() | ||
|
||
data = request.POST.dict() | ||
|
||
contact_form = ContactForm( | ||
data=data, | ||
files=request.FILES, | ||
instance=contact_instance, | ||
additional_instance_attributes={ | ||
"region": request.region, | ||
}, | ||
) | ||
|
||
if not contact_form.is_valid(): | ||
return JsonResponse( | ||
data={ | ||
"contact_form": contact_form.get_error_messages(), | ||
} | ||
) | ||
|
||
contact = contact_form.save() | ||
|
||
return JsonResponse( | ||
data={ | ||
"success": "Successfully created location", | ||
"contact_label": contact.label_in_reference_list(), | ||
"edit_url": contact.backend_edit_link, | ||
} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.