def as_view(request, template, data, slug, just_modify_data=False): ### add settings to the request so that the template ### can adjust what it displays depending on settings. data["settings"] = django.conf.settings if request.user.is_authenticated() or "cookies_work" in request.session: # Great! Cookies work. pass else: request.session.set_test_cookie() if request.session.test_cookie_worked(): request.session.delete_test_cookie() request.session["cookies_work"] = True # Where should the user be sent if she clicks 'logout'? # Depends on whether this is a login-requiring page. try: view_function, _, _ = resolve(request.path) is_login_required = isinstance(view_function, django.contrib.auth.decorators._CheckLogin) if is_login_required: data["go_here_after_logging_in_or_out"] = "/" else: data["go_here_after_logging_in_or_out"] = request.get_full_path() except: data["go_here_after_logging_in_or_out"] = "/" data["slug"] = slug # Account settings uses this. if just_modify_data: return data else: return render_response(request, template, data)
def as_view(request, template, data, slug, just_modify_data=False): ### add settings to the request so that the template ### can adjust what it displays depending on settings. data['settings'] = django.conf.settings if request.user.is_authenticated() or 'cookies_work' in request.session: # Great! Cookies work. pass else: request.session.set_test_cookie() if request.session.test_cookie_worked(): request.session.delete_test_cookie() request.session['cookies_work'] = True # Where should the user be sent if she clicks 'logout'? # Depends on whether this is a login-requiring page. try: view_function, _, _ = resolve(request.path) is_login_required = isinstance( view_function, django.contrib.auth.decorators._CheckLogin) if is_login_required: data['go_here_after_logging_in_or_out'] = '/' else: data['go_here_after_logging_in_or_out'] = request.get_full_path() except: data['go_here_after_logging_in_or_out'] = '/' data['slug'] = slug # Account settings uses this. if just_modify_data: return data else: return render_response(request, template, data)
def render_robots_txt(request): if settings.DEBUG: template_path = "robots_for_dev_env.txt" else: template_path = "robots_for_live_site.txt" return render_response(request, template_path, mimetype='text/plain')
def widget_display_js(request, user_to_display__username): # FIXME: In the future, use: html_doc = widget_display_string(request, user_to_display__username) # to generate html_doc encoded_for_js = json.dumps(html_doc) # Note: using application/javascript as suggested by # http://www.ietf.org/rfc/rfc4329.txt return render_response(request, 'base/append_ourselves.js', {'in_string': encoded_for_js}, mimetype='application/javascript')
def widget_display_js(request, user_to_display__username): # FIXME: In the future, use: html_doc = widget_display_string(request, user_to_display__username) # to generate html_doc encoded_for_js = simplejson.dumps(html_doc) # Note: using application/javascript as suggested by # http://www.ietf.org/rfc/rfc4329.txt return render_response(request, 'base/append_ourselves.js', {'in_string': encoded_for_js}, mimetype='application/javascript')
def page_to_js(request): # FIXME: In the future, use: # from django.template.loader import render_to_string # to generate html_doc html_doc = "<strong>zomg</strong>" encoded_for_js = simplejson.dumps(html_doc) # Note: using application/javascript as suggested by # http://www.ietf.org/rfc/rfc4329.txt return render_response(request, 'base/append_ourselves.js', {'in_string': encoded_for_js}, mimetype='application/javascript')
def signup(request, signup_form=None): if signup_form is None: signup_form = mysite.account.forms.UserCreationFormWithEmail() return render_response(request, 'account/signup.html', {'form': signup_form})
def register(request, template_name='authopenid/complete.html', redirect_field_name=django.contrib.auth.REDIRECT_FIELD_NAME, register_form=django_authopenid.forms.OpenidRegisterForm, auth_form=django.contrib.auth.forms.AuthenticationForm, register_account=django_authopenid.views.register_account, send_email=False, extra_context=None): """ register an openid. If user is already a member he can associate its openid with its account. A new account could also be created and automaticaly associated to the openid. :attr request: request object :attr template_name: string, name of template to use, 'authopenid/complete.html' by default :attr redirect_field_name: string, field name used for redirect. by default 'next' :attr register_form: form use to create a new account. By default `OpenidRegisterForm` :attr auth_form: form object used for legacy authentification. by default `OpenidVerifyForm` form auser auth contrib. :attr register_account: callback used to create a new account from openid. It take the register_form as param. :attr send_email: boolean, by default True. If True, an email will be sent to the user. :attr extra_context: A dictionary of variables to add to the template context. Any callable object in this dictionary will be called to produce the end result which appears in the context. """ redirect_to = request.REQUEST.get(redirect_field_name, '') openid_ = request.session.get('openid', None) if openid_ is None or not openid_: return HttpResponseRedirect( "%s?%s" % (reverse('user_signin'), urllib.urlencode({redirect_field_name: redirect_to}))) nickname = '' email = '' if openid_.sreg is not None: nickname = openid_.sreg.get('nickname', '') email = openid_.sreg.get('email', '') if openid_.ax is not None and not nickname or not email: if openid_.ax.get('http://schema.openid.net/namePerson/friendly', False): nickname = openid_.ax.get( 'http://schema.openid.net/namePerson/friendly')[0] if openid_.ax.get('http://schema.openid.net/contact/email', False): email = openid_.ax.get('http://schema.openid.net/contact/email')[0] form1 = register_form(initial={ 'username': nickname, 'email': email, }) form2 = auth_form(initial={ 'username': nickname, }) if request.POST: user_ = None if not redirect_to or '//' in redirect_to or ' ' in redirect_to: redirect_to = settings.LOGIN_REDIRECT_URL if 'email' in request.POST.keys(): form1 = register_form(data=request.POST) if form1.is_valid(): user_ = register_account(form1, openid_) extra_profile_form = mysite.account.forms.SignUpIfYouWantToHelpForm( request.POST, prefix='extra_profile_form') if extra_profile_form.is_valid(): person = user_.get_profile() method2contact_info = { 'forwarder': 'You can reach me by email at $fwd', 'public_email': 'You can reach me by email at %s' % user_.email, } info = method2contact_info[extra_profile_form.cleaned_data[ 'how_should_people_contact_you']] person.contact_blurb = info person.save() else: form2 = auth_form(data=request.POST) if form2.is_valid(): user_ = form2.get_user() if user_ is not None: # associate the user to openid uassoc = django_authopenid.models.UserAssociation( openid_url=str(openid_), user_id=user_.id) uassoc.save(send_email=send_email) django.contrib.auth.login(request, user_) return HttpResponseRedirect(redirect_to) return render_response( request, template_name, { 'form1': form1, 'form2': form2, 'extra_profile_form': mysite.account.forms.SignUpIfYouWantToHelpForm( prefix='extra_profile_form'), redirect_field_name: redirect_to, 'nickname': nickname, 'email': email }, context_instance=django_authopenid.views._build_context( request, extra_context=extra_context))
def render_robots_txt(request): template_path = "robots.txt" return render_response(request, template_path, mimetype='text/plain')
def register(request, template_name='authopenid/complete.html', redirect_field_name=django.contrib.auth.REDIRECT_FIELD_NAME, register_form=django_authopenid.forms.OpenidRegisterForm, auth_form=django.contrib.auth.forms.AuthenticationForm, register_account=django_authopenid.views.register_account, send_email=False, extra_context=None): """ register an openid. If user is already a member he can associate its openid with its account. A new account could also be created and automaticaly associated to the openid. :attr request: request object :attr template_name: string, name of template to use, 'authopenid/complete.html' by default :attr redirect_field_name: string, field name used for redirect. by default 'next' :attr register_form: form use to create a new account. By default `OpenidRegisterForm` :attr auth_form: form object used for legacy authentification. by default `OpenidVerifyForm` form auser auth contrib. :attr register_account: callback used to create a new account from openid. It take the register_form as param. :attr send_email: boolean, by default True. If True, an email will be sent to the user. :attr extra_context: A dictionary of variables to add to the template context. Any callable object in this dictionary will be called to produce the end result which appears in the context. """ redirect_to = request.REQUEST.get(redirect_field_name, '') openid_ = request.session.get('openid', None) if openid_ is None or not openid_: return HttpResponseRedirect("%s?%s" % (reverse('user_signin'), urllib.urlencode({ redirect_field_name: redirect_to }))) nickname = '' email = '' if openid_.sreg is not None: nickname = openid_.sreg.get('nickname', '') email = openid_.sreg.get('email', '') if openid_.ax is not None and not nickname or not email: if openid_.ax.get('http://schema.openid.net/namePerson/friendly', False): nickname = openid_.ax.get('http://schema.openid.net/namePerson/friendly')[0] if openid_.ax.get('http://schema.openid.net/contact/email', False): email = openid_.ax.get('http://schema.openid.net/contact/email')[0] form1 = register_form(initial={ 'username': nickname, 'email': email, }) form2 = auth_form(initial={ 'username': nickname, }) if request.POST: user_ = None if not redirect_to or '//' in redirect_to or ' ' in redirect_to: redirect_to = settings.LOGIN_REDIRECT_URL if 'email' in request.POST.keys(): form1 = register_form(data=request.POST) if form1.is_valid(): user_ = register_account(form1, openid_) extra_profile_form = mysite.account.forms.SignUpIfYouWantToHelpForm( request.POST, prefix='extra_profile_form') if extra_profile_form.is_valid(): person = user_.get_profile() method2contact_info = { 'forwarder': 'You can reach me by email at $fwd', 'public_email': 'You can reach me by email at %s' % user_.email, } info = method2contact_info[extra_profile_form.cleaned_data[ 'how_should_people_contact_you']] person.contact_blurb = info person.save() else: form2 = auth_form(data=request.POST) if form2.is_valid(): user_ = form2.get_user() if user_ is not None: # associate the user to openid uassoc = django_authopenid.models.UserAssociation( openid_url=str(openid_), user_id=user_.id ) uassoc.save(send_email=send_email) django.contrib.auth.login(request, user_) return HttpResponseRedirect(redirect_to) return render_response(request, template_name, { 'form1': form1, 'form2': form2, 'extra_profile_form': mysite.account.forms.SignUpIfYouWantToHelpForm( prefix='extra_profile_form'), redirect_field_name: redirect_to, 'nickname': nickname, 'email': email }, context_instance=django_authopenid.views._build_context(request, extra_context=extra_context))