當前位置: 首頁>>代碼示例>>Python>>正文


Python MailSnake.listInterestGroupings方法代碼示例

本文整理匯總了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
開發者ID:Quixotix,項目名稱:django-chimpusers,代碼行數:84,代碼來源:forms.py


注:本文中的mailsnake.MailSnake.listInterestGroupings方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。