Login

Tag "session"

Snippet List

Django Online Now Users - Middleware

django online users, usage: `{{ request.online_now }}` or `{{ request.online_now_ids }}`, complete tutorial: https://python.web.id/blog/django-count-online-users/, this snippet forked from: https://gist.github.com/dfalk/1472104

  • middleware
  • django
  • session
  • cache
Read More

Seeded Randomized Querysets w/ Pagination Mixin

Mixin to support pagination when randomizing querysets. Requirements: Postgres, Django Sessions Note: This shouldn't be used on large complex datasets. It utilizes the relatively slow method of '?' randomized sorting. Use with caution. Todo: MySQL support, Support for larger datasets

  • django
  • session
  • pagination
  • random
  • postgres
  • mixin
  • postgresql
  • cbv
  • seeded
Read More

Persistent Session Debugging with Django Debug Toolbar

When using [django debug toolbar](https://github.com/django-debug-toolbar/django-debug-toolbar), I like to be able to turn debugging on and off without having to edit my settings file. This callback makes that possible. Add `?debug=on` to the URL to turn debugging on. It will remain on in the current session until you turn it off with `?debug=off`. Make sure your session middleware comes before your debug toolbar middleware.

  • session
  • debug
  • debug-toolbar
Read More

Keep Me Logged In for Django

Very simple middleware to implement "remember me" functionality. Updates the session once per day to keep user logged.

  • django
  • session
  • keep
  • logged
  • middlware
Read More

orm_tools

This module contains classes that add new behavior to Django's ORM. Classes include: **Session** * Forces QuerySet objects to return identical instances when objects with the same primary key are queried. * Similar to [SQLAlchemy Session](http://www.sqlalchemy.org/docs/orm/session.html) **GraphSaver** * Save entire object graphs at once. * Automatically detects object dependencies and saves them in the correct order. **Collection** * Easier one-to-many relationships. Instructions and more information on [limscoder.com](http://www.limscoder.com/2011/01/django-orm-tools.html).

  • session
  • db
  • orm
Read More

CurrentSessionIDMiddleware

The middleware assigns a unique identifier for session. The session id doesn't depend of session or whatever else. It only need cookies to be turned on. The session id is reassigned after client close a browser. Identifier of the session could be read from request: request.current_session_id. You can setup name of the cookie in yours settings module (FLASH_SESSION_COOKIE_NAME). request.current_session_id is lazy. It means the ID will be assigned and cookie will be returned to client after first usage.

  • middleware
  • session
Read More

Remember path decorator

Decorator that stores the `request.path` URL in a session variable to be used later, e.g. in a "Continue Shopping" link on a cart page. Wrap and view that you want to be able to link back to easily. When those views are called, it updates the session with the current `request.path` value. This can be pulled back out of the session whenever you need to provide a link back from whence the user came.

  • session
  • decorator
Read More

Cookieless Session Decorator

Although many people have already posted cookieless session middlewares and related stuffs, but this one is just for only required views. You can use this as a view decorator like: @session_from_http_params @login_required def your_view(request): ... This is very useful for those who use SWFUpload. Flash has a bug with sending cookies properly, so SWFUpload offers an workaround -- session key as a POST parameter.

  • session
  • decorator
  • cookieless
Read More

Simple Age Verification Middleware

Simple middleware+decorator to handle age verification. Modeled after `django.contrib.sessions.middleware` to add an attribute to `request.user` called `is_age_verified` with consideration to [snippet 1002](http://www.djangosnippets.org/snippets/1002/). Decorator modeled after `django.contrib.auth.decorators.login_required` Installation: Create `verify_age` URLconf in `urls.py` Create age verification page that URLconf points to Define `settings.VERIFY_AGE_URL` based on URLconf Add `age_verification.AgeVerificationMiddleware` to `MIDDLEWARE_CLASSES` Import both `age_verification_required` and `REDIRECT_FIELD_NAME` in `views.py` Implement `age_verification.AgeVerification.verify` somewhere to set session attribute on successful verification. Use `@age_verification_required` decorator for views requiring age verification Example urls.py: urlpatterns += patterns('mahalo.answers.views', ... url(r'^verify_age/?$', 'verify_age', name="verify_age"), ... Example settings.py: ... VERIFY_URL = '/verify_age/' ... MIDDLEWARE_CLASSES += ( ... 'django.contrib.sessions.middleware.SessionMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'myproject.contrib.middleware.age_verification.AgeVerificationMiddleware', ... Example views.py: from myproject.contrib.decorators import age_verification_required, REDIRECT_FIELD_NAME from myproject.contrib.middleware.age_verification import AgeVerification ... @age_verification_required def some_view(request): return render_to_response("index.html", {}) def verify_age(request): # project specific template_vars = default_template(request) # form was posted if request.POST.has_key("month") and request.POST.has_key("day") and \ request.POST.has_key("year"): # "verify" user av = AgeVerification(request.session) av.verify() if request.POST.has_key(REDIRECT_FIELD_NAME): return HttpResponseRedirect(request.POST[REDIRECT_FIELD_NAME]) else: return HttpResponseRedirect(reverse("root")) # no form posted, show it else: if request.GET.has_key(REDIRECT_FIELD_NAME): template_vars["next"] = request.GET[REDIRECT_FIELD_NAME] return render_to_response("verify_age.html", template_vars) These examples assume `age_verification.py` lives in `myproject/contrib/middleware/` and `decorators.py` lives in `myproject/contrib/`

  • middleware
  • session
  • age-verification
  • session-backed
Read More

Another Cookieless Session Middleware

the snippet improve juliocarlos's greate works(see [http://www.djangosnippets.org/snippets/1235/](http://www.djangosnippets.org/snippets/1235/) ) ,merge functtions to one middlewere class, fixed url regular expression and eliminate AJAX support etc... it's tested with django 1.0.2 and work fine on my wap site. * the middlewere must before SessionMiddlewar in MIDDLEWARE_CLASSES tuple eg: MIDDLEWARE_CLASSES = ( 'django.middleware.common.CommonMiddleware', 'middleware.cookieless_session.CookielessSessionMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', )

  • middleware
  • cookie
  • session
  • cookieless
Read More

24 snippets posted so far.