multipart stuff.
This commit is contained in:
parent
b6ec851981
commit
e9e6adc823
@ -5,6 +5,11 @@ import os, logging
|
|||||||
from PyQt4 import QtCore, QtGui, QtWebKit, QtNetwork
|
from PyQt4 import QtCore, QtGui, QtWebKit, QtNetwork
|
||||||
from django.test import Client
|
from django.test import Client
|
||||||
from folders import FolderLibrary
|
from folders import FolderLibrary
|
||||||
|
from django.http.request import QueryDict
|
||||||
|
from urlparse import urlparse, parse_qs
|
||||||
|
import cgi
|
||||||
|
from io import BytesIO
|
||||||
|
from django.http.multipartparser import MultiPartParser
|
||||||
|
|
||||||
class DebugPage(QtWebKit.QWebPage):
|
class DebugPage(QtWebKit.QWebPage):
|
||||||
def sayMyName(self):
|
def sayMyName(self):
|
||||||
@ -23,6 +28,8 @@ class DejaWebView(QtWebKit.QWebView):
|
|||||||
#self.oldManager = self.page().networkAccessManager()
|
#self.oldManager = self.page().networkAccessManager()
|
||||||
self.setPage(page)
|
self.setPage(page)
|
||||||
self.page().setNetworkAccessManager(DejaNetworkAccessManager(self))
|
self.page().setNetworkAccessManager(DejaNetworkAccessManager(self))
|
||||||
|
self.client = Client()
|
||||||
|
#self.client.login(username='admin', password='admin')
|
||||||
|
|
||||||
class DejaNetworkAccessManager(QtNetwork.QNetworkAccessManager):
|
class DejaNetworkAccessManager(QtNetwork.QNetworkAccessManager):
|
||||||
'''
|
'''
|
||||||
@ -41,7 +48,7 @@ class DejaNetworkAccessManager(QtNetwork.QNetworkAccessManager):
|
|||||||
'''
|
'''
|
||||||
USE_NETWORK = False
|
USE_NETWORK = False
|
||||||
def __init__(self, parent=None):
|
def __init__(self, parent=None):
|
||||||
QtNetwork.QNetworkAccessManager.__init__(self, parent=None)
|
QtNetwork.QNetworkAccessManager.__init__(self, parent=parent)
|
||||||
if parent:
|
if parent:
|
||||||
self.folders = getattr(parent, 'folders', FolderLibrary())
|
self.folders = getattr(parent, 'folders', FolderLibrary())
|
||||||
|
|
||||||
@ -59,9 +66,7 @@ class DejaNetworkAccessManager(QtNetwork.QNetworkAccessManager):
|
|||||||
#print reply
|
#print reply
|
||||||
return reply
|
return reply
|
||||||
elif operation == self.PostOperation:
|
elif operation == self.PostOperation:
|
||||||
print data.readAll()
|
reply = PageReply(self, request.url(), self.PostOperation, request, data)
|
||||||
#print request
|
|
||||||
reply = PageReply(self, request.url(), self.PostOperation)
|
|
||||||
return reply
|
return reply
|
||||||
elif scheme == 'res':
|
elif scheme == 'res':
|
||||||
if operation == self.GetOperation:
|
if operation == self.GetOperation:
|
||||||
@ -73,8 +78,10 @@ class DejaNetworkAccessManager(QtNetwork.QNetworkAccessManager):
|
|||||||
|
|
||||||
class BasePageReply(QtNetwork.QNetworkReply):
|
class BasePageReply(QtNetwork.QNetworkReply):
|
||||||
content_type = 'text/html; charset=utf-8'
|
content_type = 'text/html; charset=utf-8'
|
||||||
def __init__(self, parent, url, operation):
|
def __init__(self, parent, url, operation, request=None, data=None):
|
||||||
QtNetwork.QNetworkReply.__init__(self, parent)
|
QtNetwork.QNetworkReply.__init__(self, parent)
|
||||||
|
self.data = data
|
||||||
|
self.request = request
|
||||||
self.content = self.initialize_content(url, operation)
|
self.content = self.initialize_content(url, operation)
|
||||||
self.offset = 0
|
self.offset = 0
|
||||||
self.setHeader(QtNetwork.QNetworkRequest.ContentTypeHeader, self.get_content_type())
|
self.setHeader(QtNetwork.QNetworkRequest.ContentTypeHeader, self.get_content_type())
|
||||||
@ -142,18 +149,40 @@ class PageReply(ResourceReply):
|
|||||||
content_type = 'text/html'
|
content_type = 'text/html'
|
||||||
|
|
||||||
def initialize_content(self, url, operation):
|
def initialize_content(self, url, operation):
|
||||||
c = Client()
|
try:
|
||||||
print "Response for %s, method %s" % (url.path(), operation)
|
c = self.parent().parent().client
|
||||||
|
except:
|
||||||
|
logging.error('Internal HTTP Client not found. Creating new.')
|
||||||
|
c = Client()
|
||||||
|
logging.info( "Response for %s, method %s" % (url.path(), operation) )
|
||||||
if operation == DejaNetworkAccessManager.GetOperation:
|
if operation == DejaNetworkAccessManager.GetOperation:
|
||||||
response = c.get(unicode(url.path()), )
|
response = c.get(unicode(url.path()), follow=True )
|
||||||
elif operation == DejaNetworkAccessManager.PostOperation:
|
elif operation == DejaNetworkAccessManager.PostOperation:
|
||||||
response = c.post(unicode(url.path()))
|
ct = str(self.request.rawHeader('Content-Type'))
|
||||||
|
cl = str(self.request.rawHeader('Content-Length'))
|
||||||
|
s = str(self.data.readAll())
|
||||||
|
if ct.startswith('multipart/form-data'):
|
||||||
|
# multipart parsing
|
||||||
|
logging.error('Multipart Parsing Try...')
|
||||||
|
b = BytesIO(s)
|
||||||
|
q, files = MultiPartParser({'CONTENT_TYPE': ct,
|
||||||
|
'CONTENT_LENGTH': cl,
|
||||||
|
},
|
||||||
|
b,
|
||||||
|
[]).parse()
|
||||||
|
response = c.post(unicode(url.path()), q, follow=True)
|
||||||
|
else:
|
||||||
|
# assume post data.
|
||||||
|
q = QueryDict( s )
|
||||||
|
response = c.post(unicode(url.path()), q, follow=True)
|
||||||
self.content_type = response.get('Content-Type', self.content_type)
|
self.content_type = response.get('Content-Type', self.content_type)
|
||||||
# response code
|
# response code
|
||||||
#print "Response Status: %s" % response.status_code
|
#print "Response Status: %s" % response.status_code
|
||||||
# note: on a 404, we might need to trigger file response.
|
# note: on a 404, we might need to trigger file response.
|
||||||
if response.status_code == 404:
|
if response.status_code == 404:
|
||||||
return ResourceReply.initialize_content(self, url, DejaNetworkAccessManager.GetOperation)
|
return ResourceReply.initialize_content(self, url, DejaNetworkAccessManager.GetOperation)
|
||||||
|
if hasattr(response, 'streaming_content'):
|
||||||
|
return ''.join(response.streaming_content)
|
||||||
return response.content
|
return response.content
|
||||||
|
|
||||||
class NoNetworkReply(BasePageReply):
|
class NoNetworkReply(BasePageReply):
|
||||||
|
@ -83,14 +83,17 @@ USE_L10N = True
|
|||||||
|
|
||||||
USE_TZ = True
|
USE_TZ = True
|
||||||
|
|
||||||
|
|
||||||
# Static files (CSS, JavaScript, Images)
|
# Static files (CSS, JavaScript, Images)
|
||||||
# https://docs.djangoproject.com/en/1.6/howto/static-files/
|
# https://docs.djangoproject.com/en/1.6/howto/static-files/
|
||||||
|
|
||||||
STATIC_URL = '/static/'
|
STATIC_URL = '/static/'
|
||||||
MEDIA_URL = '/media/'
|
MEDIA_URL = '/media/'
|
||||||
MEDIA_ROOT = r'D:\work\workspace\scon\src\scon\dj\scon\media'
|
MEDIA_ROOT = r'D:\work\workspace\scon\src\scon\dj\scon\media'
|
||||||
|
|
||||||
|
#SESSION_ENGINE = "django.contrib.sessions.backends.cache"
|
||||||
|
#SESSION_ENGINE = "django.contrib.sessions.backends.cached_db"
|
||||||
|
#SESSION_ENGINE = "django.contrib.sessions.backends.file"
|
||||||
|
#SESSION_ENGINE = "django.contrib.sessions.backends.signed_cookies"
|
||||||
|
|
||||||
DEJAQT_DIRS = {
|
DEJAQT_DIRS = {
|
||||||
STATIC_URL: '',
|
STATIC_URL: '',
|
||||||
}
|
}
|
@ -35,7 +35,7 @@ class Browser(QtGui.QMainWindow):
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
QtGui.QMainWindow.__init__(self)
|
QtGui.QMainWindow.__init__(self)
|
||||||
self.resize(800,600)
|
self.resize(860,600)
|
||||||
self.centralwidget = QtGui.QWidget(self)
|
self.centralwidget = QtGui.QWidget(self)
|
||||||
|
|
||||||
self.mainLayout = QtGui.QHBoxLayout(self.centralwidget)
|
self.mainLayout = QtGui.QHBoxLayout(self.centralwidget)
|
||||||
@ -90,7 +90,7 @@ class Browser(QtGui.QMainWindow):
|
|||||||
#url = self.tb_url.text() if self.tb_url.text() else self.default_url
|
#url = self.tb_url.text() if self.tb_url.text() else self.default_url
|
||||||
#self.html.load(QtCore.QUrl(url))
|
#self.html.load(QtCore.QUrl(url))
|
||||||
#self.html.setHtml(self.serve())
|
#self.html.setHtml(self.serve())
|
||||||
self.html.load(QtCore.QUrl('page:///admin/'))
|
self.html.load(QtCore.QUrl('page:///crafting/overview/'))
|
||||||
self.html.show()
|
self.html.show()
|
||||||
|
|
||||||
def serve(self, what=None):
|
def serve(self, what=None):
|
||||||
|
Loading…
Reference in New Issue
Block a user