Exemplo n.º 1
0
 def post(self, request):
     serializer = AccountVerifySerializer(data=request.data)
     if not serializer.is_valid():
         return BaseResponse.send_response(False, error_code.MISSING_REQUIRED_FIELD,
                                           error_message.MISSING_REQUIRED_FIELD, self.data_response,
                                           status.HTTP_200_OK)
     data = BaseRefiner.clean_data(serializer.data)
     token_all = AccountToken.objects.filter(valid_flg=True, type__exact=AccountToken.VERIFY_TOKEN)
     if token_all:
         for token in token_all:
             if token.token == data['token']:
                 try:
                     account = Account.objects.get(pk=token.account_id)
                     account.active_flg = 1
                     account.verified_flg = True
                     account.updated_date = datetime.now()
                     access_token = TokenManager.generate_token(account, AccountToken.ACCESS_TOKEN)
                     account.save()
                     token.delete()
                     self.data_response = AccountDetailSerializer(account).data
                     self.data_response['id'] = account.id
                     self.data_response['token'] = access_token.token
                     self.data_response['expired'] = access_token.expired_date
                     return BaseResponse.send_response(True, error_code.SUCCESSFUL, error_message.SUCCESSFUL,
                                                       self.data_response,
                                                       status.HTTP_200_OK)
                 except:
                     return BaseResponse.send_response(False, error_code.SERVER_ERROR, error_message.SERVER_ERROR,
                                                       self.data_response, status.HTTP_200_OK)
             else:
                 pass
     else:
         return BaseResponse.send_response(False, error_code.TOKEN_EMPTY_OR_INVALID,
                                           error_message.TOKEN_EMPTY_OR_INVALID, self.data_response,
                                           status.HTTP_200_OK)
Exemplo n.º 2
0
 def post(self, request):
     serializer = AccountRegSerializer(data=request.data)
     if not serializer.is_valid():
         return BaseResponse.send_response(False, error_code.MISSING_REQUIRED_FIELD,
                                           error_message.MISSING_REQUIRED_FIELD, self.data_response,
                                           status.HTTP_200_OK)
     data = BaseRefiner.clean_data(serializer.data)
     accounts = Account.objects.filter(email=data['email'])
     if accounts:
         return BaseResponse.send_response(False, error_code.EMAIL_EXISTED, error_message.EMAIL_EXISTED,
                                           self.data_response, status.HTTP_200_OK)
     if not len(data['password']) == 32:
         return BaseResponse.send_response(False, error_code.PASSWORD_INVALID, error_message.PASSWORD_INVALID,
                                           self.data_response, status.HTTP_200_OK)
     if any(i.isdigit() for i in data['first_name']) or any(i.isdigit() for i in data['last_name']):
         return BaseResponse.send_response(False, error_code.NAME_FORMAT, error_message.NAME_FORMAT,
                                           self.data_response, status.HTTP_200_OK)
     try:
         validate_email(data['email'])
     except:
         return BaseResponse.send_response(False, error_code.EMAIL_INVALID, error_message.EMAIL_INVALID,
                                           self.data_response, status.HTTP_200_OK)
     account = Account.objects.create(email=data['email'])
     account.password = hashlib.md5(data['password']).hexdigest()
     account.first_name = data['first_name']
     account.last_name = data['last_name']
     account.created_date = datetime.now()
     account.updated_date = datetime.now()
     account.verified_flg = False
     account.active_flg = 0
     account.save()
     token_verify = TokenManager.generate_token(account, AccountToken.VERIFY_TOKEN)
     # TODO: implement send verification mail
     print token_verify
     # end TODO
     self.data_response = {
         'id': account.id,
         'email': account.email,
         'first_name': account.first_name,
         'last_name': account.last_name
     }
     return BaseResponse.send_response(True, error_code.SUCCESSFUL, error_message.SUCCESSFUL, self.data_response,
                                       status.HTTP_200_OK)