Using cherrypy.lib.sessions.expire - cherrypy

My cherrypy class definition includes the method:
#cherrypy expose
def quit(self)
cherrypy.lib.sessions.expire(self)
return "Session closed"
My goal is to reset the current session. However, this function call returns "Missing module name". Any hints?

Ignore my question! I made a confusion. ;-(

Related

Differentiating between when a user calls a method and when a computer calls a method

I'm having trouble with figuring out how to know when a user calls a class method and when the method is called within my own code.
class animal:
def lower_stats(self):
if user_called==True:
return("You monster")
else:
self.hunger-=10
def other_code(self):
...
self.lower_stats()
How can I make it to where when a user calls .lower_stats() it returns "You monster", but when .other_code() executes .lower_stats(), 10 will be subtracted from self.hunger?
The methode itself can not know whether it got called by a code or from elsewhere.
It basically gets always called as a codeblock no distinctions made.
The only way to like get similar behaviour is making the methods private by annotating it with two double underscores at the start of the methode name.
And writing a public one that does something when a user calls it.
Geeks For Geeks Private Methodes Python
Tho that won't really stop people from calling the private function cause in python there are no real private methodes.
Another way would be having some kind of logic inside the function to authorize the call to the function.
Tho that can be bypassed...

include validation service in codeigniter4 failed

i try to make my first form validation in codeigniter4.For that i've create a controller with a function to check the post-vars. At the beginning, ci4 need to load the libary, so i do it like in the docu
$validation = \Config\Services::validation();
For this i recieve the following error.
...
message "Class 'Myth\\Auth\\Authentication\\Passwords\\ValidationRules' not found"
file "mypath/system/Validation/Validation.php"
function "loadRuleSets"
class "CodeIgniter\\Validation\\Validation"
function "run"
class "CodeIgniter\\Validation\\Validation"
....
What i do wrong, or is there something to setup for a new project before it works?
omg
i've found the problem. in the app/config autoload.php and validation.php was a class "Myth" wich is not in use. SO i commet it out and the thinks works fine.

CherryPy Tool: How to register custom tool correctly?

I want to create a simple tool but fail to register it correctly. As soon as I add it to any method I get the error:
AttributeError: 'Toolbox' object has no attribute 'authenticate'
I tried
cherrypy.tools.authenticate = cherrypy.Tool('before_handler', authenticate)
and
#cherrypy.tools.register('before_handler')
def authenticate():
The issue I likely have is placing the function in the wrong place. I have a main file launching the server and all apps:
#config stuff
if __name__ == '__main__':
cherrypy.engine.unsubscribe('graceful', cherrypy.log.reopen_files)
logging.config.dictConfig(LOG_CONF)
cherrypy.tree.mount(app1(), '/app1')
cherrypy.tree.mount(app2(), '/app2')
cherrypy.quickstart(app3)
This file is launched by a systemd unit.
If I put the authenticate function in the config area, it doesn't work. If i put it in one of the apps directly and only use it in that app, it doesn't work. Always the same error.
So where do I have to place it to make this work?
Another case of me falling into the python definition order matters trap.
Doesn't work:
class MyApp(object):
#....
#cherrypy.tools.register('on_start_resource')
def authenticate():
#....
Works:
#cherrypy.tools.register('on_start_resource')
def authenticate():
#....
class MyApp(object):

How to access the current config in a view handler?

I want to use a config method ('absolute_asset_spec') in a view handler.
How do I get the config object to invoke it on?
I've seen talk of a Config() method but where is it defined? Searching for it is, shall we say, problematic :)
Is request.registry.settings what you are looking for?
More details here:
Pyramid and .ini configuration
Or you were talking about a method. You want to access a method in every request?
def some_method():
return 'stuff you want'
# in your main function
config.add_request_method(some_method, 'stuff', reify=True)
# in a view
foo = request.stuff
Thanks for the pointer. The answer is:
#view_config(route_name='home', renderer='templates/home.pt')
def home_view(request):
config = Configurator(registry=request.registry)
path = config.absolute_asset_spec('__init__.py')
:
:

The specified object is not recognized as a fake object. Issue

I am having an issue where a FakeItEasy call in an extremely simple test is failing with the error "The specified object is not recognized as a fake object." The call is simple:
A.CallTo(myService.MyMethod(listOfStringsFilter)).MustHaveHappened();
The fake is similarly simple (A.Fake()), and fakes out an interfance with one method, that takes in a list and returns a list. In debug mode, I see the instance of myService is of type {Fake IMyInterface}. Anyway, this issue is really holding me up, thanks in advance for your help.
Update:
This was my own darn mistake, I needed to make the call say:
A.CallTo(() => myService.MyMethod(listOfStringsFilter)).MustHaveHappened();
This was my own darn mistake, I needed to make the call say:
A.CallTo(() => myService.MyMethod(listOfStringsFilter)).MustHaveHappened();

Resources