Example #1
0
    def setup_targetvalues(self, exchange):
        target_values = [
            [dimensions.g_location, 'USA'],
            [dimensions.g_location, 'USA', 'Ohio'],
            [dimensions.g_location, 'USA', 'Ohio', 'New Pittsburgh'],
            [dimensions.g_location, 'USA', 'Wisconsin'],
            [dimensions.g_location, 'USA', 'Wisconsin', 'New Richmond'],
            [dimensions.g_location, 'USA', 'New York'],
            [dimensions.g_location, 'USA', 'New York', 'New York'],
            [dimensions.g_os, 'Android'],
            [dimensions.g_os, 'Nokia OS'],
            [dimensions.g_os, 'Symbian'],
            [dimensions.g_device, 'Samsung'],
            [dimensions.g_device, 'Sony'],
            [dimensions.g_device, 'LG'],
            [dimensions.g_device, 'Samsung', 'Samsung Galaxy S'],
            [dimensions.g_device, 'Sony', 'Sony Xperia Z'],
            [dimensions.g_device, 'LG', 'Lg Swift L9'],
            [dimensions.carrier, 'WiFi'],
            [dimensions.carrier, '3G'],
            [dimensions.carrier, 'EDGE'],
            [dimensions.carrier, 'GPRS'],
            [dimensions.os_version, 'Samsung Galaxy S'],
            [dimensions.os_version, 'v11 beta']
        ]

        for row in target_values:
            category, values = row[0], row[1:]
            value = TargetMap.pack(values)
            TargetValue.objects.get_or_create(exchange=exchange, category=category, value=value)
Example #2
0
def test_searchengine_location_order_by_rank(transactional_db):
    '''
    Test if search results for location are ordered by hierarchy
    - countries should be first, then states and cities
    '''

    countries = [['California']]
    # Should there be a country with the same name as city, or state
    # it should be higher in search results

    states = [['USA', 'California']]

    cities = [['USA', 'Pensylvania', 'California'],
              ['USA', 'Missouri', 'California'],
              ['USA', 'Washington', 'California'],
              ['USA', 'California', 'Los Angeles'],
              ['USA', 'California', 'Miami'],
              ['USA', 'California', 'San Diego']]

    locations = cities + countries + states

    for location in locations:
        TargetValue.objects.create(category=dimensions.g_location,
                                   value=TargetMap.pack(location))

    call_command('rebuild_index', interactive=False)

    query = SearchQuerySet().filter(django_ct='targeting.targetvalue',
                                    category=dimensions.g_location,
                                    value="California").order_by('rank')

    values_all = [l.object.value_list for l in query]

    assert values_all[0] == countries[0]
    assert values_all[1] == states[0]
Example #3
0
def device_mock_data(db):
    from ui.targeting.models import TargetMap, TargetValue

    targeting = [
        (None, dimensions.gender, ['Male'], True),
        (None, dimensions.age_group, [age_group('17')], True),
        (None, dimensions.g_location, ['USA', 'New York', 'Manhattan'], True),
    ]

    target_values = []
    for exchange, category, value, include in targeting:
        target_values.append(({
            'exchange': exchange,
            'category': category,
            'value': TargetMap.pack(value),
        }, include))

    values = [
        ['targeting_values', TargetValue, target_values],
    ]
    test_data = [['appnexus', '12:30', '15:30', values, 'default'],
                 ['appnexus', '16:00', '18:00', values, 'default'],
                 ['facebook', '12:30', '15:30', values, 'facebook_sidebar'],
                 ['facebook', '12:30', '15:30', values, 'facebook_newsfeed']]
    return test_data
Example #4
0
    def populate_tv_ptv(cls):
        for exchange, category, value, pk, category in cls.query_iter(
                cls.SQL.tv_ptv):
            if exchange is None:
                continue
            value_list = TargetMap.unpack(value)

            if category == dimensions.g_publisher:
                # Mirroring query:
                # ui.targeting.models.PublisherTargetValueManager.get_or_create_multi_pk
                cls.set('publisher_value', exchange, value_list, pk)
            else:
                # Mirroring query:
                # ui.targeting.models.TargetValueManager.get_or_create_multi_pk
                cls.set('target_value', exchange, category, value_list, pk)
Example #5
0
def test_searchengine_notfound_results(app, kclient, searchurl, values):
    ''' Tests if some another random world does not return any results '''

    TargetValue.objects.all().delete()
    for data in values:
        TargetValue.objects.create(**data)

    call_command('rebuild_index', interactive=False)

    for query in values:
        category = query['category']
        ctx = dict(group=category)
        reverse_query = ''.join(TargetMap.unpack(query['value'])[::-1])
        results = get_search_results(kclient, searchurl, reverse_query, ctx)
        assert len(results['objects']) == 0
