Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

View a Popular Network from Search Page #115

Merged
merged 3 commits into from
Feb 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions culturemesh/blueprints/search/controllers.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,20 @@
from culturemesh.blueprints.search.constants import GO_TO_NETWORK_WAIT_SECS

import time
from random import choice

search = Blueprint('search', __name__, template_folder='templates')

MAX_SUGGESTIONS = 10


@search.route("/popular", methods=['GET'])
def go_to_popular_network():
c = Client(mock=False)
nets = c.get_popular_networks(MAX_SUGGESTIONS)
net = choice(nets)
return redirect(url_for('networks.network', id=str(net['id'])))

@search.route("/", methods=['GET', 'POST'])
def render_search_page():

Expand Down
23 changes: 16 additions & 7 deletions culturemesh/blueprints/search/templates/search.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,23 @@ <h4>Search</h4>
<div class="my-2 py-1 row">
<div class="cm-blurb">
<div class="container-fluid p-3">
<div id="cm-search-blurb">
Find people who
</div>
<div class="row p-3">
{{ form.search_type() }}
</div>
<div class="d-flex justify-content-between">
<div>
<div id="cm-search-blurb">
Find people who
</div>
<div class="row p-3">
{{ form.search_type() }}
</div>

{{ form.hidden_tag() }}
{{ form.hidden_tag() }}
</div>
<div class="row p-3">
<a href="/search/popular" id="cm-search-button"
class="my-2 btn btn-secondary btn-sm pr-3
float-right">View A Recommended Network</a>
</div>
</div>

<div class="row p-3">
{{ form.origin_or_language(
Expand Down
15 changes: 15 additions & 0 deletions culturemesh/client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import json
import datetime
import config
from random import randrange
import culturemesh
from culturemesh import app
from difflib import SequenceMatcher
Expand Down Expand Up @@ -140,6 +141,8 @@ def _mock_request(self, url, query_params, body_params):
return self._mock_get_networks(
query_params, body_params
)
elif path[1] == 'popular':
return self._mock_get_random_networks(query_params)
else:
network_id = int(path[1])
return self._mock_get_network(network_id)
Expand Down Expand Up @@ -429,6 +432,16 @@ def _mock_get_networks(self, query_params, body_params):
networks = sorted(networks, key=lambda x: x['search_rank'], reverse=True)
return networks

def _mock_get_random_networks(self, query_params):
num_nets = query_params['count']
nets = []
with open(NETWORK_DATA_LOC) as networks:
networks = json.load(networks)
while len(nets) < num_nets and len(nets) < len(networks):
i = randrange(0, len(networks))
if networks[i] not in nets:
nets.append(networks[i])
return nets

def _mock_get_network(self, network_id):
"""
Expand Down Expand Up @@ -661,6 +674,7 @@ def _mock_language_autocomplete(self, input_text):
from .networks import get_network_users
from .networks import get_network_user_count
from .networks import get_network_post_count
from .networks import get_popular_networks

# We may consider adding a wrapper around these assignments
# below to introduce more specific features for the client.
Expand Down Expand Up @@ -712,3 +726,4 @@ def _mock_language_autocomplete(self, input_text):
Client.get_network_users = get_network_users
Client.get_network_user_count = get_network_user_count
Client.get_network_post_count = get_network_post_count
Client.get_popular_networks = get_popular_networks
16 changes: 16 additions & 0 deletions culturemesh/client/networks.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@

####################### GET methods #######################


def ping_network(client):
url = 'network/ping'
return client._request(url, Request.GET)


def get_networks(client,
count,
max_id=None,
Expand Down Expand Up @@ -98,6 +100,7 @@ def get_network_users(client, networkId, count, max_id=None):
query_params['max_id'] = max_id
return client._request(url, Request.GET, query_params=query_params)


def get_network_user_count(client, networkId):
"""
:param client: the CultureMesh API client
Expand All @@ -108,6 +111,7 @@ def get_network_user_count(client, networkId):
url = 'network/%s/user_count' % str(networkId)
return client._request(url, Request.GET)


def get_network_post_count(client, networkId):
"""
:param client: the CultureMesh API client
Expand All @@ -118,5 +122,17 @@ def get_network_post_count(client, networkId):
url = 'network/%s/post_count' % str(networkId)
return client._request(url, Request.GET)


def get_popular_networks(client, numNets):
"""Get a list of popular networks

:param client: the CultureMesh API client
:param numNets: The number of networks to include. Must be in (0, 30]
:return: List of networks as JSON
"""
url = 'network/popular'
query_params = {'count': numNets}
return client._request(url, Request.GET, query_params=query_params)

####################### POST methods #######################
####################### PUT methods #######################
25 changes: 25 additions & 0 deletions test/unit/client/test_networks.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# Tests client/networks.py
#

import random
from nose.tools import assert_true, assert_equal
import test.unit.client.client_test_prep
from culturemesh.client import Client
Expand Down Expand Up @@ -87,3 +88,27 @@ def test_get_network_users():
registrations2 = c.get_network_users(2, 1, max_id="2017-02-28 11:53:30")
assert_equal(registrations2[0]['join_date'], "2017-02-27 11:53:30")
assert_equal(len(registrations2), 1)


# For random.seed(1)
expected_pop_nets = [
{'id': 1, 'location_cur': {'country_id': 1, 'region_id': 1, 'city_id': 2},
'location_origin': {'country_id': 1, 'region_id': 2, 'city_id': 3},
'language_origin': {'id': 2, 'name': 'entish', 'num_speakers': 10,
'added': 0}, 'network_class': 0,
'date_added': '1990-11-23 15:31:51', 'img_link': 'img1.png'},
{'id': 2, 'location_cur': {'country_id': 2, 'region_id': 4, 'city_id': 6},
'location_origin': {'country_id': 1, 'region_id': 1, 'city_id': 1},
'language_origin': {'id': 3, 'name': 'valarin', 'num_speakers': 1000,
'added': 0},
'network_class': 0, 'date_added': '1995-11-23 15:31:51',
'img_link': 'img2.png'}
]


def test_get_popular_networks():
c = Client(mock=True)
random.seed(1)
nets = c.get_popular_networks(2)

assert_equal(nets, expected_pop_nets)