def subscribe(stream_encoded): values = None try: values = hash_gen.decrypt(str(stream_encoded)) #values stores user_id, stream_id except TypeError: app.logger.error("Error in hashids.decrypt! {}".format(stream_encoded)) if values: user_id, stream_id = values else: user_id, stream_id = None, -1 stream = Stream.query.filter_by(id=stream_id).first_or_404() if stream.user_id != user_id: app.logger.error("Mismatching user and stream! {}, {}".format(stream.user_id, user_id)) return "Mismatching user and stream!" subscribe_form = SubscribeForm() if subscribe_form.validate_on_submit(): email = subscribe_form.email.data; frequency = int(subscribe_form.frequency.data) user = make_user(email) subscription = make_subscription(stream, user, frequency) return render_template("welcome.html", stream=stream, subscription=subscription, user=user) else: flash_errors(subscribe_form) unsubscribe = False address = request.args.get("address", "") if request.args.get("unsubscribe", None) == "1": unsubscribe = True frequency_index = request.args.get("f", None) try: frequency_index = int(frequency_index) except (ValueError, TypeError): frequency_index = None return render_template("subscribe.html", form=subscribe_form, stream=stream, unsubscribe=unsubscribe, address=address, frequency_index=frequency_index)
def build_user_stream(email, foreign_key, service, oauth_token, oauth_token_secret): user = None stream = Stream.query.filter_by(foreign_key=foreign_key).first() if stream: service_name = stream2service_filter(stream) user = stream.user if user.email != email: flash("We've updated the email address associated with your {} account. <a href=\"{}\">View your subscriber statistics</a>".format(service_name, url_for("stats_auth")), "info") else: flash("Tell your friends and family to visit this page to subscribe. <a href=\"{}\">View your subscriber statistics</a>".format(url_for("stats_auth")), "info") else: flash("Great! We are all set. Tell your friends and family to visit this page to subscribe. <a href=\"{}\">View your subscriber statistics</a>".format(url_for("stats_auth")), "success") user = make_user(email, user=user) stream = make_stream(foreign_key, user, oauth_token, oauth_token_secret, last_checked=datetime.utcnow()) return stream