def edit_profile(request, username, form_class=None, success_url=None, template_name='profiles/edit_profile.html', extra_context=None): user = get_object_or_404(User, username=username) try: profile_obj = user.get_profile() print user.get_profile().avatar except ObjectDoesNotExist: raise Http404 if success_url is None: success_url = reverse('profiles_edit_profile', kwargs={ 'username': request.user.username }) if form_class is None: form_class = utils.get_profile_form() if request.method == 'POST': form = form_class(data=request.POST, files=request.FILES, instance=profile_obj) if form.is_valid(): form.save() if form.cleaned_data['password1']<>'': tek_user = User.objects.get(pk = request.user.pk) tek_user.set_password(form.cleaned_data['password1']) tek_user.save() return HttpResponseRedirect(success_url) else: form = form_class(instance=profile_obj) if extra_context is None: extra_context = {} context = RequestContext(request) for key, value in extra_context.items(): context[key] = callable(value) and value() or value return render_to_response(template_name, { 'form': form, 'profile': profile_obj, }, context_instance=context)
def edit_profile(request, id): user = get_object_or_404(User, id=id) profile = user.get_profile() if request.user.is_superuser: form_class = get_profile_form() elif request.user == user: form_class = ProfileForm else: return HttpResponseRedirect(user.get_absolute_url()) if request.method == "POST": form = form_class(data=request.POST, files=request.FILES, instance=profile) if form.is_valid(): form.save() return HttpResponseRedirect(user.get_absolute_url()) else: form = form_class(instance=profile) return render_to_response( "profiles/edit_profile.html", {"form": form, "profile": profile}, context_instance=RequestContext(request) )
def create_profile(request, form_class=None, success_url=None, template_name='profiles/create_profile.html', extra_context=None): """ Create a profile for the current user, if one doesn't already exist. If the user already has a profile, as determined by ``request.user.get_profile()``, a redirect will be issued to the :view:`profiles.views.edit_profile` view. If no profile model has been specified in the ``AUTH_PROFILE_MODULE`` setting, ``django.contrib.auth.models.SiteProfileNotAvailable`` will be raised. **Optional arguments:** ``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. ``form_class`` The form class to use for validating and creating the user profile. This form class must define a method named ``save()``, implementing the same argument signature as the ``save()`` method of a standard Django ``ModelForm`` (this view will call ``save(commit=False)`` to obtain the profile object, and fill in the user before the final save). If the profile object includes many-to-many relations, the convention established by ``ModelForm`` of using a method named ``save_m2m()`` will be used, and so your form class should also define this method. If this argument is not supplied, this view will use a ``ModelForm`` automatically generated from the model specified by ``AUTH_PROFILE_MODULE``. ``success_url`` The URL to redirect to after successful profile creation. If this argument is not supplied, this will default to the URL of :view:`profiles.views.profile_detail` for the newly-created profile object. ``template_name`` The template to use when displaying the profile-creation form. If not supplied, this will default to :template:`profiles/create_profile.html`. **Context:** ``form`` The profile-creation form. **Template:** ``template_name`` keyword argument, or :template:`profiles/create_profile.html`. """ try: profile_obj = request.user.get_profile() return HttpResponseRedirect(reverse('profiles_edit_profile')) except ObjectDoesNotExist: pass # # We set up success_url here, rather than as the default value for # the argument. Trying to do it as the argument's default would # mean evaluating the call to reverse() at the time this module is # first imported, which introduces a circular dependency: to # perform the reverse lookup we need access to profiles/urls.py, # but profiles/urls.py in turn imports this module. # if success_url is None: success_url = reverse('profiles_profile_detail', kwargs={ 'username': request.user.username }) if form_class is None: form_class = utils.get_profile_form() if request.method == 'POST': form = form_class(data=request.POST, files=request.FILES) if form.is_valid(): profile_obj = form.save(commit=False) profile_obj.user = request.user profile_obj.save() if hasattr(form, 'save_m2m'): form.save_m2m() return HttpResponseRedirect(success_url) else: form = form_class() if extra_context is None: extra_context = {} context = RequestContext(request) for key, value in extra_context.items(): context[key] = callable(value) and value() or value return render_to_response(template_name, { 'form': form }, context_instance=context)
def edit_profile(request, form_class=None, success_url=None, template_name='profiles/edit_profile.html', extra_context=None): """ Edit the current user's profile. If the user does not already have a profile (as determined by ``User.get_profile()``), a redirect will be issued to the :view:`profiles.views.create_profile` view; if no profile model has been specified in the ``AUTH_PROFILE_MODULE`` setting, ``django.contrib.auth.models.SiteProfileNotAvailable`` will be raised. **Optional arguments:** ``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. ``form_class`` The form class to use for validating and editing the user profile. This form class must operate similarly to a standard Django ``ModelForm`` in that it must accept an instance of the object to be edited as the keyword argument ``instance`` to its constructor, and it must implement a method named ``save()`` which will save the updates to the object. If this argument is not specified, this view will use a ``ModelForm`` generated from the model specified in the ``AUTH_PROFILE_MODULE`` setting. ``success_url`` The URL to redirect to following a successful edit. If not specified, this will default to the URL of :view:`profiles.views.profile_detail` for the profile object being edited. ``template_name`` The template to use when displaying the profile-editing form. If not specified, this will default to :template:`profiles/edit_profile.html`. **Context:** ``form`` The form for editing the profile. ``profile`` The user's current profile. **Template:** ``template_name`` keyword argument or :template:`profiles/edit_profile.html`. """ try: profile_obj = request.user.get_profile() except ObjectDoesNotExist: return HttpResponseRedirect(reverse('profiles_create_profile')) # # See the comment in create_profile() for discussion of why # success_url is set up here, rather than as a default value for # the argument. # if success_url is None: success_url = reverse('profiles_profile_detail', kwargs={ 'username': request.user.username }) if form_class is None: form_class = utils.get_profile_form() if request.method == 'POST': form = form_class(data=request.POST, files=request.FILES, instance=profile_obj) if form.is_valid(): form.save() return HttpResponseRedirect(success_url) else: form = form_class(instance=profile_obj) if extra_context is None: extra_context = {} context = RequestContext(request) for key, value in extra_context.items(): context[key] = callable(value) and value() or value return render_to_response(template_name, { 'form': form, 'profile': profile_obj, }, context_instance=context)
def edit_profile(request, form_class=None, success_url=None, template_name='profiles/edit_profile.html'): """ Edit the current user's profile. If the user does not already have a profile (as determined by ``User.get_profile()``), a redirect will be issued to the :view:`profiles.views.create_profile` view; if no profile model has been specified in the ``AUTH_PROFILE_MODULE`` setting, ``django.contrib.auth.models.SiteProfileNotAvailable`` will be raised. To specify the form class used for profile editing, pass it as the keyword argument ``form_class``; this form class must have a ``save()`` method which will save updates to the profile object. If not supplied, this will default to a ``ModelForm`` for the profile model. If you supply a form class, its ``__init__()`` method must accept an instance of the profile model as the keyword argument ``instance``. To specify the URL to redirect to following a successful edit, pass it as the keyword argument ``success_url``; this will default to the URL of the :view:`profiles.views.profile_detail` view if not supplied. To specify the template to use, pass it as the keyword argument ``template_name``; this will default to :template:`profiles/edit_profile.html` if not supplied. Context: form The form for editing the profile. profile The user's current profile. Template: ``template_name`` keyword argument or :template:`profiles/edit_profile.html`. """ try: profile_obj = request.user.get_profile() except ObjectDoesNotExist: return HttpResponseRedirect(reverse('profiles_create_profile')) if success_url is None: success_url = reverse('profiles_profile_detail', kwargs={ 'username': request.user.username }) if form_class is None: form_class = utils.get_profile_form() if request.method == 'POST': form = form_class(data=request.POST, files=request.FILES, instance=profile_obj) if form.is_valid(): form.save() return HttpResponseRedirect(success_url) else: form = form_class(instance=profile_obj) return render_to_response(template_name, { 'form': form, 'profile': profile_obj, }, context_instance=RequestContext(request))
def create_profile(request, form_class=None, success_url=None, template_name='profiles/create_profile.html'): """ Create a profile for the current user, if one doesn't already exist. If the user already has a profile, as determined by ``request.user.get_profile()``, a redirect will be issued to the :view:`profiles.views.edit_profile` view. If no profile model has been specified in the ``AUTH_PROFILE_MODULE`` setting, ``django.contrib.auth.models.SiteProfileNotAvailable`` will be raised. To specify the form class used for profile creation, pass it as the keyword argument ``form_class``; if this is not supplied, it will fall back to a ``ModelForm`` for the model specified in the ``AUTH_PROFILE_MODULE`` setting. If you are supplying your own form class, it must define a method named ``save()`` which corresponds to the signature of ``save()`` on ``ModelForm``, because this view will call it with ``commit=False`` and then fill in the relationship to the user (which must be via a field on the profile model named ``user``, a requirement already imposed by ``User.get_profile()``) before finally saving the profile object. If many-to-many relations are involved, the convention established by ``ModelForm`` of looking for a ``save_m2m()`` method on the form is used, and so your form class should define this method. To specify a URL to redirect to after successful profile creation, pass it as the keyword argument ``success_url``; this will default to the URL of the :view:`profiles.views.profile_detail` view for the new profile if unspecified. To specify the template to use, pass it as the keyword argument ``template_name``; this will default to :template:`profiles/create_profile.html` if unspecified. Context: form The profile-creation form. Template: ``template_name`` keyword argument, or :template:`profiles/create_profile.html`. """ try: profile_obj = request.user.get_profile() return HttpResponseRedirect(reverse('profiles_edit_profile')) except ObjectDoesNotExist: pass if success_url is None: success_url = reverse('profiles_profile_detail', kwargs={ 'username': request.user.username }) if form_class is None: form_class = utils.get_profile_form() if request.method == 'POST': form = form_class(data=request.POST, files=request.FILES) if form.is_valid(): profile_obj = form.save(commit=False) profile_obj.user = request.user profile_obj.save() if hasattr(form, 'save_m2m'): form.save_m2m() return HttpResponseRedirect(success_url) else: form = form_class() return render_to_response(template_name, { 'form': form }, context_instance=RequestContext(request))
def register(request, success_url=None, form_class=RegistrationForm, profile_callback=None, template_name='registration/registration_form.html', extra_context=None): """ Allow a new user to register an account. Following successful registration, issue a redirect; by default, this will be whatever URL corresponds to the named URL pattern ``registration_complete``, which will be ``/accounts/register/complete/`` if using the included URLConf. To change this, point that named pattern at another URL, or pass your preferred URL as the keyword argument ``success_url``. By default, ``registration.forms.RegistrationForm`` will be used as the registration form; to change this, pass a different form class as the ``form_class`` keyword argument. The form class you specify must have a method ``save`` which will create and return the new ``User``, and that method must accept the keyword argument ``profile_callback`` (see below). To enable creation of a site-specific user profile object for the new user, pass a function which will create the profile object as the keyword argument ``profile_callback``. See ``RegistrationManager.create_inactive_user`` in the file ``models.py`` for details on how to write this function. By default, use the template ``registration/registration_form.html``; to change this, pass the name of a template as the keyword argument ``template_name``. **Required arguments** None. **Optional arguments** ``form_class`` The form class to use for registration. ``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. ``profile_callback`` A function which will be used to create a site-specific profile instance for the new ``User``. ``success_url`` The URL to redirect to on successful registration. ``template_name`` A custom template to use. **Context:** ``form`` The registration form. Any extra variables supplied in the ``extra_context`` argument (see above). **Template:** registration/registration_form.html or ``template_name`` keyword argument. """ #pdb.set_trace() if request.method == 'POST': #logging.debug("msg1") form = form_class(data=request.POST, files=request.FILES) if form.is_valid(): #logging.debug("msg2") new_user = form.save(profile_callback=profile_callback) # success_url needs to be dynamically generated here; setting a # a default value using reverse() will cause circular-import # problems with the default URLConf for this application, which # imports this file. return HttpResponseRedirect(success_url or reverse('registration_complete')) else: #logging.debug("msg3") form = form_class() profile_form = utils.get_profile_form() #logging.debug("msg4") if extra_context is None: #logging.debug("msg5") extra_context = {} context = RequestContext(request) for key, value in extra_context.items(): #logging.debug("msg6") context[key] = callable(value) and value() or value #logging.debug("msg7") return render_to_response(template_name, { 'form': form, 'p_form': profile_form }, context_instance=context)
def register(request, backend, success_url=None, form_class=None, disallowed_url='registration_disallowed', template_name='registration/registration_form.html', extra_context=None): """ Allow a new user to register an account. The actual registration of the account will be delegated to the backend specified by the ``backend`` keyword argument (see below); it will be used as follows: 1. The backend's ``registration_allowed()`` method will be called, passing the ``HttpRequest``, to determine whether registration of an account is to be allowed; if not, a redirect is issued to the view corresponding to the named URL pattern ``registration_disallowed``. To override this, see the list of optional arguments for this view (below). 2. The form to use for account registration will be obtained by calling the backend's ``get_form_class()`` method, passing the ``HttpRequest``. To override this, see the list of optional arguments for this view (below). 3. If valid, the form's ``cleaned_data`` will be passed (as keyword arguments, and along with the ``HttpRequest``) to the backend's ``register()`` method, which should return the new ``User`` object. 4. Upon successful registration, the backend's ``post_registration_redirect()`` method will be called, passing the ``HttpRequest`` and the new ``User``, to determine the URL to redirect the user to. To override this, see the list of optional arguments for this view (below). **Required arguments** None. **Optional arguments** ``backend`` The dotted Python import path to the backend class to use. ``disallowed_url`` URL to redirect to if registration is not permitted for the current ``HttpRequest``. Must be a value which can legally be passed to ``django.shortcuts.redirect``. If not supplied, this will be whatever URL corresponds to the named URL pattern ``registration_disallowed``. ``form_class`` The form class to use for registration. If not supplied, this will be retrieved from the registration backend. ``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. ``success_url`` URL to redirect to after successful registration. Must be a value which can legally be passed to ``django.shortcuts.redirect``. If not supplied, this will be retrieved from the registration backend. ``template_name`` A custom template to use. If not supplied, this will default to ``registration/registration_form.html``. **Context:** ``form`` The registration form. Any extra variables supplied in the ``extra_context`` argument (see above). **Template:** registration/registration_form.html or ``template_name`` keyword argument. """ pform_class = utils.get_profile_form() backend = get_backend(backend) if not backend.registration_allowed(request): return redirect(disallowed_url) if form_class is None: form_class = backend.get_form_class(request) if request.method == 'POST': profileform = pform_class(data=request.POST, files=request.FILES) form = form_class(data=request.POST, files=request.FILES) if form.is_valid(): new_user = backend.register(request, **form.cleaned_data) profile_obj = profileform.save(commit=False) profile_obj.user = new_user profile_obj.save() return HttpResponseRedirect('/accounts/register/complete/') # return HttpResponseRedirect(success_url or reverse('registration_complete')) else: form = form_class() profileform = pform_class() if extra_context is None: extra_context = {} context = RequestContext(request) for key, value in extra_context.items(): context[key] = callable(value) and value() or value return render_to_response(template_name,{'form': form,'profileform':profileform}, context_instance=context)
def create_profile( request, form_class=None, success_url=None, template_name="profiles/create_profile.html", extra_context=None ): """ Create a profile for the current user, if one doesn't already exist. If the user already has a profile, as determined by ``request.user.get_profile()``, a redirect will be issued to the :view:`profiles.views.edit_profile` view. If no profile model has been specified in the ``AUTH_PROFILE_MODULE`` setting, ``django.contrib.auth.models.SiteProfileNotAvailable`` will be raised. **Optional arguments:** ``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. ``form_class`` The form class to use for validating and creating the user profile. This form class must define a method named ``save()``, implementing the same argument signature as the ``save()`` method of a standard Django ``ModelForm`` (this view will call ``save(commit=False)`` to obtain the profile object, and fill in the user before the final save). If the profile object includes many-to-many relations, the convention established by ``ModelForm`` of using a method named ``save_m2m()`` will be used, and so your form class should also define this method. If this argument is not supplied, this view will use a ``ModelForm`` automatically generated from the model specified by ``AUTH_PROFILE_MODULE``. ``success_url`` The URL to redirect to after successful profile creation. If this argument is not supplied, this will default to ``profile.get_absolute_url()``. ``template_name`` The template to use when displaying the profile-creation form. If not supplied, this will default to :template:`profiles/create_profile.html`. **Context:** ``form`` The profile-creation form. **Template:** ``template_name`` keyword argument, or :template:`profiles/create_profile.html`. """ try: profile_obj = request.user.get_profile() return redirect("profiles_edit_profile") except ObjectDoesNotExist: pass if form_class is None: form_class = utils.get_profile_form() return create_update.create_object( request, form_class=form_class(request.user), extra_context=extra_context, template_name=template_name, post_save_redirect=success_url, )
def create_profile(request, form_class=None, success_url=None, template_name='profiles/create_profile.html', extra_context=None): """ Create a profile for the current user, if one doesn't already exist. If the user already has a profile, as determined by ``request.user.get_profile()``, a redirect will be issued to the :view:`profiles.views.edit_profile` view. If no profile model has been specified in the ``AUTH_PROFILE_MODULE`` setting, ``django.contrib.auth.models.SiteProfileNotAvailable`` will be raised. **Optional arguments:** ``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. ``form_class`` The form class to use for validating and creating the user profile. This form class must define a method named ``save()``, implementing the same argument signature as the ``save()`` method of a standard Django ``ModelForm`` (this view will call ``save(commit=False)`` to obtain the profile object, and fill in the user before the final save). If the profile object includes many-to-many relations, the convention established by ``ModelForm`` of using a method named ``save_m2m()`` will be used, and so your form class should also define this method. If this argument is not supplied, this view will use a ``ModelForm`` automatically generated from the model specified by ``AUTH_PROFILE_MODULE``. ``success_url`` The URL to redirect to after successful profile creation. If this argument is not supplied, this will default to the URL of :view:`profiles.views.profile_detail` for the newly-created profile object. ``template_name`` The template to use when displaying the profile-creation form. If not supplied, this will default to :template:`profiles/create_profile.html`. **Context:** ``form`` The profile-creation form. **Template:** ``template_name`` keyword argument, or :template:`profiles/create_profile.html`. """ try: profile_obj = request.user.get_profile() return HttpResponseRedirect(reverse('profiles_edit_profile')) except ObjectDoesNotExist: pass # # We set up success_url here, rather than as the default value for # the argument. Trying to do it as the argument's default would # mean evaluating the call to reverse() at the time this module is # first imported, which introduces a circular dependency: to # perform the reverse lookup we need access to profiles/urls.py, # but profiles/urls.py in turn imports this module. # if success_url is None: success_url = reverse('profiles_profile_detail', kwargs={'username': request.user.username}) if form_class is None: form_class = utils.get_profile_form() if request.method == 'POST': form = form_class(data=request.POST, files=request.FILES) if form.is_valid(): profile_obj = form.save(commit=False) profile_obj.user = request.user profile_obj.save() if hasattr(form, 'save_m2m'): form.save_m2m() return HttpResponseRedirect(success_url) else: form = form_class() if extra_context is None: extra_context = {} context = RequestContext(request) for key, value in extra_context.items(): context[key] = callable(value) and value() or value return render_to_response(template_name, {'form': form}, context_instance=context)
def edit_profile(request, form_class=None, success_url=None, template_name='profiles/edit_profile.html', extra_context=None): """ Edit the current user's profile. If the user does not already have a profile (as determined by ``User.get_profile()``), a redirect will be issued to the :view:`profiles.views.create_profile` view; if no profile model has been specified in the ``AUTH_PROFILE_MODULE`` setting, ``django.contrib.auth.models.SiteProfileNotAvailable`` will be raised. **Optional arguments:** ``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. ``form_class`` The form class to use for validating and editing the user profile. This form class must operate similarly to a standard Django ``ModelForm`` in that it must accept an instance of the object to be edited as the keyword argument ``instance`` to its constructor, and it must implement a method named ``save()`` which will save the updates to the object. If this argument is not specified, this view will use a ``ModelForm`` generated from the model specified in the ``AUTH_PROFILE_MODULE`` setting. ``success_url`` The URL to redirect to following a successful edit. If not specified, this will default to the URL of :view:`profiles.views.profile_detail` for the profile object being edited. ``template_name`` The template to use when displaying the profile-editing form. If not specified, this will default to :template:`profiles/edit_profile.html`. **Context:** ``form`` The form for editing the profile. ``profile`` The user's current profile. **Template:** ``template_name`` keyword argument or :template:`profiles/edit_profile.html`. """ try: profile_obj = request.user.get_profile() except ObjectDoesNotExist: return HttpResponseRedirect(reverse('profiles_create_profile')) # # See the comment in create_profile() for discussion of why # success_url is set up here, rather than as a default value for # the argument. # if success_url is None: success_url = reverse('profiles_profile_detail', kwargs={'username': request.user.username}) if form_class is None: form_class = utils.get_profile_form() if request.method == 'POST': form = form_class(data=request.POST, files=request.FILES, instance=profile_obj) if form.is_valid(): form.save() return HttpResponseRedirect(success_url) else: form = form_class(instance=profile_obj) if extra_context is None: extra_context = {} context = RequestContext(request) for key, value in extra_context.items(): context[key] = callable(value) and value() or value return render_to_response(template_name, { 'form': form, 'profile': profile_obj, }, context_instance=context)