Exemple #1
0
 def _cleanSearchFilter(self, category, value, libtype=None):
     # check a few things before we begin
     if category not in self.ALLOWED_FILTERS:
         raise BadRequest('Unknown filter category: %s' % category)
     if category in self.BOOLEAN_FILTERS:
         return '1' if value else '0'
     if not isinstance(value, (list, tuple)):
         value = [value]
     # convert list of values to list of keys or ids
     result = set()
     choices = self.listChoices(category, libtype)
     lookup = {c.title.lower(): unquote(unquote(c.key)) for c in choices}
     allowed = set(c.key for c in choices)
     for item in value:
         item = str((item.id or item.tag
                     ) if isinstance(item, MediaTag) else item).lower()
         # find most logical choice(s) to use in url
         if item in allowed:
             result.add(item)
             continue
         if item in lookup:
             result.add(lookup[item])
             continue
         matches = [k for t, k in lookup.items() if item in t]
         if matches:
             map(result.add, matches)
             continue
         # nothing matched; use raw item value
         log.warning('Filter value not listed, using raw item value: %s' %
                     item)
         result.add(item)
     return ','.join(result)
def get_item_from_url(url):
    # Parse the ClientID and Key from the URL
    clientid = re.findall('[a-f0-9]{40}', url)
    key = re.findall('key=(.*?)(&.*)?$', url)
    if not clientid or not key:
        raise SystemExit('Cannot parse URL: %s' % url)
    clientid = clientid[0]
    key = unquote(key[0][0])
    # Connect to the server and fetch the item
    servers = [r for r in account.resources() if r.clientIdentifier == clientid]
    if len(servers) != 1:
        raise SystemExit('Unknown or ambiguous client id: %s' % clientid)
    server = servers[0].connect()
    return server.fetchItem(key)
Exemple #3
0
 def _cleanSearchFilter(self, category, value, libtype=None):
     # check a few things before we begin
     if category not in self.ALLOWED_FILTERS:
         raise BadRequest('Unknown filter category: %s' % category)
     if category in self.BOOLEAN_FILTERS:
         return '1' if value else '0'
     if not isinstance(value, (list, tuple)):
         value = [value]
     # convert list of values to list of keys or ids
     result = set()
     choices = self.listChoices(category, libtype)
     lookup = {c.title.lower(): unquote(unquote(c.key)) for c in choices}
     allowed = set(c.key for c in choices)
     for item in value:
         item = str((item.id or item.tag) if isinstance(item, MediaTag) else item).lower()
         # find most logical choice(s) to use in url
         if item in allowed: result.add(item); continue
         if item in lookup: result.add(lookup[item]); continue
         matches = [k for t, k in lookup.items() if item in t]
         if matches: map(result.add, matches); continue
         # nothing matched; use raw item value
         log.warning('Filter value not listed, using raw item value: %s' % item)
         result.add(item)
     return ','.join(result)