* Changes to client to enable visit tracking. Incidentally, makes better use
of traffic to the db at the expense of data to the account server. * jsonfas visit manager.
This commit is contained in:
parent
f60b394cfe
commit
f60e9f162f
2 changed files with 95 additions and 3 deletions
|
@ -178,8 +178,12 @@ class BaseClient(object):
|
||||||
if auth:
|
if auth:
|
||||||
req.add_header('Cookie', self.session.output(attrs=[],
|
req.add_header('Cookie', self.session.output(attrs=[],
|
||||||
header='').strip())
|
header='').strip())
|
||||||
|
elif self._sessionCookie:
|
||||||
|
# If the cookie exists, send it so that visit tracking works.
|
||||||
|
req.add_header('Cookie', self._sessionCookie.output(attrs=[],
|
||||||
|
header='').strip())
|
||||||
try:
|
try:
|
||||||
response = urllib2.urlopen(req).read()
|
response = urllib2.urlopen(req)
|
||||||
except urllib2.HTTPError, e:
|
except urllib2.HTTPError, e:
|
||||||
if e.msg == 'Forbidden':
|
if e.msg == 'Forbidden':
|
||||||
if (inspect.currentframe().f_back.f_code !=
|
if (inspect.currentframe().f_back.f_code !=
|
||||||
|
@ -195,8 +199,14 @@ class BaseClient(object):
|
||||||
log.error(e)
|
log.error(e)
|
||||||
raise ServerError, str(e)
|
raise ServerError, str(e)
|
||||||
|
|
||||||
|
# In case the server returned a new session cookie to us
|
||||||
try:
|
try:
|
||||||
data = simplejson.loads(response)
|
self._sessionCookie.load(response.headers['set-cookie'])
|
||||||
|
except KeyError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
try:
|
||||||
|
data = simplejson.load(response)
|
||||||
except Exception, e:
|
except Exception, e:
|
||||||
regex = re.compile('<span class="fielderror">(.*)</span>')
|
regex = re.compile('<span class="fielderror">(.*)</span>')
|
||||||
match = regex.search(response)
|
match = regex.search(response)
|
||||||
|
|
82
fas/fas/jsonfasvisit.py
Normal file
82
fas/fas/jsonfasvisit.py
Normal file
|
@ -0,0 +1,82 @@
|
||||||
|
'''
|
||||||
|
This plugin provides integration with the Fedora Account System using JSON
|
||||||
|
calls to the account system server.
|
||||||
|
'''
|
||||||
|
|
||||||
|
import Cookie
|
||||||
|
|
||||||
|
from datetime import datetime
|
||||||
|
|
||||||
|
from sqlalchemy import *
|
||||||
|
from sqlalchemy.orm import class_mapper
|
||||||
|
|
||||||
|
from turbogears import config
|
||||||
|
from turbogears.visit.api import BaseVisitManager, Visit
|
||||||
|
from turbogears.database import get_engine, metadata, session, mapper
|
||||||
|
from turbogears.util import load_class
|
||||||
|
|
||||||
|
# Once this works, propogate the changes back to python-fedora and import as
|
||||||
|
# from fedora.tg.client import BaseClient
|
||||||
|
from client import BaseClient
|
||||||
|
|
||||||
|
import gettext
|
||||||
|
t = gettext.translation('python-fedora', '/usr/share/locale', fallback=True)
|
||||||
|
_ = t.ugettext
|
||||||
|
|
||||||
|
import logging
|
||||||
|
log = logging.getLogger("turbogears.identity.savisit")
|
||||||
|
|
||||||
|
class JsonFasVisitManager(BaseClient):
|
||||||
|
'''
|
||||||
|
This proxies visit requests to the Account System Server running remotely.
|
||||||
|
|
||||||
|
We don't need to worry about threading and other concerns because our proxy
|
||||||
|
doesn't cause any asynchronous calls.
|
||||||
|
'''
|
||||||
|
fasURL = config.get('fas.url', 'https://admin.fedoraproject.org/admin/fas')
|
||||||
|
cookieName = config.get('visit.cookie.name', 'tg-visit')
|
||||||
|
|
||||||
|
def __init__(self, timeout, debug=None):
|
||||||
|
super(JsonFasVisitManager,self).__init__(self.fasURL, debug=debug)
|
||||||
|
|
||||||
|
def create_model(self):
|
||||||
|
'''
|
||||||
|
Create the Visit table if it doesn't already exist
|
||||||
|
'''
|
||||||
|
# Not needed as the visit tables reside remotely in the FAS2 database.
|
||||||
|
pass
|
||||||
|
|
||||||
|
def new_visit_with_key(self, visit_key):
|
||||||
|
# Hit any URL in fas2 with the visit_key set. That will call the
|
||||||
|
# new_visit method in fas2
|
||||||
|
self._sessionCookie = Cookie.SimpleCookie()
|
||||||
|
self._sessionCookie[self.cookieName] = visit_key
|
||||||
|
data = self.send_request('', auth=True)
|
||||||
|
return Visit(self._sessionCookie[self.cookieName].value, True)
|
||||||
|
|
||||||
|
def visit_for_key(self, visit_key):
|
||||||
|
'''
|
||||||
|
Return the visit for this key or None if the visit doesn't exist or has
|
||||||
|
expired.
|
||||||
|
'''
|
||||||
|
# Hit any URL in fas2 with the visit_key set. That will call the
|
||||||
|
# new_visit method in fas2
|
||||||
|
self._sessionCookie = Cookie.SimpleCookie()
|
||||||
|
self._sessionCookie[self.cookieName] = visit_key
|
||||||
|
data = self.send_request('', auth=True)
|
||||||
|
# Knowing what happens in turbogears/visit/api.py when this is called,
|
||||||
|
# we can shortcircuit this step and avoid a round trip to the FAS
|
||||||
|
# server.
|
||||||
|
# if visit_key != self._sessionCookie[self.cookieName].value:
|
||||||
|
# # visit has expired
|
||||||
|
# return None
|
||||||
|
# # Hitting FAS has already updated the visit.
|
||||||
|
# return Visit(visit_key, False)
|
||||||
|
if visit_key != self._sessionCookie[self.cookieName].value:
|
||||||
|
return Visit(self._sessionCookie[self.cookieName].value, True)
|
||||||
|
else:
|
||||||
|
return Visit(visit_key, False)
|
||||||
|
|
||||||
|
def update_queued_visits(self, queue):
|
||||||
|
# Let the visit_manager on the FAS server manage this
|
||||||
|
pass
|
Loading…
Add table
Add a link
Reference in a new issue