本文整理汇总了Python中mailsnake.MailSnake.listInterestGroupings方法的典型用法代码示例。如果您正苦于以下问题:Python MailSnake.listInterestGroupings方法的具体用法?Python MailSnake.listInterestGroupings怎么用?Python MailSnake.listInterestGroupings使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类mailsnake.MailSnake
的用法示例。
在下文中一共展示了MailSnake.listInterestGroupings方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: groups_form_factory
# 需要导入模块: from mailsnake import MailSnake [as 别名]
# 或者: from mailsnake.MailSnake import listInterestGroupings [as 别名]
def groups_form_factory(email=None, grouping_name=None, list_id=None):
"""
Form factory for selecting the interest groups.
email An email address of a list member from which to set the
initial values for the form.
grouping_name The grouping name as defined in MailChimp. If not provided
then the first grouping will be used.
list_id The MailChimp list ID. If not provided, the value defined in
the config settings will be used.
"""
if not hasattr(settings, 'MAILCHIMP_API_KEY'):
raise ImproperlyConfigured(_("You need to specify MAILCHIMP_API_KEY" \
"in your Django settings file."))
ms = MailSnake(settings.MAILCHIMP_API_KEY)
if not list_id:
list_id = get_list_id()
# get all groupings for the list
grouping = None
response = ms.listInterestGroupings(id=list_id)
raise_if_error(response)
# get the correct grouping
if not grouping_name:
grouping = response[0]
grouping_name = grouping['name']
else:
for try_grouping in response:
if try_grouping['name'] == grouping_name:
grouping = try_grouping
if not grouping:
errmsg = _("Grouping not found: '%s'") % grouping_name
raise MailChimpGroupingNotFound(errmsg)
if email:
# get the user's group subscription to set initial field values
response = ms.listMemberInfo(id=list_id, email_address=[email])
if not response['success']:
raise MailChimpEmailNotFound
user_groupings = response['data'][0]['merges']['GROUPINGS']
for try_grouping in user_groupings:
if try_grouping['name'] == grouping_name:
user_grouping = try_grouping
# create the appropriate type of fields
if grouping['form_field'] == 'checkboxes':
fields = SortedDict()
for i, group in enumerate(grouping['groups']):
key = 'mailchimp_group_'+group['bit']
if email:
initial=bool(user_grouping['groups'].find(group['name'])+1)
else:
initial=False
fields.insert(i, key, forms.BooleanField(label=group['name'],
required=False,
initial=initial))
else: # radio or select
fields = {}
CHOICES = tuple((group['bit'], group['name']) for group in grouping['groups'])
for group in grouping['groups']:
initial=None
if email:
if bool(user_grouping['groups'].find(group['name'])+1):
initial = group['bit']
else:
initial = None
if grouping['form_field'] == 'radio':
widget = RadioSelect
else:
widget = Select
fields['mailchimp_group'] = forms.ChoiceField(choices=CHOICES,
label=grouping['name'],
widget=widget,
initial=initial)
form = type('GroupsForm', (_GroupsForm,), {'base_fields': fields})
form._grouping = grouping
return form