Advertisement
Guest User

Untitled

a guest
Apr 26th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.08 KB | None | 0 0
  1. class Web_blog():
  2. exposed=True
  3.  
  4. def __init__(self):
  5. self.config_filename = "./configuration/configuration.json"
  6. if not self._read_configuration():
  7. raise Exception("Can't configure")
  8. try:
  9. if self.debug:
  10. self.client = MongoClient(host="roadsafe.ddns.net", username="webapi", password="webapipass", authSource="roadsafedb")
  11. print("Connected successfully to roadsafe remote")
  12. else:
  13. self.client = MongoClient(host="localhost", username="webapi", password="webapipass", authSource="roadsafedb")
  14. print("Connected successfully to localhost")
  15. except Exception:
  16. print("An error occourred while configuring...")
  17. raise Exception("Can't connect to database")
  18.  
  19. def _read_configuration(self):
  20. try:
  21. config = json.load(open(self.config_filename))
  22. self.debug = config["debug"]
  23. self.key = config["key"]
  24. self.ssl_certificate_path = config["ssl_certificate_path"]
  25. self.ssl_private_key_path = config["ssl_private_key_path"]
  26. self.ssl_certificate_chain_path = config[
  27. "ssl_certificate_chain_path"]
  28. self.mount_point = config["mount_point"]
  29. self.mount_port = config["mount_port"]
  30. return True
  31. except Exception as ex:
  32. print(ex)
  33. return False
  34. #
  35.  
  36. def GET(self, *uri, **params):
  37.  
  38. try:
  39. client = MongoClient(host='roadsafe.ddns.net', username='webapi', password='webapipass',
  40. authSource='roadsafedb')
  41. db = client["roadsafedb"]
  42. #db = self.client["roadsafedb"]
  43. issues = db["blog"]
  44. #print(issues.find_one())
  45. if uri[0] == 'all':
  46. found = issues.find()
  47. return_json = mongo_dumps(found)
  48. # k = 1
  49. # list1=[]
  50. # t = issues.find({"_id": {"$gt": ObjectId(params["bookmark"])}}).count()
  51. # for doc in issues.find({"$and": [{"_id": {"$gt": ObjectId(params["bookmark"])}}, {"type": {
  52. # "$exists": True}}]}): # tmp= collection app#tmp.find({"_id": {"$gt": ObjectId("4f156022ef7b8b0317a8ad19")}})
  53. # list1.append(doc)
  54. # if k == t: # HERE
  55. # bookmark = doc['_id']
  56. # # print(bookmark)
  57. # k = k + 1
  58. #found= issues.find({"$and": [{"_id": {"$gt": ObjectId(params["bookmark"])}}, {"type": {"$exists": True}}]})
  59. #return_json = mongo_dumps(found)
  60. except Exception as ex:
  61. print(ex)
  62. raise cp.HTTPError(500)
  63. return return_json
  64.  
  65. def POST(self, *uri, **params):
  66. """POST
  67. The purpose of the POST request is to implement the functions of
  68. CREATE and UPDATE of the CRUD paradigm.
  69. :param *uri: The URIs of the URL used to make the request.
  70. :param **params: The Parameters of the URL used to make the request.
  71. """
  72. key = params["api-key"]
  73. body = cp.request.body.read()
  74. if key != self.key:
  75. raise cp.HTTPError(message="Wrong api-key")
  76. try:
  77. db = self.client["roadsafedb"]
  78. issues = db["blog"]
  79. if uri[0] == "add":
  80. issues.insert_one(json.loads(body))
  81. return_obj = "NOTIZIA INSERITA"
  82. elif uri[0] == "delete":
  83. issues.delete_one({"title": body})
  84. return_obj="NOTIZIA ELIMINATA"
  85. else:
  86. raise cp.HTTPError(500)
  87. except Exception as ex:
  88. print(ex)
  89. raise cp.HTTPError(500)
  90.  
  91. return json.dumps(return_obj)
  92.  
  93. #(login)?
  94.  
  95. class Web_email():
  96. exposed = True
  97.  
  98. def __init__(self):
  99. self.config_filename = "./configuration/configuration.json"
  100. if not self._read_configuration():
  101. raise Exception("Can't configure")
  102. try:
  103. if self.debug:
  104. self.client = MongoClient(host="roadsafe.ddns.net", username="webapi", password="webapipass", authSource="roadsafedb")
  105. print("Connected successfully to roadsafe remote")
  106. else:
  107. self.client = MongoClient(host="localhost", username="webapi", password="webapipass", authSource="roadsafedb")
  108. print("Connected successfully to localhost")
  109. except Exception:
  110. print("An error occourred while configuring...")
  111. raise Exception("Can't connect to database")
  112.  
  113. def _read_configuration(self):
  114. try:
  115. config = json.load(open(self.config_filename))
  116. self.debug = config["debug"]
  117. self.key = config["key"]
  118. self.ssl_certificate_path = config["ssl_certificate_path"]
  119. self.ssl_private_key_path = config["ssl_private_key_path"]
  120. self.ssl_certificate_chain_path = config[
  121. "ssl_certificate_chain_path"]
  122. self.mount_point = config["mount_point"]
  123. self.mount_port = config["mount_port"]
  124. return True
  125. except Exception as ex:
  126. print(ex)
  127. return False
  128.  
  129. def GET(self, *uri, **params):
  130. # """GET
  131. # The purpose of the GET request is to implement the functions of
  132. # READ and DELETE of the CRUD paradigm.
  133. # :param *uri: The URIs of the URL used to make the request.
  134. # :param **params: The Parameters of the URL used to make the request.
  135. # """
  136. key = params["api-key"]
  137. if key != self.key:
  138. raise cp.HTTPError(message="Wrong api-key")
  139. try:
  140. db = self.client["roadsafedb"]
  141. issues = db.email
  142. if uri[0] == 'all':
  143. found = issues.find()
  144. return_json = mongo_dumps(found)
  145. except Exception as ex:
  146. print(ex)
  147. raise cp.HTTPError(500)
  148. return return_json
  149. #
  150. def POST(self, *uri, **params):
  151. """POST
  152. The purpose of the POST request is to implement the functions of
  153. CREATE and UPDATE of the CRUD paradigm.
  154. :param *uri: The URIs of the URL used to make the request.
  155. :param **params: The Parameters of the URL used to make the request.
  156. """
  157. key = params["api-key"]
  158. body = cp.request.body.read()
  159. if key != self.key:
  160. raise cp.HTTPError(message="Wrong api-key")
  161. try:
  162. db = self.client["roadsafedb"]
  163. issues = db.email
  164. if uri[0] == "add":
  165. issues.insert_one(json.loads(body))
  166. return_obj = "EMAIL INSERITA"
  167. elif uri[0] == "delete":
  168. issues.delete_one({"email": body})
  169. return_obj = "EMAIL ELIMINATA"
  170. else:
  171. raise cp.HTTPError(500)
  172. except Exception as ex:
  173. print(ex)
  174. raise cp.HTTPError(500)
  175.  
  176.  
  177. if __name__ == "__main__":
  178. blog= Web_blog()
  179. email=Web_email()
  180. #webapp2= WebApp2()
  181. # webapp_issues = WebApp()
  182. # #webapp_web_issues = WebAppWebIssues()
  183. # #webapp_images = WebAppImages()
  184. server_config = {
  185. "/": {
  186. "request.dispatch": cp.dispatch.MethodDispatcher(),
  187. "request.show_traceback": True,
  188. "tools.sessions.on": True
  189. }
  190. }
  191. # #cp.tree.mount(webapp2, "/apiissuesweb2", server_config)
  192. cp.tree.mount(blog, "/blog", server_config)
  193. cp.tree.mount(email, "/email", server_config)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement