def post(self, request, format=None): data = request.data user = None try: try: user = User.objects.get(e_mail=data.get('email_or_username')) except: user = User.objects.get(username=data.get('email_or_username')) password = data.get('password') for i in range(2): password = hashlib.md5( password.encode('utf-8') + user.salt.encode('utf-8')).hexdigest() if password == user.password: random_list = 'abdjcniejknmdkjdj' ch = '' for i in range(10): ch += (list(random_list)[random.randint(0, 16)]) token = hashlib.md5( user.e_mail.encode('utf-8') + ch.encode('utf-8')).hexdigest() uid = user.uid UserToken.objects.update_or_create(owner=user, defaults={'token': token}) context = {'msg': 'Succeeded', 'Token': token, 'id': uid} return JsonResponse(context) except: return Response({'msg': 'ERROR Incorrect username or password'})
def post(self, request, format=None): data = request.data try: if User.objects.get(e_mail=data.get('email_or_username')): user = User.objects.get(e_mail=data.get('email_or_username')) else: user = User.objects.get(username=data.get('email_or_username')) for i in range(2): password = hashlib.md5( data.get('password').encode('utf-8') + user.salt.encode('utf-8')).hexdigest() if password == user.password: random_list = 'abdjcniejknmdkjdj' for i in range(10): ch += (list(random_list)[random.randint(0, 16)]) token = hashlib.md5( user.e_mail.encode('utf-8') + ch.encode('utf-8')).hexdigest() UserToken.objects.update_or_create(user=user, defaults={'token': token}) context = {'msg': 'Succeeded', 'Token': token} return JsonResponse(context) return Response({'msg': 'ERROR Incorrect username or password'}, status=status.HTTP_400_BAD_REQUEST) except: return Response({'msg': 'The user does not exist'}, status=status.HTTP_400_BAD_REQUEST)
def save(self,*args,**kwargs): ch = 'zbcdefghijklmnopqrstuvwxyz' salt = '' for i in range(10): salt += (list(ch)[random.randint(0,25)]) self.salt = salt passwd = self.password confirm_passwd = self.password passwd_encryption = hashlib.md5(passwd.encode('utf-8')).hexdigest() confirm_passwd_encryption = hashlib.md5(confirm_passwd.encode('utf-8')).hexdigest() self.password = hashlib.md5(passwd_encryption.encode('utf-8')+self.salt.encode('utf-8')).hexdigest() self.confirm_password = hashlib.md5(confirm_passwd_encryption.encode('utf-8')+self.salt.encode('utf-8')).hexdigest() super(User,self).save(*args,**kwargs)
def save(self,*args,**kwargs): ch = 'fhuendhsgjlsmlmnvjjsljlsmn' for i in range(10): self.salt += (list(ch)[random.randint(0,25)]) for i in range(2): self.password = hashlib.md5(self.password.encode('utf-8')+self.salt.encode('utf-8')).hexdigest() super(User,self).save(*args,**kwargs)
def post(self,request,format=None): ret = {'msg':None,'token':None} data = request.data username = data.get('username') password = data.get('password') try: user = User.objects.get(username__exact=username) except: return Response('用户名或密码错误',status=status.HTTP_400_BAD_REQUEST) password_1 = hashlib.md5(password.encode('utf-8')).hexdigest() password = hashlib.md5(password_1.encode('utf-8')+user.salt.encode('utf-8')).hexdigest() if username == user.username and password == user.password: token = hashlib.md5(username.encode('utf-8')).hexdigest() ret['msg'] = 'Successed' ret['token'] = token return JsonResponse(ret) UserToken.objects.update_or_create(user=user,defaults={'token':token}) #serializer = UserSerializer(user) #return Response(serializer.data,status=status.HTTP_200_OK) return Response('用户名或密码错误',status=status.HTTP_400_BAD_REQUEST)
def gravatar_icon(self, size=30): # 小组用户搜索头像覆盖 if self.is_team: icon_dir = sjoin( settings.TEAM_ICON_DIR, # team_icon self.stu_code[:3], # N19 ) icon_folder = os.path.join(settings.MEDIA_ROOT, icon_dir) if os.path.isdir(icon_folder): for img in os.listdir(icon_folder): if img.startswith(self.stu_code[3]): return sjoin( settings.MEDIA_URL, icon_dir, img, ) email = self.stu_code + '@pku.edu.cn' hasher = hashlib.md5() hasher.update(email.encode('utf-8')) email_hash = hasher.hexdigest() return "//www.gravatar.com/avatar/%s?s=%s&d=retro" % (email_hash, size)
def gravatar_icon(self, size=30): email = self.stu_code + '@pku.edu.cn' hasher = hashlib.md5() hasher.update(email.encode('utf-8')) email_hash = hasher.hexdigest() return "//www.gravatar.com/avatar/%s?s=%s&d=retro" % (email_hash, size)