Back to index

plone3  3.1.7
Public Member Functions | Static Public Attributes
PlacelessTranslationService.Negotiator.BrowserAccept Class Reference

List of all members.

Public Member Functions

def __init__
def getAccepted

Static Public Attributes

dictionary filters

Detailed Description

Definition at line 61 of file Negotiator.py.


Constructor & Destructor Documentation

Definition at line 68 of file Negotiator.py.

00068 
00069     def __init__(self, request):
00070         pass


Member Function Documentation

def PlacelessTranslationService.Negotiator.BrowserAccept.getAccepted (   self,
  request,
  kind = 'content-type' 
)

Definition at line 71 of file Negotiator.py.

00071 
00072     def getAccepted(self, request, kind='content-type'):
00073         get = request.get
00074         custom_name = ('user_%s' % kind).lower()
00075         if kind == 'content-type':
00076             header_name = ('HTTP_ACCEPT').upper()
00077         else:
00078             header_name = ('HTTP_ACCEPT_%s' % kind).upper()
00079 
00080         try:
00081             user_accepts = get(custom_name, '')
00082             http_accepts = get(header_name, '')
00083         except:
00084             from traceback import print_exc
00085             print_exc()
00086             return
00087         if user_accepts and http_accepts and user_accepts == request.cookies.get('custom_name'):
00088             user_accepts = [a.strip() for a in user_accepts.split(',')]
00089             http_accepts = [a.strip() for a in http_accepts.split(',')]
00090             for l in user_accepts:
00091                 if l not in http_accepts:
00092                     req_accepts = user_accepts + http_accepts
00093                     break
00094                 else:
00095                     # user_accepts is a subset of http_accepts
00096                     request.RESPONSE.expireCookie('custom_name', path='/')
00097                     req_accepts = http_accepts
00098         else:
00099             req_accepts = (user_accepts +','+ http_accepts).split(',')
00100 
00101         accepts = []
00102         i=0
00103         length=len(req_accepts)
00104         filters = self.filters.get(kind, ())
00105 
00106         # parse quality strings and build a tuple like
00107         # ((float(quality), lang), (float(quality), lang))
00108         # which is sorted afterwards if no quality string is given then the
00109         # list order is used as quality indicator
00110         for accept in req_accepts:
00111             for normalizer in filters:
00112                 accept = normalizer(accept)
00113             if accept:
00114                 l = accept.split(';', 2)
00115                 quality = []
00116 
00117                 if len(l) == 2:
00118                     try:
00119                         q = l[1]
00120                         if q.startswith('q='):
00121                             q = q.split('=', 2)[1]
00122                             quality = float(q)
00123                     except:
00124                         pass
00125 
00126                 if quality == []:
00127                     quality = float(length-i)
00128 
00129                 accepts.append((quality, l[0]))
00130                 i += 1
00131 
00132         # sort and reverse it
00133         accepts.sort()
00134         accepts.reverse()
00135 
00136         return [accept[1] for accept in accepts]
00137 

Here is the call graph for this function:


Member Data Documentation

Initial value:
{
        'content-type': (str_lower,),
        'language': (str_lower, lang_normalize, str_strip),
    }

Definition at line 63 of file Negotiator.py.


The documentation for this class was generated from the following file: