def test_subscribe_unsubscribe_works(self): user_a = User(name='user_a', email='*****@*****.**', email_confirmed=1, is_paid=1, stripe_plan_id="mltshp-double") user_a.set_password('asdfasdf') user_a.save() self.sign_in('user_a', 'asdfasdf') arguments = { 'name': 'asdf', 'description': 'A shake test.', 'title': 'Shake Test', } self.post_url('/shake/create', arguments=arguments) shake = Shake.get('name = %s', 'asdf') self.sign_in('admin', 'asdfasdf') self.post_url('/shake/%s/subscribe?json=1' % shake.id) #subscription #1 is the user subscribing to their own new shake subscription = Subscription.get('id=2') self.assertEqual(subscription.user_id, 1) self.assertEqual(subscription.deleted, 0) self.post_url('/shake/%s/unsubscribe?json=1' % shake.id) #subscription #1 is the user subscribing to their own new shake subscription = Subscription.get('id=2') self.assertEqual(subscription.user_id, 1) self.assertEqual(subscription.deleted, 1)
def subscribe(subscriber, resource): # HACK workaround for using get_or_create / create_or_get with GFKFields. try: # HACK workaround for querying equality of GFKField. I'd love to do the following: # Subscription.get( # (Subscription.subscriber == subscriber) & # (Subscription.resource == resource)) sub = Subscription.get( (Subscription.subscriber_id == subscriber._get_pk_value()) & (Subscription.subscriber_type == subscriber._meta.db_table) & (Subscription.resource_id == resource._get_pk_value()) & (Subscription.resource_type == resource._meta.db_table)) created = False except Subscription.DoesNotExist: sub = Subscription.create( subscriber=subscriber, resource=resource, ) created = True return sub, created
def get_subscription(user_id, id): try: user = User.get(User.id == user_id) subscription = Subscription.get(Subscription.id == id) return {'subscriptions': [subscription.json_dict_user(user)]} except Subscription.DoesNotExist: response.status = 404 except User.DoesNotExist: response.status = 401 return {}
def subscribe_subscription_topic(user_id, sid): try: user = User.get(User.id == user_id) subscription = Subscription.get(Subscription.id == sid) user.unsubscribe_all(subscription) return {'subscriptions': [subscription.json_dict_user(user)]} except Subscription.DoesNotExist: response.status = 404 except User.DoesNotExist: response.status = 401 return {}
def unsubscribe_subscription_topic(user_id, sid, tid): try: user = User.get(User.id == user_id) subscription = Subscription.get(Subscription.id == sid) topic = subscription.get_topic(tid) user.unsubscribe(subscription, topic) return {'subscriptions': [subscription.json_dict_user(user)]} except (Subscription.DoesNotExist, Topic.DoesNotExist): response.status = 404 except User.DoesNotExist: response.status = 401 return {}
def test_subscribe_unsubscribe_is_same_object(self): request = HTTPRequest( self.get_url('/user/user3/subscribe?json=1'), 'POST', {'Cookie': 'sid=%s;_xsrf=%s' % (self.sid, self.xsrf)}, "_xsrf=%s" % (self.xsrf)) self.http_client.fetch(request, self.stop) response = self.wait() first_subscription = Subscription.get('user_id=1 and shake_id=3') request = HTTPRequest( self.get_url('/user/user3/unsubscribe?json=1'), 'POST', {'Cookie': 'sid=%s;_xsrf=%s' % (self.sid, self.xsrf)}, "_xsrf=%s" % (self.xsrf)) self.http_client.fetch(request, self.stop) response = self.wait() j = json_decode(response.body) self.assertEqual(j['subscription_status'], False) second_subscription = Subscription.get('user_id=1 and shake_id=3') self.assertEqual(first_subscription.id, second_subscription.id)
def test_follow_signed_in(self): request = HTTPRequest( self.get_url('/user/user3/subscribe?json=1'), 'POST', {'Cookie': 'sid=%s;_xsrf=%s' % (self.sid, self.xsrf)}, "_xsrf=%s" % (self.xsrf)) self.http_client.fetch(request, self.stop) response = self.wait() j = json_decode(response.body) self.assertEqual(j['subscription_status'], True) subscription = Subscription.get('user_id=1 and shake_id=3') self.assertTrue(subscription) self.assertFalse(subscription.deleted)
def post(self): with db_session: email = self.get_argument("email") subscription = Subscription.get(email=email) if subscription: self.set_status(409) self.finish() return Subscription(email=email) self.set_status(201) self.finish() return
def test_creating_group_shake_creates_subscription(self): """ Create a shake for user admin. Admin should now have a subscription to that shake """ arguments = { 'name': 'yo', 'description': 'My little corner of the world', 'title': 'title' } self.post_url('/shake/create', arguments=arguments) sh = Shake.get('name=%s', arguments['name']) sub = Subscription.get('user_id=%s and shake_id=%s and deleted = 0', self.user.id, sh.id) self.assertTrue(sub)
def get(self, **args): sub = Subscription.get(Key.from_path("Subscription", long(args['id']))) if not sub or sub.deleted: helpers.createResponse(self, 'message_not_exist.html') return if not helpers.checkPermissionAndRespond(self, sub=sub, edit=False): return subItem_q = sub.subscribeditem_set.filter("deleted ==", False).order("original") cursor = self.request.get('cursor_subItem') if cursor: subItem_q = subItem_q.with_cursor(cursor) subItems = subItem_q.fetch(10) helpers.createResponse(self, 'dashboard_subItems.html', {'subItems': subItems, 'sub': sub, 'cursor_subItem': subItem_q.cursor()})