Commit d634022b authored by Pierre-Elliott Bécue's avatar Pierre-Elliott Bécue

Version plus clean, mais pas encore fonctionnelle, à finir quand on voudra un truc dynamique

parent 684d50f6
#!/usr/bin/env python2.7
# -*- coding: utf-8 -*-
import json
import keep_alive
import nk
class WhoAmiMiddleware(object):
"""Met à jour les données utilisateurs en live"""
def process_response(self, request, response):
"""Let's hijack a session"""
idbde = request.session.get("whoami", {}).get('idbde', None)
if idbde is None:
return response
# On récupère la socket dans keep_alive.CONNS
if idbde in keep_alive.CONNS:
sock = keep_alive.CONNS[idbde]
else:
messages.add_error(request, messages.ERRMSG_NOSOCKET)
return (False, HttpResponseRedirect(_gen_redirect_postlogin(request)))
sock.write(json.dumps(["whoami"]))
out = nk.full_read(sock)
whoami = out["msg"]
request.session["whoami"] = whoami
return response
......@@ -178,7 +178,10 @@ def login_NK(request, username, password, form, masque=[[], [], False]):
"full_link": "%s%s/" % (settings.NOTE_ROOT_URL, p[1]),
})
request.session["pages"] = save_pages
socket_refresh_user(request, sock)
sock.write(json.dumps(["whoami"]))
out = full_read(sock)
whoami = out["msg"]
request.session["whoami"] = whoami
# On conserve la connexion au serveur NK2015
keep_alive.CONNS[whoami["idbde"]] = sock
# On redirige vers /index, sauf si on était en train de se reloguer en venant d'un endroit particulier
......@@ -195,13 +198,6 @@ def login_NK(request, username, password, form, masque=[[], [], False]):
variables["form"] = form
return render(request, 'note/login.html', variables)
def socket_refresh_user(request, sock):
"""Rafraîchit le contenu de la variable de session ``whoami''"""
sock.write(json.dumps(["whoami"]))
out = full_read(sock)
whoami = out["msg"]
request.session["whoami"] = whoami
def socket_still_alive(request):
"""
Récupère dans :py:mod:`keep_alive` la socket de communication avec le serveur NK2015
......
......@@ -171,6 +171,7 @@ TEMPLATE_CONTEXT_PROCESSORS = (
#: A tuple of middleware classes to use. See `Middleware <https://docs.djangoproject.com/en/dev/topics/http/middleware/>`_.
MIDDLEWARE_CLASSES = (
# 'note.middleware.WhoAmiMiddleware',
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.locale.LocaleMiddleware',
......
......@@ -389,8 +389,6 @@ def get_varsock(request, socket=False):
"""
if request.session.get("logged", "no") == "ok":
# Le login a réussi
if socket:
nk.socket_refresh_user(request, socket)
whoami = request.session["whoami"]
variables_standard = basic._fundamental_variables()
variables_standard["pages"] = request.session["pages"]
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment