def test_sluggify(self): """ test sluggify function """ str1 = 'g-e-nie' str2 = 'g e n i e' str3 = 'G E-ni e' str4 = 'gen' str5 = '1234567890123456' str6 = '123456789012345' self.assertEqual(Slug.objects.sluggify(str1), 'g_e_nie') self.assertEqual(Slug.objects.sluggify(str2), 'g_e_n_i_e') self.assertEqual(Slug.objects.sluggify(str3), 'g_e_ni_e') self.assertRegexpMatches(Slug.objects.sluggify(str4), 'gen[a-z0-9]{2}') #self.assertEqual(Slug.objects.sluggify(str4), 'geni_____') self.assertEqual(Slug.objects.sluggify(str5), '123456789012345') self.newslug = Slug(content_object=self.user1, slug="123456789012345") self.newslug.save() self.assertEqual(Slug.objects.sluggify(str6), '123456789012341')
def handle_noargs(self, *args, **options): self.stdout.write("This script will clean the Slug model and rebuild it.\n") go = raw_input("Are you sure you want to do this? [y/N]: ") if go not in 'yY': self.stdout.write("Smart move.\n") exit() Slug.objects.all().delete() for user in User.objects.all(): print user.username if Slug.objects.filter(object_id=user.pk).count() == 0: slug_str = Slug.objects.sluggify(user.username) new_slug = Slug(slug=slug_str, content_object=user) new_slug.save() profile = user.get_profile() from user_profiles.models import UserProfile UserProfile.objects.filter(id=profile.id).update(slug=new_slug) for group in Group.objects.all(): print group.name if Slug.objects.filter(object_id=group.pk).count() == 0: if group.is_mutual_group: loop_count = 0 for member_id in group.members: if loop_count == 0: user1 = User.objects.get(id=member_id) elif loop_count == 1: user2 = User.objects.get(id=member_id) loop_count = loop_count + 1 print user1.username print user2.username if user1.username[0] > user2.username[0]: slug_str = "%s-%s" % (user2.username, user1.username) else: slug_str = "%s-%s" % (user1.username, user2.username) else: slug_str = Slug.objects.sluggify(group.name) new_slug = Slug(slug=slug_str, content_object=group) new_slug.save() group.slug = slug_str group.save()
def setUp(self): self.user1 = User.objects.create(username="******") self.user2 = User.objects.create(username="******") self.exist_slug = Slug(content_object=self.user1, slug="test_slug") self.exist_slug.save()
class Test_Meta(MongoTestCase): def setUp(self): self.user1 = User.objects.create(username="******") self.user2 = User.objects.create(username="******") self.exist_slug = Slug(content_object=self.user1, slug="test_slug") self.exist_slug.save() def test_validate(self): """ test validate """ str1 = 'g-e-nie' str2 = '12345' str3 = '123456' str4 = '123' str5 = '123456789012345' str6 = '1234567890123456' str7 = 'admin' str8 = 'test_slug' self.assertTrue(not Slug.objects.validate(str1)) self.assertTrue(Slug.objects.validate(str2)) self.assertTrue(Slug.objects.validate(str3)) self.assertTrue(not Slug.objects.validate(str4)) self.assertTrue(Slug.objects.validate(str5)) self.assertTrue(not Slug.objects.validate(str6)) self.assertTrue(not Slug.objects.validate(str7)) self.assertTrue(not Slug.objects.validate(str8)) def test_sluggify(self): """ test sluggify function """ str1 = 'g-e-nie' str2 = 'g e n i e' str3 = 'G E-ni e' str4 = 'gen' str5 = '1234567890123456' str6 = '123456789012345' self.assertEqual(Slug.objects.sluggify(str1), 'g_e_nie') self.assertEqual(Slug.objects.sluggify(str2), 'g_e_n_i_e') self.assertEqual(Slug.objects.sluggify(str3), 'g_e_ni_e') self.assertRegexpMatches(Slug.objects.sluggify(str4), 'gen[a-z0-9]{2}') #self.assertEqual(Slug.objects.sluggify(str4), 'geni_____') self.assertEqual(Slug.objects.sluggify(str5), '123456789012345') self.newslug = Slug(content_object=self.user1, slug="123456789012345") self.newslug.save() self.assertEqual(Slug.objects.sluggify(str6), '123456789012341') def test_change_slug_name(self): """ test change slug name function """ success = self.exist_slug.change_slug_name('admin') self.assertTrue(not success) self.assertEqual(self.exist_slug.slug, 'test_slug') success = self.exist_slug.change_slug_name('genie1') self.assertTrue(success) self.assertEqual(self.exist_slug.slug, 'genie1') success = self.exist_slug.change_slug_name('genie2') self.assertTrue(success) self.assertEqual(self.exist_slug.slug, 'genie2') success = self.exist_slug.change_slug_name('genie3') self.assertTrue(success) self.assertEqual(self.exist_slug.slug, 'genie3') success = self.exist_slug.change_slug_name('genie4') self.assertTrue(not success) self.assertEqual(self.exist_slug.slug, 'genie3')
def create_profile(sender, **kwargs): user = kwargs["instance"] if kwargs["created"] == True: new_slug = Slug(slug=user.username, content_object=user) new_slug.save() up = UserProfile.objects.create(user=user, slug=new_slug)