Example #6
0
    def setup_targetvalues(self, targetvalues, targetvalue_class=None):
        '''
        Setup targetingValues for given base class

        :param list targetvalues: list of dictionaries
            [{
                'category': some_category_name,
                'exchange': exchange_instance,
                'values': ('aaa', 'bb'),
                'represented_values': ('foo', 'bar')
            },]
            represented_values can be optional.

            .. note::

                 represented_values will equal values this should create
                 unmoderated targetvalues

                 dictionaries without represented_values will result in self
                 representant
                 other case will create standard representant/represented
                 element

        :param class targetvalue_class: class type that will be used to create
            TargetValue. Default will result in TargetValue being used

        '''

        from ui.targeting.models import TargetValue, TargetMap
        if not targetvalue_class:
            targetvalue_class = TargetValue

        created = []
        for row in targetvalues:
            value = TargetMap.pack(row['values'])
            target, _ = targetvalue_class.objects.get_or_create(
                exchange=row['exchange'],
                category=row['category'],
                value=value)

            if row.get('represented_values'):
                targetvalue_class.objects.create_value_with_representant(
                    row['represented_values'], target)
            created.append(target)

        return created
Example #7
0
def test_searchengine_found_results(app, kclient, searchurl, values):
    '''Tests if search engine works for given values/query strings'''
    TargetValue.objects.all().delete()
    for data in values:
        TargetValue.objects.create(**data)

    call_command('rebuild_index', interactive=False)

    assert SearchQuerySet().all().count() == 2

    for query in values:
        category = query['category']
        ctx = dict(group=category)
        for query_str in TargetMap.unpack(query['value']):
            results = get_search_results(kclient, searchurl, query_str, ctx)
            assert SearchQuerySet().all().count() > 0
            assert len(results['objects']) > 0
            for result in results['objects']:
                assert query_str in result['value']
Example #8
0
    def prepare_value(self, target_value):
        '''
        Add hierarchy name to location target values, to enable filtering
        by hierarchy

        Example:
        'USA' -> 'USA country'
        'USA;Wisconsin' -> 'USA;Wisconsin state'
        'USA;Pensylvania;California' -> ''USA;Pensylvania;California city'
        '''
        if not target_value.category == dimensions.g_location:
            return target_value.value

        # Hierarchy names which are appended to index values
        location_hierarchies = ['country', 'state', 'city']

        values = list(target_value.value_list)
        last_index = len(values) - 1
        hierarchy = location_hierarchies[last_index]
        values[last_index] = "%s %s" % (values[last_index], hierarchy)
        return TargetMap.pack(values)
Example #9
0
def test_searchengine_location_with_hierarchy(transactional_db):
    '''
    Test if search results for location are limited to given dimension
    if query contains it
    '''
    def filter_locations(value=None):
        filters = {
            'django_ct': 'targeting.targetvalue',
            'category': dimensions.g_location
        }
        if value:
            filters['value'] = value
        return SearchQuerySet().filter(**filters)

    states = (['USA', 'California'], )
    cities = (['USA', 'California',
               'Los Angeles'], ['USA', 'Pensylvania', 'California'],
              ['USA', 'Missouri',
               'California'], ['USA', 'Arkansas', 'California'])

    for location in states + cities:
        TargetValue.objects.create(category=dimensions.g_location,
                                   value=TargetMap.pack(location))

    call_command('rebuild_index', interactive=False)

    search_all = \
        [l.object.value_list for l in filter_locations('california')]
    assert sorted(search_all) == sorted(states + cities)

    search_state = \
        [l.object.value_list for l in filter_locations('california state')]
    assert sorted(search_state) == sorted(states)

    search_city = \
        [l.object.value_list for l in filter_locations('california city')]
    assert sorted(search_city) == sorted(cities)
def test_location_appnexus_translation(
        clean_targeting, location, tv_count, representant):
    original_location, _ = TargetValue.objects.get_or_create_representant(
        dimensions.g_location, location, 'appnexus')
    translate_countries_and_states_from_csv()

    assert TargetValue.objects.count() == tv_count

    translate_countries_and_states_from_csv()

    assert TargetValue.objects.count() == tv_count

    location_translated = TargetValue.objects.get(
        category=dimensions.g_location,
        value=TargetMap.pack(representant)
    )

    assert location_translated.is_representant is True
    # we expect to translated having a representant
    # only if we expect it to be translated
    original_location = TargetValue.objects.get(pk=original_location.pk)
    is_translated = location != representant
    assert original_location.has_representant == is_translated
    assert original_location.is_representant != is_translated
Example #11
0
import csv
import json

import pytest
from django.core.management import call_command
from django.conf import settings as django_settings

