def stats(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 session.get('digestif') is None: return redirect(url_for("stats_auth", service=stream.service)) oauth_token = session.get("digestif")["a"] if oauth_token != stream.oauth_token: # we'll 404 for now but should have better logic # it is possible the token we have stored is invalid and the # user may need to reauthorize other_stream = Stream.query.filter_by(oauth_token=oauth_token).first_or_404() stream_encoded=hash_gen.encrypt(other_stream.user_id, other_stream.id) return redirect(url_for("stats", stream_encoded=stream_encoded)) if stream.user_id != user_id: app.logger.error("Mismatching user and stream! {}, {}".format(stream.user_id, user_id)) return "Mismatching user and stream!" owner = stream.user.email subscribers = Subscription.query.filter_by(stream_id=stream.id).filter(Subscription.frequency != 0).all() digest_counts = {} for subscriber in subscribers: digest_counts[subscriber] = Digest.query.filter_by(subscription_id=subscriber.id).count() return render_template("stats.html", stream=stream, owner=owner, subscribers=subscribers, digest_counts=digest_counts)
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 display_digest(digest_encoded): values = hash_gen.decrypt(str(digest_encoded)) if not values: digest_id = -1 else: digest_id = values[0] digest = Digest.query.filter_by(id=digest_id).first_or_404() subscription = digest.subscription if not subscription: return "Unknown subscription" stream = subscription.stream if stream.service == FLICKR: entries = FlickrPhoto.query.filter(FlickrPhoto.date_uploaded > digest.start_date, FlickrPhoto.date_uploaded <= digest.end_date, FlickrPhoto.stream_id == stream.id).order_by(FlickrPhoto.date_taken).all() elif stream.service == INSTAGRAM: entries = InstagramPhoto.query.filter(InstagramPhoto.date_uploaded > digest.start_date, InstagramPhoto.date_uploaded <= digest.end_date, InstagramPhoto.stream_id == stream.id).order_by(InstagramPhoto.date_taken).all() else: entries = [] return render_template("digest.html", entries=entries, email=request.args.get("email", None), stream=stream, digest_encoded=digest_encoded, digest=digest)