Skip to content

Commit

Permalink
urls can now have keyword params parsed out i.e. /contact/1
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon Dunleavy committed Apr 13, 2012
1 parent 0bc7653 commit 34be90f
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 50 deletions.
1 change: 1 addition & 0 deletions framework/handlers/__init__.py
@@ -0,0 +1 @@
__author__ = 'jond'
48 changes: 48 additions & 0 deletions framework/handlers/qnetworkhandler.py
@@ -0,0 +1,48 @@
__author__ = 'jond'

from PySide import QtCore, QtNetwork

class FakeReply(QtNetwork.QNetworkReply):
"""
The reply class that is used when a url is to be dealt with by the application
and is not to be dealt with by the usual method
"""
def __init__(self, parent, request, operation, f, args={}):
"""
@type args: dict
"""
QtNetwork.QNetworkReply.__init__(self, parent)
self.setRequest(request)
self.setUrl(request.url())
self.setOperation(operation)
self.open(self.ReadOnly | self.Unbuffered)
# if any are lists of 1 item then just send the item through
for k, v in args.items():
if type(v) is type([]):
if len(v) is 1:
args[k] = v[0]
self.content = f(**args)
self.offset = 0

self.setHeader(QtNetwork.QNetworkRequest.ContentTypeHeader, "application/json; charset=UTF-8")
self.setHeader(QtNetwork.QNetworkRequest.ContentLengthHeader, len(self.content))

QtCore.QTimer.singleShot(0, self, QtCore.SIGNAL("readyRead()"))
QtCore.QTimer.singleShot(0, self, QtCore.SIGNAL("finished()"))


def abort(self):
pass

def bytesAvailable(self):
return len(self.content) - self.offset

def isSequential(self):
return True

def readData(self, maxSize):
if self.offset < len(self.content):
end = min(self.offset + maxSize, len(self.content))
data = self.content[self.offset:end]
self.offset = end
return data
57 changes: 9 additions & 48 deletions framework/networkaccessmanager.py
Expand Up @@ -2,9 +2,9 @@
__author__ = 'jond'

import re
import json
import urlparse
import urllib
import framework.handlers.qnetworkhandler as qnetworkhandler

try:
from PyQt4 import QtCore, QtNetwork
Expand All @@ -19,75 +19,36 @@

class NetworkAccessManager(QtNetwork.QNetworkAccessManager):
"""
Our network asccess manager that is used instead of the default
This allows us to jump in and interupt the calls to usually external
Our network access manager that is used instead of the default
This allows us to jump in and interrupt the calls to usually external
services
"""
def createRequest(self, operation, request, data):
"""
Deal with the request when it comes in
TODO: Only deals with keyworded get requests in urls not ordered
"""
reply = None
requrl = request.url()
requrlstr = requrl.toString()
for urltuple in urls.REDIRECTS:
if re.search(urltuple[0], requrlstr):
m = re.search(urltuple[0], requrlstr)
if m:
argd = {}
if data is not None:
# parse the post data
postargs = unicode(data.readAll())
argd = urlparse.parse_qs(urllib.unquote_plus(postargs.encode('ascii')).decode('utf-8'),
keep_blank_values=True)
# add get data keyword arguments
argd.update(m.groupdict())

reply = FakeReply(self, request, operation, urltuple[1], argd)
reply = qnetworkhandler.FakeReply(self, request, operation, urltuple[1], argd)
# set up the reply with the correct status
reply.setAttribute(QtNetwork.QNetworkRequest.HttpStatusCodeAttribute, 200)
if reply is None:
reply = QtNetwork.QNetworkAccessManager.createRequest(self, operation, request, data)
return reply

class FakeReply(QtNetwork.QNetworkReply):
"""
The reply class that is used when a url is to be dealt with by the application
and is not to be dealt with by the usual method
"""
def __init__(self, parent, request, operation, f, args={}):
"""
@type args: dict
"""
QtNetwork.QNetworkReply.__init__(self, parent)
self.setRequest(request)
self.setUrl(request.url())
self.setOperation(operation)
self.open(self.ReadOnly | self.Unbuffered)
# if any are lists of 1 item then just send the item through
for k, v in args.items():
if type(v) is type([]):
if len(v) is 1:
args[k] = v[0]
self.content = f(**args)
self.offset = 0

self.setHeader(QtNetwork.QNetworkRequest.ContentTypeHeader, "application/json; charset=UTF-8")
self.setHeader(QtNetwork.QNetworkRequest.ContentLengthHeader, len(self.content))

QtCore.QTimer.singleShot(0, self, QtCore.SIGNAL("readyRead()"))
QtCore.QTimer.singleShot(0, self, QtCore.SIGNAL("finished()"))


def abort(self):
pass

def bytesAvailable(self):
return len(self.content) - self.offset

def isSequential(self):
return True

def readData(self, maxSize):
if self.offset < len(self.content):
end = min(self.offset + maxSize, len(self.content))
data = self.content[self.offset:end]
self.offset = end
return data

5 changes: 3 additions & 2 deletions urls.py
Expand Up @@ -3,5 +3,6 @@
import re
import views

REDIRECTS = ((re.compile('contacts'), views.getcontacts),
(re.compile('contact/add.json'), views.addcontact))
REDIRECTS = ((re.compile(r'contacts'), views.getcontacts),
(re.compile(r'contact/(?P<id>[\d]+)'), views.getcontacts),
(re.compile('contact/add'), views.addcontact))

0 comments on commit 34be90f

Please sign in to comment.