def get(self): stream_id = self.request.get(IDENTIFIER_STREAM_ID) user_email = self.request.get(IDENTIFIER_USER_EMAIL) if user_email and stream_id: # check if the request is to subscribe or un-subscribe subscribe_bool = self.request.get('subscribe_bool') == 'true' if subscribe_bool: # subscribing request new_subscription_entry = Subscription(user_email=user_email, stream_id=stream_id) new_subscription_entry.put() print 'SubscriptionHandler:: user' + user_email + ' subscribed to stream' + stream_id self.redirect('/manage') else: # ub-subscribing request # fetch the data entry of this subscription subscription_entry = Subscription.query(Subscription.stream_id == stream_id, Subscription.user_email == user_email).fetch() if len(subscription_entry) != 0: subscription_entry[0].deleteSubscription() print 'SubscriptionHandler:: user' + user_email + ' un-subscribed to stream' + stream_id self.redirect('/manage') else: # no such subscription entry can be found template_values = { 'error_msg' : "no such subscription entry can be found. (userid=" + user_email + ", streamid=" + stream_id + ")" } self.render("Error.html", **template_values) else: #no user can be found in the session; transfer to error page template_values = { 'error_msg' : "no user can be found in the session" } self.render("Error.html", **template_values)
def post(self): req_json = json.loads(self.request.body) user_email = req_json[IDENTIFIER_USER_EMAIL] is_check_subscription = req_json[IDENTIFIER_CHECK_SUBSCRIPTION] if not is_check_subscription: print 'StreamQueryService >> checking all streams created by user[', user_email, ']' user_streams_list = ndb.Query(ancestor=ndb.Key('Account', user_email)).fetch() sorted_streams_list = sorted(user_streams_list, key=lambda stream: stream.last_add, reverse=True) sid_list = [] for stream in sorted_streams_list: sid_list.append(stream.stream_id) dumpablelist = [Stream.get_stream(stream_id).dumpStream() for stream_id in sid_list] self.respond(user_streams_list=dumpablelist, status="success") else: print 'StreamQueryService >> checking all streams subscribed by user[', user_email, ']' subscribed_list = Subscription.query(Subscription.user_email == user_email).fetch() subscribed_stream_ids = [] for subscribed_item in subscribed_list: sub_stream = Stream.query(Stream.stream_id == subscribed_item.stream_id).fetch() if len(sub_stream) != 0: subscribed_stream_ids.append(sub_stream[0].stream_id) dumpablelist = [Stream.get_stream(stream_id).dumpStream() for stream_id in subscribed_stream_ids] self.respond(user_sub_streams_list=dumpablelist, status="success")
def post(self): req_json = json.loads(self.request.body) stream_id = req_json[IDENTIFIER_STREAM_ID] current_user_email = req_json[IDENTIFIER_USER_EMAIL] print "SubscriptionService >> checking subscription stream_id: " + stream_id + ", user_email:" + current_user_email sub_query = Subscription.query(Subscription.user_email == current_user_email, Subscription.stream_id == stream_id).fetch() if len(sub_query) != 0: # subscription exists subscribe_option = "Unsubscribe" subscribe_url = "/subscribe?stream_id="+stream_id+"&subscribe_bool=false"\ + "&"+IDENTIFIER_USER_EMAIL+"=" + current_user_email else: # subscription doesn't exist subscribe_option = "Subscribe" subscribe_url = "/subscribe?stream_id="+stream_id+"&subscribe_bool=true"\ + "&"+IDENTIFIER_USER_EMAIL+"=" + current_user_email self.respond(subscribe_option=subscribe_option, subscribe_url=subscribe_url, status="success")
def get(self): stream_id_lst=list() stream_name_lst=list() cover_img_url_list=list() stream_lst=list() is_view_all_subscribed = self.request.get(IS_VIEW_ALL_SUBSCRIBED) usr_id = self.request.get(IDENTIFIER_USER_EMAIL) if is_view_all_subscribed: print 'mViewAllStreamsService:: getting subscribed streams list for user', usr_id subscribed_list = Subscription.query(Subscription.user_email == usr_id).fetch() for subscribed_item in subscribed_list: stream_lst.append(Stream.get_stream(subscribed_item.stream_id)) else: print 'mViewAllStreamsService:: getting all streams list' stream_lst = Stream.query().fetch() sorted_streams = sorted(stream_lst, key=lambda stream:stream.last_add, reverse=True) nrof_streams = len(sorted_streams) start_idx = self.request.get(VIEW_ALL_START_INDEX) if not start_idx or (start_idx > nrof_streams): start_idx = 0 last_idx = start_idx+NUM_STREAMS_PER_PAGE if last_idx > nrof_streams: last_idx = nrof_streams-1 for i in xrange(start_idx, last_idx+1): stream = sorted_streams[i] stream_id_lst.append(stream.stream_id) stream_name_lst.append(stream.stream_name) cover_img_url_list.append(stream.cover_url) print "mViewAllStreamsService:: stream_id_lst:" + str(stream_id_lst) + " |||| stream_name_lst:" + \ str(stream_name_lst) + " |||| cover_img_url_list:" + str(cover_img_url_list) + " |||| last_idx:" + \ str(last_idx) self.respond(stream_id_lst=stream_id_lst, stream_name_lst=stream_name_lst, cover_img_url_list=cover_img_url_list, last_idx=last_idx, status="success")
def post(self, account): subscription = Subscription.query.filter( Subscription.account_id == account.account_id, Subscription.end_date > datetime.datetime.now()).first() if subscription: return { "status": "failed", "message": get_request_message(request, "ALREADY_ACTIVE_SUBSCRIPTION") }, 409 subscription = Subscription(account_id=account.account_id, cost=SUBSCRIPTION_COST, start_date=datetime.datetime.now(), end_date=(datetime.datetime.now() + datetime.timedelta(days=+30))) database.session.add(subscription) database.session.commit() return { "status": "success", "data": subscription_schema.dump(subscription).data }, 201