from haystack.query import SearchQuerySet
from etc import dimensions
from etc.config import settings
from bidrequest.constants import EXCHANGES
from ui.targeting.models import TargetValue, TargetMap, ContentCategory
from ui.targeting.targetcache import TargetCache

TARGET_VALUES = (dict(category=dimensions.g_location,
                      value=TargetMap.pack(['BARUSA', 'FOO', 'new foo'])),
                 dict(category=dimensions.g_device,
                      value=TargetMap.pack(['kanary', 'mobile'])))

QUERY_MOCK_DATA = (('/api/targeting/%(group)s/search/', TARGET_VALUES), )


def get_search_results(client, url, query, context):
    url = url + '?q=' + query
    url = url % context
    return json.loads(client.get(url).content)


@pytest.mark.parametrize('searchurl, values', QUERY_MOCK_DATA)
def test_searchengine_found_results(app, kclient, searchurl, values):
    '''Tests if search engine works for given values/query strings'''
Example #12
0
def insert_targetvalues(target_values, exchange):
    for category, values in target_values:
        value = TargetMap.pack(values)
        TargetValue.objects.get_or_create(exchange=exchange,
                                          category=category,
                                          value=value)
def translate_countries_and_states_from_csv():
    """
    Translates yet untranslated Appnexus target values reading mappings from CSV files.

    # Replaces country short code like 'CA' with full name like 'Canada'
    # Replaces USA state short code like 'ME' with 'Maine'
    """
    # Read mappings from file
    countries = {}
    regions_usa = {}

    with open(csv_dir / 'iso3166.csv') as countries_file,\
            open(csv_dir / 'states.csv') as us_states_file:
        for country in csv.DictReader(countries_file, ['code', 'name']):
            countries[country['code']] = country['name']

        for state in csv.DictReader(us_states_file, ['name', 'code']):
            regions_usa[state['code']] = state['name']

    locations = TargetValue.objects.filter(
        category=dimensions.g_location,  # location
        exchange=EXCHANGES.appnexus,  # appnexus
        representant__isnull=True,  # it is representant
        represented__isnull=True  # but does not represent anything
    )

    log.info('[TRANSLATE_LOCATIONS] Found %s locations that need to be translated' % len(locations))
    translated_count = 0
    for location in locations:
        country_code = location.value_dict.get(dimensions.country)
        country_name = countries.get(country_code)
        region_code = location.value_dict.get(dimensions.region)
        region_name = regions_usa.get(region_code)

        location_values = location.value_list
        print len(location_values)

        # Set full name for country
        if country_name:
            location_values[0] = country_name
        else:
            log.info('[TRANSLATE_LOCATIONS] Can\'t translate location %s' % location)
            # No values can be translated, skip translation
            continue

        # Set full name for US region
        if country_code == 'US' and region_code:
            if not region_name:
                log.info('[TRANSLATE_LOCATIONS] Can\'t translate US region %s' % region_code)
            else:
                location_values[1] = region_name

        # Create exchange agnostic value
        representant, create = TargetValue.objects.get_or_create(
            exchange=None,
            category=dimensions.g_location,
            value=TargetMap.pack(location_values)
        )
        if representant.is_representant:
            location.make_represented(representant)
            location.save()
            translated_count += 1
        else:
            log.info('[TRANSLATE_LOCATIONS] Can\'t translate {0}, because {1} is not representant'.format(
                location, representant))

    log.info('[TRANSLATE_LOCATIONS] Succesfully translated %s out of %s locations' % (translated_count, len(locations)))
Example #14
0
def report_data(raw_table, overview, report_name, start_date, end_date,
                dimension, running_objects, strategy_obj):
    """
    Provides full data for report

    :param list raw_table: report table query result
    :param dict overview: dict of aggregated metrics (same metrics as in single table row)
    :param str report_name: report name 'Campaign' or 'Strategy'
    :param datetime start_date: start date used for the query
    :param datetime end_date: end date used for the query
    :param str dimension: name of dimension used for the query
    :param list running_objects: ids of objects currently running
    :param object strategy_obj: strategy object or None for campaign
    """
    def metrics_dict(row):
        return {metric: row[metric] for metric in METRICS.keys()}

    # this flag tells that we should display or hide whitelist/blacklist buttons
    dimension_from_targeting = strategy_obj and dimension in Strategy.DIMENSIONS_TO_FIELDS_MAP.keys(
    )

    targeting_exclude_values = []
    report_related_field = None

    if dimension_from_targeting:
        # checking included and excluded values from given dimension
        report_related_field = Strategy.DIMENSIONS_TO_FIELDS_MAP.get(dimension)
        targeting_exclude_values = getattr(strategy_obj, '%s_exclude' % report_related_field)\
                                        .values_list('id', flat=True)
        if report_related_field == 'targeting_values':
            # adding filter by category
            targeting_exclude_values = targeting_exclude_values.filter(
                category=dimensions.dim_groups[dimension])

    table = []
    for row in raw_table:
        # Label can be null.
        packed_label = row.get('label') or 'Unknown'
        # Family line of a label can be an empty string.
        label = TargetMap.family_line(packed_label) or 'Unknown'

        table.append([{
            'pk':
            row['object_id'],
            'label':
            label,
            'is_running':
            running_objects is None or row['object_id'] in running_objects,
            'blacklist':
            row['object_id'] in targeting_exclude_values,
        },
                      metrics_dict(row)])

    result = {
        'date_range': {
            'start': start_date.isoformat(),
            'end': end_date.isoformat(),
        },
        'dimension': dimension,
        'dimension_from_targeting': dimension_from_targeting,
        'report_related_field': report_related_field,
        'level': report_name,
        'overview': metrics_dict(overview),
        'table': table,
    }

    return {'response': result}
