def process_response(self, server_url): response = utils.do_request(server_url, 'getUpdates', {'offset': self.offset}) if response and isinstance(response, dict) and response.get('ok'): updates = response['result'] if updates and isinstance(updates, list): logging.warning('got updates: %s', str(updates)) for update in updates: try: msg = update.get('message') msg_out = None if msg and 'text' in msg: msg_out = self.process_text_message(msg) if msg_out is not None: for msg in msg_out: action = 'sendPhoto' if 'photo' in msg else 'sendLocation' if 'latitude' in msg else 'sendMessage' if action == 'sendMessage': msg['disable_web_page_preview'] = True msg['parse_mode'] = 'Markdown' #logging.warning('sending: %s', str(msg)) utils.do_request(server_url, action, msg) except Exception as ex: logging.error('failed to process update: %s\n%s', str(update), str(ex)) raise self.offset = max(update['update_id']+1, self.offset) else: logging.error('failed to get updates: %s', str(response))
def process_response(self, server_url): response = utils.do_request(server_url, 'getUpdates', {'offset': self.offset}) if response and isinstance(response, dict) and response.get('ok'): updates = response['result'] if updates and isinstance(updates, list): logging.warning('got updates: %s', str(updates)) for update in updates: try: msg = update.get('message') msg_out = None if msg and 'text' in msg: msg_out = self.process_text_message(msg) if msg_out is not None: for msg in msg_out: action = 'sendPhoto' if 'photo' in msg else 'sendLocation' if 'latitude' in msg else 'sendMessage' if action == 'sendMessage': msg['disable_web_page_preview'] = True msg['parse_mode'] = 'Markdown' #logging.warning('sending: %s', str(msg)) utils.do_request(server_url, action, msg) except Exception as ex: logging.error('failed to process update: %s\n%s', str(update), str(ex)) raise self.offset = max(update['update_id'] + 1, self.offset) else: logging.error('failed to get updates: %s', str(response))
def _set_ideascale_counters(self): connector = self.cp_connector url_cb = get_url_cb(connector, 'get_ideas_cb') url = build_request_url(url_cb.url, url_cb.callback, {'initiative_id': self.testing_initiative.external_id}) resp = do_request(connector, url, url_cb.callback.method) ideas = get_json_or_error(connector.name, url_cb.callback, resp) self.num_initial_ideas_ideascale = len(ideas) url_cb = get_url_cb(connector, 'get_comments_cb') url = build_request_url(url_cb.url, url_cb.callback, {'initiative_id': self.testing_initiative.external_id}) resp = do_request(connector, url, url_cb.callback.method) comments = get_json_or_error(connector.name, url_cb.callback, resp) self.num_initial_comments_ideascale = len(comments)
def produce_question(self, url): self.question_queue.put(url) answers_url = self._parse_answer_url(url) answer_json_objs = do_request(answers_url) count = 100 while not answer_json_objs['paging']['is_end'] and count < 50: for obj in answer_json_objs['data']: self.answer_queue.put(obj['url']) count -= 1 url = answer_json_objs['paging']['next'] answer_json_objs = do_request(url)
def _initial(self): pull_data() push_data() connector = self.cp_connector url_cb = get_url_cb(connector, 'get_ideas_cb') url = build_request_url( url_cb.url, url_cb.callback, {'initiative_id': self.testing_initiative.external_id}) resp = do_request(connector, url, url_cb.callback.method) ideas = get_json_or_error(connector.name, url_cb.callback, resp) replicated_ideas = 0 for idea in ideas: if self._exist_platform_name_in_text(self.social_network.name, idea['text']): replicated_ideas += 1 self.assertEqual(replicated_ideas, self.num_initial_ideas_fb) url_cb = get_url_cb(connector, 'get_comments_cb') url = build_request_url( url_cb.url, url_cb.callback, {'initiative_id': self.testing_initiative.external_id}) resp = do_request(connector, url, url_cb.callback.method) comments = get_json_or_error(connector.name, url_cb.callback, resp) replicated_comments = 0 for comment in comments: if self._exist_platform_name_in_text(self.social_network.name, comment['text']): replicated_comments += 1 self.assertEqual(replicated_comments, self.num_initial_comments_fb) params = {'app': self.social_network} posts = call_social_network_api(self.social_network.connector, 'get_posts', params) replicated_ideas = 0 replicated_comments = 0 for post in posts: if self._exist_platform_name_in_text( self.consultation_platform.name, post['text']): replicated_ideas += 1 if 'comments_array' in post.keys(): for comment in post['comments_array']: if self._exist_platform_name_in_text( self.consultation_platform.name, comment['text']): replicated_comments += 1 if 'comments_array' in comment.keys(): for reply in comment['comments_array']: if self._exist_platform_name_in_text( self.consultation_platform.name, reply['text']): replicated_comments += 1 self.assertEqual(replicated_ideas, self.num_initial_ideas_ideascale) self.assertEqual(replicated_comments, self.num_initial_comments_ideascale)
def _clean_up_ideascale_community(self): connector = self.cp_connector url_cb = get_url_cb(connector, 'get_comments_cb') url = build_request_url(url_cb.url, url_cb.callback, {'initiative_id': self.testing_initiative.external_id}) resp = do_request(connector, url, url_cb.callback.method) comments = get_json_or_error(connector.name, url_cb.callback, resp) delayed_comments = [] # First delete replies to comments for comment in comments: if self._exist_platform_name_in_text(self.social_network.name, comment['text']): if comment['parent_type'] == 'idea': delayed_comments.append(comment) else: url_cb = get_url_cb(connector, 'delete_comment_cb') url = build_request_url(url_cb.url, url_cb.callback, {'comment_id': comment['id']}) do_request(connector, url, url_cb.callback.method) # Second delete comments to ideas for delayed_comment in delayed_comments: url_cb = get_url_cb(connector, 'delete_comment_cb') url = build_request_url(url_cb.url, url_cb.callback, {'comment_id': delayed_comment['id']}) do_request(connector, url, url_cb.callback.method) # Finally delete ideas url_cb = get_url_cb(connector, 'get_ideas_cb') url = build_request_url(url_cb.url, url_cb.callback, {'initiative_id': self.testing_initiative.external_id}) resp = do_request(connector, url, url_cb.callback.method) ideas = get_json_or_error(connector.name, url_cb.callback, resp) for idea in ideas: if self._exist_platform_name_in_text(self.social_network.name, idea['text']): url_cb = get_url_cb(connector, 'delete_idea_cb') url = build_request_url(url_cb.url, url_cb.callback, {'idea_id': idea['id']}) do_request(connector, url, url_cb.callback.method)
def _set_ideascale_counters(self): connector = self.cp_connector url_cb = get_url_cb(connector, 'get_ideas_cb') url = build_request_url( url_cb.url, url_cb.callback, {'initiative_id': self.testing_initiative.external_id}) resp = do_request(connector, url, url_cb.callback.method) ideas = get_json_or_error(connector.name, url_cb.callback, resp) self.num_initial_ideas_ideascale = len(ideas) url_cb = get_url_cb(connector, 'get_comments_cb') url = build_request_url( url_cb.url, url_cb.callback, {'initiative_id': self.testing_initiative.external_id}) resp = do_request(connector, url, url_cb.callback.method) comments = get_json_or_error(connector.name, url_cb.callback, resp) self.num_initial_comments_ideascale = len(comments)
def _create_connector(): ''' Creates a new connector ''' response = utils.do_request( f'{constants.KAFKA_CONNECT}/connectors', method='POST', data={ 'name': CONNECTOR_NAME, 'config': { 'connector.class': 'io.confluent.connect.jdbc.JdbcSourceConnector', 'key.converter': 'org.apache.kafka.connect.json.JsonConverter', 'key.converter.schemas.enable': 'false', 'value.converter': 'org.apache.kafka.connect.json.JsonConverter', 'value.converter.schemas.enable': 'false', 'batch.max.rows': 500, 'connection.url': constants.POSTGRESQL_CONN, 'connection.user': constants.POSTGRESQL_USER, 'connection.password': constants.POSTGRESQL_PASS, 'table.whitelist': 'stations', 'mode': 'incrementing', 'incrementing.column.name': 'stop_id', 'topic.prefix': constants.TOPIC_PREFIX, 'poll.interval.ms': 60 * 1000 # every minute } }) response.raise_for_status()
def run(self, month): self._set_weather(month) response = utils.do_request( f'{constants.REST_PROXY}/topics/{self.topic_name}', method='POST', headers={'Content-Type': 'application/vnd.kafka.avro.v2+json'}, data={ 'key_schema': json.dumps(Weather.key_schema), 'value_schema': json.dumps(Weather.value_schema), 'records': [{ 'value': { 'temperature': int(self.temp), 'status': self.status.name }, 'key': { 'timestamp': self.time_millis() } }] }) response.raise_for_status() logger.debug(f'weather is {self.temp}ºF {self.status.name}')
def _connector_exists(): ''' Checks if the connector exists ''' response = utils.do_request( f'{constants.KAFKA_CONNECT}/connectors/{CONNECTOR_NAME}') return response.status_code == 200
def _parse(self, json_objs): urls = [] for obj in json_objs['data']: t = obj.get('type') if t != 'roundtable': continue urls.append(obj['url']) questions_url = [u + '/questions?excerpt_len=75' for u in urls] for url in questions_url: objs = do_request(url) while not objs['paging']['is_end']: for obj in objs['data']: if obj['type'] != 'question': continue self.produce_question(obj['url']) next_url = objs['paging']['next'] objs = do_request(next_url)
def _initial(self): pull_data() push_data() connector = self.cp_connector url_cb = get_url_cb(connector, 'get_ideas_cb') url = build_request_url(url_cb.url, url_cb.callback, {'initiative_id': self.testing_initiative.external_id}) resp = do_request(connector, url, url_cb.callback.method) ideas = get_json_or_error(connector.name, url_cb.callback, resp) replicated_ideas = 0 for idea in ideas: if self._exist_platform_name_in_text(self.social_network.name, idea['text']): replicated_ideas += 1 self.assertEqual(replicated_ideas, self.num_initial_ideas_fb) url_cb = get_url_cb(connector, 'get_comments_cb') url = build_request_url(url_cb.url, url_cb.callback, {'initiative_id': self.testing_initiative.external_id}) resp = do_request(connector, url, url_cb.callback.method) comments = get_json_or_error(connector.name, url_cb.callback, resp) replicated_comments = 0 for comment in comments: if self._exist_platform_name_in_text(self.social_network.name, comment['text']): replicated_comments += 1 self.assertEqual(replicated_comments, self.num_initial_comments_fb) params = {'app': self.social_network} posts = call_social_network_api(self.social_network.connector, 'get_posts', params) replicated_ideas = 0 replicated_comments = 0 for post in posts: if self._exist_platform_name_in_text(self.consultation_platform.name, post['text']): replicated_ideas += 1 if 'comments_array' in post.keys(): for comment in post['comments_array']: if self._exist_platform_name_in_text(self.consultation_platform.name, comment['text']): replicated_comments += 1 if 'comments_array' in comment.keys(): for reply in comment['comments_array']: if self._exist_platform_name_in_text(self.consultation_platform.name, reply['text']): replicated_comments += 1 self.assertEqual(replicated_ideas, self.num_initial_ideas_ideascale) self.assertEqual(replicated_comments, self.num_initial_comments_ideascale)
def _parse(self, json_objs): urls = [] while not json_objs['paging']['is_end']: for obj in json_objs['data']: if obj['type'] == 'column': urls.append(obj['url']) url = json_objs['paging']['next'] json_objs = do_request(url) article_urls = [] for url in urls: url = self._get_article_urls(url) article_urls.append(url) for article_url in article_urls: json_objs = do_request(article_url) for obj in json_objs['data']: if obj['type'] != 'article': continue self.produce_article(obj['url'])
def _deletes(self, content_to_del): # 1. Delete idea created on ideascale url_cb = get_url_cb(self.cp_connector, 'delete_idea_cb') url = build_request_url(url_cb.url, url_cb.callback, {'idea_id': content_to_del['id_is_new_idea']}) do_request(self.cp_connector, url, url_cb.callback.method) # 2. Delete idea created on facebook params = { 'app': self.social_network, 'id_post': content_to_del['id_fb_new_idea'] } call_social_network_api(self.social_network.connector, 'delete_post', params) # 3. Delete comment created on ideascale url_cb = get_url_cb(self.cp_connector, 'delete_comment_cb') url = build_request_url( url_cb.url, url_cb.callback, {'comment_id': content_to_del['id_is_comment']}) do_request(self.cp_connector, url, url_cb.callback.method) # 4. Delete comment created on facebook params = { 'app': self.social_network, 'id_comment': content_to_del['id_fb_comment'] } call_social_network_api(self.social_network.connector, 'delete_comment', params) # Sync modifications pull_data() delete_data() # Check if the deletion of the idea created on ideascale was replicated (1) url_cb = get_url_cb(self.cp_connector, 'get_idea_cb') url = build_request_url(url_cb.url, url_cb.callback, {'idea_id': content_to_del['id_is_new_idea']}) resp = do_request(self.cp_connector, url, url_cb.callback.method) self.assertEqual(resp.status_code, 400) # Check if the deletion of the idea created on facebook was replicated (2) params = { 'app': self.social_network, 'id_post': content_to_del['id_fb_new_idea'] } del_post = call_social_network_api(self.social_network.connector, 'get_post', params) self.assertIsNone(del_post) # Check if the deletion of the comment created on ideascale was replicated (3) url_cb = get_url_cb(self.cp_connector, 'get_comment_cb') url = build_request_url( url_cb.url, url_cb.callback, {'comment_id': content_to_del['id_is_comment']}) resp = do_request(self.cp_connector, url, url_cb.callback.method) self.assertEqual(resp.status_code, 400) # Check if the deletion of the comment created on facebook was replicated (4) params = { 'app': self.social_network, 'id_post': content_to_del['id_fb_comment'] } del_comment = call_social_network_api(self.social_network.connector, 'get_post', params) self.assertIsNone(del_comment)
def _start(self, url, direction): while True: try: json_objs = do_request(url) if json_objs['paging']['is_end']: self._logger.info('end reached, sleep for 10 mins!') time.sleep(600) url = self._start_url json_objs = do_request(url) continue self._parse(json_objs) url = json_objs['paging'][direction] except: exstr = traceback.format_exc() self._logger.error(exstr) continue finally: time.sleep(10)
def _clean_up_ideascale_community(self): connector = self.cp_connector url_cb = get_url_cb(connector, 'get_comments_cb') url = build_request_url( url_cb.url, url_cb.callback, {'initiative_id': self.testing_initiative.external_id}) resp = do_request(connector, url, url_cb.callback.method) comments = get_json_or_error(connector.name, url_cb.callback, resp) delayed_comments = [] # First delete replies to comments for comment in comments: if self._exist_platform_name_in_text(self.social_network.name, comment['text']): if comment['parent_type'] == 'idea': delayed_comments.append(comment) else: url_cb = get_url_cb(connector, 'delete_comment_cb') url = build_request_url(url_cb.url, url_cb.callback, {'comment_id': comment['id']}) do_request(connector, url, url_cb.callback.method) # Second delete comments to ideas for delayed_comment in delayed_comments: url_cb = get_url_cb(connector, 'delete_comment_cb') url = build_request_url(url_cb.url, url_cb.callback, {'comment_id': delayed_comment['id']}) do_request(connector, url, url_cb.callback.method) # Finally delete ideas url_cb = get_url_cb(connector, 'get_ideas_cb') url = build_request_url( url_cb.url, url_cb.callback, {'initiative_id': self.testing_initiative.external_id}) resp = do_request(connector, url, url_cb.callback.method) ideas = get_json_or_error(connector.name, url_cb.callback, resp) for idea in ideas: if self._exist_platform_name_in_text(self.social_network.name, idea['text']): url_cb = get_url_cb(connector, 'delete_idea_cb') url = build_request_url(url_cb.url, url_cb.callback, {'idea_id': idea['id']}) do_request(connector, url, url_cb.callback.method)
def execute_statement(): ''' Executes the KSQL statement against the KSQL API ''' if utils.topic_exists(constants.TOPIC_TURNSTILE_SUMMARY): return logger.debug('executing ksql statement...') response = utils.do_request( f'{constants.KSQL}/ksql', method='POST', headers={'Content-Type': 'application/vnd.ksql.v1+json'}, data={ 'ksql': KSQL_STATEMENT, 'streamsProperties': { 'ksql.streams.auto.offset.reset': 'earliest' } }) response.raise_for_status()
def _deletes(self, content_to_del): # 1. Delete idea created on ideascale url_cb = get_url_cb(self.cp_connector, 'delete_idea_cb') url = build_request_url(url_cb.url, url_cb.callback, {'idea_id': content_to_del['id_is_new_idea']}) do_request(self.cp_connector, url, url_cb.callback.method) # 2. Delete idea created on facebook params = {'app': self.social_network, 'id_post': content_to_del['id_fb_new_idea']} call_social_network_api(self.social_network.connector, 'delete_post', params) # 3. Delete comment created on ideascale url_cb = get_url_cb(self.cp_connector, 'delete_comment_cb') url = build_request_url(url_cb.url, url_cb.callback, {'comment_id': content_to_del['id_is_comment']}) do_request(self.cp_connector, url, url_cb.callback.method) # 4. Delete comment created on facebook params = {'app': self.social_network, 'id_comment': content_to_del['id_fb_comment']} call_social_network_api(self.social_network.connector, 'delete_comment', params) # Sync modifications pull_data() delete_data() # Check if the deletion of the idea created on ideascale was replicated (1) url_cb = get_url_cb(self.cp_connector, 'get_idea_cb') url = build_request_url(url_cb.url, url_cb.callback, {'idea_id':content_to_del['id_is_new_idea']}) resp = do_request(self.cp_connector, url, url_cb.callback.method) self.assertEqual(resp.status_code, 400) # Check if the deletion of the idea created on facebook was replicated (2) params = {'app': self.social_network, 'id_post': content_to_del['id_fb_new_idea']} del_post = call_social_network_api(self.social_network.connector, 'get_post', params) self.assertIsNone(del_post) # Check if the deletion of the comment created on ideascale was replicated (3) url_cb = get_url_cb(self.cp_connector, 'get_comment_cb') url = build_request_url(url_cb.url, url_cb.callback, {'comment_id':content_to_del['id_is_comment']}) resp = do_request(self.cp_connector, url, url_cb.callback.method) self.assertEqual(resp.status_code, 400) # Check if the deletion of the comment created on facebook was replicated (4) params = {'app': self.social_network, 'id_post': content_to_del['id_fb_comment']} del_comment = call_social_network_api(self.social_network.connector, 'get_post', params) self.assertIsNone(del_comment)
def _modifications(self): # 1. Add new comment to a facebook idea idea = Idea.objects.get(text='Terere Machine') idea_mod_1 = idea url_cb = get_url_cb(self.cp_connector, 'create_comment_idea_cb') url = build_request_url(url_cb.url, url_cb.callback, {'idea_id': idea.cp_id}) params = {'text': 'Testing comment!'} body_param = build_request_body(self.cp_connector, url_cb.callback, params) resp = do_request(self.cp_connector, url, url_cb.callback.method, body_param) new_comment_is = get_json_or_error(self.cp_connector.name, url_cb.callback, resp) # 2. Add new comment to an ideascale idea idea = Idea.objects.get(cp_id='721262') idea_mod_2 = idea url_cb = get_url_cb(self.cp_connector, 'create_comment_idea_cb') url = build_request_url(url_cb.url, url_cb.callback, {'idea_id': idea.cp_id}) params = {'text': 'Testing comment 2!'} body_param = build_request_body(self.cp_connector, url_cb.callback, params) do_request(self.cp_connector, url, url_cb.callback.method, body_param) # 3. Vote on an idea idea = Idea.objects.get(cp_id='718882') idea_mod_3 = idea if idea.positive_votes > 0: idea_vote_value = -1 else: idea_vote_value = 1 url_cb = get_url_cb(self.cp_connector, 'vote_idea_cb') url = build_request_url(url_cb.url, url_cb.callback, {'idea_id': idea.cp_id}) params = {'value': idea_vote_value} body_param = build_request_body(self.cp_connector, url_cb.callback, params) do_request(self.cp_connector, url, url_cb.callback.method, body_param) # 4. Add new comment to a facebook idea idea = Idea.objects.get(sn_id='437938162969648_817265021703625') idea_mod_4 = idea params = {'app': self.social_network, 'id_post': idea.sn_id, 'message': 'Testing comment 3!'} new_comment_fb = call_social_network_api(self.social_network.connector, 'comment_post', params) # 5. Add new comment to an ideascale idea idea = Idea.objects.get(title='Testing param') idea_mod_5 = idea params = {'app': self.social_network, 'id_post': idea.sn_id, 'message': 'Testing comment 4!'} call_social_network_api(self.social_network.connector, 'comment_post', params) # 6. Create new idea on facebook params = {'app': self.social_network, 'message': 'Testing Idea!'} new_idea_fb = call_social_network_api(self.social_network.connector, 'publish_post', params) # 7. Create new idea on ideascale url_cb = get_url_cb(self.cp_connector, 'create_idea_cb') url = build_request_url(url_cb.url, url_cb.callback, {'initiative_id': self.testing_initiative.external_id}) params = {'title': 'Testing Idea to be deleted', 'text': 'Testing Idea to be deleted', 'campaign_id': self.testing_initiative.campaign_set.all()[0].external_id} body_param = build_request_body(self.cp_connector, url_cb.callback, params) resp = do_request(self.cp_connector, url, url_cb.callback.method, body_param) new_idea_is = get_json_or_error(self.cp_connector.name, url_cb.callback, resp) # Sync modifications pull_data() push_data() # Check if modification 1) was replicated on facebook idea = Idea.objects.get(text='Terere Machine') params = {'app': self.social_network, 'id_post': idea.sn_id} post = call_social_network_api(self.social_network.connector, 'get_post', params) self.assertEqual(len(post['comments_array']), idea_mod_1.comments+1) # Check if modification 2) was replicated on facebook idea = Idea.objects.get(cp_id='721262') params = {'app': self.social_network, 'id_post': idea.sn_id} post = call_social_network_api(self.social_network.connector, 'get_post', params) self.assertEqual(len(post['comments_array']), idea_mod_2.comments+1) # Check if modification 3) was replicated on facebook idea = Idea.objects.get(cp_id='718882') params = {'app': self.social_network, 'id_post': idea.sn_id} post = call_social_network_api(self.social_network.connector, 'get_post', params) if idea_vote_value == 1: self.assertEqual(post['positive_votes'], idea_mod_3.positive_votes+1) else: self.assertEqual(post['positive_votes'], idea_mod_3.positive_votes-1) # Check if modification 4) was replicated on ideascale idea = Idea.objects.get(sn_id='437938162969648_817265021703625') url_cb = get_url_cb(self.cp_connector, 'get_idea_cb') url = build_request_url(url_cb.url, url_cb.callback, {'idea_id': idea.cp_id}) resp = do_request(self.cp_connector, url, url_cb.callback.method) idea_is = get_json_or_error(self.cp_connector.name, url_cb.callback, resp) self.assertEqual(idea_is['comments'], idea_mod_4.comments+1) # Check if modification 5) was replicated on ideascale idea = Idea.objects.get(title='Testing param') url = build_request_url(url_cb.url, url_cb.callback, {'idea_id': idea.cp_id}) resp = do_request(self.cp_connector, url, url_cb.callback.method) idea_is = get_json_or_error(self.cp_connector.name, url_cb.callback, resp) self.assertEqual(idea_is['comments'], idea_mod_5.comments+1) return {'id_fb_new_idea': new_idea_fb['id'], 'id_fb_idea': idea_mod_4.sn_id, 'id_fb_comment': new_comment_fb['id'], 'id_is_new_idea': new_idea_is['id'], 'id_is_idea': idea_mod_4.cp_id, 'id_is_comment': new_comment_is['id']}
def _modifications(self): # 1. Add new comment to a facebook idea idea = Idea.objects.get(text='Terere Machine') idea_mod_1 = idea url_cb = get_url_cb(self.cp_connector, 'create_comment_idea_cb') url = build_request_url(url_cb.url, url_cb.callback, {'idea_id': idea.cp_id}) params = {'text': 'Testing comment!'} body_param = build_request_body(self.cp_connector, url_cb.callback, params) resp = do_request(self.cp_connector, url, url_cb.callback.method, body_param) new_comment_is = get_json_or_error(self.cp_connector.name, url_cb.callback, resp) # 2. Add new comment to an ideascale idea idea = Idea.objects.get(cp_id='721262') idea_mod_2 = idea url_cb = get_url_cb(self.cp_connector, 'create_comment_idea_cb') url = build_request_url(url_cb.url, url_cb.callback, {'idea_id': idea.cp_id}) params = {'text': 'Testing comment 2!'} body_param = build_request_body(self.cp_connector, url_cb.callback, params) do_request(self.cp_connector, url, url_cb.callback.method, body_param) # 3. Vote on an idea idea = Idea.objects.get(cp_id='718882') idea_mod_3 = idea if idea.positive_votes > 0: idea_vote_value = -1 else: idea_vote_value = 1 url_cb = get_url_cb(self.cp_connector, 'vote_idea_cb') url = build_request_url(url_cb.url, url_cb.callback, {'idea_id': idea.cp_id}) params = {'value': idea_vote_value} body_param = build_request_body(self.cp_connector, url_cb.callback, params) do_request(self.cp_connector, url, url_cb.callback.method, body_param) # 4. Add new comment to a facebook idea idea = Idea.objects.get(sn_id='437938162969648_817265021703625') idea_mod_4 = idea params = { 'app': self.social_network, 'id_post': idea.sn_id, 'message': 'Testing comment 3!' } new_comment_fb = call_social_network_api(self.social_network.connector, 'comment_post', params) # 5. Add new comment to an ideascale idea idea = Idea.objects.get(title='Testing param') idea_mod_5 = idea params = { 'app': self.social_network, 'id_post': idea.sn_id, 'message': 'Testing comment 4!' } call_social_network_api(self.social_network.connector, 'comment_post', params) # 6. Create new idea on facebook params = {'app': self.social_network, 'message': 'Testing Idea!'} new_idea_fb = call_social_network_api(self.social_network.connector, 'publish_post', params) # 7. Create new idea on ideascale url_cb = get_url_cb(self.cp_connector, 'create_idea_cb') url = build_request_url( url_cb.url, url_cb.callback, {'initiative_id': self.testing_initiative.external_id}) params = { 'title': 'Testing Idea to be deleted', 'text': 'Testing Idea to be deleted', 'campaign_id': self.testing_initiative.campaign_set.all()[0].external_id } body_param = build_request_body(self.cp_connector, url_cb.callback, params) resp = do_request(self.cp_connector, url, url_cb.callback.method, body_param) new_idea_is = get_json_or_error(self.cp_connector.name, url_cb.callback, resp) # Sync modifications pull_data() push_data() # Check if modification 1) was replicated on facebook idea = Idea.objects.get(text='Terere Machine') params = {'app': self.social_network, 'id_post': idea.sn_id} post = call_social_network_api(self.social_network.connector, 'get_post', params) self.assertEqual(len(post['comments_array']), idea_mod_1.comments + 1) # Check if modification 2) was replicated on facebook idea = Idea.objects.get(cp_id='721262') params = {'app': self.social_network, 'id_post': idea.sn_id} post = call_social_network_api(self.social_network.connector, 'get_post', params) self.assertEqual(len(post['comments_array']), idea_mod_2.comments + 1) # Check if modification 3) was replicated on facebook idea = Idea.objects.get(cp_id='718882') params = {'app': self.social_network, 'id_post': idea.sn_id} post = call_social_network_api(self.social_network.connector, 'get_post', params) if idea_vote_value == 1: self.assertEqual(post['positive_votes'], idea_mod_3.positive_votes + 1) else: self.assertEqual(post['positive_votes'], idea_mod_3.positive_votes - 1) # Check if modification 4) was replicated on ideascale idea = Idea.objects.get(sn_id='437938162969648_817265021703625') url_cb = get_url_cb(self.cp_connector, 'get_idea_cb') url = build_request_url(url_cb.url, url_cb.callback, {'idea_id': idea.cp_id}) resp = do_request(self.cp_connector, url, url_cb.callback.method) idea_is = get_json_or_error(self.cp_connector.name, url_cb.callback, resp) self.assertEqual(idea_is['comments'], idea_mod_4.comments + 1) # Check if modification 5) was replicated on ideascale idea = Idea.objects.get(title='Testing param') url = build_request_url(url_cb.url, url_cb.callback, {'idea_id': idea.cp_id}) resp = do_request(self.cp_connector, url, url_cb.callback.method) idea_is = get_json_or_error(self.cp_connector.name, url_cb.callback, resp) self.assertEqual(idea_is['comments'], idea_mod_5.comments + 1) return { 'id_fb_new_idea': new_idea_fb['id'], 'id_fb_idea': idea_mod_4.sn_id, 'id_fb_comment': new_comment_fb['id'], 'id_is_new_idea': new_idea_is['id'], 'id_is_idea': idea_mod_4.cp_id, 'id_is_comment': new_comment_is['id'] }
def _do_job(cls): while True: url = cls._queue.get() json_obj = do_request(url) obj = cls(json_obj) obj.save()