fabiocaccamo / django-admin-interface

:superhero: :zap: django's default admin interface with superpowers - customizable themes, popup windows replaced by modals and many other features.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Don't close related modal if error exists

ArkieCoder opened this issue · comments

Hello,

I am overriding save_model in a ModelAdmin class that is related to another class. In my save_model I am detecting if there were errors in the save process, which can happen if a user provides a malformed file to one of the fields. If there is an error, I use django.contrib.messages to display the error to the user.

The problem with using the related modal for this in my app is that the modal closes and the error message does not display. Is there a way to either 1) prevent the modal from closing if there is a message to display or 2) to force the parent page to reload if there are messages in the queue to display?

Thanks - this is a great module that radically improved the UX of my app with just a few small changes. Very much appreciated.

Fund with Polar

@ArkieCoder nice catch... I never faced it personally.

I think the best thing to do here would be display the errors in the modal without closing it automatically.

Agreed. Does django-admin-interface support not closing the modal in the event of error messages? Or is this something that will require changes to be made to the module? I would be happy to work on a solution and submit a PR if needed.

I took an extended look at this issue. Unfortunately simply blocking the close in Javascript brings no joy, because the code still redirects to popup_response.html which just displays a spinner. I don't mind the spinner, per se, but would like the error message to display over the top of it. I think by typing this out here I may have formulated a plan. :) We will see.

Ok I have a solution - I added a popup_response_template to my admin class:

popup_response_template = "music_library/popup_response.html"

popup_response.html:

<html>
  <head><title>{% trans 'Popup closing...' %}</title></head>
  <body>
    {% for message in messages %}
      {% if message.level_tag != 'success' %}
        {% include "admin/base_site.html" %}
          <input
            onClick="parent.dismissRelatedObjectModal(); return false;"
            type="submit"
            style="position: absolute; top: 162px; right: 5px; padding: 0; height: 30px; width: 95px; background-color: 
#f00;"
            value="Cancel">
      {% else %}
        {% include "music_library/popup_success_js.html" %}
      {% endif %}
    {% endfor %}
  </body>
</html>

popup_success_js.html:

{% load static %}<script type="text/javascript"
            id="django-admin-popup-response-constants"
            src="{% static "admin/js/popup_response.js" %}"
            {% if popup_response_data %}
            data-popup-response="{{ popup_response_data }}"
            {% else %}
            data-popup-response='{"action":"{{ action|escape }}","value":"{{ value|escape }}","obj":"{{ obj|escape }}","
new_value":"{{ new_value|escape }}"}'
            {% endif %}>
    </script>
    <script type="text/javascript">
      window.dismissRelatedObjectModal();
    </script>

This provides the desired behavior - normal behavior on success, and display of the error message when there is one. Perhaps this can be of help to others.

@ArkieCoder thank you for sharing your solution!

Maybe this should be included in the package, I keep the issue open.