Skip to content

Instantly share code, notes, and snippets.

@pyrodney
Created June 24, 2014 22:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pyrodney/ed447e0c3ec0ad6f720b to your computer and use it in GitHub Desktop.
Save pyrodney/ed447e0c3ec0ad6f720b to your computer and use it in GitHub Desktop.
# This is where the magic for Yumalish.com and MePlayGameNow.com happens:
def index(request):
numresources = Resource.objects.count()
if request.method == 'POST':
form = SearchForm(request.POST)
if form.is_valid():
query = form.cleaned_data['query']
tags = parse_tags(query.lower())
matches = set([])
firstTagAdded = False
for tag in tags:
# 'tags__name__in' query requires a list
singleTagList = []
singleTagList.append(tag)
# Get all matches for tag in the 'name', 'address' and 'tags' fields
tagquery = Resource.objects.filter(Q(name__icontains=tag) | Q(address__icontains=tag) | Q(tags__name__in=singleTagList))
# Convert queryset to list of primary keys
resourcematches = tagquery.values_list('pk', flat=True)
# Update the set (this will state unique)
if firstTagAdded:
matches = matches.intersection(resourcematches)
else:
matches.update(resourcematches)
firstTagAdded = True
# Make it a queryset
resourcelist = Resource.objects.filter(pk__in=matches).filter(isActive=True).order_by('-score')
querymade = True
numresults = resourcelist.count()
else:
resourcelist = Resource.objects.filter(isActive=True).order_by('-created')[:50]
else:
resourcelist = Resource.objects.filter(isActive=True).order_by('-created')[:50]
form = SearchForm()
return render(request, 'index.html', locals())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment