Created
June 24, 2014 22:57
-
-
Save pyrodney/ed447e0c3ec0ad6f720b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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