from base import *
from search import code_listing
import re
from srsbrowser.models import SRS, REF_TYPE_CHOICES
 
types = "|".join([type[0] for type in REF_TYPE_CHOICES])    
search_match = re.compile("(%s):([0-9]+)" % types)

def list(request, type=None):
    search = request.GET.get("search","")
    search = search.lower()
    match = search_match.findall(search)
    if match:
        code = match[0]
        srs = SRS.objects.filter(type=code[0], srs_id=code[1])
        if srs.count() == 1:
            return HttpResponseRedirect(srs[0].get_absolute_url()) 
    template_data = code_listing(request, type)
    return render_to_response('references/list.html', template_data)

