def validate_partial_update_set_unset(source_dict, source_schema): """ DO PARTIAL VALIDATION OF Update Keys in $set and $unset Oprators """ for key, value in source_dict.items(): key_split = key.split(".") check_key = key_split[0] if value is None or not value: if check_key in (source_schema.get("required") or []): raise CustomException("%s is a required field" % key) continue if len(key_split) > 1: if source_schema.get("properties").get(check_key).get("type") in \ ["object", "array", "list"]: continue raise CustomException("%s INVALID TYPE" % key) try: validate_schema( value, source_schema.get("properties", {}).get(key) or INTERNAL_FIELDS.get(key)) except Exception as e: raise CustomException(str(e), Codes.INVALID_PARAM)
def create(self, first_name, email, last_name='', mobile='', **kwargs): """ Contact Create: first_name :string email :string """ data = { "first_name": first_name, "last_name": last_name, "email": email, "mobile": mobile, } _fields = self._get_detail_fields() _contact = Contact.get({'email': email}, fields=_fields) if _contact: raise CustomException('Sorry, it looks like {} belongs to an existing contact.'.format(email) + "Please try new email", Codes.CONFLICT) try: contact = Contact(data) contact_id = contact.insert() except Exception as e: raise CustomException(str(e), Codes.INVALID_PARAM) contact_data = Contact.get({'_id': contact_id}) return rjsonify(contact_data.get_data())
def delete_id(self, _id, **kwargs): """ Delete record """ contact = Contact.get({'_id': _id}, fields=['first_name', 'email']) if contact: contact.delete() try: contact.delete() except Exception as e: raise CustomException("Error while deleting Contact : {}".format(e)) else: raise CustomException("_id not found", Codes.NOT_FOUND) return rjsonify({'_id': _id, 'is_deleted': True})
def decorated_function(*args, **kwargs): for param in params: if param not in kwargs: raise CustomException( '{} is a required parameter'.format(param), Codes.NULL_PARAM) return f(*args, **kwargs)
def is_valid_field(schema_name, field, hidden_fields=True): field = field.split('.')[0] if field not in schemas[schema_name]['schema']['properties'].keys(): raise CustomException('Invalid field', Codes.INVALID_PARAM) if hidden_fields: is_hidden_field(schema_name, field) return schemas[schema_name]['schema']['properties'][field]['type']
def is_required_field(schema_name, field): field = field.split('.')[0] if field in schemas[schema_name]['schema']['required']: raise CustomException("Unable to Delete,field Is Required", Codes.INVALID_PARAM) is_valid_field(schema_name, field) return schemas[schema_name]['schema']['properties'][field]['type']
def create(self, first_name, email, password, last_name='', **kwargs): """ Registers new user :param first_name: :param email: :param password: :param last_name: :param kwargs: :return: Required : email, name, password """ _fields = self._get_detail_fields() with suppress(Exception): email = email.lower() _user = User.get({'email': email}, fields=['_id']) password = self.__salt_hashed_password(email, password) if _user: _user_data = _user.get_data() _user_id = _user_data['_id'] _user_valid = User.get({ 'email': email, 'password': password }, fields=['_id']) if not _user_valid: raise CustomException( 'Sorry, it looks like {} belongs to an existing account.'. format(email) + "Please try logging in", Codes.CONFLICT) else: new_user = { 'first_name': first_name, 'last_name': last_name, 'email': email, 'password': password, } try: _user = User(new_user) _user_id = _user.insert() except Exception as e: raise CustomException(str(e), Codes.INVALID_PARAM) user_data = User.get({'_id': _user_id}, fields=_fields).get_data() return rjsonify(user_data)
def edit_id(self, _id, **kwargs): contact = Contact.get({'_id': _id}) if not contact: raise CustomException("Contact Not Found") contact_data = contact.get_data() _u = {} for key in ["first_name", "last_name", "email", "mobile"]: if kwargs.get(key): if kwargs[key] != contact_data.get(key): ignore_last_update = False if not _u.get('$set'): _u['$set'] = {} _u['$set'][key] = kwargs.get(key) try: contact.patch_update(_u, ignore_last_update=ignore_last_update) except Exception as e: raise CustomException("Error while updating Training Lesson : {}".format(e)) return rjsonify(Contact.get({'_id': _id}).get_data())
def decorated_function(*args, **kwargs): kwargs['current_user'] = None if not kwargs['primary_schema'] in skip: token = kwargs.get('token') if token: try: user_id = GEN_TOKEN.loads(token, max_age=21000000) user_default_fields = dict( User._default_fields_.items()) _user = User.get(user_id, fields=user_default_fields) kwargs['current_user'] = _user.get( user_id, fields=user_default_fields).get_data() except Exception as e: print(e) raise CustomException('Token Expired', 401) else: raise CustomException( 'Pass token to authorize your request', 401) kwargs['skip'], kwargs['limit'] = validate_pagination(**kwargs) data = f(*args, **kwargs) return data
def validate_pagination(**kwargs): """ Check Pagination values skip and limit and validate with global settings """ primary_schema = kwargs.get('primary_schema') or 'default' pagination = app.config['PAGINATION'].get( primary_schema, app.config['PAGINATION'].get('default')) kwargs['skip'] = int(kwargs.get('skip') or pagination['skip']) kwargs['limit'] = int(kwargs.get('limit') or pagination['limit']) if pagination.get('max_skip') and kwargs['skip'] > pagination['max_skip']: raise CustomException("You are not allowed to View More.") if pagination.get( 'max_limit') and kwargs['limit'] > pagination['max_limit']: kwargs['limit'] = pagination['max_limit'] return kwargs['skip'], kwargs['limit']
def validate_partial_update_array_params(source_dict, source_schema): """ DO PARTIAL VALIDATION OF Update Keys in $push, $pull, $pullAll, $pushAll and $addToSet Operators """ for field in source_dict.keys(): datatype = None field_split = field.split(".") field = field_split[0] if source_schema.get("properties").get(field): datatype = source_schema["properties"][field].get("type") if not datatype: continue if (len(field_split) == 1 and datatype not in ["list", "array"]) or \ (len(field_split) > 1 and datatype not in ["object"]): raise CustomException("%s INVALID TYPE" % field)
def is_hidden_field(schema_name, field): field = field.split('.')[0] if field in schemas[schema_name]['hidden_fields']: raise CustomException('Invalid field', Codes.INVALID_PARAM)
def is_reserved_field(schema_name, field): field = field.split('.')[0] if field in schemas[schema_name]['reserve_fields']: raise CustomException('Invalid field', Codes.INVALID_PARAM) is_valid_field(schema_name, field, False) return schemas[schema_name]['schema']['properties'][field]['type']
def check_in_possible_values(data, schema): if schema.get('possible_values') and data not in schema['possible_values']: raise CustomException("Invalid arg value possible_values are: " + ",".join(map(str, schema['possible_values'])))