def validate_contact_us_schema(info, schema):
     for field in schema:
         if field not in info:
             msg = f"Argument {field} missing"
             raise StatusException(msg, http.BAD_REQUEST)
         if not isinstance(info[field], schema[field]):
             msg = f"Argument {field} has invalid type"
             raise StatusException(msg, http.BAD_REQUEST)
Пример #2
0
 def get_all(cls, args: dict):
     try:
         collection = MONGO.db[cls.db_name]
         result = list(collection.find(args))
         return result
     except Exception as ex:
         e = StatusException(ex)
         e.status = http.INTERNAL_SERVER_ERROR
         raise e
Пример #3
0
 def patch(self, data):
     self.validate_update_data(data, True, self._id)
     try:
         collection = MONGO.db[self.db_name]
         collection.update_one({'_id': ObjectId(self._id)}, {"$set": data})
         return self._id
     except Exception as ex:
         e = StatusException(ex)
         e.status = http.INTERNAL_SERVER_ERROR
         raise e
 def check_active_principle_existence(data):
     if 'active_principle' not in data:
         return
     code = data['active_principle']
     active_principles = ActivePrinciple.get_all({'code': code})
     if len(active_principles) == 1:
         return
     msg = f"The active principle code {code} does not exist in data base"
     raise StatusException(msg, http.BAD_REQUEST)
Пример #5
0
 def check_unique_value(cls, field, value, _id=None):
     if not cls.unique_values[field]:
         return
     result = cls.get_all({field: value})
     if len(result) == 0:
         return
     if len(result) == 1 and _id is not None:
         if _id == str(result[0]['_id']):
             return
     msg = f"field {field} already exists"
     raise StatusException(msg, http.BAD_REQUEST)
Пример #6
0
 def post(self):
     try:
         collection = MONGO.db[self.db_name]
         result = collection.insert_one(self._data)
         if not result.acknowledged:
             e = StatusException('write concern was disabled')
             e.status = http.INTERNAL_SERVER_ERROR
             raise e
         self._id = result.inserted_id
         return self._id
     except Exception as ex:
         e = StatusException(ex)
         e.status = http.INTERNAL_SERVER_ERROR
         raise e
Пример #7
0
 def delete(self):
     try:
         collection = MONGO.db[self.db_name]
         res = collection.delete_one({"_id": ObjectId(self._id)})
         if res.deleted_count != 1:
             msg = f"The number of deleted docs were {res.deleted_count}"
             e = StatusException(msg)
             e.status = http.INTERNAL_SERVER_ERROR
             raise e
         return self._id
     except Exception as ex:
         e = StatusException(ex)
         e.status = http.INTERNAL_SERVER_ERROR
         raise e
Пример #8
0
 def get(cls, _id):
     try:
         ObjectId(_id)
     except Exception as ex:
         e = StatusException(ex)
         e.status = http.BAD_REQUEST
         raise e
     try:
         collection = MONGO.db[cls.db_name]
         doc = collection.find_one({"_id": ObjectId(_id)})
     except Exception as ex:
         e = StatusException(ex)
         e.status = http.INTERNAL_SERVER_ERROR
         raise e
     if doc is not None:
         doc = dict(doc)
         cls.validate_data(doc, False)
     return doc
Пример #9
0
 def validate_key_in_schema(cls, field):
     if field not in cls.schema.keys():
         name = cls.__class__.__name__
         e = StatusException(f"{field} is not a valid attribute of {name}")
         e.status = http.BAD_REQUEST
         raise e
Пример #10
0
 def validate_value_format(value, callback, field, name):
     if not callback(value):
         msg = f"{field} does not meet the required format of {name}"
         raise StatusException(msg, http.BAD_REQUEST)
Пример #11
0
 def validate_schema(cls, data):
     for key in cls.schema:
         if key not in data:
             e = StatusException(f"Argument {key} missing")
             e.status = http.BAD_REQUEST
             raise e
Пример #12
0
 def validate_data_types(cls, data):
     for key in data:
         if not cls.validate_type(key, data[key]):
             e = StatusException(f"Argument {key} has invalid type")
             e.status = http.BAD_REQUEST
             raise e