0

we can decorate a view with login_required so that unauthenticated user would be redirected to a login page.

I'd like to create a decorator to show a modal in the current page when an unauthenticated user accesses a view.

I guess I can handle when request is made with ajax.
I could use $.ajaxSetup to handle 401 errors to show the modal.

But how do I show a modal when a regular request is made?

Edit

Similar question is asked, Django authentication and Ajax - URLs that require login

But it only covers the ajax requests.

Community
  • 1
  • 1
eugene
  • 39,839
  • 68
  • 255
  • 489

1 Answers1

0

Hmm i would offer different approach.

Modals and such pop up when javascript does something. In order for javascript to "do something" there has to be some kind of marker added to html.

I suggest you use context processor for it.

1) Create context processor, whichs only purpose is to create/add marker in the end of the file. Relevant template part would be something like

<script>var show_modal = {{ SHOW_MODAL }}</script>

Where the value of SHOW_MODAL comes from context processor

2) Add your js code to show modal, if the value of show_modal variable indicates you should do it.

Odif Yltsaeb
  • 5,575
  • 12
  • 49
  • 80
  • There are 3rd party library codes that would return 401 response for ajax request (for example rest-api frameworks). Can context processor look at the response status code? I just looked at the django doc, but not sure about when the context processor is called.. before response status is set or not.. – eugene Mar 13 '14 at 08:15
  • Context processor does not have to look at the response status code. Context processor recieves request as argument and you can just check if request.user.is_authenticated or not. – Odif Yltsaeb Mar 13 '14 at 09:02
  • well, I want only subset of views (with custom_login_required decorator) to test the user.is_authenticated and add the variable. – eugene Mar 13 '14 at 09:07
  • Aha. In that case skip the context processor and use decorator to set some value in wrapped view to indicate if modal should be shown or not. Similar to this example: http://stackoverflow.com/questions/15794419/decorator-to-set-attributes-of-function. He sets permission_required. You could set modal_required. Then pass the value of it into template context and have js pick it up from there, as i suggested in original answer. – Odif Yltsaeb Mar 13 '14 at 09:12
  • ok I now know that it can be done. but it requires the modification of original function. (you are suggesting my view to look at modal_required and stuff that in with other context variables) Couldn't this be done in decorator ? :( Another twist is that, actually I don't want to show the requested view when authentication condition is not met. I want to show the referring page. – eugene Mar 13 '14 at 09:22
  • Decorator is just a decorator. It wraps original function, but it still returns original function not something else. And you need the original function to release information about if modal should be shown or not. Now about the page you want to show - why not use redirect there, if condition is not met. THAt you can and should do with decorator. – Odif Yltsaeb Mar 13 '14 at 09:56
  • you redirect to the referrer(previous page), but how does the previous page set up show_modal variable? (as far as I know, redirect sends client 304 or so and client visits the given url) – eugene Mar 13 '14 at 09:59
  • It does it exactly the way we have discussed so far - either in decorator, by setting wrapped views parameter to something and then injecting that something to template context, or doing the same thing using context_processor. – Odif Yltsaeb Mar 13 '14 at 10:01
  • So that means, I need to wrap every view that could lead to restricted area with a decorator(which looks aweful) and somehow knows that if the request to itself is because of the redirect-due-to-authentication or not, it adds a context variable. Or is there something I am missing? – eugene Mar 13 '14 at 10:04
  • Well. You want to restrict some views. But the other views still need to be able to present login forms, even if they themselves are not restricted views. If you do not want to wrap them into the same decorator you already have - you still have to have some kind of means for displaying the modal form. You could create different decorator there though. For example. If you are redirecting back to non restricted view, you could add some get parameter to url like ?show_modal=something. And check for this parameter in decorator, and use it as indicator for showing form. – Odif Yltsaeb Mar 13 '14 at 10:07
  • You could do the same thing in each and every view, but thats not very dry. Doing it in decorator is "dryer" haha :D – Odif Yltsaeb Mar 13 '14 at 10:08
  • Sorry for late response @Odif, I quit on the approach back then.. and am revisiting the issue now.. – eugene Sep 04 '14 at 06:00