def validate(self, attrs): username = attrs.get("username") password = attrs.get("password") if not username: raise CustomException( code=10, message=self.error_messages['invalid_username']) if '@' in username: kwargs = {'email': username} else: kwargs = {'username': username} try: self.user = get_user_model().objects.get(**kwargs) if self.user.check_password(password): if self.user.is_active: attrs['user'] = self.user return attrs else: raise CustomException( code=12, message=self.error_messages['inactive_account']) except User.DoesNotExist: pass raise CustomException( code=11, message=self.error_messages['invalid_credentials'])
def validate(self, attrs): agency_id = attrs.get("agency_id") agency_name = attrs.get("agency_name") admin_id = attrs.get("admin_id") api_username = attrs.get("api_username") api_password = attrs.get("api_password") if not agency_id: raise CustomException( code=10, message=self.error_messages['invalid_agency_id']) if not agency_name: raise CustomException(code=11, message=self.error_messages['invalid_name']) if not api_username: raise CustomException( code=12, message=self.error_messages['invalid_api_username']) if not api_password: raise CustomException( code=13, message=self.error_messages['invalid_api_password']) try: agency = Agency.objects.get(id=agency_id) attrs['agency'] = agency return attrs except ObjectDoesNotExist: raise CustomException( code=14, message=self.error_messages['invalid_data_source_id'])
def validate(self, attrs): token = attrs.get("token") password = attrs.get("password") if not token or not token.isdigit(): raise CustomException(code=10, message=self.error_messages['invalid_token']) if not password or len(password) < 6: raise CustomException( code=11, message=self.error_messages['invalid_password']) try: user = User.objects.get(password_reset_token=int(token)) if user.password_reset_sent_at.replace( tzinfo=None) < datetime.now() - timedelta(minutes=10): raise CustomException( code=12, message=self.error_messages['token_expired']) if not user.is_active: raise CustomException( code=12, message=self.error_messages['inactive_account']) attrs['password'] = make_password(attrs['password']) return attrs except User.DoesNotExist: pass raise CustomException(code=10, message=self.error_messages['invalid_token'])
def validate(self, attrs): subscription = attrs.get("subscription") service = attrs.get("service") servers = attrs.get("servers") subscription_id = 0 if not subscription: raise CustomException( code=10, message=self.error_messages['invalid_subscription']) if not service: raise CustomException( code=11, message=self.error_messages['invalid_service']) try: if Subscription.objects.filter(subscription=subscription).exists(): subscription_id = Subscription.objects.filter( subscription=subscription).values_list('id', flat=True)[0] subscription = Subscription.objects.get(id=subscription_id) attrs['subscription'] = subscription else: raise CustomException( code=14, message=self.error_messages['invalid_subscription']) except ObjectDoesNotExist: raise CustomException( code=14, message=self.error_messages['invalid_subscription']) return attrs
def validate(self, attrs): email = attrs.get("email") username = attrs.get("username") name = attrs.get("name") password = attrs.get("password") email_re = '^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$' address_name = attrs.get("address_name") address = attrs.get("address") city = attrs.get("city") state = attrs.get("state") Zip = attrs.get("Zip") if not email or not bool(re.match( email_re, email)) or User.objects.filter(email=email).exists(): raise CustomException(code=11, message=self.error_messages['invalid_email']) if username: if not bool(re.match('^[a-zA-Z0-9]+$', username)) or len(username) > 40: raise CustomException( code=12, message=self.error_messages['invalid_username']) elif User.objects.filter(username=username).exists(): raise CustomException( code=15, message=self.error_messages['duplicate_username']) if not name: raise CustomException(code=13, message=self.error_messages['invalid_name']) if not password or len(password) < 6: raise CustomException( code=14, message=self.error_messages['invalid_password']) if not address_name: raise CustomException( code=16, message=self.error_messages['invalid_address_name']) if not address: raise CustomException( code=17, message=self.error_messages['invalid_address']) if not city: raise CustomException(code=18, message=self.error_messages['invalid_city']) if not state: raise CustomException(code=19, message=self.error_messages['invalid_state']) if not Zip: raise CustomException(code=20, message=self.error_messages['invalid_Zip']) attrs['password'] = make_password(attrs['password']) customer = stripe.Customer.create( email=email, name=name, ) attrs['customer_id'] = customer.id return attrs
def validate(self, attrs): name = attrs.get("team_name") is_booking = attrs.get("is_booking") admin_id = attrs.get("admin_id") if not name: raise CustomException(code=10, message=self.error_messages['invalid_name']) if is_booking is None: raise CustomException( code=12, message=self.error_messages['invalid_booking']) return attrs
def validate(self, attrs): followed_id = attrs.get("followed_id") if not followed_id: raise CustomException( code=10, message=self.error_messages['invalid_followed']) try: attrs['user_followed'] = User.objects.get(id=followed_id) return attrs except ObjectDoesNotExist: raise CustomException( code=10, message=self.error_messages['invalid_followed'])
def validate(self, attrs): user_id = attrs.get("user_id") if not user_id: raise CustomException(code=10, message=self.error_messages['empty_user_id']) try: attrs['user'] = User.objects.get(id=user_id) return attrs except ObjectDoesNotExist: raise CustomException(code=10, message=self.error_messages['invalid_user'])
def validate(self, attrs): product_id = attrs.get("product_id") if not product_id: raise CustomException( code=10, message=self.error_messages['invalid_product']) try: attrs['report'] = Product.objects.get(id=product_id) return attrs except ObjectDoesNotExist: raise CustomException( code=10, message=self.error_messages['invalid_product'])
def validate(self, attrs): team_id = attrs.get("team_id") if not team_id: raise CustomException(code=10, message=self.error_messages['empty_team_id']) try: attrs['team'] = Team.objects.get(id=team_id) return attrs except ObjectDoesNotExist: raise CustomException(code=11, message=self.error_messages['invalid_team'])
def validate(self, attrs): agency_id = attrs.get("agency_id") if not agency_id: raise CustomException( code=10, message=self.error_messages['empty_agency_id']) try: attrs['agency'] = Agency.objects.get(id=agency_id) return attrs except ObjectDoesNotExist: raise CustomException( code=11, message=self.error_messages['invalid_agency'])
def validate(self, attrs): is_active = attrs.get("is_active") password = attrs.get("password") if is_active is None: raise CustomException( code=11, message=self.error_messages['invalid_is_active']) if not password: raise CustomException( code=12, message=self.error_messages['invalid_password']) attrs['password'] = make_password(attrs['password']) return attrs
def set_topic(self, topic): if topic.topicid in self._generations: self._cur_generation = self._generations[topic.topicid] self._cur_generation.set_topic(topic) else: raise CustomException('genertion of topicid "%s" not exist.' % topic.topicid)
def validate(self, attrs): name = attrs.get("name") currency = attrs.get("currency") date_type = attrs.get("date_type") if not name: raise CustomException(code=10, message=self.error_messages['invalid_name']) if not currency: raise CustomException( code=11, message=self.error_messages['invalid_currency']) if not date_type: raise CustomException( code=12, message=self.error_messages['invalid_date_type']) return attrs
def validate(self, attrs): username = attrs.get("username") if not username: raise CustomException( code=10, message=self.error_messages['invalid_username']) return attrs
def validate(self, attrs): agency_name = attrs.get("agency_name") admin_id = attrs.get("admin_id") api_username = attrs.get("api_username") api_password = attrs.get("api_password") if not agency_name: raise CustomException(code=10, message=self.error_messages['invalid_name']) if not api_username: raise CustomException( code=11, message=self.error_messages['invalid_api_username']) if not api_password: raise CustomException( code=12, message=self.error_messages['invalid_api_password']) return attrs
def validate(self, attrs): assets = attrs.get("assets") seller_id = attrs.get("seller_id") price = attrs.get("price") currency = attrs.get("currency") title = attrs.get("title") description = attrs.get("description") if not assets: raise CustomException( code=15, message=self.error_messages['invalid_assets']) if not seller_id: raise CustomException(code=10, message=self.error_messages['invalid_user']) if not (price and price.replace('.', '', 1).isdigit()): raise CustomException(code=11, message=self.error_messages['invalid_price']) if not currency: raise CustomException( code=12, message=self.error_messages['invalid_currency']) if not title: raise CustomException(code=13, message=self.error_messages['invalid_title']) if not description: raise CustomException( code=14, message=self.error_messages['invalid_description']) try: seller = User.objects.get(id=seller_id) attrs['seller'] = seller return attrs except ObjectDoesNotExist: raise CustomException(code=10, message=self.error_messages['invalid_user'])
def validate(self, attrs): subscription = attrs.get("subscription") service = attrs.get("service") servers = attrs.get("servers") print(servers) if not subscription: raise CustomException(code=10, message=self.error_messages['invalid_subscription']) if not service: raise CustomException(code=11, message=self.error_messages['invalid_service']) try: subscription = Subscription.objects.get(subscription=subscription) attrs['subscription'] = subscription except ObjectDoesNotExist: raise CustomException(code=14, message=self.error_messages['invalid_subscription']) return attrs
def validate(self, attrs): user_id = attrs.get("user_id") username = attrs.get("username") name = attrs.get("name") if username: if not bool(re.match('^[a-zA-Z0-9]+$', username)) or len(username) > 40: raise CustomException( code=10, message=self.error_messages['invalid_username']) elif User.objects.filter(username=username).exclude( id=user_id).exists(): raise CustomException( code=12, message=self.error_messages['duplicate_username']) if not name: raise CustomException(code=11, message=self.error_messages['invalid_name']) return attrs
def validate(self, attrs): status = attrs.get("status") server_id = attrs.get("server_id") server_type = attrs.get("server_type") if not status: raise CustomException(code=10, message=self.error_messages['invalid_status']) if not server_id: raise CustomException(code=11, message=self.error_messages['invalid_server_id']) if not server_type: raise CustomException(code=12, message=self.error_messages['invalid_server_type']) try: server = Server.objects.get(id=server_id) server_type = ServerType.objects.get(name=server_type) attrs['server'] = server attrs['server_type'] = server_type return attrs except ObjectDoesNotExist: raise CustomException(code=14, message=self.error_messages['invalid_server_id'])
def validate(self, attrs): current_password = attrs.get("current_password") new_password = attrs.get("new_password") user_id = attrs.get("user_id") if not current_password: raise CustomException( code=10, message=self.error_messages['invalid_current_password']) if not new_password: raise CustomException( code=11, message=self.error_messages['invalid_password']) user = User.objects.get(pk=user_id) if user.check_password(current_password): attrs['new_password'] = make_password(attrs['new_password']) return attrs else: raise CustomException( code=10, message=self.error_messages['invalid_current_password'])
def validate(self, attrs): team_id = attrs.get("team_id") team_name = attrs.get("team_name") is_booking = attrs.get("is_booking") if team_id is None: raise CustomException( code=10, message=self.error_messages['invalid_team_id']) if team_name is None: raise CustomException( code=11, message=self.error_messages['invalid_team_name']) if is_booking is None: raise CustomException( code=12, message=self.error_messages['invalid_is_booking']) try: attrs['team'] = Team.objects.get(id=team_id) return attrs except ObjectDoesNotExist: raise CustomException( code=14, message=self.error_messages['invalid_request'])
def validate(self, attrs): user_id = attrs.get("user_id") is_active = attrs.get("is_active") first_name = attrs.get("first_name") last_name = attrs.get("last_name") email = attrs.get("email") if user_id is None: raise CustomException( code=10, message=self.error_messages['invalid_user_id']) if is_active is None: raise CustomException( code=12, message=self.error_messages['invalid_is_active']) if first_name is None: raise CustomException( code=13, message=self.error_messages['invalid_first_name']) if last_name is None: raise CustomException( code=13, message=self.error_messages['invalid_last_name']) if email is None: raise CustomException(code=14, message=self.error_messages['invalid_email']) try: attrs['user'] = User.objects.get(id=user_id) return attrs except ObjectDoesNotExist: raise CustomException( code=15, message=self.error_messages['invalid_user_id'])
def validate(self, attrs): email = attrs.get("email") username = attrs.get("username") first_name = attrs.get("first_name") last_name = attrs.get("last_name") password = attrs.get("password") email_re = '^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$' if not email or not bool(re.match( email_re, email)) or User.objects.filter(email=email).exists(): raise CustomException(code=11, message=self.error_messages['invalid_email']) if username: if not bool(re.match('^[a-zA-Z0-9]+$', username)) or len(username) > 40: raise CustomException( code=12, message=self.error_messages['invalid_username']) elif User.objects.filter(username=username).exists(): raise CustomException( code=13, message=self.error_messages['invalid_username']) if not first_name: raise CustomException( code=14, message=self.error_messages['invalid_first_name']) if not last_name: raise CustomException( code=15, message=self.error_messages['invalid_last_name']) if not password or len(password) < 6: raise CustomException( code=16, message=self.error_messages['invalid_password']) attrs['password'] = make_password(attrs['password']) return attrs
def validate(self, attrs): email = attrs.get("email") first_name = attrs.get("first_name") last_name = attrs.get("last_name") team_id = attrs.get("team_id") agency_id = attrs.get("agency_id") password = attrs.get("password") if not email: raise CustomException(code=10, message=self.error_messages['invalid_email']) if not first_name: raise CustomException( code=12, message=self.error_messages['invalid_first_name']) if not last_name: raise CustomException( code=13, message=self.error_messages['invalid_last_name']) if not password: raise CustomException( code=14, message=self.error_messages['invalid_password']) attrs['password'] = make_password(attrs['password']) return attrs
def validate(self, attrs): address_name = attrs.get("address_name") address = attrs.get("address") city = attrs.get("city") state = attrs.get("state") Zip = attrs.get("Zip") if not address_name: raise CustomException( code=10, message=self.error_messages['invalid_address_name']) if not address: raise CustomException( code=10, message=self.error_messages['invalid_address']) if not city: raise CustomException(code=11, message=self.error_messages['invalid_city']) if not state: raise CustomException(code=12, message=self.error_messages['invalid_state']) if not Zip: raise CustomException(code=13, message=self.error_messages['invalid_Zip']) return attrs
def validate(self, attrs): access_token = attrs.get("access_token") provider = attrs.get("provider") if not access_token: raise CustomException(code=10, message=self.error_messages['invalid_token']) if provider not in ["google", "facebook"]: raise CustomException( code=11, message=self.error_messages['invalid_provider']) if provider == "google": payload = {'id_token': access_token} # validate the token r = requests.get('https://oauth2.googleapis.com/tokeninfo', params=payload) data = json.loads(r.text) if 'error' in data: raise CustomException( code=12, message=self.error_messages['wrong_token']) attrs['user_info'] = data return attrs
def validate(self, attrs): purchase_id = attrs.get("purchase_id") product_id = attrs.get("product_id") msg = attrs.get("msg") if not purchase_id: raise CustomException( code=10, message=self.error_messages['invalid_purchase']) if not product_id: raise CustomException( code=10, message=self.error_messages['invalid_product']) if not msg: raise CustomException(code=11, message=self.error_messages['Empty_msg']) try: attrs['product'] = Product.objects.get(id=product_id) attrs['reviewable'] = Purchases.objects.get(id=purchase_id, product=product_id, state='1') return attrs except ObjectDoesNotExist: raise CustomException( code=12, message=self.error_messages['invalid_reviewable'])
def validate(self, attrs): first_name = attrs.get("first_name") last_name = attrs.get("last_name") email_address = attrs.get("email_address") currency = attrs.get("currency") date_type = attrs.get("date_type") if not first_name: raise CustomException( code=10, message=self.error_messages['invalid_first_name']) if not last_name: raise CustomException( code=10, message=self.error_messages['invalid_last_name']) if not email_address: raise CustomException( code=12, message=self.error_messages['invalid_email_address']) if not currency: raise CustomException( code=13, message=self.error_messages['invalid_currency']) if not date_type: raise CustomException( code=14, message=self.error_messages['invalid_date_type']) return attrs
def load(self, frm): """ 从字符串中实例化domain、intent、slots参数 :param frm: :return: """ if isinstance(frm, str): semantic = json.loads(frm) elif isinstance(frm, dict): semantic = frm else: raise CustomException('%s is invalid.' % frm) # if DOMAIN in semantic: # self.domain = semantic[DOMAIN] # if INTENT in semantic: # self.intent = semantic[INTENT] if SLOTS in semantic: for arg, val in semantic[SLOTS].items(): self.slots[arg] = val