Example #15
0
def test_appnexus_profile_update(clean_strategy):
    '''
    1. Checking if celery task is setting the correct data through API.
    2. Checking if celery task is fired (by cache mapper) after changing targeting in strategy
    '''

    # undeleting one strategy
    strategy = Strategy.objects.all()[0]
    strategy.is_paused = False
    strategy.save()

    expected_response = nothing_set_profile.copy()
    expected_response['description'] = 'Automatic profile (on)'
    expected_response[
        'passthrough_percent'] = settings.appnexus_profile_passthrough
    expected_response['supply_type_targets'] = list(
        SUPPLY_TYPES[strategy.type])

    # checking ads sizes
    expected_response['size_targets'] = [{'height': 90, 'width': 728}]
    check_update_response(expected_response)

    # setting targeting on USA
    usa_appnexus = TargetValue.objects.create(
        exchange=EXCHANGES.appnexus,
        category=dimensions.g_location,
        value=TargetMap.pack(['USA', None, None]),
    )
    strategy.targeting_values.add(usa_appnexus)

    # setting targeting on Germany in Nexage
    germany_nexage = TargetValue.objects.create(
        exchange=EXCHANGES.nexage,
        category=dimensions.g_location,
        value=TargetMap.pack(['Germany', None, None]),
    )

    # setting AppNexus representant for Nexage country
    germany_appnexus = TargetValue.objects.create(
        exchange=EXCHANGES.appnexus,
        category=dimensions.g_location,
        value=TargetMap.pack(['GER', None, None]),
        representant=germany_nexage)

    # setting targeting on Germany from Nexage
    strategy.targeting_values.add(germany_nexage)

    # checking countries targeting
    expected_response['country_action'] = 'include'
    expected_response['country_targets'] = [{
        'country': 'GER'
    }, {
        'country': 'USA'
    }]
    check_update_response(expected_response)

    # changing country to region
    germany_nexage.value = TargetMap.pack(['Germany', 'LT', None])
    germany_nexage.save()
    germany_appnexus.value = TargetMap.pack(['GER', 'LT', None])
    germany_appnexus.save()

    # setting AppNexus representant for Nexage region in USA
    # (chenging country to region)
    usa_appnexus.value = TargetMap.pack(['USA', 'FL', None])
    usa_appnexus.save()

    expected_response['region_action'] = 'include'
    expected_response['region_targets'] = [{
        'region': 'USA:FL'
    }, {
        'region': 'GER:LT'
    }]
    check_update_response(expected_response)

    # setting targeting on Proximic Safety Level segment
    FAKE_APPNEXUS_ID = 330728
    strategy.segment_proximic_safety_level.add(
        SegmentProximicSafetyLevel.objects.create(appnexus_id=FAKE_APPNEXUS_ID,
                                                  name='Safe from Adult'))

    # checking segments targeting
    expected_response['segment_boolean_operator'] = 'or'
    expected_response['segment_targets'] = [{
        'id': FAKE_APPNEXUS_ID,
        'action': 'include'
    }]
    check_update_response(expected_response)

    # checking if celery task is triggered (inside cache mapper)
    # after changes in strategy
    with patch('ui.campaign.tasks.appnexus_update_profile.delay') as mock_task:
        assert mock_task.call_count == 0
        # strategy changes, we should update AppNexus profile
        trigger_cache_mappings()
        assert mock_task.call_count == 1
        # no changes - no task
        trigger_cache_mappings()
        assert mock_task.call_count == 1

    # Remove all adverts from strategies and check if profile is empty
    for strategy in Strategy.objects.all():
        for advert in strategy.advert_set.all():
            advert.delete()

    check_update_response(nothing_set_profile)
