def subscribe_real_time_updates(self, request, queryset): try: for obj in queryset: if not obj.callback_real_time_updates or not obj.field_real_time_updates or \ not obj.object_real_time_updates or not obj.page_id: self.message_user(request, 'Please indicate the page id, callback, field and object', level=messages.WARNING) else: if not obj.subscribed_read_time_updates: connector = obj.connector token = hashlib.sha1(os.urandom(128)).hexdigest() obj.token_real_time_updates = token obj.save() post_data = {'object': obj.object_real_time_updates, 'fields': obj.field_real_time_updates, 'callback_url': obj.callback_real_time_updates, 'verify_token': token} try: params = {'app': obj, 'data': post_data} call_social_network_api(connector, 'subscribe_real_time_updates', params) obj.subscribed_read_time_updates = True obj.save() self.message_user(request, 'Successful subscription to receive real time updates') except ConnectorError as e: self.message_user(request, e.reason, level=messages.ERROR) else: self.message_user(request, 'Please delete the current subscription to receive real time ' 'updates') except AppError as e: self.message_user(request, e.reason, level=messages.ERROR)
def _pull_content_social_network(social_network, initiative): if social_network.community: params = {'app': social_network} posts = call_social_network_api(social_network.connector, 'get_posts', params) for post in posts: ret_data = save_sn_post(social_network, post, initiative) if ret_data: if 'comments_array' in post.keys(): for comment in post['comments_array']: save_sn_comment(social_network, comment) # Process comment's replies if 'comments_array' in comment.keys(): for reply in comment['comments_array']: save_sn_comment(social_network, reply) # Process comment's votes if 'votes_array' in comment.keys(): for vote in comment['votes_array']: save_sn_vote(social_network, vote) # Save/Update votes if 'votes_array' in post.keys(): for vote in post['votes_array']: save_sn_vote(social_network, vote) else: logger.warning( 'Cannot pull content from the social network {}. Reason: Community not found' .format(social_network.name))
def _pull_content_social_network(social_network, initiative): if social_network.community: params = {'app': social_network} posts = call_social_network_api(social_network.connector, 'get_posts', params) for post in posts: ret_data = save_sn_post(social_network, post, initiative) if ret_data: if 'comments_array' in post.keys(): for comment in post['comments_array']: save_sn_comment(social_network, comment) # Process comment's replies if 'comments_array' in comment.keys(): for reply in comment['comments_array']: save_sn_comment(social_network, reply) # Process comment's votes if 'votes_array' in comment.keys(): for vote in comment['votes_array']: save_sn_vote(social_network, vote) # Save/Update votes if 'votes_array' in post.keys(): for vote in post['votes_array']: save_sn_vote(social_network, vote) else: logger.warning('Cannot pull content from the social network {}. Reason: Community not found'. format(social_network.name))
def delete_subscription_real_time_updates(self, request, queryset): for obj in queryset: if obj.subscribed_read_time_updates: connector = obj.connector post_data = {'object': obj.object_real_time_updates} try: params = {'app': obj, 'data': post_data} call_social_network_api(connector, 'delete_subscription_real_time_updates', params) obj.subscribed_read_time_updates = False obj.token_real_time_updates = None obj.save() self.message_user(request, 'Successful delete of the subscription to receive real time updates') except ConnectorError as e: self.message_user(request, e.reason, level=messages.ERROR) else: self.message_user(request, 'The social network has not been subscribed to receive real time update', level=messages.WARNING)
def subscribe_real_time_updates(self, request, queryset): try: for obj in queryset: if not obj.callback_real_time_updates or not obj.field_real_time_updates or \ not obj.object_real_time_updates or not obj.page_id: self.message_user( request, 'Please indicate the page id, callback, field and object', level=messages.WARNING) else: if not obj.subscribed_read_time_updates: connector = obj.connector token = hashlib.sha1(os.urandom(128)).hexdigest() obj.token_real_time_updates = token obj.save() post_data = { 'object': obj.object_real_time_updates, 'fields': obj.field_real_time_updates, 'callback_url': obj.callback_real_time_updates, 'verify_token': token } try: params = {'app': obj, 'data': post_data} call_social_network_api( connector, 'subscribe_real_time_updates', params) obj.subscribed_read_time_updates = True obj.save() self.message_user( request, 'Successful subscription to receive real time updates' ) except ConnectorError as e: self.message_user(request, e.reason, level=messages.ERROR) else: self.message_user( request, 'Please delete the current subscription to receive real time ' 'updates') except AppError as e: self.message_user(request, e.reason, level=messages.ERROR)
def save_model(self, request, obj, form, change): # Get long-lived access token params = {'app_id': obj.snapp.app_id, 'app_secret': obj.snapp.app_secret, 'access_token': obj.access_token} try: ret_token = call_social_network_api(obj.snapp.connector, 'get_long_lived_access_token', params) obj.access_token = ret_token['access_token'] obj.access_token_exp = calculate_token_expiration_time(ret_token['expiration']) obj.save() messages.success(request, 'It was successfully obtained the user\'s long-lived access token') except ConnectorError: messages.error(request, 'It was not possible to get the user\'s long-lived access token')
def delete_subscription_real_time_updates(self, request, queryset): for obj in queryset: if obj.subscribed_read_time_updates: connector = obj.connector post_data = {'object': obj.object_real_time_updates} try: params = {'app': obj, 'data': post_data} call_social_network_api( connector, 'delete_subscription_real_time_updates', params) obj.subscribed_read_time_updates = False obj.token_real_time_updates = None obj.save() self.message_user( request, 'Successful delete of the subscription to receive real time updates' ) except ConnectorError as e: self.message_user(request, e.reason, level=messages.ERROR) else: self.message_user( request, 'The social network has not been subscribed to receive real time update', level=messages.WARNING)
def save_model(self, request, obj, form, change): # Get long-lived access token params = { 'app_id': obj.snapp.app_id, 'app_secret': obj.snapp.app_secret, 'access_token': obj.access_token } try: ret_token = call_social_network_api(obj.snapp.connector, 'get_long_lived_access_token', params) obj.access_token = ret_token['access_token'] obj.access_token_exp = calculate_token_expiration_time( ret_token['expiration']) obj.save() messages.success( request, 'It was successfully obtained the user\'s long-lived access token' ) except ConnectorError: messages.error( request, 'It was not possible to get the user\'s long-lived access token' )