Add similar competence finding to Competence

This commit is contained in:
Gergely Polonkai 2015-01-06 15:47:34 +01:00 committed by Gergely Polonkai
parent d2e56db0d9
commit 7274a32233
4 changed files with 53 additions and 0 deletions

View File

@ -3,6 +3,8 @@ from django.contrib.auth.models import User
from django.utils import timezone from django.utils import timezone
from django.conf import settings from django.conf import settings
from fuzzywuzzy import fuzz
from .ducklevel import minutes_to_level from .ducklevel import minutes_to_level
class Species(models.Model): class Species(models.Model):
@ -31,6 +33,20 @@ class Competence(models.Model):
def __str__(self): def __str__(self):
return self.name return self.name
@staticmethod
def get_similar_comps(name):
comps = __class__.objects.values_list('name', flat = True)
ret = ()
for c in comps:
r = fuzz.ratio(name.lower(), c.lower())
# This ratio is subject to change
if r > settings.MIN_FUZZY_SIMILARITY:
ret = ret + (c,)
return ret
class Duck(models.Model): class Duck(models.Model):
"""Model to hold Duck data""" """Model to hold Duck data"""

View File

@ -13,6 +13,10 @@ class FrontTest(TestCase):
def setUp(self): def setUp(self):
self.client = Client() self.client = Client()
def test_index_page(self):
response = self.client.get('/')
self.assertEqual(response.status_code, 200)
def test_vocabulary_page(self): def test_vocabulary_page(self):
response = self.client.get('/vocabulary.html') response = self.client.get('/vocabulary.html')
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
@ -214,3 +218,34 @@ class TestListing(TestCase):
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.assertEqual(len(response.context['duck_list']), 1) self.assertEqual(len(response.context['duck_list']), 1)
class SimilarCompTest(TestCase):
def setUp(self):
admin = User(username = 'admin')
admin.save()
competence_list = (
'Creativity',
'Administration',
'Perl',
'Python',
'TCSH',
)
for c in competence_list:
comp = Competence(name = c, added_by = admin)
comp.save()
def test_good_similar_competences(self):
l = Competence.get_similar_comps('perl')
self.assertEquals(len(l), 1)
l = Competence.get_similar_comps('pzthon')
self.assertEquals(len(l), 1)
l = Competence.get_similar_comps('kreativitás')
self.assertEqual(len(l), 1)
def test_bad_similar_competence(self):
l = Competence.get_similar_comps('development')
self.assertEqual(len(l), 0)

View File

@ -87,3 +87,4 @@ USE_TZ = True
STATIC_URL = '/static/' STATIC_URL = '/static/'
MAX_DUCK_LEVEL = 5 MAX_DUCK_LEVEL = 5
MIN_FUZZY_SIMILARITY = 75

View File

@ -2,3 +2,4 @@ Django>=1.7,<1.8
WebTest==2.0.17 WebTest==2.0.17
django-webtest==1.7.7 django-webtest==1.7.7
django-js-reverse==0.3.3 django-js-reverse==0.3.3
fuzzywuzzy==0.4.0