How to use pyramid #view_defaults to specify multiple routes for same view - decorator

I'm finding I can use multiple #view_config decorators to cause a view to handle more than one route, like this:
#view_config(route_name = "user_create")
#view_config(route_name = "user_edit")
def handle_it(request):
pass
But when I try to achieve the same thing with the class decorator, #view_defaults, it doesn't work:
#view_defaults(route_name = "user_create")
#view_defaults(route_name = "user_edit")
class Foo(object):
def __init__(self, request):
self.request = request
def handle_it(self):
pass
What happens in this last case is the first route mentioned (user_create) is honored by pyramid, but request for the user_edit route produces a 404 error.
Shouldn't I be able to use #view_defaults this way?
Michael

view_defaults are used as an input to each view_config call. They are not a replacement. The only thing routing cares about is the actual views registered, which is done with view_config or config.add_view. view_defaults does not register views.

Related

How to fix "NameError name 'changePlaying' is not defined

I'm using cogs to shorten and organise my discord bot. However upon attempting a "Events" cog I'm faced with a NameError of changePlaying not being defined despite it literally being about the on_ready command
A: I forgot to import discord.ext and hence imported that.
B: I've tried changing the location of the list of possible statuses in and out of the changePlaying event
PlayingList = [Maximus.py.","!help"]
async def changePlaying(self):
while True:
await self.bot.change_presence(game=Game(name=random.choice(PlayingList)))
await asyncio.sleep(120)
async def on_ready(self):
print('Logged in as')
print(self.bot.user.name)
print(self.bot.user.id)
print('-----------------------------------------')
print('Log in complete')
for x in range(5):
print("")
self.bot.loop.create_task(changePlaying(self))
Well I think it's clear what the expected results are but to clarify the bot is supposed to boot. It does come online and does listen to commands but the status bar does not change
In view of the self argument of the methods, I see they are in the cog. You should use PlayingList as an attribute of the cog, that is, in its __init__ add instead self.PlayingList = ["Maximus.py.","!help"] and then access it through self. in the methods.
And so the answer is : you aren't using methods correctly. You have to do self.changePlaying() not changePlaying(self).
By the way, use a tuple instead of a list if you do not plan to modify it through the execution. And a variable name shouldn't start with a capital letter, as it is commonly reserved to classes. See PEP 8.

How to route DooPHP URIs that include querystring?

I need to be able route requests that pass information in the querystring. For example, let's say my application calls /api/company/delete?id=17. How can I route that in DooPHP? I already have a catchall route defined, which is grabbing this request, but I need to be able to process these without a catchall route.
# this doesn't work
$route['get']['/api/company/delete?id=:id'] = array('AdminController', 'deletecompany');
# at the bottom of my routes I have this catchall route, which works but it catches --everything--
$route['*']['catchall']['/:id'] = array('HomeController', 'getregions');
Am I missing something obvious?
routes
$route['get']['/api/company/delete/:id'] = array('AdminController', 'deletecompany');
controller
$this->params['id']
or
routes
$route['post']['/api/company/delete'] = array('AdminController', 'deletecompany');
controller
$_POST['id']

How do you Identify the request that a QNetworkReply finished signal is emitted in response to when you are doing multiple requests In QtNetwork?

I have a project will load a HTTP page, parse it, and then open other pages based on the data it received from the first page.
Since Qt's QNetworkAccessManager works asyncronusly, it seems I should be able to load more than one page at a time by continuing to make HTTP requests, and then taking care of the response would happen in the order the replies come back and would be handled by the even loop.
I'm a having a few problems figuring out how to do this though:
First, I read somewhere on stackoverflow that you should use only one QNetworkAccess manager. I do not know if that is true.
The problem is that I'm connecting to the finished slot on the single QNetworkAccess manager. If I do more than one request at a time, I don't know what request the finished signal is in response to. I don't know if there is a way to inspect the QNetworkReply object that is passed from the signal to know what reply it is in response to? Or should I actually be using a different QNetworkAccessManager for each request?
Here is an example of how I'm chaining stuff together right now. But I know this won't work when I'm doing more than one request at at time:
from PyQt4 import QtCore,QtGui,QtNetwork
class Example(QtCore.QObject):
def __init__(self):
super().__init__()
self.QNetworkAccessManager_1 = QtNetwork.QNetworkAccessManager()
self.QNetworkCookieJar_1 = QtNetwork.QNetworkCookieJar()
self.QNetworkAccessManager_1.setCookieJar(self.QNetworkCookieJar_1)
self.app = QtGui.QApplication([])
def start_request(self):
QUrl_1 = QtCore.QUrl('https://erikbandersen.com/')
QNetworkRequest_1 = QtNetwork.QNetworkRequest(QUrl_1)
#
self.QNetworkAccessManager_1.finished.connect(self.someurl_finshed)
self.QNetworkAccessManager_1.get(QNetworkRequest_1)
def someurl_finshed(self, NetworkReply):
# I do this so that this function won't get called for a diffent request
# But it will only work if I'm doing one request at a time
self.QNetworkAccessManager_1.finished.disconnect(self.someurl_finshed)
page = bytes(NetworkReply.readAll())
# Do something with it
print(page)
QUrl_1 = QtCore.QUrl('https://erikbandersen.com/ipv6/')
QNetworkRequest_1 = QtNetwork.QNetworkRequest(QUrl_1)
#
self.QNetworkAccessManager_1.finished.connect(self.someurl2_finshed)
self.QNetworkAccessManager_1.get(QNetworkRequest_1)
def someurl2_finshed(self, NetworkReply):
page = bytes(NetworkReply.readAll())
# Do something with it
print(page)
kls = Example()
kls.start_request()
I am not familiar to PyQt but from general Qt programming point of view
Using only one QNetworkAccessManager is right design choice
finished signal provides QNetworkReply*, with that we can identify corresponding request using request().
I hope this will solve your problem with one manager and multiple requests.
This is a C++ example doing the same.

Twisted post length limit

How do you set the limit on the length of a POST request in twisted? I've looked around in the docs and can't find anything. It would even help if I knew the default limit.
I don't believe there is a default limit on the size of incoming POST data, but you could pretty easily impose one by overriding the Request.handleContentChunk() method, with something like:
from twisted.web import server
class SizeLimitingRequest(server.Request):
def handleContentChunk(self, data):
if self.content.tell() + len(data) > self.size_limit_on_post_data:
raise SomeKindOfError("too much data nooooooo")
return server.Request.handleContentChunk(self, data)
the actual method you'd be overriding is in twisted.web.http.Request, which is a superclass of server.Request. To make use of your shiny new class, just set your Site instance's requestFactory attribute:
mysite.requestFactory = SizeLimitingRequest

JAX-WS: define server side handler chain in code and not in external xml

i want to enable http compression for my jax-ws webservice. i found out that i have to do it with a custom handlerchain that can modify the http-headers.
all tutorials i found refer to the annotation #HandlerChain that points to a handler chain configuration xml-file but my problem is that my webservice has to be as lightweight as possible therefore i cant define my handler chain in an external xml file.
i tried the following but did not succeed:
final Endpoint ep = Endpoint.publish("http://localhost:8878/mywebservice",
new WebserviceImpl() );
final Binding binding = ep.getBinding();
final List<Handler> handlerChain = binding.getHandlerChain();
handlerChain.add(new MySuperbSOAPHandler());
binding.setHandlerChain(handlerChain);
does anyone know how to do this? is it even possible?
It doesn't appear that you can change the handler chain when the service has already been published.
If your actual use case is as above, it's easy to fix by simply create()ing and then publish()ing.
final Endpoint ep = Endpoint.create(new WebserviceImpl() );
final Binding binding = ep.getBinding();
final List<Handler> handlerChain = binding.getHandlerChain();
handlerChain.add(new MySuperbSOAPHandler());
binding.setHandlerChain(handlerChain);
ep.publish("http://localhost:8878/mywebservice");

Resources