def __init__(self, db): super(SiteCompareApplication, self).__init__() self.db = db self.users = Users(db) self.pages_collection = PagesCollection(db) self.add_resource('static', FileServerApplication(static_dir))
class SiteCompareApplication(RestApplication): def __init__(self, db): super(SiteCompareApplication, self).__init__() self.db = db self.users = Users(db) self.pages_collection = PagesCollection(db) self.add_resource('static', FileServerApplication(static_dir)) def GET(self, request, collection=None, resource=None): if collection is None: latest_rvn = self.db.views.sitecompare.runByTime(startkey=["releaseVSnightly", {}], descending=True, limit=1)[0] latest_rvn.tests = self.db.views.sitecompare.testByRun(key=latest_rvn._id) latest_h4v5 = self.db.views.sitecompare.runByTime(startkey=["html4VShtml5", {}], descending=True, limit=1)[0] latest_h4v5.tests = self.db.views.sitecompare.testByRun(key=latest_h4v5._id) return MakoResponse('index', latest=(latest_rvn, latest_h4v5,)) if collection == "pages": if resource is None: view_result = self.db.views.sitecompare.byType(key="page") return MakoResponse('pages', pages=view_result) else: d = self.db.get(resource) d.query = dict(request.query) tests = self.db.views.sitecompare.testByPage(startkey=[d._id,0], endkey=[d._id,{}]) return MakoResponse('page', tests=tests, **dict(d)) if collection == 'runs': if resource is None: runs = self.db.views.sitecompare.runByTime(descending=True) return MakoResponse('runs', runs=runs) else: run = self.db.get(resource) run.tests = self.db.views.sitecompare.testByRun(key=run._id) return MakoResponse('run', run=run) if collection == 'tests': if resource is None: pass else: test = self.db.get(resource) run = self.db.get(test['run-id']) return MakoResponse('test', test=test, run=run) if collection == 'builds': if resource is None: pass else: build = self.db.get(resource) runs = self.db.views.sitecompare.runByBuild(startkey=build._id, endkey=build._id+'0') return MakoResponse('build', build=build, runs=runs) def POST(self, request, collection=None, resource=None): if collection == "pages": if resource is None: if request['CONTENT_TYPE'] == "application/json": resp = self.pages_collection.add_resource( simplejson.loads(str(request.body))) elif request['CONTENT_TYPE'] == "application/x-www-form-urlencoded": # Check if this already exists: k = request.body['uri'] r = self.db.views.sitecompare.pageByURI(key=k) if len(r) is not 0: return webenv.Response303("/sitecompare/pages/"+str(r[0]['_id'])+'?m=already') resp = self.pages_collection.add_resource(dict(request.body)) return webenv.Response303("/sitecompare/pages/"+str(resp['id'])) else: if request['CONTENT_TYPE'] == "application/json": resp = self.pages_collection.update_resource( simplejson.loads(str(request.body))) elif request['CONTENT_TYPE'] == "application/x-www-form-urlencoded": resp = self.pages_collection.update_resource(dict(request.body)) elif collection == "add_notification": user = self.users.get_user_by_email(request.body.form['email']) threshold = request.body.form['threshold'] if not threshold: threshold = 0 threshold = int(threshold) notifications = user.setdefault('email_notifications', {}).setdefault("site_compare", []) if resource not in notifications: user['email_notifications']['site_compare'].append({"threshold":threshold,"pageid":resource}) self.db.update(user) if user.email_verified: return HtmlResponse("<html><title>Hurray!</title><body>You will be notified when this page differs above your threshold.</body></html>") else: return HtmlResponse("<html><title>Verification Pending</title><body>A verification email was sent to your Inbox and we are waiting for it to be verified.</body></html>")