def setUp(self): """ user_a -> admin user_b -> user2 user_a uploads shared file. We authenticate to the API with user_b. user_b subscribes to user_a's shake.] """ super(APIResourceRequests, self).setUp() self.user_a = User( name='admin', email='*****@*****.**', email_confirmed=1, is_paid=1, about="admin", website='https://mltshp.com') self.user_a.set_password('asdfasdf') self.user_a.save() self.sid = self.sign_in('admin', 'asdfasdf') self.xsrf = self.get_xsrf() self.test_file1_path = os.path.abspath("test/files/1.png") self.test_file1_sha1 = Sourcefile.get_sha1_file_key(self.test_file1_path) self.test_file1_content_type = "image/png" response = self.upload_file(file_path=self.test_file1_path, sha1=self.test_file1_sha1, content_type=self.test_file1_content_type, user_id=self.user_a.id, sid=self.sid, xsrf=self.xsrf) self.user_b = User(name='user2', email='*****@*****.**', email_confirmed=1, is_paid=1) self.user_b.set_password('asdfasdf') self.user_b.save() self.group_shake = self.user_b.create_group_shake(title='Group Shake', name='groupshake', description='This is a group shake.') self.group_shake_2 = self.user_a.create_group_shake(title='Another Group', name='anothergroup') # Add user_b to user_a's group shake, so we get it in user_b's /shakes endpoint. shake_manager = ShakeManager(user_id=self.user_b.id, shake_id=self.group_shake_2.id) shake_manager.save() self.app = App(user_id=self.user_a.id, title='An App', description='Nothing yet.', redirect_url='http://client.example.com/return') self.app.save() self.authorization = Authorizationcode.generate(self.app.id, self.app.redirect_url, self.user_b.id) self.access_token = Accesstoken.generate(self.authorization.id) extra_authorization = Authorizationcode.generate(self.app.id, self.app.redirect_url, self.user_b.id) self.ratelimited_access_token = Accesstoken.generate(extra_authorization.id) now_hour = datetime.utcnow().strftime('%Y-%m-%d %H:00:00') ratelimit = Apihit(accesstoken_id=self.ratelimited_access_token.id, hits=options.api_hits_per_hour - 2, hour_start=now_hour) ratelimit.save() #subscription self.user_b.subscribe(self.user_a.shake())
def post(self, shake_name): is_json = self.get_argument('json', None) current_user_object = self.get_current_user_object() invitation_notification = Notification.get( 'id = %s and receiver_id = %s and type = %s and deleted = 0', self.get_argument('id', 0), current_user_object.id, 'invitation') if not invitation_notification: if is_json: return self.write({'error': 'error'}) else: return self.redirect("/") shake_object = Shake.get('id = %s', invitation_notification.action_id) shake_manager = ShakeManager.get('user_id = %s and shake_id = %s', current_user_object.id, shake_object.id) if shake_manager: shake_manager.deleted = 0 shake_manager.save() else: shake_manager = ShakeManager(user_id=current_user_object.id, shake_id=shake_object.id) shake_manager.save() invitation_notification.deleted = 1 invitation_notification.save() #send email to owner of shake #set all invitation notifications for this shake, user, to deleted all_notifications = Notification.where( 'sender_id = %s and action_id = %s and receiver_id = %s and deleted = 0', invitation_notification.sender_id, invitation_notification.action_id, invitation_notification.receiver_id) for n in all_notifications: n.deleted = 1 n.save() if is_json: remaining_notifications_count = Notification.where_count("type = %s and receiver_id = %s and deleted = 0", \ "invitation", current_user_object.id) return self.write({ 'response': 'ok', 'count': remaining_notifications_count }) else: return self.redirect("/%s", shake_object.name)