This repository has been archived by the owner on Oct 30, 2018. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixes bug 1141173 - using autocompeter.com
- Loading branch information
Showing
17 changed files
with
594 additions
and
11 deletions.
There are no files selected for viewing
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
8 changes: 8 additions & 0 deletions
8
airmozilla/main/static/main/autocompeter/autocompeter.min.css
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
._ac-wrap{position:relative;display:inline-block} | ||
._ac-wrap ._ac-hint{position:absolute;top:0;left:0;border-color:transparent;box-shadow:none;opacity:1;color:#b4b4b4;background:#fff} | ||
._ac-wrap ._ac-foreground{background-color:transparent;position:relative;vertical-align:top} | ||
._ac-wrap ._ac-results{z-index:10;position:absolute;background-color:#fff;border:1px solid #ebebeb;width:400px;display:none;border-radius:3px;box-shadow:0 1px 5px rgba(0,0,0,.25)} | ||
._ac-wrap ._ac-results p{padding:5px;margin:0;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;border-bottom:1px solid #ebebeb;cursor:pointer;text-align:left} | ||
._ac-wrap ._ac-results p a{display:block} | ||
._ac-wrap ._ac-results p:last-child{border-bottom:none} | ||
._ac-wrap ._ac-results p.selected{background-color:#f2f2f2} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* This file is all about overrides for the autocompeter.min.css | ||
library. */ | ||
|
||
._ac-wrap ._ac-hint { | ||
background: none; | ||
color: #2D373B; | ||
} | ||
._ac-wrap ._ac-results { | ||
width: 290px; | ||
} | ||
@media only screen and (max-device-width : 320px) { | ||
._ac-wrap ._ac-results { | ||
width: 230px; | ||
} | ||
} | ||
._ac-results p { | ||
background-color: #20272A; | ||
color: #B4B4B4; | ||
} | ||
._ac-wrap ._ac-results { | ||
border-color: #666; | ||
} | ||
._ac-wrap ._ac-results p { | ||
border-bottom: 1px dotted #666; | ||
font-size: .857em; | ||
} | ||
._ac-wrap ._ac-results p.selected { | ||
background-color: #2D373B; | ||
} | ||
|
||
._ac-wrap input[type="text"]:focus { | ||
background-color: transparent; | ||
} |
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 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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
$(function() { | ||
var config = $('autocompeter'); | ||
if (config.length) { | ||
var options = {}; | ||
if (config.data('url')) { | ||
options.url = config.data('url'); | ||
} | ||
if (config.data('groups')) { | ||
options.groups = config.data('groups'); | ||
} | ||
if (config.data('domain')) { | ||
options.domain = config.data('domain'); | ||
} | ||
Autocompeter(document.getElementById('id_q'), options); | ||
} | ||
}); |
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 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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
import datetime | ||
import json | ||
import time | ||
|
||
import requests | ||
|
||
from django.conf import settings | ||
from django.utils import timezone | ||
|
||
from funfactory.urlresolvers import reverse | ||
|
||
from airmozilla.main.models import Event, EventHitStats | ||
|
||
|
||
def update( | ||
verbose=False, all=False, flush_first=False, max_=1000, | ||
since=datetime.timedelta(minutes=60) | ||
): | ||
if not getattr(settings, 'AUTOCOMPETER_KEY', None): | ||
if verbose: # pragma: no cover | ||
print "Unable to submit titles to autocompeter.com" | ||
print "No settings.AUTOCOMPETER_KEY set up" | ||
return | ||
|
||
autocompeter_url = getattr( | ||
settings, | ||
'AUTOCOMPETER_URL', | ||
'https://autocompeter.com/v1' | ||
) | ||
if flush_first: | ||
assert all, "must be all if you're flushing" | ||
t0 = time.time() | ||
response = requests.delete( | ||
autocompeter_url + '/flush', | ||
headers={ | ||
'Auth-Key': settings.AUTOCOMPETER_KEY, | ||
}, | ||
verify=not settings.DEBUG | ||
) | ||
t1 = time.time() | ||
if verbose: # pragma: no cover | ||
print response | ||
print "Took", t1 - t0, "seconds to flush" | ||
assert response.status_code == 204, response.status_code | ||
|
||
now = timezone.now() | ||
|
||
if all: | ||
hits_map = dict( | ||
EventHitStats.objects.all().values_list('event', 'total_hits') | ||
) | ||
values = hits_map.values() | ||
if values: | ||
median_hits = sorted(values)[len(values) / 2] | ||
else: | ||
median_hits = 0 | ||
events = Event.objects.approved() | ||
else: | ||
events = ( | ||
Event.objects.approved() | ||
.filter(modified__gte=now-since)[:max_] | ||
) | ||
if events: | ||
# there are events, we'll need a hits_map and a median | ||
hits_map = dict( | ||
EventHitStats.objects.filter(event__in=events) | ||
.values_list('event', 'total_hits') | ||
) | ||
values = ( | ||
EventHitStats.objects.all() | ||
.values_list('total_hits', flat=True) | ||
) | ||
if values: | ||
median_hits = sorted(values)[len(values) / 2] | ||
else: | ||
median_hits = 0 | ||
|
||
documents = [] | ||
for event in events: | ||
url = reverse('main:event', args=(event.slug,)) | ||
title = event.title | ||
if event.start_time > now: | ||
# future events can be important too | ||
popularity = median_hits | ||
else: | ||
hits = hits_map.get(event.id, 0) | ||
popularity = hits | ||
if event.privacy == Event.PRIVACY_PUBLIC: | ||
group = '' | ||
else: | ||
group = event.privacy | ||
|
||
documents.append({ | ||
'title': title, | ||
'url': url, | ||
'popularity': popularity, | ||
'group': group, | ||
}) | ||
|
||
if verbose: # pragma: no cover | ||
from pprint import pprint | ||
pprint(documents) | ||
|
||
if not documents: | ||
if verbose: # pragma: no cover | ||
print "No documents." | ||
return | ||
|
||
t0 = time.time() | ||
response = requests.post( | ||
autocompeter_url + '/bulk', | ||
data=json.dumps({'documents': documents}), | ||
headers={ | ||
'Auth-Key': settings.AUTOCOMPETER_KEY, | ||
}, | ||
verify=not settings.DEBUG | ||
) | ||
t1 = time.time() | ||
assert response.status_code == 201, response.status_code | ||
if verbose: # pragma: no cover | ||
print response | ||
print "Took", t1 - t0, "seconds to bulk submit" |
Oops, something went wrong.