Skip to content
This repository has been archived by the owner on Jan 19, 2021. It is now read-only.

Commit

Permalink
Merge pull request #1006 from akatsoulas/1022621
Browse files Browse the repository at this point in the history
[fix bug 1022621] Upgrade API v1 to support new geolocation fields.
  • Loading branch information
akatsoulas committed Jul 15, 2014
2 parents ac877e1 + af94cd8 commit 499f62e
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 19 deletions.
2 changes: 1 addition & 1 deletion mozillians/phonebook/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ def edit_profile(request):
redeem_invite(profile, request.session.get('invite-code'))
messages.info(request, _(u'Your account has been created.'))
elif user.username != old_username:
# Notify the user that their old profile URL won't work.
# Notify the user that their old profile URL won't work.
messages.info(request,
_(u'You changed your username; please note your '
u'profile URL has also changed.'))
Expand Down
21 changes: 16 additions & 5 deletions mozillians/users/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ class UserResource(ClientCacheResourceMixIn, GraphiteMixIn, ModelResource):
languages = fields.CharField()
url = fields.CharField()
accounts = fields.CharField()
city = fields.CharField(attribute='geo_city__name', null=True, readonly=True, default='')
region = fields.CharField(attribute='geo_region__name', null=True, readonly=True, default='')
country = fields.CharField(attribute='geo_country__code', null=True, readonly=True, default='')

class Meta:
queryset = UserProfile.objects.all()
Expand Down Expand Up @@ -82,11 +85,19 @@ def build_filters(self, filters=None):
elif value == 'false':
database_filters['is_vouched'] = Q(is_vouched=False)

for possible_filter in ['country', 'region', 'city', 'ircname']:
if possible_filter in valid_filters:
database_filters[possible_filter] = Q(
**{'{0}__iexact'.format(possible_filter):
getvalue(possible_filter)})
if 'country' in valid_filters:
database_filters['country'] = Q(geo_country__code=getvalue('country'))

if 'region' in valid_filters:
database_filters['region'] = Q(geo_region__name=getvalue('region'))

if 'city' in valid_filters:
database_filters['city'] = Q(geo_city__name=getvalue('city'))

if 'ircname' in valid_filters:
database_filters['ircname'] = Q(
**{'{0}__iexact'.format('ircname'):
getvalue('ircname')})

for group_filter in ['groups', 'skills']:
if group_filter in valid_filters:
Expand Down
34 changes: 21 additions & 13 deletions mozillians/users/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

from mozillians.api.tests import APIAppFactory
from mozillians.common.tests import TestCase
from mozillians.geo.tests import CityFactory, CountryFactory, RegionFactory
from mozillians.groups.tests import GroupFactory, SkillFactory
from mozillians.users.models import ExternalAccount
from mozillians.users.tests import UserFactory
Expand All @@ -19,7 +20,10 @@
class UserResourceTests(TestCase):
def setUp(self):
voucher = UserFactory.create()
self.user = UserFactory.create(vouched=False)
country = CountryFactory()
self.user = UserFactory.create(
userprofile={'vouched': False,
'geo_country': country})
self.user.userprofile.vouch(voucher.userprofile)
group = GroupFactory.create()
group.add_member(self.user.userprofile)
Expand Down Expand Up @@ -77,7 +81,7 @@ def test_get_detail_mozilla_app(self):
eq_(data['bio'], profile.bio)
eq_(data['photo'], profile.photo)
eq_(data['ircname'], profile.ircname)
eq_(data['country'], profile.country)
eq_(data['country'], profile.geo_country.code)
eq_(data['region'], profile.region)
eq_(data['city'], profile.city)
eq_(data['date_mozillian'], profile.date_mozillian)
Expand Down Expand Up @@ -201,21 +205,22 @@ def test_search_groups(self):
eq_(data['objects'][0]['id'], user_1.userprofile.id)

def test_search_combined_skills_country(self):
country = 'fr'
user_1 = UserFactory.create(userprofile={'country': country})
UserFactory.create(userprofile={'country': country})
country = CountryFactory.create(code='fr')
user_1 = UserFactory.create(userprofile={'geo_country': country})
UserFactory.create(userprofile={'geo_country': country})
skill = SkillFactory.create()
user_1.userprofile.skills.add(skill)
client = Client()
url = urlparams(self.mozilla_resource_url,
skills=skill.name, country=country)
skills=skill.name, country=country.code)
response = client.get(url, follow=True)
data = json.loads(response.content)
eq_(len(data['objects']), 1)
eq_(data['objects'][0]['id'], user_1.userprofile.id)

def test_query_with_space(self):
user = UserFactory.create(userprofile={'city': 'Mountain View'})
city = CityFactory.create(name='Mountain View')
user = UserFactory.create(userprofile={'geo_city': city})
client = Client()
url = urlparams(self.mozilla_resource_url, city='mountain view')
request = client.get(url, follow=True)
Expand Down Expand Up @@ -243,29 +248,32 @@ def test_search_username(self):
eq_(data['objects'][0]['id'], user.userprofile.id)

def test_search_country(self):
user = UserFactory.create(userprofile={'country': 'fr'})
country = CountryFactory.create(code='fr')
user = UserFactory.create(userprofile={'geo_country': country})
url = urlparams(self.mozilla_resource_url,
country=user.userprofile.country)
country=user.userprofile.geo_country.code)
client = Client()
response = client.get(url, follow=True)
data = json.loads(response.content)
eq_(len(data['objects']), 1)
eq_(data['objects'][0]['id'], user.userprofile.id)

def test_search_region(self):
user = UserFactory.create(userprofile={'region': 'la lo'})
region = RegionFactory.create(name='la lo')
user = UserFactory.create(userprofile={'geo_region': region})
url = urlparams(self.mozilla_resource_url,
region=user.userprofile.region)
region=user.userprofile.geo_region.name)
client = Client()
response = client.get(url, follow=True)
data = json.loads(response.content)
eq_(len(data['objects']), 1)
eq_(data['objects'][0]['id'], user.userprofile.id)

def test_search_city(self):
user = UserFactory.create(userprofile={'city': u'αθήνα'})
city = CityFactory.create(name=u'αθήνα')
user = UserFactory.create(userprofile={'geo_city': city})
url = urlparams(self.mozilla_resource_url,
city=user.userprofile.city)
city=user.userprofile.geo_city.name)
client = Client()
response = client.get(url, follow=True)
data = json.loads(response.content)
Expand Down

0 comments on commit 499f62e

Please sign in to comment.