Skip to content
This repository has been archived by the owner on Jan 22, 2021. It is now read-only.

Automatic user profile creation #36

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 26 additions & 3 deletions basic/profiles/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,19 @@
from basic.profiles.models import *
from basic.profiles.forms import *

from django.core.exceptions import ObjectDoesNotExist

def get_or_create_profile(user):
try:
#Update by Leon de Almeida:
# enable profiles to be created automatically if they don't exist
#profile = user.get_profile()
profile = Profile.objects.get(user=user)
except ObjectDoesNotExist:
profile = Profile(user=user)
profile.save()

return profile

def profile_list(request):
return list_detail.object_list(
Expand All @@ -23,7 +36,11 @@ def profile_detail(request, username):
user = User.objects.get(username__iexact=username)
except User.DoesNotExist:
raise Http404
profile = Profile.objects.get(user=user)

#Update by Leon de Almeida:
# enable profiles to be created automatically if they don't exist
#profile = Profile.objects.get(user=user)
profile = get_or_create_profile(user=user)
context = { 'object':profile }
return render_to_response('profiles/profile_detail.html', context, context_instance=RequestContext(request))

Expand All @@ -33,7 +50,10 @@ def profile_edit(request, template_name='profiles/profile_form.html'):
"""Edit profile."""

if request.POST:
profile = Profile.objects.get(user=request.user)
#Update by Leon de Almeida:
# enable profiles to be created automatically if they don't exist
#profile = Profile.objects.get(user=request.user)
profile = get_or_create_profile(user=request.user)
profile_form = ProfileForm(request.POST, request.FILES, instance=profile)
user_form = UserForm(request.POST, instance=request.user)
service_formset = ServiceFormSet(request.POST, instance=profile)
Expand All @@ -53,7 +73,10 @@ def profile_edit(request, template_name='profiles/profile_form.html'):
'link_formset': link_formset
}
else:
profile = Profile.objects.get(user=request.user)
#Update by Leon de Almeida:
# enable profiles to be created automatically if they don't exist
#profile = Profile.objects.get(user=request.user)
profile = get_or_create_profile(user=request.user)
service_formset = ServiceFormSet(instance=profile)
link_formset = LinkFormSet(instance=profile)
context = {
Expand Down