class ActionOptCodesManager(BaseOptCodesManager): CREATE_URL = Item.prepare_url('opt_codes/action/create') UPDATE_URL = Item.prepare_url('opt_codes/action/update/%(optcode_id)s') CREATE_FIELDS = { 'website': lambda x: Item.sanitize_integer_value(x, 'website', blank=True), 'campaign': lambda x: Item.sanitize_integer_value(x, 'campaign', blank=True), 'desc_mode': lambda x: Item.sanitize_integer_value(x, 'desc_mode'), 'url': lambda x: Item.sanitize_string_value(x, 'url'), 'method': lambda x: Item.sanitize_integer_value(x, 'method'), 'action_type': lambda x: Item.sanitize_integer_value(x, 'action_type'), 'status': lambda x: Item.sanitize_integer_value(x, 'status'), } UPDATE_FIELDS = { 'desc_mode': lambda x: Item.sanitize_integer_value(x, 'desc_mode', blank=True), 'url': lambda x: Item.sanitize_string_value(x, 'url', blank=True), 'method': lambda x: Item.sanitize_integer_value(x, 'method', blank=True), 'action_type': lambda x: Item.sanitize_integer_value(x, 'action_type', blank=True), 'status': lambda x: Item.sanitize_integer_value(x, 'status', blank=True), }
class PaymentsSettings(Item): """ Get payments settings by currency """ SCOPE = 'private_data_balance' URL = Item.prepare_url('me/payment/settings') CURRENCY_URL = Item.prepare_url('me/payment/settings/%(currency)s') def __call__(self, **kwargs): return self.get(**kwargs) def get(self, currency=None): """ Args: currency (str) """ request_data = { 'currency': Item.sanitize_currency_value(currency, blank=True), 'url': self.CURRENCY_URL if currency else self.URL } return self.transport.get().request(**request_data)
class CouponsCategories(CouponsBase): SCOPE = 'public_data' URL = Item.prepare_url('coupons/categories') SINGLE_URL = Item.prepare_url('coupons/categories/%(coupon_category_id)s') def get(self, **kwargs): """ Args: limit (int) offset (int) """ return self.transport.get().set_pagination(**kwargs).request(url=self.URL) def getOne(self, coupon_category_id): """ Args: coupon_category_id (int) """ request_data = { 'url': self.SINGLE_URL, 'coupon_category_id': Item.sanitize_id(coupon_category_id) } return self.transport.get().request(**request_data)
class SystemLanguages(Item): """ List of system languages """ SCOPE = 'public_data' URL = Item.prepare_url('languages') SINGLE_URL = Item.prepare_url('languages/%(code)s') def get(self, **kwargs): """ Args: limit (int) offset (int) """ return self.transport.get().set_pagination(**kwargs).request(url=self.URL) def getOne(self, code='ru'): """ Args: code (str) """ request_data = { 'url': self.SINGLE_URL, 'code': Item.sanitize_string_value(code, 'code', 2, 2, False) } return self.transport.get().request(**request_data)
class PaymentsManage(Item): """ Manage payments """ SCOPE = 'manage_payments' CREATE_URL = Item.prepare_url('payments/request/%(code)s') CONFIRM_URL = Item.prepare_url('payments/confirm/%(payment_id)s') DELETE_URL = Item.prepare_url('payments/delete/%(payment_id)s') def create(self, _code, **kwargs): """ Create a payment request. _code is a code of currency Args: _code (str) """ request_data = { 'url': self.CREATE_URL, 'code': Item.sanitize_currency_value(_code) } return self.transport.post().request(**request_data) def confirm(self, _id, **kwargs): """ Confirm a payment request. _id is a payment id. Args: _id (int) """ request_data = { 'url': self.CONFIRM_URL, 'payment_id': Item.sanitize_id(_id) } return self.transport.post().request(**request_data) def delete(self, _id, **kwargs): """ Delete a payment request. _id is a payment id. Args: _id (int) """ request_data = { 'url': self.DELETE_URL, 'payment_id': Item.sanitize_id(_id) } return self.transport.post().request(**request_data)
class BrokenLinks(Item): SCOPE = 'broken_links' URL = Item.prepare_url('broken_links') SINGLE_URL = Item.prepare_url('broken_links/%(broken_link_id)s') def get(self, **kwargs): """ Args: website (list of int) campaign (list of int) search (str) reason (int) date_start (date) date_end (date) limit (int) offset (int) """ filtering = { 'filter_by': kwargs, 'available': { 'website': lambda x: Item.sanitize_integer_array(x, 'website', blank=True ), 'campaign': lambda x: Item.sanitize_integer_array( x, 'campaign', blank=True), 'search': lambda x: Item.sanitize_string_value(x, 'search', blank=True), 'reason': lambda x: Item.sanitize_integer_value(x, 'reason', blank=True), 'date_start': lambda x: Item.sanitize_date(x, 'date_start', blank=True), 'date_end': lambda x: Item.sanitize_date(x, 'date_end', blank=True), } } return self.transport.get() \ .set_pagination(**kwargs) \ .set_filtering(filtering) \ .request(url=self.URL) def getOne(self, broken_link_id): """ Args: broken_link_id (int) """ request_data = { 'url': self.SINGLE_URL, 'broken_link_id': Item.sanitize_id(broken_link_id) } return self.transport.get().request(**request_data)
class News(Item): """ List of news """ SCOPE = 'public_data' URL = Item.prepare_url('news') SINGLE_URL = Item.prepare_url('news/%(news_id)s') def get(self, **kwargs): """ Args: limit (int) offset (int) language (str) """ filtering = { 'filter_by': kwargs, 'available': { 'language': lambda x: Item.sanitize_string_value(x, 'language', 2, 2, True ), } } return self.transport.get() \ .set_pagination(**kwargs) \ .set_filtering(filtering) \ .request(url=self.URL) def getOne(self, news_id, **kwargs): """ Args: news_id (int) language (str) """ request_data = { 'url': self.SINGLE_URL, 'news_id': self.sanitize_id(news_id) } filtering = { 'filter_by': kwargs, 'available': { 'language': lambda x: Item.sanitize_string_value(x, 'language', 2, 2, True ), } } return self.transport.get().set_filtering(filtering).request( **request_data)
class Campaigns(Item): """ List of advertising campaigns """ SCOPE = 'advcampaigns' URL = Item.prepare_url('advcampaigns') SINGLE_URL = Item.prepare_url('advcampaigns/%(campaign_id)s') def get(self, **kwargs): """ Args: website (int) has_tool (list of str) limit (int) offset (int) language (str) """ filtering = { 'filter_by': kwargs, 'available': { 'website': lambda x: Item.sanitize_integer_value(x, 'website', blank=True ), 'has_tool': lambda x: Item.sanitize_string_array(x, 'has_tool', blank=True ), 'language': lambda x: Item.sanitize_string_value(x, 'language', blank=True ), } } return self.transport.get() \ .set_pagination(**kwargs) \ .set_filtering(filtering) \ .request(url=self.URL) def getOne(self, _id, **kwargs): """ Here _id is an a campaign id Args: _id (int) """ request_data = { 'url': self.SINGLE_URL, 'campaign_id': Item.sanitize_id(_id) } return self.transport.get().request(**request_data)
class OptCodes(BaseOptCodes): SCOPE = 'opt_codes' ORDERING = ('action_type', 'method', 'desc_mode') URL = Item.prepare_url('opt_codes') SINGLE_URL = Item.prepare_url('opt_codes/%(optcode_id)s') def get(self, **kwargs): """ Args: campaign (int) website (int) limit (int) offset (int) order_by (list of str) """ ordering = { 'order_by': kwargs.get('order_by', []), 'available': self.ORDERING } filtering = { 'filter_by': kwargs, 'available': { 'campaign': lambda x: Item.sanitize_integer_value( x, 'campaign', blank=True), 'website': lambda x: Item.sanitize_integer_value( x, 'campaign', blank=True), } } return self.transport.get() \ .set_pagination(**kwargs) \ .set_ordering(ordering) \ .set_filtering(filtering) \ .request(url=self.URL) def getOne(self, optcode_id, **kwargs): """ Args: optcode_id (int) """ request_data = { 'url': self.SINGLE_URL, 'optcode_id': Item.sanitize_id(optcode_id) } return self.transport.get().request(**request_data)
class CampaignCategories(Item): """ List of campaigns categories """ SCOPE = 'public_data' ORDERING = ('name',) URL = Item.prepare_url('categories') SINGLE_URL = Item.prepare_url('categories/%(id)s') def get(self, **kwargs): """ Args: campaign (list of int) language (str) order_by (str) limit (int) offset (int) """ ordering = { 'order_by': kwargs.get('order_by', None), 'available': self.ORDERING } filtering = { 'filter_by': kwargs, 'available': { 'campaign': lambda x: Item.sanitize_integer_array(x, 'campaign', True), 'language': lambda x: Item.sanitize_string_value(x, 'language', 2, 2, True), } } return self.transport.get() \ .set_pagination(**kwargs) \ .set_ordering(ordering) \ .set_filtering(filtering) \ .request(url=self.URL) def getOne(self, _id, **kwargs): """ Args: _id (int) """ request_data = { 'url': self.SINGLE_URL, 'id': Item.sanitize_id(_id) } return self.transport.get().request(**request_data)
class Coupons(CouponsBase): """ List of coupons """ SCOPE = 'coupons' URL = Item.prepare_url('coupons') SINGLE_URL = Item.prepare_url('coupons/%(coupon_id)s') def get(self, **kwargs): """ Args: campaign (list of int) campaign_category (list of int) category (list of int) type (str) limit (int) offset (int) order_by (str) """ filtering = { 'filter_by': kwargs, 'available': self.FILTERING } ordering = { 'order_by': kwargs.get('order_by', None), 'available': self.ORDERING } return self.transport.get() \ .set_pagination(**kwargs) \ .set_ordering(ordering) \ .set_filtering(filtering) \ .request(url=self.URL) def getOne(self, _id, **kwargs): """ Args: _id (int) """ request_data = { 'url': self.SINGLE_URL, 'coupon_id': Item.sanitize_id(_id) } return self.transport.get().request(**request_data)
class LostOrders(Item): SCOPE = 'lost_orders' URL = Item.prepare_url('lost_orders') SINGLE_URL = Item.prepare_url('lost_orders/%(lost_order_id)s') def get(self, **kwargs): """ Args: campaign (id) website (id) status (string) start_date (date) end_date (date) appeal_id (string) appeal_status (string) limit (int) offset (int) """ filtering = { 'filter_by': kwargs, 'available': { 'campaign': lambda x: Item.sanitize_integer_value(x, 'campaign', blank=True), 'website': lambda x: Item.sanitize_integer_value(x, 'website', blank=True), 'status': lambda x: Item.sanitize_string_value(x, 'status', blank=True), 'start_date': lambda x: Item.sanitize_string_value(x, 'start_date', blank=True), 'end_date': lambda x: Item.sanitize_string_value(x, 'end_date', blank=True), 'appeal_id': lambda x: Item.sanitize_string_value(x, 'appeal_id', blank=True), 'appeal_status': lambda x: Item.sanitize_string_value(x, 'appeal_status', blank=True), } } return self.transport.get() \ .set_filtering(filtering) \ .set_pagination(**kwargs) \ .request(url=self.URL) def getOne(self, lost_order_id): """ Args: lost_order_id (int) """ request_data = { 'url': self.SINGLE_URL, 'lost_order_id': Item.sanitize_id(lost_order_id) } return self.transport.get().request(**request_data)
class TicketsManager(Item): SCOPE = 'manage_tickets' CREATE_URL = Item.prepare_url('tickets/create') COMMENT_URL = Item.prepare_url('tickets/%(ticket_id)s/create') CREATE_FIELDS = { 'subject': lambda x: Item.sanitize_string_value(x, 'subject'), 'text': lambda x: Item.sanitize_string_value(x, 'text'), 'campaign': lambda x: Item.sanitize_integer_value(x, 'campaign'), 'category': lambda x: Item.sanitize_integer_value(x, 'category'), 'priority': lambda x: Item.sanitize_integer_value(x, 'priority'), } COMMENT_FIELDS = { 'text': lambda x: Item.sanitize_string_value(x, 'text'), } def create(self, **kwargs): """ Args: subject (str) text (str) campaign (int) category (int) priority (int) """ data = Item.sanitize_fields(self.CREATE_FIELDS, **kwargs) return self.transport.post().set_data(data).request(url=self.CREATE_URL) def comment(self, ticket_id, **kwargs): """ Args: ticket_id (int) text (str) """ request_data = { 'url': self.COMMENT_URL, 'ticket_id': Item.sanitize_id(ticket_id) } data = Item.sanitize_fields(self.COMMENT_FIELDS, **kwargs) return self.transport.post().set_data(data).request(**request_data)
class CampaignsManage(Item): """ Manage an advertising campaign """ SCOPE = 'manage_advcampaigns' CONNECT_URL = Item.prepare_url( 'advcampaigns/%(campaign_id)s/attach/%(website_id)s') DISCONNECT_URL = Item.prepare_url( 'advcampaigns/%(campaign_id)s/detach/%(website_id)s') def connect(self, c_id, w_id, **kwargs): """ Connect an advertising campaign for a website Here w_id is a website id and c_id is a campaign id Args: c_id (int) w_id (int) """ request_data = { 'url': self.CONNECT_URL, 'campaign_id': Item.sanitize_id(c_id), 'website_id': Item.sanitize_id(w_id) } return self.transport.post().request(**request_data) def disconnect(self, c_id, w_id, **kwargs): """ Disconnect an advertising campaign from a website Here w_id is a website id and c_id is a campaign id Args: c_id (int) w_id (int) """ request_data = { 'url': self.DISCONNECT_URL, 'campaign_id': Item.sanitize_id(c_id), 'website_id': Item.sanitize_id(w_id) } return self.transport.post().request(**request_data)
class PaymentsStatement(Item): SCOPE = 'payments' URL = Item.prepare_url('payments/%(payment_id)s/statement') def get(self, payment_id, **kwargs): """ Args: detailed (bool) limit (int) offset (int) """ filtering = { 'filter_by': kwargs, 'available': { 'detailed': lambda x: Item.sanitize_bool_integer_value( x, 'detailed', blank=True) } } request_data = { 'url': self.URL, 'payment_id': Item.sanitize_id(payment_id) } return self.transport.get() \ .set_pagination(**kwargs) \ .set_filtering(filtering) \ .request(**request_data)
class BaseOptCodesManager(BaseOptCodes): SCOPE = 'manage_opt_codes' DELETE_URL = Item.prepare_url('opt_codes/delete/%(optcode_id)s') CREATE_URL = '' UPDATE_URL = '' CREATE_FIELDS = {} UPDATE_FIELDS = {} def delete(self, optcode_id): request_data = { 'url': self.DELETE_URL, 'optcode_id': Item.sanitize_id(optcode_id), } return self.transport.post().request(**request_data) def create(self, **kwargs): data = Item.sanitize_fields(self.CREATE_FIELDS, **kwargs) return self.transport.post().set_data(data).request( url=self.CREATE_URL) def update(self, optcode_id, **kwargs): data = Item.sanitize_fields(self.UPDATE_FIELDS, **kwargs) request_data = { 'url': self.UPDATE_URL, 'optcode_id': Item.sanitize_id(optcode_id), } return self.transport.post().set_data(data).request(**request_data)
class StatisticWebsites(StatisticBase): """ Statistics by websites """ SCOPE = 'statistics' URL = Item.prepare_url('statistics/websites') def get(self, **kwargs): """ Args: date_start (date) date_end (date) website (int) campaign (int) subid (str) total (int) limit (int) offset (int) order_by (list of str) """ return super(StatisticWebsites, self).get(self.URL, **kwargs)
class ManageBrokenLinks(Item): SCOPE = 'manage_broken_links' RESOLVE_URL = Item.prepare_url('broken_links/resolve') def resolve(self, broken_link_ids): """ Args: broken_links_ids (list of int) """ filtering = { 'filter_by': { 'link_id': broken_link_ids }, 'available': { 'link_id': lambda x: Item.sanitize_integer_array(x, 'link_id', blank=True) } } return self.transport.post() \ .set_filtering(filtering) \ .request(url=self.RESOLVE_URL)
class DeeplinksManage(Item): SCOPE = 'deeplink_generator' CREATE_URL = Item.prepare_url( 'deeplink/%(website_id)s/advcampaign/%(campaign_id)s') CREATE_FIELDS = { 'ulp': lambda x: Item.sanitize_string_array(x, 'ulp'), 'subid': lambda x: Item.sanitize_string_value(x, 'subid', max_length=30), # todo: subid[1-4] } def create(self, website_id, campaign_id, **kwargs): """ Args: website_id (int) campaign_id (int) ulp (list of str) subid (str) """ data = Item.sanitize_fields(self.CREATE_FIELDS, **kwargs) request_data = { 'url': self.CREATE_URL, 'website_id': Item.sanitize_id(website_id), 'campaign_id': Item.sanitize_id(campaign_id), } return self.transport.get().set_data(data).request(**request_data)
class Payments(Item): """ List of webmaster payments """ SCOPE = 'payments' URL = Item.prepare_url('payments') SINGLE_URL = Item.prepare_url('payments/%(payment_id)s') def get(self, **kwargs): """ Args: has_statement (bool) limit (int) offset (int) """ filtering = { 'filter_by': kwargs, 'available': { 'has_statement': lambda x: Item.sanitize_bool_integer_value( x, 'has_statement', blank=True) } } return self.transport.get() \ .set_pagination(**kwargs) \ .set_filtering(filtering) \ .request(url=self.URL) def getOne(self, _id, **kwargs): """ Args: _id (int) """ request_data = { 'url': self.SINGLE_URL, 'payment_id': Item.sanitize_id(_id) } return self.transport.get().request(**request_data)
class Referrals(Item): """ List of referrals """ SCOPE = 'referrals' URL = Item.prepare_url('referrals') SINGLE_URL = Item.prepare_url('referrals/%(referral_id)s') def get(self, **kwargs): """ Args: date_start (date) date_end (date) limit (int) offset (int) """ filtering = { 'filter_by': kwargs, 'available': { 'date_start': lambda x: Item.sanitize_date(x, 'date_start', True), 'date_end': lambda x: Item.sanitize_date(x, 'date_end', True) } } return self.transport.get() \ .set_pagination(**kwargs) \ .set_filtering(filtering) \ .request(url=self.URL) def getOne(self, _id, **kwargs): """ Args: _id (int) """ request_data = { 'url': self.SINGLE_URL, 'referral_id': Item.sanitize_id(_id) } return self.transport.get().request(**request_data)
class CampaignsForWebsite(Item): """ List of advertising campaigns for a website """ SCOPE = 'advcampaigns_for_website' URL = Item.prepare_url('advcampaigns/website/%(website_id)s') SINGLE_URL = Item.prepare_url( 'advcampaigns/%(campaign_id)s/website/%(website_id)s') def get(self, _id, **kwargs): """ Here _id is a website id Args: _id (int) limit (int) offset (int) """ request_data = {'url': self.URL, 'website_id': Item.sanitize_id(_id)} return self.transport.get().set_pagination(**kwargs).request( **request_data) def getOne(self, _id, c_id, **kwargs): """ Here _id is a website id and c_id is a campaign id Args: _id (int) c_id (int) """ request_data = { 'url': self.SINGLE_URL, 'website_id': Item.sanitize_id(_id), 'campaign_id': Item.sanitize_id(c_id) } return self.transport.get().request(**request_data)
class Tickets(Item): SCOPE = 'tickets' URL = Item.prepare_url('tickets') SINGLE_URL = Item.prepare_url('tickets/%(ticket_id)s') def get(self, **kwargs): """ Args: date_start (date) date_end (date) status (int) limit (int) offset (int) """ filtering = { 'filter_by': kwargs, 'available': { 'date_start': lambda x: Item.sanitize_date(x, 'date_start', True), 'date_end': lambda x: Item.sanitize_date(x, 'date_end', True), 'status': lambda x: Item.sanitize_integer_value(x, 'status', True), } } return self.transport.get() \ .set_pagination(**kwargs) \ .set_filtering(filtering) \ .request(url=self.URL) def getOne(self, ticket_id): """ Args: ticket_id (int) """ request_data = { 'url': self.SINGLE_URL, 'ticket_id': Item.sanitize_id(ticket_id) } return self.transport.get().request(**request_data)
class StatisticKeywords(StatisticBase): """ Statistics by keywords """ SCOPE = 'statistics' ORDERING = ( 'actions', 'clicks', 'cr', 'ecpc', 'keyword', 'leads', 'payment_sum', 'payment_sum_approved', 'payment_sum_declined', 'payment_sum_open', 'sales', 'source', ) FILTERING = { 'date_start': lambda x: Item.sanitize_date(x, 'date_start', blank=True), 'date_end': lambda x: Item.sanitize_date(x, 'date_end', blank=True), 'website': lambda x: Item.sanitize_integer_value(x, 'website', blank=True), 'campaign': lambda x: Item.sanitize_integer_value(x, 'campaign', blank=True), 'source': StatisticBase.check_sources, } URL = Item.prepare_url('statistics/keywords') def get(self, **kwargs): """ Args: date_start (date) date_end (date) website (int) campaign (int) source (str) limit (int) offset (int) order_by (list of str) """ return super(StatisticKeywords, self).get(self.URL, **kwargs)
class Balance(Item): """ Get balance information """ SCOPE = 'private_data_balance' URL = Item.prepare_url('me/balance') EXTENDED_URL = Item.prepare_url('me/balance/extended') def __call__(self, **kwargs): return self.get(**kwargs) def get(self, **kwargs): """ Args: extended (bool) """ url = self.EXTENDED_URL if kwargs.get('extended', False) else self.URL return self.transport.get().request(url=url)
class Me(Item): """ Get private information """ SCOPE = 'private_data private_data_email private_data_phone' URL = Item.prepare_url('me') def __call__(self): return self.get() def get(self): return self.transport.get().request(url=self.URL)
class BannersForWebsite(Item): """ List of banners for the website """ SCOPE = 'banners_for_website' URL = Item.prepare_url('banners/%(campaign_id)s/website/%(website_id)s') def get(self, _id, w_id, **kwargs): """ Here _id is an id of advertising campaign and w_id is a id of website Args: _id (int) w_id (int) mobile_content (bool) landing (int) uri_scheme (str) limit (int) offset (int) """ request_data = { 'url': self.URL, 'campaign_id': Item.sanitize_id(_id), 'website_id': Item.sanitize_id(w_id) } filtering = { 'filter_by': kwargs, 'available': { 'mobile_content': lambda x: Item.sanitize_bool_value(x, blank=True), 'landing': lambda x: Item.sanitize_integer_value(x, 'landing', blank=True), 'uri_scheme': lambda x: Item.sanitize_string_value(x, 'uri_scheme', blank=True) } } return self.transport.get() \ .set_pagination(**kwargs) \ .set_filtering(filtering) \ .request(**request_data)
class SystemCurrencies(Item): """ List of system currencies """ SCOPE = 'public_data' URL = Item.prepare_url('currencies') def get(self, **kwargs): """ Args: limit (int) offset (int) """ return self.transport.get().set_pagination(**kwargs).request(url=self.URL)
class WebsiteRegions(Item): """ List of websites regions """ SCOPE = 'public_data' URL = Item.prepare_url('websites/regions') def get(self, **kwargs): """ Args: limit (int) offset (int) """ return self.transport.get().set_pagination(**kwargs).request(url=self.URL)
class LinksValidator(Item): SCOPE = 'validate_links' URL = Item.prepare_url('validate_links') GET_FIELDS = { 'link': lambda x: Item.sanitize_string_value(x, 'link'), } def get(self, link, **kwargs): """ Args: link (str) """ data = Item.sanitize_fields(self.GET_FIELDS, link=link) return self.transport.get().set_data(data).request(url=self.URL)
def test_prepare_url(self): self.assertEqual(Item.prepare_url('somepath'), '%ssomepath/' % BASE_URL) self.assertEqual(Item.prepare_url('somepath/'), '%ssomepath/' % BASE_URL) self.assertEqual(Item.prepare_url('/somepath'), '%ssomepath/' % BASE_URL) self.assertEqual(Item.prepare_url('/somepath/'), '%ssomepath/' % BASE_URL)