Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Web_blog():
- exposed=True
- def __init__(self):
- self.config_filename = "./configuration/configuration.json"
- if not self._read_configuration():
- raise Exception("Can't configure")
- try:
- if self.debug:
- self.client = MongoClient(host="roadsafe.ddns.net", username="webapi", password="webapipass", authSource="roadsafedb")
- print("Connected successfully to roadsafe remote")
- else:
- self.client = MongoClient(host="localhost", username="webapi", password="webapipass", authSource="roadsafedb")
- print("Connected successfully to localhost")
- except Exception:
- print("An error occourred while configuring...")
- raise Exception("Can't connect to database")
- def _read_configuration(self):
- try:
- config = json.load(open(self.config_filename))
- self.debug = config["debug"]
- self.key = config["key"]
- self.ssl_certificate_path = config["ssl_certificate_path"]
- self.ssl_private_key_path = config["ssl_private_key_path"]
- self.ssl_certificate_chain_path = config[
- "ssl_certificate_chain_path"]
- self.mount_point = config["mount_point"]
- self.mount_port = config["mount_port"]
- return True
- except Exception as ex:
- print(ex)
- return False
- #
- def GET(self, *uri, **params):
- try:
- client = MongoClient(host='roadsafe.ddns.net', username='webapi', password='webapipass',
- authSource='roadsafedb')
- db = client["roadsafedb"]
- #db = self.client["roadsafedb"]
- issues = db["blog"]
- #print(issues.find_one())
- if uri[0] == 'all':
- found = issues.find()
- return_json = mongo_dumps(found)
- # k = 1
- # list1=[]
- # t = issues.find({"_id": {"$gt": ObjectId(params["bookmark"])}}).count()
- # for doc in issues.find({"$and": [{"_id": {"$gt": ObjectId(params["bookmark"])}}, {"type": {
- # "$exists": True}}]}): # tmp= collection app#tmp.find({"_id": {"$gt": ObjectId("4f156022ef7b8b0317a8ad19")}})
- # list1.append(doc)
- # if k == t: # HERE
- # bookmark = doc['_id']
- # # print(bookmark)
- # k = k + 1
- #found= issues.find({"$and": [{"_id": {"$gt": ObjectId(params["bookmark"])}}, {"type": {"$exists": True}}]})
- #return_json = mongo_dumps(found)
- except Exception as ex:
- print(ex)
- raise cp.HTTPError(500)
- return return_json
- def POST(self, *uri, **params):
- """POST
- The purpose of the POST request is to implement the functions of
- CREATE and UPDATE of the CRUD paradigm.
- :param *uri: The URIs of the URL used to make the request.
- :param **params: The Parameters of the URL used to make the request.
- """
- key = params["api-key"]
- body = cp.request.body.read()
- if key != self.key:
- raise cp.HTTPError(message="Wrong api-key")
- try:
- db = self.client["roadsafedb"]
- issues = db["blog"]
- if uri[0] == "add":
- issues.insert_one(json.loads(body))
- return_obj = "NOTIZIA INSERITA"
- elif uri[0] == "delete":
- issues.delete_one({"title": body})
- return_obj="NOTIZIA ELIMINATA"
- else:
- raise cp.HTTPError(500)
- except Exception as ex:
- print(ex)
- raise cp.HTTPError(500)
- return json.dumps(return_obj)
- #(login)?
- class Web_email():
- exposed = True
- def __init__(self):
- self.config_filename = "./configuration/configuration.json"
- if not self._read_configuration():
- raise Exception("Can't configure")
- try:
- if self.debug:
- self.client = MongoClient(host="roadsafe.ddns.net", username="webapi", password="webapipass", authSource="roadsafedb")
- print("Connected successfully to roadsafe remote")
- else:
- self.client = MongoClient(host="localhost", username="webapi", password="webapipass", authSource="roadsafedb")
- print("Connected successfully to localhost")
- except Exception:
- print("An error occourred while configuring...")
- raise Exception("Can't connect to database")
- def _read_configuration(self):
- try:
- config = json.load(open(self.config_filename))
- self.debug = config["debug"]
- self.key = config["key"]
- self.ssl_certificate_path = config["ssl_certificate_path"]
- self.ssl_private_key_path = config["ssl_private_key_path"]
- self.ssl_certificate_chain_path = config[
- "ssl_certificate_chain_path"]
- self.mount_point = config["mount_point"]
- self.mount_port = config["mount_port"]
- return True
- except Exception as ex:
- print(ex)
- return False
- def GET(self, *uri, **params):
- # """GET
- # The purpose of the GET request is to implement the functions of
- # READ and DELETE of the CRUD paradigm.
- # :param *uri: The URIs of the URL used to make the request.
- # :param **params: The Parameters of the URL used to make the request.
- # """
- key = params["api-key"]
- if key != self.key:
- raise cp.HTTPError(message="Wrong api-key")
- try:
- db = self.client["roadsafedb"]
- issues = db.email
- if uri[0] == 'all':
- found = issues.find()
- return_json = mongo_dumps(found)
- except Exception as ex:
- print(ex)
- raise cp.HTTPError(500)
- return return_json
- #
- def POST(self, *uri, **params):
- """POST
- The purpose of the POST request is to implement the functions of
- CREATE and UPDATE of the CRUD paradigm.
- :param *uri: The URIs of the URL used to make the request.
- :param **params: The Parameters of the URL used to make the request.
- """
- key = params["api-key"]
- body = cp.request.body.read()
- if key != self.key:
- raise cp.HTTPError(message="Wrong api-key")
- try:
- db = self.client["roadsafedb"]
- issues = db.email
- if uri[0] == "add":
- issues.insert_one(json.loads(body))
- return_obj = "EMAIL INSERITA"
- elif uri[0] == "delete":
- issues.delete_one({"email": body})
- return_obj = "EMAIL ELIMINATA"
- else:
- raise cp.HTTPError(500)
- except Exception as ex:
- print(ex)
- raise cp.HTTPError(500)
- if __name__ == "__main__":
- blog= Web_blog()
- email=Web_email()
- #webapp2= WebApp2()
- # webapp_issues = WebApp()
- # #webapp_web_issues = WebAppWebIssues()
- # #webapp_images = WebAppImages()
- server_config = {
- "/": {
- "request.dispatch": cp.dispatch.MethodDispatcher(),
- "request.show_traceback": True,
- "tools.sessions.on": True
- }
- }
- # #cp.tree.mount(webapp2, "/apiissuesweb2", server_config)
- cp.tree.mount(blog, "/blog", server_config)
- cp.tree.mount(email, "/email", server_config)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement