def get_queue(self): if len(self.required_post_ids) > 3: return Reply("You cannot have more than 3 un-voted posts!").error() find = { "$and": [{ "to": { "$in": [self.username] } }, { "seen": { "$nin": [self.username] } }] } posts = Post.create_from_db_obj(mongo.db.posts.find(find).limit(5)) Post.mark_seen(self, posts) mongo.db.users.update( {"username": self.username}, {"$push": { "requiredPostIds": { "$each": Post.get_ids(posts) } }}) post_jsons = [post.get_safe_json() for post in posts] return Reply(post_jsons).ok()
def set_patch(): user = User.create_from_db_obj.User( mongo.db.users.find_one({"sessionKey": request.form["apiKey"]})) photos = UploadSet('photos', IMAGES) if request.method == 'POST' and 'pic' in request.files: configure_uploads(app, photos) filename = photos.save(request.files['pic']) user.set_patch(filename) return Reply().ok() else: return Reply("could not save image!").error()
def login(): # print(request.) salt = bcrypt.gensalt() #print(bcrypt.hashpw(request.json['password'].encode(), salt)) user = User.login(request.json['username'], request.json['password']) if user is not None: return Reply(user).ok() else: return Reply("Invalid username or password!").error()
def check_api_key(): if "debug" in request.json: user = mongo.db.users.find_one( {"username": request.json["debug"]}) user = User.create_from_db_obj(user) return api_method(user) elif "apiKey" in request.json: api_key = request.json["apiKey"] user = mongo.db.users.find_one({"sessionKey": api_key}) if user is not None: user = User.create_from_db_obj(user) return api_method(user) else: return Reply("Invalid apiKey!").error() else: return Reply("Invalid apiKey!").error()
def posts(username): mongo.db.posts.remove({}) user = User.get_from_username("critique") for i in range(20): post = Post.create_post(user, [username], "This is a Critique post!", "Test post " + str(i)) print(post.send(user, notify=False)) return Reply().ok()
def create_new_user(username, password, validating=True, patch="default.png", required_post_ids=[], score=0, following=[], info={}): if validating: if len(username) < 6: return Reply( "Your username must be at least 6 characters!").error() elif len(password) < 6: return Reply( "Your password must be at least 6 characters!").error() try: salt = bcrypt.gensalt() following.append(username) #print(bcrypt.hashpw(password.encode(), salt)) mongo.db.users.insert({ "username": username, "password": bcrypt.hashpw(password.encode(), salt), "patch": patch, "sessionKey": None, "requiredPostIds": required_post_ids, "score": score, "following": following, "info": info }) except pymongo.errors.PyMongoError as e: print(e) return Reply("This username is already taken!").error() return Reply().ok()
def get_post(self, db_id): find = { "$and": [{ "_id": ObjectId(db_id) }, { "to": { "$in": [self.getUsername()] } }, { "seen": { "$in": [self.getUsername()] } }] } results = mongo.db.posts.find(find) if results is not None: return Reply(Post.create_from_db_obj(results)).ok() else: return Reply("Could not find post with that Id!").error()
def search(requester): query = mongo.db.users.find( {"username": { "$regex": request.json["search"], "$options": "i" }}) users = User.create_from_db_obj(query) overviews = [user.get_overview(requester) for user in users] return Reply(overviews).ok()
def follow(self, user, following): # DO CHECK TO MAKE SURE USER EXISTS if following: if user not in self.following: mongo.db.users.update({"username": self.username}, {"$addToSet": { "following": user }}) return Reply().ok() else: return Reply("you are already following this user!").error() else: if user in self.following: mongo.db.users.update({"username": self.username}, {"$pull": { "following": user }}) return Reply().ok() else: return Reply("you weren't following this user!").error()
def send(self, requester, notify=True): for user in self.to: if not requester.is_mutual(user): return Reply( str(user) + " is not your mutual or does not exist!").error() post = { "username": self.username, "seen": self.seen, "votes": self.votes, "to": self.to, "content": self.content, "title": self.title, "type": self.type } mongo.db.posts.insert(post) if notify: self.notify(self.to, requester) return Reply().ok()
def cast_votes(self, votes): ids = [] print(votes) for vote in votes: ids.append(ObjectId(vote["id"])) if vote["vote"] != 0 and vote["vote"] != 1: return Reply("Invalid vote IDs!").error() if not self.ids_required(ids): return Reply("Invalid vote IDs!").error() mongo.db.users.update({"username": self.username}, {"$pull": { "requiredPostIds": { "$in": ids } }}) posts = Post.create_from_db_ids(ids) for i, post in enumerate(posts): post.vote(self, votes[i]["vote"]) return Reply().ok()
def send_post_notification(self, user): url = 'https://fcm.googleapis.com/fcm/send' key = 'AAAAxe-zm-c:APA91bFo5NK_jcUydvxbwbp1wWD3KCND2ul9xRLvZvi14aNjbAeQi6eJkbdU9wiFwawo7b6Af3rPuqoUH8q0vOfGYA40nRpIC436_SxBx2wbC1pl_CXTkA2Q_ev_yb-RUXQF66hS1YZq' body = { "registration_ids": [self.get_info("notificationKey")], "priority": "high", "data": { "title": "Critique", "body": "from " + user.username, } } headers = { "Content-Type": "application/json", "Authorization": "key=" + key } r = requests.post(url, data=json.dumps(body), headers=headers) return Reply(str(r.reason)).ok()
def hard_reset(): mongo.db.users.remove({}) mongo.db.posts.remove({}) User.create_new_user("marc", "nohash", validating=False, following=["adam", "john"]) User.create_new_user("adam", "password", validating=False, patch="adam.png", following=["marc", "noah"]) User.create_new_user("john", "password", validating=False, patch="john.png", following=["adam", "critique"]) User.create_new_user("critique", "critique", validating=False, patch="critique.png", following=["adam", "john", "marc", "noah"]) return Reply().ok()
def get_archive(user): return Reply(user.get_archive(request.json["page"], request.json["count"])).ok()
def notify(user): user.send_post_notification(user) return Reply().ok()
def reset(): mongo.db.users.update({}, {"$set": {"requiredPostIds": []}}, upsert=True) mongo.db.posts.remove({}) return Reply().ok()
def get_mutuals(user): return Reply(user.get_mutuals()).ok()
def custom500(error): return Reply("Internal Server error!").error()
def set_n_key(user): print(request.json["key"]) user.add_info("notificationKey", request.json["key"]) return Reply().ok()