def post(): post_body = request.form["post_body"] is_anonymous = request.form.get("is_anonymous", None) == "on" allow_guesses = request.form.get("allow_guesses", None) == "on" user = current_user if type(user.is_authenticated) == bool or not user.is_authenticated(): is_anonymous = True allow_guesses = False poster = tagged = None if not is_anonymous or allow_guesses: poster = user.sid m = re.search(r"@([^_\W]+)", post_body) if m: tagged = m.group(1) tags = re.findall(r"#([^_\W]+)", post_body) tag_s = "" if tags else None for tag in tags: tag_s += "%s|" % tag guesses = NUMBER_GUESSES if allow_guesses else 0 p = Post(post_body, approved=False, is_anonymous=is_anonymous, poster=poster, allow_guesses=allow_guesses, tags=tag_s) p.save() #like = Like(pid=p.pid, like_count=0) #like.save(g.conn) gs = GuessSetting(p.pid, tagged=tagged, num_guesses=guesses, remaining=guesses) if tagged is not None: print tagged, p.pid send_mail("*****@*****.**" % tagged, "Looks like someone admirers you! Checkout what they said at admirers.cloudapp.net/search/id/%s" % p.pid) gs.save() return redirect("/")
def search_tag(tag): tag = tag.replace("'", "") filters = [] if tag != "": filters.append(Contains("tags", tag)) posts = Post.select(filters, [("pid", GuessSetting, "pid"), ("poster", User, "sid")]) posts = Post.prepare_view(current_user, posts) return render_template("main.html", **{"posts": posts})
def search_pid(pid): filters = [] pid = pid.replace("'", "") if pid != "": filters.append(Equal("pid", pid)) posts = Post.select(filters, [("pid", GuessSetting, "pid"), ("poster", User, "sid")]) posts = Post.prepare_view(current_user, posts) return render_template("main.html", **{"posts": posts})
def search_user(user): filters = [] user = user.replace("'", "") if user != "": filters.append(Equal("tagged", "'%s'" %user, GuessSetting.table)) posts = Post.select(filters, [("pid", GuessSetting, "pid"), ("poster", User, "sid")]) posts = Post.prepare_view(current_user, posts) return render_template("main.html", **{"posts": posts})
def own_posts(): filters = [] user = current_user filters.append(Equal("poster", user.sid)) filters.append(Equal("is_anonymous", False)) filters = Filter.and_reduce(filters) posts = Post.select(filters, [("pid", GuessSetting, "pid"), ("poster", User, "sid")]) posts = Post.prepare_view(user, posts) return render_template("main.html", **{"posts": posts})
def index(alert=None): """ User.create_table() Post.create_table() Comment.create_table() GuessSetting.create_table() Like.create_table() """ user = current_user posts = Post.get_all([("pid", GuessSetting, "pid"), ("poster", User, "sid")]) posts = Post.prepare_view(user, posts) return render_template("main.html", **{"posts": posts, "alert": alert})
def search(): text = request.json.replace("'", "") pid = request.form.get("post_id", "").replace("'", "").replace(";", "") contains = request.form.get("contains", "").replace("'", "").replace(";","") tagged = request.form.get("tagged", "").replace("'", "").replace(";", "") tags = request.form.get("tags", "").replace("'", "").replace(";", "") filters = [] search_text = contains if contains != "" else text if pid != "": pid_filter = Equal("pid", int(pid)) filters.append(pid_filter) if search_text != "" and search_text is not None: filters.append(Contains("post_body", search_text)) if tagged != "" and tagged is not None: filters.append(Contains("post_body", tagged)) for tag in [x for x in tags.split(",") if not x == ""]: tag = tag.strip() filters.append(Contains("tags", tag)) filters = Filter.and_reduce(filters) posts = Post.select(filters, [("pid", GuessSetting, "pid"), ("poster", User, "sid")]) posts = Post.prepare_view(current_user, posts) return render_template("main.html", **{"posts": posts})
def like(): if type(current_user.is_authenticated) == bool or not current_user.is_authenticated(): flash(u'You must be logged in to like posts') return redirect("/") pid = int(request.args.get("pid")) post = Post.select([Equal("pid", pid)], []) filters = [Equal("pid", pid), Equal("sid", current_user.sid)] filter = Filter.and_reduce(filters) likes = Like.select(filter, []) if not likes and post: print("Like") post = post[0] post.like_count += 1 post.save() l = Like(pid=pid, sid=current_user.sid) l.save() else: flash("You can't like the same post twice") return redirect("/")
def guess(): print("Guessing!") print(request.json) pid = int(request.json.get("pid")) guess = request.json.get("guess", None) post = Post.select([Equal("pid", pid)], [("pid", GuessSetting, "pid"), ("poster", User, "sid")])[0] gs = GuessSetting.select([Equal("gsid", post.gsid)], [])[0] user = current_user if not post.allow_guesses or not user.uni == post.tagged or gs.remaining < 1: flash(u'Guessing not permitted') return redirect("/") gs.remaining -= 1 if guess is not None and guess == post.uni: gs.matched = True send_guessed_mail(post.email, user.uni) send_guesser_mail(user.email, guess) flash(u"You've found a match! You should go ahead and message them!") print "MATCH!!!!! LOVE IS IN THE AIR" else: flash(u"Guess '%s' not matched :(" % guess) gs.save() return redirect("/")