Example #16
0
    def handle(self, *args, **options):

        self.stdout.write('Filling campaigns with fake data')

        # Account
        a, _ = Account.objects.get_or_create(
            name='Test account',
            timezone='UTC',
        )

        u, _ = User.objects.get_or_create(
            username='******',
            email='*****@*****.**',
            account=a,
            is_signup_complete=True
        )
        u.set_password('kanary')
        u.save()

        account2, _ = Account.objects.get_or_create(
            name='Gravity4 DSP',
            timezone='UTC',
        )

        gravity_u, _ = User.objects.get_or_create(
            username='******',
            email='*****@*****.**',
            account=a,
            is_signup_complete=True
        )
        gravity_u.set_password('gravity')
        gravity_u.save()

        admin, _ = User.objects.get_or_create(
            username='******',
            email='*****@*****.**',
            account=a,
            is_signup_complete=True,
            is_staff=True,
            is_active=True,
            is_superuser=True,
        )
        admin.set_password('admin')
        admin.save()

        # Events

        EventCategory.objects.get_or_create(name='Basic information')
        EventCategory.objects.get_or_create(name='Warnings')

        # Creatives
        path = os.path.join(settings.MEDIA_ROOT, 'creatives', '')

        if not os.path.exists(path):
            os.makedirs(path)

        image_name = self.download_placeholder(125, 125, path)
        co1, _ = CreativeImage.objects.get_or_create(
            name='Image One',
            owner=a,
            width=125,
            height=125,
            defaults={'image': "creatives/%s" % image_name},
            appnexus_status='a',
        )

        image_name = self.download_placeholder(125, 125, path)
        co2, _ = CreativeImage.objects.get_or_create(
            name='Image 2',
            owner=a,
            width=125,
            height=125,
            defaults={'image': "creatives/%s" % image_name},
            appnexus_status='a'
        )

        image_name = self.download_placeholder(125, 125, path)
        co3, _ = CreativeImage.objects.get_or_create(
            name='Image Three',
            owner=a,
            width=125,
            height=125,
            defaults={'image': "creatives/%s" % image_name},
            appnexus_status='p',
            appnexus_audit_blocked_no_funds=True
        )

        image_name = self.download_placeholder(125, 125, path)
        co4, _ = CreativeImage.objects.get_or_create(
            name='Image rejected',
            owner=a,
            width=125,
            height=125,
            appnexus_status='r',
            defaults={'image': "creatives/%s" % image_name},
        )
        image_name = self.download_placeholder(125, 125, path)
        co5, _ = CreativeImage.objects.get_or_create(
            name='Image expired',
            owner=a,
            width=125,
            height=125,
            appnexus_status='e',
            defaults={'image': "creatives/%s" % image_name},
        )

        co6, _ = CreativeImage.objects.get_or_create(
            name='Image audited',
            owner=a,
            width=125,
            height=125,
            appnexus_status='a',
            defaults={'image': "creatives/%s" % image_name},
        )

        # Video
        video_name = 'video_creative.mpg'
        self.upload_video(video_name, path)

        video1, _ = CreativeVideo.objects.get_or_create(
            name='video_active',
            owner=a,
            width=480,
            height=720,
            file='creatives/%s' % video_name,
            liverail_id=214900,  # Real uploaded video on LiveRail. Features a lovely shell session.
            duration=10,
            media_variants=[  # Real liverail-converted media.
                {
                    u'bitrate': 736,
                    u'duration': 10,
                    u'height': 360,
                    u'type': u'video/mp4',
                    u'url': u'http://cdn.liverail.com/adasset4/62531/64866/214900/lo.mp4',
                    u'width': 480
                }, {
                    u'bitrate': 1472,
                    u'duration': 10,
                    u'height': 480,
                    u'type': u'video/mp4',
                    u'url': u'http://cdn.liverail.com/adasset4/62531/64866/214900/me.mp4',
                    u'width': 640
                }, {
                    u'bitrate': 3072,
                    u'duration': 10,
                    u'height': 720,
                    u'type': u'video/mp4',
                    u'url': u'http://cdn.liverail.com/adasset4/62531/64866/214900/hi.mp4',
                    u'width': 960
                }, {
                    u'bitrate': 736,
                    u'duration': 10,
                    u'height': 360,
                    u'type': u'video/webm',
                    u'url': u'http://cdn.liverail.com/adasset4/62531/64866/214900/lo.webm',
                    u'width': 480
                }, {
                    u'bitrate': 1472,
                    u'duration': 10,
                    u'height': 480,
                    u'type': u'video/webm',
                    u'url': u'http://cdn.liverail.com/adasset4/62531/64866/214900/me.webm',
                    u'width': 640
                }, {
                    u'bitrate': 3072,
                    u'duration': 10,
                    u'height': 720,
                    u'type': u'video/webm',
                    u'url': u'http://cdn.liverail.com/adasset4/62531/64866/214900/hi.webm',
                    u'width': 960
                }, {
                    u'bitrate': 736,
                    u'duration': 10,
                    u'height': 360,
                    u'type': u'video/x-flv',
                    u'url': u'http://cdn.liverail.com/adasset4/62531/64866/214900/lo.flv',
                    u'width': 480
                }, {
                    u'bitrate': 1472,
                    u'duration': 10,
                    u'height': 480,
                    u'type': u'video/x-flv',
                    u'url': u'http://cdn.liverail.com/adasset4/62531/64866/214900/me.flv',
                    u'width': 640
                }, {
                    u'bitrate': 3072,
                    u'duration': 10,
                    u'height': 720,
                    u'type': u'video/x-flv',
                    u'url': u'http://cdn.liverail.com/adasset4/62531/64866/214900/hi.flv',
                    u'width': 960
                }, {
                    u'bitrate': 736,
                    u'duration': 10,
                    u'height': 360,
                    u'type': u'video/x-ms-wmv',
                    u'url': u'http://cdn.liverail.com/adasset4/62531/64866/214900/lo.wmv',
                    u'width': 480
                }, {
                    u'bitrate': 1472,
                    u'duration': 10,
                    u'height': 480,
                    u'type': u'video/x-ms-wmv',
                    u'url': u'http://cdn.liverail.com/adasset4/62531/64866/214900/me.wmv',
                    u'width': 640
                }, {
                    u'bitrate': 3072,
                    u'duration': 10,
                    u'height': 720,
                    u'type': u'video/x-ms-wmv',
                    u'url': u'http://cdn.liverail.com/adasset4/62531/64866/214900/hi.wmv',
                    u'width': 960
                }
            ]
        )
        # set required liverail status, which gets set to pending by post_save signal
        video1.liverail_status = LiverailStatus.active
        video1.save()

        video2, _ = CreativeVideo.objects.get_or_create(
            name='video_pending',
            owner=a,
            file='creatives/%s' % video_name,
            liverail_status=LiverailStatus.pending
        )

        # Audiences
        audience1, _ = Audience.objects.get_or_create(
            name="First audience",
            owner=a
        )
        audience2, _ = Audience.objects.get_or_create(
            name="Second audience",
            owner=a
        )

        # Sites
        site, _ = Site.objects.get_or_create(
            owner=a,
            url='http://www.onet.pl'
        )
        Site.objects.get_or_create(
            owner=a,
            url='http://www.wp.pl'
        )
        Site.objects.get_or_create(
            owner=a,
            url='http://www.google.com'
        )

        # Campaigns

        now = datetime.datetime.utcnow()
        campaign_1, _ = Campaign.objects.get_or_create(
            account=a,
            name='Test Campaign One',
            budget_total=Decimal('8000.'),
            landing_site=site,
            id_random='0',
            defaults={
                'start_UTC': now - datetime.timedelta(days=10),
                'end_UTC': now + datetime.timedelta(days=5)
            }
        )

        campaign_2, _ = Campaign.objects.get_or_create(
            account=a,
            name='Test Campaign Two',
            budget_total=Decimal('8000.'),
            landing_site=site,
            id_random='1',
            defaults={
                'start_UTC': now - datetime.timedelta(days=2),
                'end_UTC': now + datetime.timedelta(days=30)
            }
        )

        campaign_3, _ = Campaign.objects.get_or_create(
            account=a,
            name='Test Campaign Three',
            budget_total=Decimal('8000.'),
            landing_site=site,
            id_random='2',
            defaults={
                'start_UTC': now + datetime.timedelta(days=2),
                'end_UTC': now + datetime.timedelta(days=300)
            }
        )

        conversion_1, _ = Conversion.objects.get_or_create(
            campaign=campaign_1)

        conversion_2, _ = Conversion.objects.get_or_create(
            campaign=campaign_2)

        conversion_3, _ = Conversion.objects.get_or_create(
            campaign=campaign_3)

        nexage = EXCHANGES.nexage
        smaato = EXCHANGES.smaato
        appnexus = EXCHANGES.appnexus

        strategy_1, _ = Strategy.objects.get_or_create(
            campaign=campaign_1,
            name='Test Strategy One',
            id_random='0',

            defaults={
                'budget_total': Decimal('1000.'),
                'budget_bid_CPM': Decimal('2'),
                'is_day_parted': True
            }
        )

        site_ratio, _ = SiteRatio.objects.get_or_create(
            site=Site.objects.all()[1],
            ratio=1,
            custom_variable='?test=2',
        )

        site_ratio_2, _ = SiteRatio.objects.get_or_create(
            site=Site.objects.all()[1],
            ratio=1,
            custom_variable='?test=3',
        )

        strategy_1.landing_sites.add(site_ratio, site_ratio_2)

        strategy_2, _ = Strategy.objects.get_or_create(
            campaign=campaign_2,
            name='Test Strategy Two',
            type=Strategy.MOBILE,
            defaults={
                'budget_total': Decimal('1200.'),
                'budget_daily': Decimal('120.'),
                'budget_bid_CPM': Decimal('5'),
                'id_random': '1',
            }
        )

        strategy_2_2, _ = Strategy.objects.get_or_create(
            campaign=campaign_2,
            name='Test Strategy Two II',
            defaults={
                'budget_total': Decimal('1500.'),
                'budget_daily': Decimal('150.'),
                'budget_bid_CPM': Decimal('5'),
                'id_random': '1_1',
            }
        )

        strategy_2.landing_sites.add(site_ratio, site_ratio_2)

        strategy_2_2.landing_sites.add(site_ratio, site_ratio_2)

        strategy_3, _ = Strategy.objects.get_or_create(
            campaign=campaign_3,
            name='Test Strategy Three',
            type=Strategy.MOBILE,
            defaults={
                'budget_total': Decimal('2500.'),
                'budget_daily': Decimal('300.'),
                'budget_bid_CPM': Decimal('3'),
                'id_random': '2',
            }
        )

        strategy_3.landing_sites.add(site_ratio, site_ratio_2)

        Payment.objects.create(
            account=a,
            datetime=datetime.datetime.utcnow(),
            amount=5000
        )

        Payment.objects.create(
            account=a,
            datetime=datetime.datetime.utcnow(),
            amount=700
        )

        # Bidding periods

        time1 = datetime.time(14, 30)
        time2 = datetime.time(16, 0)

        biddingperiod_1, _ = BiddingPeriod.objects.get_or_create(
            strategy=strategy_1,
            start=time1,
            end=time2
        )

        adverts_str_1 = [
            Advert.objects.create(
                strategy=strategy_1,
                creative=co1,
                landing_site=site,
                custom_variable='?test=%s' % n,
                id_random=str(n)
            ) for n in range(11)
        ]

        adverts_str_2 = [
            Advert.objects.create(
                strategy=strategy_2,
                creative=co2,
                landing_site=site,
                custom_variable='?test=%s' % n,
                id_random=str(n)
            ) for n in range(11, 13)
        ]

        adverts_str_2_2 = [
            Advert.objects.create(
                strategy=strategy_2_2,
                creative=co2,
                landing_site=site,
                custom_variable='?test=%s' % n,
                id_random=str(n)
            ) for n in range(13, 15)
        ]

        adverts_str_3 = [
            Advert.objects.create(
                strategy=strategy_3,
                creative=co3,
                landing_site=site,
                custom_variable='?test=%s' % n,
                id_random=str(n)
            ) for n in range(16, 20)
        ]

        adverts = adverts_str_1 + adverts_str_2 + adverts_str_2_2 + adverts_str_3

        creative_period_1, _ = AdvertPeriod.objects.get_or_create(
            advert=adverts[0],
            period=biddingperiod_1,
            custom_bid_CPM=2,
        )

        # Targeting
        self.setup_targetvalues(nexage)

        # publishers
        segments = [
            (95002, 'Brand Protection Data::Non-standard Content::Alcohol (proximic)'),
            (94999, 'Brand Protection Data::Non-standard Content::Adult (proximic)'),
            (94435, 'Automotive::Auto Parts (proximic)'),
        ]

        for segment in segments:
            AppNexusSegment.objects.create(
                appnexus_id=segment[0],
                name=segment[1]
            )

        publisherset_app, _ = PublisherSet.objects.get_or_create(
            owner=a, name='Awsome app publishers', inventory='app'
        )
        publisherset_web, _ = PublisherSet.objects.get_or_create(
            owner=a, name='Awsome web publishers', inventory='site'
        )

        publishers = [
            [dimensions.g_publisher, appnexus,
                ('Application', 'Rubicon'),
                ('app', '23'),
                ['720x90', '50x50'],
                ['below', 'above']],
            [dimensions.g_publisher, appnexus,
                ('Web', 'Rubicon'),
                ('site', '23'),
                ['720x100', '640x80'],
                ['above']],
            [dimensions.g_publisher, smaato,
                ('Web', 'Smaato'),
                ('site', constants.SMAATO_NETWORK_ID),
                ['720x100', '640x80'], [1]],
            [dimensions.g_publisher, smaato,
                ('Application', 'Smaato'),
                ('app', constants.SMAATO_NETWORK_ID),
                ['340x50'], [2]],
            [dimensions.g_publisher, nexage,
                ('Application', 'Nexage'),
                ('app', constants.NEXAGE_NETWORK_ID),
                ['340x50'], [1]],
            [dimensions.g_publisher, nexage,
                ('Web', 'Nexage'),
                ('site', constants.NEXAGE_NETWORK_ID),
                ['560x50'], [2]],
            [dimensions.g_publisher, appnexus,
                ('Application', 'Rubicon', 'Ninja Fruit'),
                ('app', '23', 'Ninja Fruit'),
                ['800x100', '640x80'],
                ['above']],
            [dimensions.g_publisher, appnexus,
                ('Web', 'Rubicon', 'rafalmichal.com'),
                ('site', '23', 'rafalmichal.com'),
                ['80x30'],
                ['below']],
            [dimensions.g_publisher, nexage,
                ('Application', 'Nexage', 'Ninja Fruit'),
                ('app', constants.NEXAGE_NETWORK_ID, 'Ninja Fruit'),
                ['640x80'], [2]],
            [dimensions.g_publisher, nexage,
                ('Web', 'Nexage', 'rafalmichal.com'),
                ('site', constants.NEXAGE_NETWORK_ID, 'rafalmichal.com'),
                ['450x80'], [3]],
            [dimensions.g_publisher, smaato,
                ('Application', 'Smaato', 'Ninja Fruit'),
                ('app', constants.SMAATO_NETWORK_ID, 'Ninja Fruit'),
                ['640x80'], [2]],
            [dimensions.g_publisher, smaato,
                ('Web', 'Smaato', 'rafalmichal.com'),
                ('site', constants.SMAATO_NETWORK_ID, 'rafalmichal.com'),
                ['450x80'], [3]],
        ]

        for row in publishers:
            category, exchange, values, representant_values, sizes, positions = row
            value = TargetMap.pack(values)
            target, _ = PublisherTargetValue.objects.get_or_create(
                exchange=exchange,
                category=category,
                value=value
            )

            if representant_values:
                # add restrictions
                segment = AppNexusSegment.objects.brand_protection().order_by('?')[0]
                target.sizes = sizes
                target.positions = positions
                target.save()
                target.segments.add(segment)

                PublisherTargetValue.objects.create_value_with_representant(
                    representant_values, target)

            if target.key == dimensions.network:
                continue

            if target.inventory_key == 'app':
                publisherset_app.target_values.add(target)
            else:
                publisherset_web.target_values.add(target)

        # domains
        domains = [
            u'greenfelder.biz', u'uptonschulist.com', u'haag.info',
            u'watsica.com', u'sipeshand.com', u'emard.com', u'larson.biz',
            u'stracke.org', u'lockman.com', u'jakubowski.com', u'littel.org',
            u'stark.com', u'schmitt.com', u'mitchell.net', u'goodwin.com',
            u'okeefe.net', u'treutel.org', u'friesenkiehn.net', u'lynch.org',
            u'kris.org', u'walsh.com', u'murazik.com', u'mclaughlin.com',
            u'ratkerohan.net', u'jacobikuvalis.com', u'funk.com', u'hills.org',
            u'ornharris.info', u'bartonoconnell.com', u'hintzrau.com',
            u'lubowitz.com', u'sawayn.info', u'moen.com', u'friesen.org',
            u'oharahayes.com', u'vandervort.com', u'douglas.net',
            u'dietrich.com', u'ernserpacocha.info', u'crist.com',
            u'collier.com', u'hicklerau.biz', u'feestspencer.info',
            u'trantow.com', u'prosacco.info', u'haleydamore.com', u'hayes.com',
            u'bergstrom.com', u'heller.com', u'gerlach.com', u'schaden.com',
            u'harris.info', u'turcotte.com', u'breitenberg.info',
            u'pfannerstill.com', u'hauck.net', u'hilll.com', u'osinski.com',
            u'lebsack.com', u'naderbernier.info', u'huel.com', u'abshire.com',
            u'feest.biz', u'hansenwest.com', u'welch.com', u'tromp.com',
            u'gerhold.com', u'oconner.com', u'pagac.info', u'wisoky.com',
            u'kuphal.com', u'hettinger.com', u'murazikstanton.com',
            u'luettgen.com', u'cristlesch.com', u'barrowskuhlman.com',
            u'okuneva.org', u'lesch.com', u'volkman.net', u'marvin.info',
            u'bashiriannienow.org', u'donnelly.info', u'daniel.com',
            u'morissette.info', u'mohr.com', u'ziemann.com', u'daugherty.net',
            u'kleinrohan.com', u'roobwalter.com', u'huelhudson.com',
            u'ryanschinner.org', u'trantowemard.info', u'reichel.net',
            u'rutherford.com', u'gutmannkuhic.com', u'koelpin.com',
            u'weber.com', u'bosco.biz', u'barrowsfadel.info', u'kerluke.com'
        ]

        for domain in domains:
            TargetValue.objects.get_or_create(
                exchange=appnexus,
                category=dimensions.domain,
                value=domain
            )

        management.call_command('rebuild_index', interactive=False)