当前位置: 首页>>代码示例>>Python>>正文


Python SettingsForm.save_m2m方法代码示例

本文整理汇总了Python中forms.SettingsForm.save_m2m方法的典型用法代码示例。如果您正苦于以下问题:Python SettingsForm.save_m2m方法的具体用法?Python SettingsForm.save_m2m怎么用?Python SettingsForm.save_m2m使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在forms.SettingsForm的用法示例。


在下文中一共展示了SettingsForm.save_m2m方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: render_settings

# 需要导入模块: from forms import SettingsForm [as 别名]
# 或者: from forms.SettingsForm import save_m2m [as 别名]
def render_settings(request):
    user = User.objects.get(pk=request.user.id)
    delete_account_request = False
    try:
        delete_request = DeleteAccountRequest.objects.filter(user=user).first()
        if delete_request:
            delete_account_request = True
    except ObjectDoesNotExist:
        delete_account_request = False
    try:
        settings = UserProfile.objects.get(user=user)
        user_groups = user.groups.all()
        group_list = []
        for g in user_groups:
            group_list.append({
                'id': g.id,
                'name': g.name,
                'receive_updates': settings.get_group_notification_preference(g.id)
            })
    except UserProfile.DoesNotExist:
        settings = None
    if request.method == 'POST':
        user_form = UserForm(request.POST, instance=user)
        if settings:
            settings_form = SettingsForm(request.POST, instance=settings)
        else:
            settings_form = SettingsForm(request.POST)
        if user_form.is_valid() and settings_form.is_valid():
            user = user_form.save()
            settings = settings_form.save(commit=False)
            settings.user_id = request.user.id
            settings.save()
            settings_form.save_m2m()
            # update the interface language
            if settings.interface_lang:
                translation.activate(settings.interface_lang)
                request.LANGUAGE_CODE = translation.get_language()

            # get group update settings
            for g in group_list:
                gets_notif = True if request.POST.get('group_notif_' + str(g.get('id'))) else False
                settings.set_group_notification_preference(g.get('id'), gets_notif)
                g['receive_updates'] = gets_notif

            # check for delete account requests
            if request.POST.get('delete_account_option') == 'delete':
                DeleteAccountRequest.objects.create(user=user)
                delete_account_request = True
                settings.set_active(False)
                ctx = {
                    "user": user,
                }
                html = render_to_string('emails/delete_account_request.html', ctx)
                email = EmailMessage('Delete user account request',
                                     html,
                                     constance.config.NO_REPLY_EMAIL,
                                     [config.ACTIVATE_USER_EMAIL])
                email.content_subtype = "html"
                email.send()
            else:
                if delete_account_request:
                    DeleteAccountRequest.objects.filter(user=user).delete()
                    delete_account_request = False
                    settings.set_active(True)
            messages.add_message(request, messages.SUCCESS, 'Profile Update Successful.')
    else:
        user_form = UserForm(instance=request.user)
        settings_form = SettingsForm(instance=settings)

    errors = dict(user_form.errors)
    errors.update(settings_form.errors)

    interest_types = {'languages': [(lang.id, lang.name) for lang in Language.objects.all()],
                      'interests': [(interest.id, interest.name) for interest in Interest.objects.all()],
                      'regions': [(region.id, region.name) for region in Region.objects.all()]}

    return render_to_response('users/user_settings_v2.html',
                              {
                                'settings_form': settings_form,
                                'user_form': user_form,
                                'group_list': group_list,
                                'interest_types': interest_types,
                                'has_password': user.has_usable_password(),
                                'errors': errors,
                                'delete_account_request': delete_account_request,
                              },
                              context_instance=RequestContext(request))
开发者ID:Utsira,项目名称:movements,代码行数:89,代码来源:views.py


注:本文中的forms.SettingsForm.save_m2m方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。