def modifyContext(self, properties): """Update the user object with the given properties""" # If updating the twitterUsername, get its Twitter ID if properties.get("twitterUsername", None): api = get_twitter_api(self.request.registry) properties["twitterUsernameId"] = get_userid_from_twitter(api, properties["twitterUsername"]) self.updateFields(properties) if self.get("twitterUsername", None) is None and self.get("twitterUsernameId", None) is not None: del self["twitterUsernameId"] # someone changed notifications settings for this context if "notifications" in properties: notifier = RabbitNotifications(self.request) if self.get("notifications", False): for user in self.subscribedUsers(): notifier.bind_user_to_context(self, user["username"]) else: for user in self.subscribedUsers(): notifier.unbind_user_from_context(self, user["username"]) if "url" in properties: self["hash"] = sha1(self["url"]).hexdigest() self.save()
def getContextAvatar(context, request): """ Get context avatar To the date, this is only implemented to work integrated with Twitter. """ chash = context['hash'] twitter_username = context['twitterUsername'] base_folder = request.registry.settings.get('avatar_folder') avatar_folder = get_avatar_folder(base_folder, 'contexts', chash) context_image_filename = '%s/%s' % (avatar_folder, chash) api = get_twitter_api(request.registry) if not os.path.exists(context_image_filename): download_twitter_user_image(api, twitter_username, context_image_filename) if os.path.exists(context_image_filename): # Calculate time since last download and set if we have to re-download or not modification_time = os.path.getmtime(context_image_filename) hours_since_last_modification = (time.time() - modification_time) / 60 / 60 if hours_since_last_modification > 3: download_twitter_user_image(api, twitter_username, context_image_filename) else: context_image_filename = '{}/missing-context.png'.format(base_folder) data = open(context_image_filename).read() image = Response(data, status_int=200) image.content_type = 'image/png' return image
def test_get_twitter_api_no_settings(self): """ Given an empty cloudapis settings When i try to get the twitter api object I get nothing in response """ from max.utils.twitter import get_twitter_api self.app.registry.cloudapis_settings['twitter'] = {} api = get_twitter_api(self.app.registry) self.assertEqual(api, None)
def test_get_twitter_api(self): """ Given a valid cloudapis settings When i try to get the api Then get the tweepy wrapper object And the expected methods are there """ from max.utils.twitter import get_twitter_api api = get_twitter_api(self.app.registry) self.assertTrue(hasattr(api, 'verify_credentials')) self.assertTrue(hasattr(api, 'get_user'))
def buildObject(self): super(Context, self).buildObject() # If creating with the twitterUsername, get its Twitter ID if self.data.get("twitterUsername", None): api = get_twitter_api(self.request.registry) self["twitterUsernameId"] = get_userid_from_twitter(api, self.data["twitterUsername"]) self["hash"] = self.getIdentifier() # Set displayName only if it's not specified self["displayName"] = self.get("displayName", self["url"])
def test_get_twitter_avatar(self): """ Given a valid cloudapis settings When i try to get a twitter user Then get a user object And the expected properties are there """ from max.utils.twitter import get_twitter_api from max.utils.twitter import download_twitter_user_image api = get_twitter_api(self.app.registry) imagefile = os.path.join(self.tempfolder, 'twitter.png') download_twitter_user_image(api, 'maxupcnet', imagefile) self.assertFileExists(imagefile)
def test_get_twitter_user(self): """ Given a valid cloudapis settings When i try to get a twitter user Then get a user object And the expected properties are there """ from max.utils.twitter import get_twitter_api from max.utils.twitter import get_userid_from_twitter api = get_twitter_api(self.app.registry) userid = get_userid_from_twitter(api, 'maxupcnet') self.assertEqual(userid, '526326641')