|
| 1 | +from pymongo import MongoClient |
| 2 | +from bson.objectid import ObjectId |
| 3 | + |
| 4 | +client = MongoClient() |
| 5 | + |
| 6 | +db = client.pretty |
| 7 | + |
| 8 | +contracts = db.contract |
| 9 | +users = db.users |
| 10 | + |
| 11 | +def createContract(ownerID, sLat, sLong, eLat, eLon, price): |
| 12 | + contract = { |
| 13 | + "ownerID" : ownerID, |
| 14 | + "acceptID" : -1, |
| 15 | + "startLocation" :[sLat, sLong], |
| 16 | + "endLocation" : [eLat, eLon], |
| 17 | + "price" : price, |
| 18 | + "valid" : true, |
| 19 | + "active" : false, |
| 20 | + } |
| 21 | + |
| 22 | + result = contracts.insert(contract); |
| 23 | + return ObjectID.toString(result); |
| 24 | + |
| 25 | +def createUser(name, password, userID, rating): |
| 26 | + user = { |
| 27 | + "name" : name, |
| 28 | + "password" : password, |
| 29 | + "username" : userID, |
| 30 | + "rating" : rating |
| 31 | + } |
| 32 | + result = users.insert(user) |
| 33 | + |
| 34 | + return ObjectID.toString(result); |
| 35 | + |
| 36 | + |
| 37 | +def acceptContract(userID, contractID): |
| 38 | + objID = ObjectID(contractID) |
| 39 | + found = contracts.find( {"$and" :[{"_id" : objID}, {"valid": true}]}).count() |
| 40 | + |
| 41 | + if found > 0: |
| 42 | + contracts.update({"_id": objID}, { "$set" : {"valid": false, "active": true, "acceptID": userID } }) |
| 43 | + return true |
| 44 | + else: |
| 45 | + return false |
| 46 | + |
| 47 | +def completeContract(contractID): |
| 48 | + objID = ObjectID(contractID) |
| 49 | + found = contracts.find( {"$and" : [{"_id" : objID}, {"active": true}]} ).count() |
| 50 | + |
| 51 | + if found > 0: |
| 52 | + contracts.update( {"_id" : objID}, { "$set" : {"active" : false}}) |
| 53 | + return true |
| 54 | + else: |
| 55 | + return false |
| 56 | + |
| 57 | + |
| 58 | + |
| 59 | + |
| 60 | +def validLogin(username, password): |
| 61 | + userProfile = users.find_one({"screenName" : username}) |
| 62 | + for attribute in userProfile: |
| 63 | + passw = attribute.get("password") |
| 64 | + |
| 65 | + if password == passw: |
| 66 | + return [true, ObjectID.toString(userProfile)] |
| 67 | + else: |
| 68 | + return [false, -1] |
| 69 | + |
| 70 | + |
| 71 | + |
| 72 | +def getContract(contractID): |
| 73 | + contract = contracts.find_one({"_id": ObjectID(contractID)}) |
| 74 | + |
| 75 | + for data in contract: |
| 76 | + toReturn = [data.get("owner"), data.get("startLocation"), data.get("endLocation"), data.get("price")] |
| 77 | + |
| 78 | + return toReturn |
| 79 | + |
| 80 | +def getUser(blockID): |
| 81 | + contract = contracts.find({"_id": ObjectID(blockID)}) |
| 82 | + |
| 83 | + for data in contract: |
| 84 | + toReturn = [data.get("name"), data.get("password"), data.get("screenName"), data.get("rating")] |
| 85 | + |
| 86 | + return toReturn |
| 87 | + |
| 88 | + |
| 89 | + |
| 90 | +#createContract("Theo" , 20, 20, 60, 40, 100) |
| 91 | +#createUser("Arjun", "arjun", "arjunmishra", 0) |
| 92 | +#getContract("Theo") |
| 93 | + |
| 94 | + |
| 95 | + |
| 96 | + |
0 commit comments