rail_rails cache templates based on current_user - ruby-on-rails-4

I'm using rabl_rails for generating json for my rails app and caching most of them for faster response. Things work great for non logged in users but I also want to able to cache responses for logged in users with their ID append in the cache key but as per the documentation, instance variables are not available in the rabl template due to the way its designed. You can find it here https://github.com/ccocchi/rabl-rails#how-it-works
What I am trying to achieve is this
cache "api/v1/home/top_menu_data-#{#current_user.id}"
but this doesn't work according to the document.
Has anyone done this before ? How do I achieve this ?
PS: This is feasible with rabl but not with rail_rails as far as I know. Would love to know ideas on how to be able to do this.
Thanks in advance.

As per the discussion with developer of the gem, its found that it can't access any instance variables or params hash. The discussion could be found here
I started using rabl for generating json for my views which works great.

Related

How I get the time zone of logged user by using Django?

I want to manage(such as Change the default language) the country region details of in my Django website. Does anyone have a best way to do that?
There is no built-in way in Django to achieve that. See adamcharnock/django-tz-detect. The project is maintained well, still active and has an easy-to-understand documentation.

Django 1.8: Password Protect Entire Project

I have built my first Django App! It is built to help my business track inventory. As such, I would not like it to be publicly available.
Maybe someday I will set up multiple user accounts, etc, but for now I really just need a basic password gate to get it up and running.
Does anyone have any middleware that works for this? All the solutions that I am finding are pretty old and they do not seem to work with the latest version of Django.
If you just need a single username/password couple, handling it directly via HTTP authentication in your webserver configuration will be the easiest way to achieve this. The benefits of this approach are:
You can set it up in 5 minutes: example with nginx, example with apache
You don't have to write code you'll delete later
It will protect all your website, including static files, third-party apps, admin, etc.
I found an answer that worked for me posted here:
#login_required for multiple views
Make sure the LOGIN_REQUIRED_URLS_EXCEPTIONS path is correctly set to your login page.

Django - email app

Could you please suggest any available app for sending & managing emails in django?
You can find a detailed comparison here: http://www.djangopackages.com/grids/g/email/
You can try implement this youself using http://docs.python.org/library/poplib.html and http://docs.python.org/library/smtplib.html#smtp-example but, if you describe what concrete functionality you need, may be i can suggest some ready Django app for you needs.

Django request paths

I've been working through an issue with my django project. The issue is I've got one project, which will retrieve data for users of different clients. I need to know 'from where' a viewer is coming from (request.path [my original solution]). I've been looking at a number of different options which sound close to what I want to do, but I'm not sure what the best option is, not having done this before.
My first option was to add a url in the urls.py with a 'tag' or 'keyword' then look for that tag/keyword in the request.path, which I'd add as a session key. Then go onto get the data.
Something else I started looking at was the sites framework. After reading through the documentation, I'm still confused how sites actually works, so I'm not sure if this is the right option.
Another solution talked about using middleware, this came up in connection with the research into using the sites framework.
And then yet another talked about doing this in apache.
Could some one help point me in the right direction?
Cheers,
T
If you need to know from which URL came your user to your currrent page you should check the REFERER http header, available in request.META.get('HTTP_REFERER').
See http://docs.djangoproject.com/en/1.2/ref/request-response/#ref-request-response for more informations.
Be careful though, the referer meta is not mandatory and could be missing due to private browsing or direct access to the page from the URL bar.
It's not completely clear from your question, but if you're asking for the URL that the user was on before coming to the current page, you probably want request.META['HTTP_REFERRER'].
Edit after comment
That would be a very bad idea. Global variables are not safe given that you potentially have multiple requests being processed at the same time. The referrer is already available from the request, which can be accessed in all views and templates, so I don't know what else a middleware would give you.

Besides URL rewriting, what options are available for maintaining sessions without using cookies?

I've seen various options for URL rewriting here on Stack Overflow, and other places on the web, but was curious to see if there were other options.
This is speculation, as Cookies and URL Rewriting are the big two, but technologically, I think it'd be possible to:
do some massive hackery with javascript that captures all links and submits a form with information.
track the session on the server based on IP
Both have their downsides and holes obviously.
Session variables? At work, we are not allowed to use non session-cookies without a load of permissions.
You can either maintain state through a cookie or through a query parameter. The browser needs to be able to pass data to the web server somehow and those are the only two options.
I suppose that would depend on what technology you are using. In ColdFusion you can maintain session variables without cookies.
Using a client-side database storage, such as Google Gears (sqlite) ? Html5 is expected to include one (webkit already does it).