Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2018
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.00 KB | None | 0 0
  1. from flask import Flask,request,flash
  2. from flask import jsonify # <- jsonify instead of json
  3. from werkzeug import secure_filename
  4. from flask_pymongo import PyMongo
  5. from PIL import Image, ImageDraw
  6. import os
  7.  
  8. _name_ = "_main_"
  9. app = Flask(_name_)
  10.  
  11. from pymongo import MongoClient
  12. client = MongoClient()
  13. client = MongoClient('localhost', 27017)
  14. db = client.test_database
  15. collection = db.test_collection
  16. profiles = db.profiles
  17. contacts = db.contacts
  18. shared = db.shared #who you've shared resumes with
  19. requested = db.requested #who you've requested to see resumes of
  20. os.makedirs(os.path.join(app.instance_path, 'files'), exist_ok=True)
  21.  
  22. #requesting a resume?
  23. #seeing who you've shared resume with/who's shared with you
  24.  
  25. @app.route("/createAccount", methods=['POST'])
  26. def create():
  27. email = request.form['email']
  28. if(profiles.find({"email": email}).count() == 0):
  29. liprofile = request.form['liprofile']
  30. phone = request.form['phone']
  31. fname = request.form['fname']
  32. lname = request.form['lname']
  33. pfname = request.form['pfname']
  34. password = request.form['password']
  35. user = {"phone": phone, "email": email, "fname": fname,
  36. "lname":lname, "pfname":pfname, "liprofile":liprofile, 'password':password}
  37. profile_id = profiles.insert_one(user).inserted_id
  38. contact = {"email": email, "emails": []}
  39. contacts.insert_one(contact)
  40. sharedd = {"email": email, "emails": []}
  41. shared.insert_one(sharedd)
  42. requestedd = {"email": email, "emails": []}
  43. requested.insert_one(requestedd)
  44. return jsonify(str(profile_id))
  45. else:
  46. return "Email already taken"
  47.  
  48. @app.route("/shareResume", methods=['POST'])
  49. def shareResume():
  50. email1 = request.form['emailOrigin']
  51. email2 = request.form['emailFriend']
  52. result = shared.find_one({"email": email1})
  53. if(email2 in result['emails']):
  54. return "Already shared"
  55. if(len(result['emails']) == 0):
  56. templist = [email2]
  57. result['emails'] = templist
  58. else:
  59. result['emails'].append(email2)
  60.  
  61. shared.update({"email": email1}, result)
  62. return jsonify("Success")
  63.  
  64. #@app.route("/requestResume", methods=['POST'])
  65.  
  66.  
  67. @app.route("/createQR", methods=['POST'])
  68. def createQR():
  69. email = request.form['email']
  70. return jsonify(email)
  71.  
  72. @app.route("/getAccount", methods=['POST'])
  73. def getAccount():
  74. email1 = request.form['emailOrigin']
  75. email2 = request.form['emailFriend']
  76. result = profiles.find_one({"email": email2})
  77. result.pop('_id')
  78. result2 = shared.find_one({"email": email2})
  79. if(email1 in result2['emails']):
  80. result['Resume'] = "http://0.0.0.0:8000/instance/files/"+email.replace("@", "")+".pdf"
  81. return jsonify(result)
  82.  
  83. @app.route("/addContact", methods=['POST'])
  84. def addContact():
  85. email1 = request.form['emailOrigin']
  86. email2 = request.form['emailFriend']
  87. result = contacts.find_one({"email": email1})
  88. if(email2 in result['emails']):
  89. return "Already friends"
  90. if(len(result['emails']) == 0):
  91. templist = [email2]
  92. result['emails'] = templist
  93. else:
  94. result['emails'].append(email2)
  95.  
  96. contacts.update({"email": email1}, result)
  97. result = contacts.find_one({"email": email2})
  98. if(len(result['emails']) == 0):
  99. templist = [email1]
  100. result['emails'] = templist
  101. else:
  102. result['emails'].append(email1)
  103.  
  104. contacts.update({"email":email2}, result)
  105. return jsonify("Success")
  106.  
  107. @app.route("/getContacts", methods=['POST'])
  108. def getContacts():
  109. email = request.form['email']
  110. result = contacts.find_one({"email": email})
  111. contactslist = []
  112. for contact in result["emails"]:
  113. profile = profiles.find_one({"email": str(contact)})
  114. profile.pop('_id')
  115. contactslist.append(profile)
  116. return jsonify(contactslist)
  117.  
  118. @app.route("/getShared", methods=['POST'])
  119. def getShared():
  120. email = request.form['email']
  121. result = shared.find_one({"email": email})
  122. sharedlist = []
  123. for sharedd in result["emails"]:
  124. profile = profiles.find_one({"email": str(sharedd)})
  125. profile.pop('_id')
  126. sharedlist.append(profile)
  127. return jsonify(sharedlist)
  128.  
  129. @app.route("/getReceived", methods=['POST'])
  130. def getReceived():
  131. email = request.form['email']
  132. receivedlist = []
  133. for e in shared.find():
  134. for f in e['emails']:
  135. if(f == email):
  136. profile = profiles.find_one({"email":e['email']})
  137. profile.pop('_id')
  138. receivedlist.append(profile)
  139. return jsonify(receivedlist)
  140.  
  141. @app.route("/signIn", methods=['POST'])
  142. def signIn():
  143. email = request.form['email']
  144. if(profiles.find({"email": email}).count() == 0):
  145. return "Email not found."
  146.  
  147. result = profiles.find_one({'email': email})
  148. password = request.form['password']
  149. if(result['password'] != password):
  150. return "Incorrect password."
  151.  
  152. result.pop('_id')
  153. return jsonify(result)
  154.  
  155. @app.route('/uploadResume', methods = ['GET', 'POST'])
  156. def uploadResume():
  157. if request.method == 'POST':
  158. email = request.form["email"]
  159. f = request.files['file']
  160.  
  161. # when saving the file
  162. f.save(os.path.join(app.instance_path, 'files', secure_filename(email)+".pdf"))
  163. return jsonify('file uploaded successfully')
  164.  
  165. @app.route('/uploadCard', methods = ['GET', 'POST'])
  166. def uploadCard():
  167. if request.method == 'POST':
  168. email = request.form["email"]
  169. f = request.files['file']
  170.  
  171. # when saving the file
  172. f.save(os.path.join(app.instance_path, 'cards', secure_filename(email)+".jpg"))
  173. return 'file uploaded successfully'
  174.  
  175. #matt's fancy thing gives a dictionary in
  176. #email = in['email'];
  177. #if(profiles.find({"email": email}).count() == 0)
  178. #{
  179. # liprofile = in['liprofile']
  180. # phone = in['phone']
  181. # fname = in['fname']
  182. # lname = in['lname']
  183. # pfname = in['pfname']
  184. # password = in['password']
  185. # user = {"phone": phone, "email": email, "fname": fname,
  186. # "lname":lname, "pfname":pfname, "liprofile":liprofile, 'password':password}
  187. # profile_id = profiles.insert_one(user).inserted_id
  188. # contact = {"email": email, "emails": []}
  189. # contacts.insert_one(contact)
  190. # sharedd = {"email": email, "emails": []}
  191. # shared.insert_one(sharedd)
  192. # requestedd = {"email": email, "emails": []}
  193. # requested.insert_one(requestedd)
  194. #}
  195.  
  196. if _name_ == "_main_":
  197. app.run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement