django-allauth caching login and signup pages - django

is there a way to set up Django Redis caching for login and signup views from django-allauth? I looked at docu and found nothing. I don't want whole site caching but only some views and these two are part of it.

Django Redis makes use of Django's caching framework. So the documentation bit you are looking for is here.
The short bit:
A more granular way to use the caching framework is by caching the output of individual views. django.views.decorators.cache defines a cache_page decorator that will automatically cache the view’s response for you.
For allauth, you'd need to match the login and signup URL before you include allauth.urls and then use the decorator in the url conf:
from django.views.decorators.cache import cache_page
from allauth.account.views import login
urlpatterns = [
url(r'^accounts/login$', cache_page(60 * 15)(login)),
# same for signup
url(r'^accounts/$', include('allauth.urls')
]

Related

Two views same URL

I have a single dashboard_view URL path("", view=dashboard_view, name="dashboard").
On this page you can see the homepage unauthenticated. However, if you login, I present a modal popup to allow a user to populate a CreateForm.
The issue is that the dashboard_view doesn't have the form ( I have that in another view ). What is the best practice for this? Best for the user to have different options on the same page without having to switch pages.
You can use the login_required decorator. In login_required login_url is an optional parameter if you have declared the login path in the settings.py file. If your entire site has a login URL is the same. You can put LOGIN_URL = 'login_form_url' in the settings.py file.
from django.contrib.auth.decorators import login_required
#login_required(login_url='/login_form_url/')
def dashboard_view(request):
return render(request,'app_name/dashboard.html')

django ckeditor image upload

I'm using Django-ckeditor in my website.
I'm especially using
RichTextUploadingField()
in my model. and other option just works fine, except image upload.
1. Error Message
I'm getting an error message of
"Incorrect Server Response" and especially, chrome devtools indicates that
ckeditor.js:21 [CKEDITOR] Error code: filetools-response-error.
ckeditor.js:21 [CKEDITOR] For more information about this error go to https://docs.ckeditor.com/ckeditor4/docs/#!/guide/dev_errors-section-filetools-response-error
2. Guess
I have tried uploading images using ckeditor in my admin page,
authorized as superuser in django, it works.
However, logged in as the normal user account, I've tried the same thing, but it does not work.
So my guess is it has some kind of authorization problem. But I can't figure out where to start debugging in my django-ckeditor.
What things should I be checking? Thanks in advance.
This is happening because the default urls are decorated with #staff_member_required(https://github.com/django-ckeditor/django-ckeditor/blob/master/ckeditor_uploader/urls.py). To avoid this, instead of including the urls like so url(r'^ckeditor/', include('ckeditor_uploader.urls')) you could define them one by one in your urls.py with the login_required decorator:
from django.conf.urls import url
from django.contrib.auth.decorators import login_required
from ckeditor_uploader import views
urlpatterns = [
.....your other urls
url(r'^ckeditor/upload/', login_required(views.upload), name='ckeditor_upload'),
url(r'^ckeditor/browse/', never_cache(login_required(views.browse)), name='ckeditor_browse'),
]
Like this you are limiting the uploads to all users that are logged in.
Add following imports in the project urls.py:
from django.contrib.auth.decorators import login_required
from django.views.decorators.cache import never_cache
from ckeditor_uploader import views as ckeditor_views
Replace the following row in the urls.py:
path('ckeditor/', include('ckeditor_uploader.urls')),
with
path('ckeditor/upload/', login_required(ckeditor_views.upload), name='ckeditor_upload'),
path('ckeditor/browse/', never_cache(login_required(ckeditor_views.browse)), name='ckeditor_browse'),
it works if you are logged in as an admin(localhost:8000/admin), simple is that.

How to serve a static file in a Django app only to logged-in users?

I need to serve a particular static asset only to users logged into my Django site. I'm serving static assets through Apache. For various reasons I’m not interested in standing up a full CMS — I just need to make sure non-qualified site visitors cannot download this particular file.
This is really low traffic site so failing all else I can hardcode a urlpattern & serve it as a template.
But there's gotta be a smarter way to do this, right?
EDIT:
Here’s where I settled for now:
# views.py
from django.shortcuts import render
from django.contrib.auth.decorators import login_required
#login_required
def secretfile(request):
return render(request, 'secretfile.xls')
# urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'secretfile.xls', views.secretfile),
]

Applying ratelimit decorator to auth views.py

I want to rate limit several views in my Django app (login, register_account, password reset, ...). I am already using Django-Ratelimit. But I am unsure how to add this decorator to existing views. Writing my own views and using them in a custom urls.py looks like a lot of boilerplate code just to add some decorators.
You can use decorators directly in your urls.
url(r'^login/$', ratelimit(key='whatever')(login_func), name='login'),

how to change a default django admin login view to generate token on login to admin site

my site.py:
from django.contrib.admin import AdminSite
class OptiAdminSite(AdminSite):
def get_urls(self):
from django.conf.urls import patterns, url, include
from core import views
from django.contrib.contenttypes import views as contenttype_views
urlpatterns = patterns('',
#url(r'^$', wrap(self.index), name='index'),
url(r'^login/$', views.login, name='login'),
url(r'^logout/$', views.logout, name='logout'),
)
return urlpatterns
opti_site = OptiAdminSite()
I'm developing an authentication API. When user logs in to my API it generates a code which get destroyed once user hit logout.
My problem is that whenever I'm running my API and django admin site in same browser, then if I login into admin-site It automatically login me in my API too with out any token. When I try to logout in that case from my API it generates an error - 'Token does not exist'. I want to generate token when admin user login to admin-site.
I've tried to do it with above trick as in official documentation but didn't find the right way to do it.
Please suggest me the correct way to do it. Is it necessary to make a separate app for it?
Thanks! in advance.
This solution is almost complete... Almost, because you're simply creating your own admin site in opti_site variable, but probably not using it anywhere.
To make it work, you can monkey-patch default admin site with your site, using:
from django.contrib import admin
admin.sites.site = opti_site
admin.site = admin.sites.site
Remember that you must do it before root urlpatterns definition (especially before defining urls to your admin site).
Another approach is to change default admin to your admin in include of url patterns:
url(r'^admin/', include(opti_site.urls)),