def insert_ingested_assets(ingested_assets, campaign_id, channel_id, creative_map): """Insert assets(adgroups and tiles) to database, upload creatives to S3 if neccessary Note that the S3 uploading and database operations are interleaved in this function. The image_uri and enhanced_image_uri for each tile are set as the key (URL) on S3. """ # a function scope cache to speedup the uploading of duplicate creatives s3_key_cache = dict() bucket, headers = setup_s3() with session_scope() as session: for key, value in ingested_assets.iteritems(): # at least two items in the value, i.e. one adgroup and 1+ tiles assert(len(value) >= 2) adgroup, tiles = value[0], value[1:] inserted_adgroup = insert_adgroup(session, adgroup) for tile in tiles: image, ext = creative_map[tile["image_uri"]] tile["image_uri"] = \ upload_creative_to_s3(image, ext, bucket, headers, s3_key_cache) image, ext = creative_map[tile["enhanced_image_uri"]] tile["enhanced_image_uri"] = \ upload_creative_to_s3(image, ext, bucket, headers, s3_key_cache) tile["adgroup_id"] = inserted_adgroup["id"] insert_tile(session, tile)
def test_put_account(self): """Test updating an account via API (PUT).""" new_account_data = { 'name': 'New Account Name', 'contact_name': 'New Contact Name', 'contact_email': '*****@*****.**', 'contact_phone': '(123) 456-7890', } # A PUT to a non existent account should 404. url = url_for('api.account.account', account_id=1) data = json.dumps(new_account_data) response = self.client.put(url, data=data, content_type='application/json') assert_equal(response.status_code, 404) # Create an account. with session_scope() as session: account_id = insert_account(session, self.account_data)['id'] # Update the account with the new data. url = url_for('api.account.account', account_id=account_id) response = self.client.put(url, data=data, content_type='application/json') assert_equal(response.status_code, 200) # Verify the data. account = get_account(account_id) del account['id'] del account['created_at'] assert_equal(account, new_account_data)
def test_put_campaign(self): """Test updating a campaign via API (PUT).""" from flask_restful.inputs import datetime_from_iso8601 new_campaign_data = { 'name': 'New Campaign Name', 'paused': True, 'account_id': 2, 'channel_id': 2, 'countries': ['CA', 'FR'], 'start_date': '2015-10-30T14:04:55+00:00', 'end_date': '2015-12-30T14:04:55+00:00', } # Create a new campaign. with session_scope() as session: campaign_id = insert_campaign(session, self.campaign_data)['id'] # Update the campaign with the new data. url = url_for('api.campaign.campaign', campaign_id=campaign_id) data = json.dumps(new_campaign_data) response = self.client.put(url, data=data, content_type='application/json') assert_equal(response.status_code, 200) # Verify the data. campaign = get_campaign(campaign_id) for field in [ 'name', 'account_id', 'channel_id', 'paused', 'countries' ]: assert_equal(campaign[field], new_campaign_data[field]) for field in ['start_date', 'end_date']: assert_equal(campaign[field], datetime_from_iso8601(new_campaign_data[field]))
def _sync_to_database(content_record, new_content, new_version, original_url, original_hash, pub_key, freeze): version_record = { "original_url": original_url, "original_hash": original_hash, "signing_key": pub_key } with session_scope() as session: if new_content: # added a new content and its first version content_record["version"] = new_version version_record["version"] = new_version content_record["versions"] = [version_record] content_rec = insert_content(session, content_record) # this inserts the new version as well version_rec = content_rec.pop("versions") else: # just worked on an existing content if freeze: # modified an existing version version_rec = update_version(session, content_record['id'], new_version, version_record) content_rec = content_record content_rec.pop("versions") # pop versions before sending it back to the user else: # added a new version content_record["version"] = new_version version_record["version"] = new_version content_record.pop("versions") # content update doesn't need this field content_rec = update_content(session, content_record['id'], content_record) version_rec = insert_version(session, content_record['id'], version_record) return content_rec, version_rec
def test_put_campaign(self): """Test updating a campaign via API (PUT).""" from flask_restful.inputs import datetime_from_iso8601 new_campaign_data = { 'name': 'New Campaign Name', 'paused': True, 'account_id': 2, 'channel_id': 2, 'countries': ['CA', 'FR'], 'start_date': '2015-10-30T14:04:55+00:00', 'end_date': '2015-12-30T14:04:55+00:00', } # Create a new campaign. with session_scope() as session: campaign_id = insert_campaign(session, self.campaign_data)['id'] # Update the campaign with the new data. url = url_for('api.campaign.campaign', campaign_id=campaign_id) data = json.dumps(new_campaign_data) response = self.client.put(url, data=data, content_type='application/json') assert_equal(response.status_code, 200) # Verify the data. campaign = get_campaign(campaign_id) for field in ['name', 'account_id', 'channel_id', 'paused', 'countries']: assert_equal(campaign[field], new_campaign_data[field]) for field in ['start_date', 'end_date']: assert_equal(campaign[field], datetime_from_iso8601(new_campaign_data[field]))
def insert_ingested_assets(ingested_assets, campaign_id, channel_id, creative_map): """Insert assets(adgroups and tiles) to database, upload creatives to S3 if neccessary Note that the S3 uploading and database operations are interleaved in this function. The image_uri and enhanced_image_uri for each tile are set as the key (URL) on S3. """ # a function scope cache to speedup the uploading of duplicate creatives s3_key_cache = dict() bucket, headers = setup_s3() with session_scope() as session: for key, value in ingested_assets.iteritems(): # at least two items in the value, i.e. one adgroup and 1+ tiles assert (len(value) >= 2) adgroup, tiles = value[0], value[1:] inserted_adgroup = insert_adgroup(session, adgroup) for tile in tiles: image, ext = creative_map[tile["image_uri"]] tile["image_uri"] = \ upload_creative_to_s3(image, ext, bucket, headers, s3_key_cache) image, ext = creative_map[tile["enhanced_image_uri"]] tile["enhanced_image_uri"] = \ upload_creative_to_s3(image, ext, bucket, headers, s3_key_cache) tile["adgroup_id"] = inserted_adgroup["id"] insert_tile(session, tile)
def post(self): args = self.reqparse.parse_args() try: with session_scope() as session: new = insert_adgroup(session, args) except InvalidRequestError as e: return {"message": e.message}, 400 else: return {"result": marshal(new, adgroup_fields)}, 201
def post(self): """Creates an account.""" args = self.reqparse.parse_args() try: with session_scope() as session: new = insert_account(session, args) except IntegrityError as e: return {'message': e.message}, 400 else: return {'result': marshal(new, account_fields)}, 201
def post(self): """Creates a campaign.""" args = self.reqparse_post.parse_args() try: with session_scope() as session: new = insert_campaign(session, args) except IntegrityError: return {'message': 'Invalid account_id or channel_id provided.'}, 400 else: return {'result': marshal(new, campaign_fields)}, 201
def put(self, account_id): args = self.reqparse.parse_args() try: with session_scope() as session: account = update_account(session, account_id, args) except NoResultFound as e: return {'message': e.message}, 404 except IntegrityError as e: return {'message': e.message}, 400 else: return {'result': marshal(account, account_fields)}, 200
def put(self, adgroup_id): args = self.reqparse.parse_args() try: with session_scope() as session: adgroup = update_adgroup(session, adgroup_id, args) except NoResultFound as e: return {"message": e.message}, 404 except InvalidRequestError as e: return {"message": e.message}, 400 else: return {"result": marshal(adgroup, adgroup_fields)}, 200
def put(self, campaign_id): """updates the campaign with given campaign_id.""" args = self.reqparse.parse_args() try: with session_scope() as session: campaign = update_campaign(session, campaign_id, args) except NoResultFound as e: return {'message': e.message}, 404 except IntegrityError as e: return {'message': 'Invalid account_id or channel_id provided.'}, 400 return {'result': marshal(campaign, campaign_fields)}, 200
def put(self, tile_id): args = self.reqparse.parse_args() try: jsonschema.validate(args, API_TILE_SCHEMA_PUT) with session_scope() as session: tile = update_tile(session, tile_id, args) except jsonschema.exceptions.ValidationError as e: return {"message": e.message}, 400 except NoResultFound as e: return {"message": e.message}, 404 else: return {"result": marshal(tile, tile_fields)}, 200
def post(self): """Creates a campaign.""" args = self.reqparse_post.parse_args() try: with session_scope() as session: new = insert_campaign(session, args) except IntegrityError: return { 'message': 'Invalid account_id or channel_id provided.' }, 400 else: return {'result': marshal(new, campaign_fields)}, 201
def test_get_campaign_by_id(self): """Test getting the details of a specific campaign via API (GET).""" # Create a new campaign. with session_scope() as session: campaign_id = insert_campaign(session, self.campaign_data)['id'] # Verify the API returns it with the right data. url = url_for('api.campaign.campaign', campaign_id=campaign_id) response = self.client.get(url) assert_equal(response.status_code, 200) resp = json.loads(response.data) campaign = resp['result'] for field in ['name', 'account_id', 'channel_id', 'countries', 'paused']: assert_equal(campaign[field], self.campaign_data[field])
def put(self, campaign_id): """updates the campaign with given campaign_id.""" args = self.reqparse.parse_args() try: with session_scope() as session: campaign = update_campaign(session, campaign_id, args) except NoResultFound as e: return {'message': e.message}, 404 except IntegrityError as e: return { 'message': 'Invalid account_id or channel_id provided.' }, 400 return {'result': marshal(campaign, campaign_fields)}, 200
def handler_content_resign_all(): succeeded, failed = [], [] for content in get_contents(): try: new_pub_key = resign_content(content) updated_record = {"signing_key": new_pub_key} with session_scope() as session: for version in content["versions"]: update_version(session, content["id"], version["version"], updated_record) except Exception as e: failed.append("%s: %s" % (content['name'], e)) else: succeeded.append(content['name']) return jsonify(succeeded=succeeded, failed=failed)
def test_get_campaign_by_id(self): """Test getting the details of a specific campaign via API (GET).""" # Create a new campaign. with session_scope() as session: campaign_id = insert_campaign(session, self.campaign_data)['id'] # Verify the API returns it with the right data. url = url_for('api.campaign.campaign', campaign_id=campaign_id) response = self.client.get(url) assert_equal(response.status_code, 200) resp = json.loads(response.data) campaign = resp['result'] for field in [ 'name', 'account_id', 'channel_id', 'countries', 'paused' ]: assert_equal(campaign[field], self.campaign_data[field])
def test_get_accounts(self): """Test getting the list of accounts via API (GET).""" # Initially, there are no accounts. API should return an empty list. url = url_for('api.account.accounts') response = self.client.get(url) assert_equal(response.status_code, 200) assert_equal(len(json.loads(response.data)['results']), 0) # Create two accounts. with session_scope() as session: insert_account(session, self.account_data) insert_account(session, {'name': 'Another Account'}) # Verify two accounts are returned. response = self.client.get(url) assert_equal(response.status_code, 200) assert_equal(len(json.loads(response.data)['results']), 2)
def post(self): """ HTTP end point to create new tile. Note the initial status of a new tile is always set as 'unapproved' """ args = self.reqparse.parse_args() # the status of new tiles are alway set as unapproved args["status"] = "unapproved" try: # validate background color, url, and image fields again jsonschema.validate(args, API_TILE_SCHEMA_POST) with session_scope() as session: new = insert_tile(session, args) except jsonschema.exceptions.ValidationError as e: return {"message": e.message}, 400 except InvalidRequestError as e: return {"message": e.message}, 400 else: return {"result": marshal(new, tile_fields)}, 201
def test_get_account_by_id(self): """Test getting the details of a specific account via API (GET).""" # Initially, there are no accounts. API should return a 404. account_id = 1 url = url_for('api.account.account', account_id=account_id) response = self.client.get(url) assert_equal(response.status_code, 404) # Create an account. with session_scope() as session: account_id = insert_account(session, self.account_data)['id'] # Verify the API returns it with the right data. url = url_for('api.account.account', account_id=account_id) response = self.client.get(url) assert_equal(response.status_code, 200) resp = json.loads(response.data) account = resp['result'] del account['id'] del account['created_at'] assert_equal(account, self.account_data)
def _sync_to_database(content_record, new_content, new_version, original_url, original_hash, pub_key, freeze): version_record = { "original_url": original_url, "original_hash": original_hash, "signing_key": pub_key } with session_scope() as session: if new_content: # added a new content and its first version content_record["version"] = new_version version_record["version"] = new_version content_record["versions"] = [version_record] content_rec = insert_content( session, content_record) # this inserts the new version as well version_rec = content_rec.pop("versions") else: # just worked on an existing content if freeze or new_version == content_record.get("version"): # modified an existing version version_rec = update_version(session, content_record['id'], new_version, version_record) content_rec = content_record content_rec.pop( "versions" ) # pop versions before sending it back to the user else: # added a new version content_record["version"] = new_version version_record["version"] = new_version content_record.pop( "versions") # content update doesn't need this field content_rec = update_content(session, content_record['id'], content_record) version_rec = insert_version(session, content_record['id'], version_record) return content_rec, version_rec
def test_put_account_only_phone(self): """Test updating the phone number of an account.""" new_account_data = { 'contact_phone': '123456789', } # Create an account. with session_scope() as session: account_id = insert_account(session, self.account_data)['id'] # Update the account with the new data. url = url_for('api.account.account', account_id=account_id) data = json.dumps(new_account_data) response = self.client.put(url, data=data, content_type='application/json') assert_equal(response.status_code, 200) # Verify the data. Make sure the fields not sent don't get updated (nulled). account = get_account(account_id) assert_equal(account['name'], self.account_data['name']) assert_equal(account['contact_name'], self.account_data['contact_name']) assert_equal(account['contact_email'], self.account_data['contact_email']) assert_equal(account['contact_phone'], new_account_data['contact_phone'])