def register(request): if request.method == 'POST': user_name = request.POST.get('username', '') pass_word_1 = request.POST.get('password_1', '') pass_word_2 = request.POST.get('password_2', '') nick_name = request.POST.get('nickname', '') email = request.POST.get('email', '') avatar = request.FILES.get('avatar') if User.objects.filter(username=user_name): return render(request, 'register.html', {'error': '用户已存在'}) #将表单写入数据库 if (pass_word_1 != pass_word_2): return render(request, 'register.html', {'error': '两次密码请输入一致'}) user = User() if avatar: user.avatar = 'media/' + user_name + '.png' img = Image.open(avatar) size = img.size print(size) # 因为是要圆形,所以需要正方形的图片 r2 = min(size[0], size[1]) if size[0] != size[1]: img = img.resize((r2, r2), Image.ANTIALIAS) # 最后生成圆的半径 r3 = int(r2 / 2) img_circle = Image.new('RGBA', (r3 * 2, r3 * 2), (255, 255, 255, 0)) pima = img.load() # 像素的访问对象 pimb = img_circle.load() r = float(r2 / 2) # 圆心横坐标 for i in range(r2): for j in range(r2): lx = abs(i - r) # 到圆心距离的横坐标 ly = abs(j - r) # 到圆心距离的纵坐标 l = (pow(lx, 2) + pow(ly, 2))**0.5 # 三角函数 半径 if l < r3: pimb[i - (r - r3), j - (r - r3)] = pima[i, j] img_circle.save('blog/static/media/' + user_name + '.png') user.username = user_name user.password = pass_word_1 user.email = email user.nickname = nick_name user.save() #返回注册成功页面 return render(request, 'index_unlog.html') else: return render(request, 'register.html')
def register(request): # pass if request.method == "POST": user_name = request.POST.get('username', '') pass_word_1 = request.POST.get('password_1', '') pass_word_2 = request.POST.get('password_2', '') nick_name = request.POST.get('nickname', '') email = request.POST.get('email', '') avatar = request.FILES.get('avatar') if User.objects.filter(username=user_name): return render(request, 'blog/register.html', {'error': '用户已存在'}) if (pass_word_1 != pass_word_2): return render(request, 'blog/register.html', {'error': '两次密码输入不一致'}) user = User() # 将头像设置为圆形 if avatar: user.avatar = 'media/' + user_name + '.png' img = Image.open(avatar) size = img.size r2 = min(size[0], size(1)) if size[0] != size[1]: img = img.resize((r2, r2), Image.ANTIALIAS) r3 = int(r2 / 2) img_circle = Image.new('RGBA', (r3 * 2, r3 * 2), (255, 255, 255, 0)) pima = img.load() pimb = img_circle.load() r = float(r2 / 2) for i in range(r2): for j in range(r2): lx = abs(i - r) ly = abs(j - r) l = (pow(lx, 2) + pow(ly, 2)) ** 0.5 if l < r3: pimb[i - (r - r3), j - (r - r3)] = pima[i, j] img_circle.save('static/media/' + user_name + '.png') user.username = user_name user.password = pass_word_1 user.email = email user.nickname = nick_name user.save() return render(request, 'blog/index_unlogin.html') else: return render(request, 'blog/register.html')
def test_avatar(self): u = User(username='******', email='*****@*****.**') self.assertEqual(u.avatar(128), ('https://www.gravatar.com/avatar/' 'd4c74594d841139328695756648b6bd6' '?d=identicon&s=128'))
def test_avatar(self): u = User(nickname='john', email='*****@*****.**') avatar = u.avatar(128) expected = 'http://www.gravatar.com/avatar/d4c74594d841139328695756648b6bd6' assert avatar[0:len(expected)] == expected
def test_avatar(self): u = User(nicename='john', email='*****@*****.**') avatar = u.avatar(128) expected = 'http://www.gravatar.com/avatar/d4c74594d841139328695756648b6bd6' assert avatar[0:len(expected)] == expected
class TestUserModelNoDb(unittest.TestCase): def setUp(self): self.u = User("Test", "*****@*****.**", "password") def test_avatar(self): avatar = self.u.avatar(1) expected = \ "http://www.gravatar.com/avatar/55502f40dc8b7c769880b10874abc9d0" assert avatar.startswith(expected) def test_avatar_size(self): avatar = self.u.avatar(5) expected = \ "55502f40dc8b7c769880b10874abc9d0?d=mm&s=5" assert avatar.endswith(expected) def test_avatar_largersize(self): avatar = self.u.avatar(10) expected = \ "55502f40dc8b7c769880b10874abc9d0?d=mm&s=10" assert avatar.endswith(expected) def test_created_at(self): expected = datetime.utcnow() time = self.u.created_at assert (time - expected).total_seconds() < 1 def test_no_posts(self): expected = 0 number_posts = self.u.posts.count() assert number_posts == expected def test_password_algorithm(self): expected = "sha1" algorithm = self.u.password[:4] assert algorithm == expected def test_password_length(self): expected = 62 length = len(self.u.password) assert length == expected def test_active(self): expected = True active = self.u.is_active() assert active == expected def test_anonymous(self): expected = False anonymous = self.u.is_anonymous() assert anonymous == expected def test_authenticated(self): expected = True authenticated = self.u.is_authenticated() assert authenticated == expected def test_repr(self): expected = r"<User 'No id set': 'Test'>" expected_type = str representation = repr(self.u) assert expected == representation assert type(representation) == expected_type def test_unicode(self): expected = u"Test" expected_type = unicode unicode_repr = unicode(self.u) assert unicode_repr == expected assert type(unicode_repr) == expected_type