def unsub_command(self, message=None): """Unsubscribe the user from a service""" user = message.sender.split('/')[0] plist = message.body.split(' ' ) service_name = plist[1] if len(plist)>2: type = "sms" user = plist[2] else: type = "xmpp" service = Service.all().filter('name = ', service_name).get() if service: subscription = Subscription.all().filter('address =', user).filter('service = ', service).filter('type =', type).get() if subscription: subscription.delete() if type == "xmpp": mobile = Mobile.all().filter('subscription = ', subscription).get() if mobile: mobile.delete() message.reply("Unsubscribed %s from service %s" % (user, service.name)) else: message.reply("user %s is not subscribed to service %s" % (user, service.name)) else: message.reply("Sorry, I couldn't find a service called " "%s" % service_name)
def get(self): user = users.get_current_user() checklist_q = Checklist.all().filter("user ==", user).filter("deleted ==", False).order("title"); cursor = self.request.get('cursor_cl') if cursor: checklist_q = checklist_q.with_cursor(cursor) checklists = checklist_q.fetch(10) checklist_q = checklist_q.with_cursor(checklist_q.cursor()) subs_by_cl = [] for cl in checklists: subs = [] for sub in cl.subscription_set: subs.append(sub) subs_by_cl.append(subs) subs_q = Subscription.all().filter("user ==", user).filter("deleted ==", False); cursor = self.request.get('cursor_sub') if cursor: subs_q = subs_q.with_cursor(cursor) subs = subs_q.fetch(10) subs_q = subs_q.with_cursor(subs_q.cursor()) helpers.createResponse(self, 'dashboard_cls.html', {'checklists': checklists, 'cursor_cl': checklist_q.cursor(), 'subs_by_cl': subs_by_cl, 'subs': subs, 'cursor_sub': subs_q.cursor(), 'more_subs': subs_q.count(1) == 1, 'more_cls': checklist_q.count(1) == 1, })
def sub_command(self, message=None): """Subscribe the user to XMPP or SMS""" user = message.sender.split('/')[0] plist = message.body.split(' ' ) service_name = plist[1] if len(plist)>2: type = "sms" user = plist[2] else: type = "xmpp" service = Service.all().filter('name = ', service_name).get() if service: subscription = Subscription.all().filter('address =', user).filter('service = ', service).get() if subscription: message.reply("user %s is already subscribed to service %s" % (user, service.name)) else: subscription = Subscription(key_name=hashlib.sha1(user).hexdigest(), type=type, address=user, service=service) subscription.put() message.reply("Subscribed %s to service %s" % (user, service.name)) else: message.reply("Sorry, I couldn't find a service called " "%s" % service_name)
def unsms_command(self, message=None): """Unsubscribe the user from a service""" plist = message.body.split(' ') if len(plist)==2: user = message.sender.split('/')[0] service_name = plist[1] service = Service.all().filter('name = ', service_name).get() if service: subscription = Subscription.all().filter('address =', user).filter('service = ', service).get() if subscription: mobile = Mobile.all().filter('subscription = ', subscription).get() if mobile: message.reply("Unsubscribed user %s from backup mobile %s for service %s" % (user, mobile.number,service_name)) mobile.delete() else: message.reply("No backup mobile for user %s on %s service" % (user,service_name)) else: message.reply("User %s is not subscribed to service %s" % (user, service.name)) else: message.reply("Sorry, I couldn't find a service called " "%s" % service_name) else: message.reply("Usege: unsms SERVICE +6112345678")
def get(self, **args): cl = Checklist.get(Key.from_path("Checklist", long(args['id']))) if not cl or cl.deleted: helpers.createResponse(self, 'message_not_exist.html') return if not helpers.checkPermissionAndRespond(self, cl=cl, edit=False): return item_q = cl.item_set.filter("deleted ==", False).order("creation_time") cursor = self.request.get('cursor') if cursor: item_q = item_q.with_cursor(cursor) items = item_q.fetch(20) user = users.get_current_user() subscribed = False for sub in Subscription.all().filter("user ==", user).filter("deleted ==", False): if sub.source.key() == cl.key(): subscribed = True break helpers.createResponse(self, 'dashboard_subscribe.html', {'items': items, 'cl': cl, 'cursor_item': item_q.cursor(), 'subscribed': subscribed, }, )
def sms_command(self, message=None): """Subscribe the user to a offline SMS""" plist = message.body.split(' ') if len(plist)==3: user = message.sender.split('/')[0] service_name = plist[1] number = plist[2] service = Service.all().filter('name = ', service_name).get() if service: subscription = Subscription.all().filter('address =', user).filter('service = ', service).get() if subscription: mobile = Mobile.all().filter('number =', number).get() if mobile: message.reply("user %s is already registered backup mobile %s for service %s" % (user, mobile.number,service_name)) else: mobile = Mobile(number=number, subscription = subscription) mobile.put() message.reply("Subscribed user %s to backup mobile %s for service %s" % (user, number,service_name)) else: message.reply("Sorry, I couldn't find a subscription on %s for %s" % (service_name,user)) else: message.reply("Sorry, I couldn't find a service called " "%s" % service_name) else: message.reply("Usage: sms SERVICE +61412345678")
def get(self): user = users.get_current_user() checklist_q = Checklist.all().filter("user ==", user).filter("deleted ==", False).order("title"); cursor = self.request.get('cursor_cl') if cursor: checklist_q = checklist_q.with_cursor(cursor) checklists = checklist_q.fetch(20) cursor_cl = checklist_q.cursor() checklist_q = checklist_q.with_cursor(cursor_cl) subs_q = Subscription.all().filter("user ==", user).filter("deleted ==", False); cursor = self.request.get('cursor_sub') if cursor: subs_q = subs_q.with_cursor(cursor) subs = subs_q.fetch(20) cursor_sub = subs_q.cursor() subs_q = subs_q.with_cursor(cursor_sub) helpers.createResponse(self, 'dashboard_cls.html', {'checklists': checklists, 'cursor_cl': cursor_cl, 'subs': subs, 'cursor_sub': cursor_sub, 'more_subs': subs_q.count(1) == 1, 'more_cls': checklist_q.count(1) == 1, })
def post(self): cl = Checklist.get(Key.from_path('Checklist', long(self.request.get('cl_id')))) if not helpers.checkPermissionAndRespond(self, cl=cl, edit=False): return user = users.get_current_user() for sub in Subscription.all().filter("user ==", user).filter("deleted ==", False): if sub.source.key() == cl.key(): helpers.createResponse(self, 'message_already_subscribed.html', {'old_checklist': cl}) sub = Subscription( user=user, source=cl, deleted=False, ) sub.put() for item in cl.item_set: subItem = SubscribedItem( subscription=sub, original=item, finished=False, deleted=False, ) subItem.put() helpers.createResponse(self, 'message_subscribed.html')
def test_cannot_follow_self(self): request = HTTPRequest( self.get_url('/user/admin/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.assertTrue('error' in j) subscription = Subscription.all() self.assertTrue(len(subscription) == 0)
def test_notification_created_when_subscription_created(self): """ User is followed. Notification created. """ 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() subscriptions = Subscription.all() self.assertEqual(len(subscriptions), 1) notifications = Notification.all() self.assertEqual(len(notifications), 1) notification = notifications[0] self.assertEqual(notification.sender_id, self.admin.id) self.assertEqual(notification.receiver_id, self.user3.id) self.assertEqual(notification.action_id, subscriptions[0].id) self.assertEqual(notification.type, 'subscriber')
def post(self): cl = Checklist.get(Key.from_path('Checklist', long(self.request.get('cl_id')))) subscribe = self.request.get('subscribe') if not helpers.checkPermissionAndRespond(self, cl=cl, edit=False): return user = users.get_current_user() if subscribe == 'false': for sub in Subscription.all().filter("user ==", user).filter("deleted ==", False): if sub.source.key() == cl.key(): for subItem in sub.subscribeditem_set: subItem.delete() sub.delete() cl.subscribers = cl.subscribers - 1 self.response.write("unsubscribed") else: sub = Subscription( user=user, source=cl, deleted=False, ) sub.put() for item in cl.item_set.filter("deleted ==", False): subItem = SubscribedItem( subscription=sub, original=item, finished=False, deleted=False, ) subItem.put() cl.subscribers = cl.subscribers + 1 self.response.write("subscribed") cl.put()