def _get_place(country_code, url_slug): if url_slug.startswith('unlisted-'): code = url_slug.replace('unlisted-', '')[1:] pk = converter.to_int(code) place = get_object_or_404(Place, pk = pk, country__country_code = country_code ) if not place.is_unlisted: return place, (country_code, place.url_slug()) else: return place, False else: place = get_object_or_404(Place, slug = url_slug, country__country_code = country_code ) if place.is_unlisted: return place, (country_code, place.url_slug()) else: return place, False
def metadata(request, ids): ids = ids.replace('/', '') ids = [id.strip() for id in ids.split(',') if id.strip()] if len(set(ids)) > MAX_METADATA: return api_response(request, 500, { 'ok': False, 'reason': 'Max %s ids allowed' % MAX_METADATA }) results = dict(zip(ids, [{'ok': False} for id in ids])) from shorturl.views import PREFIXES to_process = {} for shortcode in ids: if shortcode[0] in PREFIXES: try: pk = converter.to_int(shortcode[1:]) except ValueError: continue to_process.setdefault( PREFIXES[shortcode[0]], {} )[pk] = shortcode for model, codes in to_process.items(): pks = codes.keys() found = model.objects.in_bulk(pks) for pk, obj in found.items(): results[codes[pk]] = { 'ok': True, 'shortcode': codes[pk], 'shorturl': 'http://wlny.eu/%s' % codes[pk], 'title': str(obj), 'url': 'http://www.wildlifenearyou.com%s' % \ obj.get_absolute_url(), 'type': model._meta.verbose_name, } return api_response(request, 200, {'results': results, 'ok': True})
def species_identifiers(request): all = Species.objects.all().order_by('slug') allowed_args = ('namespace', 'source', 'key', 'code', 'slug', 'after') external_identifier_args = ('namespace', 'source', 'key') provided_args = [ (arg, request.GET.get(arg, None)) for arg in allowed_args ] for arg, value in provided_args: if value is None: continue if arg in external_identifier_args: all = all.filter(**{ 'external_identifiers__%s' % arg: value }) elif arg == 'code': all = all.filter(pk = converter.to_int(value[1:])) elif arg == 'slug': all = all.filter(slug = value) elif arg == 'after': # Deal with pagination all = all.filter(slug__gte = value) # We only serve up 50 at a time all = list(all[:51]) next_after = None if len(all) == 51: next_after = all[-1].slug all = all[:-1] # Now fetch the external identifiers in one big go identifiers = ExternalIdentifier.objects.filter(**dict([ (k, v) for k, v in provided_args if k in external_identifier_args and v is not None ])).filter( species__in = [s.pk for s in all] ).values('species_id', 'source', 'namespace', 'key', 'uri') species_identifiers = defaultdict(list) for identifier in identifiers: species_identifiers[identifier['species_id']].append(identifier) species = [] for s in all: species.append({ 'url': 'http://www.wildlifenearyou.com%s' % ( s.get_absolute_url() ), 'code': s.short_code(), 'short_url': s.short_url(), 'common_name': s.common_name, 'latin_name': s.latin_name, 'slug': s.slug, 'external_identifiers': [{ 'source': d['source'], 'namespace': d['namespace'], 'key': d['key'], 'uri': d['uri'], } for d in species_identifiers.get(s.pk, [])] }) result = { 'ok': True, 'species': species, 'args': {} } for arg, value in provided_args: if value is not None: result['args'][arg] = value if next_after: result['next'] = 'http://www.wildlifenearyou.com%s?%s' % ( request.path, urllib.urlencode(dict([ (key, value) for key, value in provided_args if ( key != 'after' and value is not None ) ] + [('after', next_after)])) ) return api_response(request, 200, result)