/
bulk_location_ad_extensions.py
242 lines (206 loc) · 9.56 KB
/
bulk_location_ad_extensions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
from bingads.v12.internal.bulk.mappings import _SimpleBulkMapping
from bingads.v12.internal.bulk.string_table import _StringTable
from bingads.service_client import _CAMPAIGN_OBJECT_FACTORY_V12
from .common import *
from .common import _BulkAdExtensionBase
from .common import _BulkCampaignAdExtensionAssociation
from .common import _BulkAccountAdExtensionAssociation
_LocationAdExtension = type(_CAMPAIGN_OBJECT_FACTORY_V12.create('LocationAdExtension'))
class BulkLocationAdExtension(_BulkAdExtensionBase):
""" Represents an location ad extension.
This class exposes the :attr:`location_ad_extension` property that can be read and written
as fields of the Location Ad Extension record in a bulk file.
For more information, see Location Ad Extension at https://go.microsoft.com/fwlink/?linkid=846127.
*See also:*
* :class:`.BulkServiceManager`
* :class:`.BulkOperation`
* :class:`.BulkFileReader`
* :class:`.BulkFileWriter`
"""
def __init__(self, account_id=None, ad_extension=None):
if ad_extension and not isinstance(ad_extension, _LocationAdExtension):
raise ValueError('The type of ad_extension is: {0}, should be: {1}'.format(
type(ad_extension),
'LocationAdExtension'
))
super(BulkLocationAdExtension, self).__init__(
account_id=account_id,
ad_extension=ad_extension
)
@property
def location_ad_extension(self):
""" The location ad extension.
see Location Ad Extension at https://go.microsoft.com/fwlink/?linkid=846127.
"""
return self._ad_extension
@location_ad_extension.setter
def location_ad_extension(self, value):
self._ad_extension = value
_MAPPINGS = [
_SimpleBulkMapping(
header=_StringTable.BusinessName,
field_to_csv=lambda c: c.location_ad_extension.CompanyName,
csv_to_field=lambda c, v: setattr(c.location_ad_extension, 'CompanyName', v)
),
_SimpleBulkMapping(
header=_StringTable.PhoneNumber,
field_to_csv=lambda c: bulk_optional_str(c.location_ad_extension.PhoneNumber, c.location_ad_extension.Id),
csv_to_field=lambda c, v: setattr(c.location_ad_extension, 'PhoneNumber', v if v else '')
),
_SimpleBulkMapping(
header=_StringTable.GeoCodeStatus,
field_to_csv=lambda c: bulk_str(c.location_ad_extension.GeoCodeStatus),
csv_to_field=lambda c, v: setattr(c.location_ad_extension, 'GeoCodeStatus', v if v else None)
),
_SimpleBulkMapping(
header=_StringTable.AddressLine1,
field_to_csv=lambda c: BulkLocationAdExtension.get_address_part(
c,
lambda x: x.StreetAddress
),
csv_to_field=lambda c, v: BulkLocationAdExtension.set_address_part(
c,
lambda x: setattr(x, 'StreetAddress', v)
)
),
_SimpleBulkMapping(
header=_StringTable.AddressLine2,
field_to_csv=lambda c: BulkLocationAdExtension.get_address_part(
c,
lambda x: bulk_optional_str(x.StreetAddress2, c.location_ad_extension.Id)
),
csv_to_field=lambda c, v: BulkLocationAdExtension.set_address_part(
c,
lambda x: setattr(x, 'StreetAddress2', v if v else '')
)
),
_SimpleBulkMapping(
header=_StringTable.City,
field_to_csv=lambda c: BulkLocationAdExtension.get_address_part(c, lambda x: x.CityName),
csv_to_field=lambda c, v: BulkLocationAdExtension.set_address_part(
c,
lambda x: setattr(x, 'CityName', v)
)
),
_SimpleBulkMapping(
header=_StringTable.ProvinceName,
field_to_csv=lambda c: BulkLocationAdExtension.get_address_part(c, lambda x: x.ProvinceName),
csv_to_field=lambda c, v: BulkLocationAdExtension.set_address_part(
c,
lambda x: setattr(x, 'ProvinceName', v)
)
),
_SimpleBulkMapping(
header=_StringTable.StateOrProvince,
field_to_csv=lambda c: BulkLocationAdExtension.get_address_part(c, lambda x: x.ProvinceCode),
csv_to_field=lambda c, v: BulkLocationAdExtension.set_address_part(
c,
lambda x: setattr(x, 'ProvinceCode', v)
)
),
_SimpleBulkMapping(
header=_StringTable.PostalCode,
field_to_csv=lambda c: BulkLocationAdExtension.get_address_part(c, lambda x: x.PostalCode),
csv_to_field=lambda c, v: BulkLocationAdExtension.set_address_part(
c,
lambda x: setattr(x, 'PostalCode', v)
)
),
_SimpleBulkMapping(
header=_StringTable.CountryCode,
field_to_csv=lambda c: BulkLocationAdExtension.get_address_part(c, lambda x: x.CountryCode),
csv_to_field=lambda c, v: BulkLocationAdExtension.set_address_part(
c,
lambda x: setattr(x, 'CountryCode', v)
)
),
_SimpleBulkMapping(
header=_StringTable.Latitude,
field_to_csv=lambda c: BulkLocationAdExtension.get_geo_point_part(
c,
lambda x: bulk_str(
float(x.LatitudeInMicroDegrees) / 1000000.0
)
if x.LatitudeInMicroDegrees is not None else None
),
csv_to_field=lambda c, v: BulkLocationAdExtension.set_geo_point_part(
c,
lambda x, latitude: setattr(x, 'LatitudeInMicroDegrees', int(round(float(latitude) * 1000000))),
v
)
),
_SimpleBulkMapping(
header=_StringTable.Longitude,
field_to_csv=lambda c: BulkLocationAdExtension.get_geo_point_part(
c,
lambda x: bulk_str(
float(x.LongitudeInMicroDegrees) / 1000000.0
)
if x.LongitudeInMicroDegrees is not None else None
),
csv_to_field=lambda c, v: BulkLocationAdExtension.set_geo_point_part(
c,
lambda x, longitude: setattr(x, 'LongitudeInMicroDegrees', int(round(float(longitude) * 1000000))),
v
)
),
]
@staticmethod
def get_address_part(bulk_ad_extension, get_func):
if bulk_ad_extension.location_ad_extension.Address is not None:
return get_func(bulk_ad_extension.location_ad_extension.Address)
else:
return None
@staticmethod
def set_address_part(bulk_ad_extension, set_func):
if bulk_ad_extension.location_ad_extension.Address is None:
bulk_ad_extension.location_ad_extension.Address = _CAMPAIGN_OBJECT_FACTORY_V12.create('Address')
set_func(bulk_ad_extension.location_ad_extension.Address)
@staticmethod
def get_geo_point_part(bulk_ad_extension, get_func):
if bulk_ad_extension.location_ad_extension.GeoPoint is not None:
return get_func(bulk_ad_extension.location_ad_extension.GeoPoint)
else:
return None
@staticmethod
def set_geo_point_part(bulk_ad_extension, set_func, value):
if not value:
return
if bulk_ad_extension.location_ad_extension.GeoPoint is None:
bulk_ad_extension.location_ad_extension.GeoPoint = _CAMPAIGN_OBJECT_FACTORY_V12.create('GeoPoint')
set_func(bulk_ad_extension.location_ad_extension.GeoPoint, value)
def process_mappings_from_row_values(self, row_values):
self.location_ad_extension = _CAMPAIGN_OBJECT_FACTORY_V12.create('LocationAdExtension')
self.location_ad_extension.Type = 'LocationAdExtension'
if row_values[_StringTable.Latitude] or row_values[_StringTable.Longitude]:
self.location_ad_extension.GeoPoint = _CAMPAIGN_OBJECT_FACTORY_V12.create('GeoPoint')
super(BulkLocationAdExtension, self).process_mappings_from_row_values(row_values)
row_values.convert_to_entity(self, BulkLocationAdExtension._MAPPINGS)
def process_mappings_to_row_values(self, row_values, exclude_readonly_data):
self._validate_property_not_null(self.location_ad_extension, 'location_ad_extension')
super(BulkLocationAdExtension, self).process_mappings_to_row_values(row_values, exclude_readonly_data)
self.convert_to_values(row_values, BulkLocationAdExtension._MAPPINGS)
class BulkAccountLocationAdExtension(_BulkAccountAdExtensionAssociation):
""" Represents an account level location ad extension.
This class exposes properties that can be read and written
as fields of the Account Location Ad Extension record in a bulk file.
For more information, see Account Location Ad Extension at https://go.microsoft.com/fwlink/?linkid=846127.
*See also:*
* :class:`.BulkServiceManager`
* :class:`.BulkOperation`
* :class:`.BulkFileReader`
* :class:`.BulkFileWriter`
"""
pass
class BulkCampaignLocationAdExtension(_BulkCampaignAdExtensionAssociation):
""" Represents a campaign level location ad extension.
This class exposes properties that can be read and written
as fields of the Campaign Location Ad Extension record in a bulk file.
For more information, see Campaign Location Ad Extension at https://go.microsoft.com/fwlink/?linkid=846127.
*See also:*
* :class:`.BulkServiceManager`
* :class:`.BulkOperation`
* :class:`.BulkFileReader`
* :class:`.BulkFileWriter`
"""
pass