According to the Django channel docs,
You get access to a user’s normal Django session using the http_session
decorator - that gives you a message.http_session attribute that behaves
just like request.session. You can go one further and use
http_session_user which will provide a message.user attribute as well as
the session attribute.
Is there anyway I can set a session variable like so?
I did try it, and I get an error saying NoneType is not iterable.
if 'username' not in message.http_session:
message.http_session['username'] = 'temp'
In other words, message.http_session returns a NoneType. I've tried using the decorators #http_session and #channel_session_user_from_http, but they didn't help either.
You just set the variable.
foo = 'bar'
And it persists throughout the session
Related
I have an var containing all documents from mongoDB as an object. The way I set this is like this:
questions = Questions.objects.order_by('-openDate')
This works fine. When called, the objects show the attributes I expect. But now I want to add an attribute to it. Its not defined in the database, but I will set it base on some simple python code. To break it down:
One of the fields is responses, which contain an userId and response.
By going through all responses:
for i in question.responses:
if i.id == current_user:
(Now the magic should happen)
Now comes what does not work. I want to add an attribute userResponded to the object. This way in the template I can simply ask if userResponded is true or false. To be clear, the userResponded should not be added to responses, but a new attribute.
Just to add to this, I already tried:
questions[0].test = 'test'
I expected it was now added to the first object in questions. However, this did not happen.
Please help, already stuck on this for way too long!
I have implemented a code to calculate api time.
#api.app.before_request
def before_request():
g.request_start_time = time.time()
#api.app.after_request
def after_request():
elapsed = time.time() - g.request_start_time ------> Error here.
I am having error for ~1-2% api calls.
Error:
'_AppCtxGlobals' object has no attribute 'request_start_time'
I am not able to debug this. This can only happen if :
the api does not pass through before_request function.
the global context get reset in between the code.
First seems not to be happen as every api request should pass the before_request function.
I am not able to find when the second case can occur. What are the scenarios when the global context get reset in between call.
Is there anything I am missing here ?
EDIT:
I have been observing the cases when this error is occurring and found the similarity that all calls were having 400 BAD REQUEST. But I am still not able to get the root cause of this.
I had a similar issue and is due because you called a variable g, so flask confuse it with the global application context also called g.
Have a look here:
https://flask.palletsprojects.com/en/1.1.x/api/#flask.ctx._AppCtxGlobals
Creating an app context automatically creates this object, which is made available as the g proxy.
So try renaming your g variable in g.request_start_time = time.time()that you probably inherit from somewhere else.
If you are referring the flask context and having error correlated to 404 requests, it might be that when you call after_request() it does not find any attribute g.request_start_time , maybe because if requests fail the attribute is not created.
I am facing problems while passing custom Attributes to the SP.
Details:
I am using developersOKTA admin profile for IDP.
My SP is a python application which user Django/jinja/Django-CMS
My app attribute statement value is this.
Attempt1:
userName|${user.userName}|urn:oasis:names:tc:SAML:2.0:attrname-format:basic, firstName|${user.firstName}|urn:oasis:names:tc:SAML:2.0:attrname-format:basic, lastName|${user.lastName}|urn:oasis:names:tc:SAML:2.0:attrname-format:basic, email|${user.email}|urn:oasis:names:tc:SAML:2.0:attrname-format:basic, is_publisher|${template_saml_2_0.is_publisher}|urn:oasis:names:tc:SAML:2.0:attrname-format:basic, userRole|${template_saml_2_0.userRole}|urn:oasis:names:tc:SAML:2.0:attrname-format:basic,
Where, is_publisher(type boolean) and userRole(type string) are custom attributes defined and given value on both places - in user profile and in app user profile. And template_saml_2_0 is the app user object and user is the user object.
Next, I have mapped these variables together:
Mapping of okta->app
user.is_publisher is mapped to is_publisher, and
Mapping of app->okta
appuser.is_publisher is mapped to is_publisher.
Done similar for other attributes.
On click the app chiklet, with this attributes statement generates "500 internal server error". This error triggers before okta gives call to the SP. I have found nothing is logged in my SP's logs. And I do not know how to track OKTA IDP's logs.
Attempt2:.
Here I am taking values from the user object for the custom attributes, and rest keeping all same as in attempt 1.
is_publisher|${user.is_publisher}|urn:oasis:names:tc:SAML:2.0:attrname-format:basic, userRole|${user.userRole}|urn:oasis:names:tc:SAML:2.0:attrname-format:basic,
Now this change gives me the value of userRole(string type) and not for is_publisher(boolean type) in the assertion xml passed to the SP by OKTA. I guess this returns values if the custom attribute is only of string type and not for any others types. Is it the case?
Can you please tell me where I am going wrong? I need to set roles of users according the application which I have failed to do in Attempt1. and In Attempt2 I am only getting String type CustomAttribute's value.
I am total newbie with cherrypy.
My setup: Arch Linux, Python 3.3, tornado, cherrypy 3.2
Trying to implement session handling for a web app using cherrypy.lib.sessions (for some reason often referred to as cherrypy.sessions in various forums, might be another version)
I am looking for an example of the following:
instantiate a session object
set a value of an arbitrarily named attribute
write session into a session file
read session info from session file
access the the value of the modified attribute
My (relevant) code:
import cherrypy
class RequestHandlerSubmittedRequest(tornado.web.RequestHandler):
def get(self):
SetState(self)
def SetState(self):
cherrypy.config.update({'tools.sessions.on': True})
cherrypy.config.update({'tools.sessions.storage_type': 'file'})
#directory does exist
cherrypy.config.update({'tools.sessions.storage_path': '/tmp/cherrypy_sessions'})
cherrypy.config.update({'tools.sessions.timeout': 60})
cherrypy.config.update({'tools.sessions.name': 'hhh'})
So far so good. Now:
obj_session = cherrypy.lib.sessions.FileSession
Here I get the first snag (or misunderstanding).
The returned obj_session contains no session ID of any kind, just an empty object frame. Also: no file is created at this point in /tmp/cherrypy_sessions -
should not it be there now? I would expect it to be created and named after its session ID.
OK, no ID in the object, let's assign one:
session_id = obj_session.generate_id(self)
This returns a long random string as it should I guess
And now I don't know how to proceed with assignments and saving calling obj_session.save() or obj_session.load() with several variations of input gives "AttributeError: 'module' object has no attribute X" where X can be "load" and couple of other keywords. Passing self or obj_session itself to the methods does not help, just changes the wording of the error. I must be going in a very wrong direction in general.
So, is there an example of those five steps above? I could not find one anywhere.
Thanks.
Igor
I am trying to check if the session does have anything in it. What I did is:
if request.session:
# do something
but it is not working. Is there any way of knowing whether the session contains something at that moment?
If I do request.session['some_name'], it works, but in some cases, I just need to know if the session is empty or not.
Asking with some specific names is not always a wanted thing.
Eg. if there is no session, it returns an error, since some_name doesn't exist.
request.session is an instance of SessionBase object which behaves like dictionary but it it is not a dictionary. This object has a "private" field ( actually it's a property ) called _session which is a dictionary which holds all data.
The reason for that is that Django does not load session until you call request.session[key]. It is lazily instantiated.
So you can try doing that:
if request.session._session:
# do something
or you can do it by looking at keys like this:
if request.session.keys():
# do something
Note how .keys() works:
django/contrib/sessions/backends/base.py
class SessionBase(object):
# some code
def keys(self):
return self._session.keys()
# some code
I always recommend reading the source code directly.
Nowadays there's a convenience method on the session object
request.session.is_empty()