def user_profile(request): # Add this to improve folder selection if not request.session.get('filer_last_folder_id'): from filer.models import Folder folder = Folder.objects.get(owner=request.user, name=request.user.username) request.session['filer_last_folder_id'] = folder.id try: profile_form = ProfileForm(instance=request.user.userprofile) except: profile_form = ProfileForm() if request.POST: try: profile_form = ProfileForm(request.POST, instance=request.user.userprofile) except: profile_form = ProfileForm(request.POST) if profile_form.is_valid(): profile = profile_form.save(commit=False) profile.user = request.user profile.save() messages.add_message(request, messages.INFO, _(u'Your profile is saved.')) else: messages.add_message( request, messages.ERROR, _(u'One or more errors have been found in the form.')) return render_to_response("userProfile.html", { 'form': profile_form, }, context_instance=RequestContext(request))
def signup(request): """ signup """ if request.user.is_authenticated(): return render_to_response( 'core/index.html', {}, context_instance=RequestContext(request) ) if request.method == 'POST': form = UserCreationForm(request.POST) user_form = ProfileForm(None,request.POST) if form.is_valid() and user_form.is_valid(): new_user = form.save() user_data = user_form.cleaned_data new_user.email = user_data['email'] new_user.first_name = user_data['first_name'] new_user.last_name = user_data['last_name'] new_user.save() new_user = authenticate(username=new_user.username, password=form.cleaned_data['password1']) login(request,new_user) new_user.message_set.create(message="Thanks For Register, Enjoy") return HttpResponseRedirect(reverse('core-home')) else: form = UserCreationForm() user_form = ProfileForm(user=None) return render_to_response("registration/register.html", { 'form' : form, 'user_form':user_form, },context_instance=RequestContext(request))
def profile(request): profile_inline_formset = inlineformset_factory(User, UserProfile, fields=('avatar', )) if request.method == 'POST': form = ProfileForm(data=request.POST, instance=request.user) formset = profile_inline_formset(request.POST, request.FILES, instance=request.user) if form.is_valid(): perfil = form.save(commit=False) formset = profile_inline_formset(request.POST, request.FILES, instance=perfil) if formset.is_valid(): perfil.save() formset.save() return redirect('dashboard') else: form = ProfileForm(instance=request.user) formset = profile_inline_formset(instance=request.user) context = { 'form': form, 'formset': formset, } return render(request, 'profile.html', context)
def settings(request): user = request.user #import pdb; pdb.set_trace() if request.method == 'POST': form = ProfileForm(request.POST) if form.is_valid(): user.first_name = form.cleaned_data.get('first_name') user.last_name = form.cleaned_data.get('last_name') user.profile.job_title = form.cleaned_data.get('job_title') user.email = form.cleaned_data.get('email') user.profile.url = form.cleaned_data.get('url') user.profile.location = form.cleaned_data.get('location') user.save() messages.add_message(request, messages.SUCCESS, 'Your profile was successfully edited.') else: form = ProfileForm(instance=user, initial={ 'job_title': user.profile.job_title, 'url': user.profile.url, 'location': user.profile.location }) #import pdb; pdb.set_trace() return render(request, 'core/settings.html', {'form': form})
def family(request): if not request.user.is_authenticated(): return render(request, 'core/login_needed.html', {'navbar':'transact'}) # for navbar active state triggering profile = UserProfile.objects.get(user_id=request.user.id) form = ProfileForm(request.POST or None, instance=profile) User = get_user_model() family_members = User.objects.filter( userprofile__family_id=request.user.userprofile.family_id).\ exclude(id=request.user.id) if form.is_valid(): # print(form.cleaned_data['new_pin']) instance = form.save(commit=False) new_family = Family.objects.get(id=instance.family_id.id) if instance.family_id.id == request.user.userprofile.family_id.id: return render(request, 'core/same_family_pin.html', {'family_requested':new_family.id, 'navbar':'settings'}) if new_family.pin == form.cleaned_data['new_pin']: family_members = User.objects.filter(userprofile__family_id=new_family.id).\ exclude(id=request.user.id) instance.save() else: return render(request, 'core/incorrect_pin.html', {'family_requested':new_family.id, 'navbar':'settings'}) return render(request, 'core/correct_pin.html', {'family_requested': new_family.id, 'family_members': family_members, 'navbar':'settings'}) userr = User.objects.get(username=request.user) return render(request, 'core/family.html', {'form': form, 'family_members': family_members, 'navbar':'settings', 'userr':userr})
def settings(request): user = request.user if request.method == 'POST': form = ProfileForm(request.POST, instance=request.user) user_form = UserEditForm(instance=request.user, data=request.POST) if form.is_valid() and user_form.is_valid(): user.first_name = form.cleaned_data.get('first_name') user.last_name = form.cleaned_data.get('last_name') user.profile.job_title = form.cleaned_data.get('job_title') user.email = form.cleaned_data.get('email') user.profile.url = form.cleaned_data.get('url') user.profile.location = form.cleaned_data.get('location') user.profile.about = form.cleaned_data.get('about') user.save() messages.add_message(request, messages.SUCCESS, 'Your profile was successfully edited.') else: user_form = UserEditForm(instance=request.user) form = ProfileForm(instance=user, initial={ 'job_title': user.profile.job_title, 'url': user.profile.url, 'location': user.profile.location, 'about': user.profile.about, }) return render(request, 'core/settings.html', {'form': form})
def user_profile(request): #Add this to improve folder selection if not request.session.get('filer_last_folder_id'): from filer.models import Folder folder = Folder.objects.get(owner=request.user, name=request.user.username) request.session['filer_last_folder_id'] = folder.id try: profile_form = ProfileForm(instance=request.user.userprofile) except: profile_form = ProfileForm() if request.POST: try: profile_form = ProfileForm(request.POST,instance=request.user.userprofile) except: profile_form = ProfileForm(request.POST) if profile_form.is_valid(): profile = profile_form.save(commit=False) profile.user = request.user profile.save() messages.add_message(request, messages.INFO, _(u'Your profile is saved')) else: messages.add_message(request, messages.ERROR, _(u'Error in the form')) return render_to_response("userProfile.html", {'form':profile_form, }, context_instance=RequestContext(request))
def signup(request): if request.method == 'POST': form2 = ProfileForm(request.POST) form1 = SignUpForm(request.POST) if form1.is_valid() and form2.is_valid(): # form1.save() # user = form1.save() # form2 = form2.save(commit=False) # form2.user = user # form2.save() user = form1.save(commit=False) user.is_active = False # Deactivate account till it is confirmed user.save() form2 = form2.save(commit=False) form2.user = user form2.save() current_site = get_current_site(request) mail_subject = 'Activate your Ergo Life Account.' message = render_to_string( 'account/email_confirmation_message.html', { 'user': user, 'domain': current_site.domain, 'uid': urlsafe_base64_encode(force_bytes(user.pk)), 'token': account_activation_token.make_token(user), }) to_email = form1.cleaned_data.get('email') email = EmailMessage(mail_subject, message, to=[to_email]) email.send() messages.success(request, ( 'Please Confirm your email to complete registration. You are able to login once your email gets verified' )) # return HttpResponse('Please confirm your email address to complete the registration') return redirect('/') # current_site = get_current_site(request) # subject = 'Activate Your ErgoLife Account' # message = render_to_string('account/email_confirmation_message.html', { # 'user': user, # 'domain': current_site.domain, # 'uid': urlsafe_base64_encode(force_bytes(user.pk)), # 'token': account_activation_token.make_token(user), # }) # user.email_user(subject, message) # form2.save() # messages.success(request, ('Please Confirm your email to complete registration.')) # messages.success(request, f'Registration complete! You may log in!') # username = form1.cleaned_data.get('username') # raw_password = form1.cleaned_data.get('password1') # user = authenticate(username=username, password=raw_password) # login(request, user) # return redirect('home') # return redirect('/') else: form2 = ProfileForm() form1 = SignUpForm() return render(request, 'signup.html', {'form1': form1, 'form2': form2})
def test_invalid_profile_form(self): user = self.create_user_with_profile() data = { "past_address": user.profile.past_address, "present_address": user.profile.present_address, "phone_number": "" } form = ProfileForm(data=data) self.assertFalse(form.is_valid())
def update_profile(request): if request.method == 'POST': profile_form = ProfileForm(request.POST, instance=request.user.profile) if profile_form.is_valid(): profile_form.save() messages.success(request, 'Your profile picture was successfully updated.') return redirect('settings:profile') #or core:profile else: profile_form = ProfileForm(instance=request.user.profile) return render(request, 'core/profile_form.html', {'profile_form': profile_form})
def profile_edition(request): if request.method == 'POST': form = ProfileForm(request.POST, instance=request.user.profile) if form.is_valid(): form.save() return HttpResponseRedirect(reverse('core:my_profile')) else: form = ProfileForm(instance=request.user.profile) context = {'form': form} return render(request, 'profile/edition.html', context)
def update_profile(request): user = Profile.objects.get(user=request.user) form = ProfileForm(instance=request.user) if request.method == "POST": form = ProfileForm(request.POST,instance=request.user) if form.is_valid(): profile = form.save(commit=False) profile.save() return redirect(to='home') else: form = ProfileForm(instance=request.user) return render(request, 'update_profile.html', {'form': form})
def update_profile(request): if request.method == 'POST': form = ProfileForm(request.POST, instance=request.user.profile) if form.is_valid(): form.save() messages.success(request, _('성공적으로 변경되었습니다.')) return redirect('home') else: messages.error(request, _('에러를 확인해 주십시오.')) else: form = ProfileForm(instance=request.user.profile) return render(request, 'profile/profile-update.html',{'form':form})
def test_it_is_possible_to_create_profiles(self): """ Ensure users can create new profiles """ form = ProfileForm(data=self.profile_data) self.assertTrue(form.is_valid()) profile = form.save() self.assertEqual(profile.user.first_name, self.profile_data['first_name']) self.assertEqual(profile.user.last_name, self.profile_data['last_name']) self.assertEqual(profile.user.email, self.profile_data['email'])
def test_blank_data(self): """ Ensure the form return error message with required fields """ form = ProfileForm(data={}) default_error_message = { 'first_name': ['Este campo é obrigatório.'], 'email': ['Este campo é obrigatório.'], 'password': ['Este campo é obrigatório.'], 'password_confirm': ['Este campo é obrigatório.'] } self.assertFalse(form.is_valid()) self.assertEqual(form.errors, default_error_message)
def settings(request): user = request.user if request.method == 'POST': form = ProfileForm(request.POST) if form.is_valid(): user.first_name = form.cleaned_data.get('first_name') user.last_name = form.cleaned_data.get('last_name') user.email = form.cleaned_data.get('email') user.save() messages.add_message(request, messages.SUCCESS, 'Your profile was successfully edited.') else: form = ProfileForm(instance=user) return render(request, 'core/settings.html', {'form': form})
def ManagerMemberView(request, email): post = Profile.objects.get(company_id=email) if request.method == "POST": form = ProfileForm(request.POST or None, request.FILES or None, instance=post) if form.is_valid(): post = form.save(commit=False) post.save() return redirect('home') else: form = ProfileForm(instance=post) template = 'core/member.html' context = { 'form': form, 'post': post } return render(request, template, context)
def profile_edit(request): if Profile.objects.filter(user=request.user.pk).count(): p = Profile.objects.filter(user=request.user.pk)[:1].get() else: p = Profile() p.user = request.user.pk if request.method == 'GET': form = ProfileForm(instance=p) context = { 'form': form, } return render(request, 'profile.html', context) elif request.method == 'POST': form = ProfileForm(request.POST, request.FILES, instance=p) if form.is_valid(): form.save() messages.success(request, 'Perfil atualizado com sucesso!') return redirect('root:profile_edit') return render(request, 'profile.html', {'form': form})
def core_profile(request): if request.method == 'POST': user_form = UserForm(request.POST, instance=request.user) profile_form = ProfileForm(request.POST, instance=request.user.profile) if user_form.is_valid() and profile_form.is_valid(): user_form.save() profile_form.save() messages.success(request, 'Your profile was successfully updated!') return redirect('core_index') else: messages.error(request, 'Please correct the error below.') else: user_form = UserForm(instance=request.user) profile_form = ProfileForm(instance=request.user.profile) return render(request, 'core_profile_edit.html', { 'user_form': user_form, 'profile_form': profile_form })
def profile_edit(request,username): """ profile edit context: - form """ user = get_object_or_404(User,username=username) if request.method == 'POST': form = ProfileForm(user,request.POST) if form.is_valid(): form.save() request.user.message_set.create(message="Profile has been updated") return HttpResponseRedirect(reverse('core-profile',args=[user.username])) else: form = ProfileForm(user) return render_to_response('core/profile_edit.html', {'form':form}, context_instance = RequestContext(request))
def family(request): if not request.user.is_authenticated(): return render( request, 'core/login_needed.html', {'navbar': 'transact'}) # for navbar active state triggering profile = UserProfile.objects.get(user_id=request.user.id) form = ProfileForm(request.POST or None, instance=profile) User = get_user_model() family_members = User.objects.filter( userprofile__family_id=request.user.userprofile.family_id).\ exclude(id=request.user.id) if form.is_valid(): # print(form.cleaned_data['new_pin']) instance = form.save(commit=False) new_family = Family.objects.get(id=instance.family_id.id) if instance.family_id.id == request.user.userprofile.family_id.id: return render(request, 'core/same_family_pin.html', { 'family_requested': new_family.id, 'navbar': 'settings' }) if new_family.pin == form.cleaned_data['new_pin']: family_members = User.objects.filter(userprofile__family_id=new_family.id).\ exclude(id=request.user.id) instance.save() else: return render(request, 'core/incorrect_pin.html', { 'family_requested': new_family.id, 'navbar': 'settings' }) return render( request, 'core/correct_pin.html', { 'family_requested': new_family.id, 'family_members': family_members, 'navbar': 'settings' }) userr = User.objects.get(username=request.user) return render( request, 'core/family.html', { 'form': form, 'family_members': family_members, 'navbar': 'settings', 'userr': userr })
def settings(request): user = request.user if request.method == "POST": form = ProfileForm(request.POST) if form.is_valid(): user.first_name = form.cleaned_data.get("first_name") user.last_name = form.cleaned_data.get("last_name") user.profile.job_title = form.cleaned_data.get("job_title") user.email = form.cleaned_data.get("email") user.profile.url = form.cleaned_data.get("url") user.profile.location = form.cleaned_data.get("location") user.save() messages.add_message(request, messages.SUCCESS, "Your profile were successfully edited.") else: form = ProfileForm( instance=user, initial={"job_title": user.profile.job_title, "url": user.profile.url, "location": user.profile.location}, ) return render(request, "core/settings.html", {"form": form})
def post(self, request, *args, **kwargs): p = Company.objects.get(email=request.user.username) profile = Profile() picture = Profile.picture form = ProfileForm(request.POST or None, request.FILES or None) if form.is_valid(): profile1 = form.save(commit=False) profile1.user = request.user profile1.name = request.user.first_name profile1.company = p profile1.save() return redirect('profile') else: form = ProfileForm() context = { 'picture': picture, 'form': form, 'p': p } return render(request, self.template_name, context)
def profile_page(request,pk): user = Profile.objects.get(pk=pk) existing_contact = Contact.objects.filter(user=request.user).filter(name=user) user_contacts = Contact.objects.filter(user=request.user) if request.method == 'POST' and 'save-profile' in request.POST: form = ProfileForm(request.POST, instance=request.user.profile) if form.is_valid(): form.save() messages.success(request, 'Your profile was updated successfully!') pk = request.user.id return redirect(to='profile', pk=pk) if request.method == 'POST' and 'search-contact' in request.POST: search_user_form = ProfileSearch(request.POST) if search_user_form.is_valid(): search_name = search_user_form.cleaned_data.get('user_search') search_filter = User.objects.filter(username=search_name) if search_filter: user_pk=search_filter[0].pk return redirect(to='profile', pk=user_pk) else: messages.info(request, 'Profile not found') return redirect(to='profile', pk=pk) else: form = ProfileForm(instance=request.user.profile) search_user_form = ProfileSearch() return render(request, 'profile.html', { 'user' : user, 'existing_contact': existing_contact, 'form': form, 'search_user_form': search_user_form, 'user_contacts': user_contacts, })
def settings(request): user = request.user if request.method == 'POST': form = ProfileForm(request.POST) if form.is_valid(): user.first_name = form.cleaned_data.get('first_name') user.last_name = form.cleaned_data.get('last_name') user.profile.job_title = form.cleaned_data.get('job_title') user.email = form.cleaned_data.get('email') user.profile.url = form.cleaned_data.get('url') user.profile.location = form.cleaned_data.get('location') user.save() messages.add_message(request, messages.SUCCESS, 'Your profile were successfully edited.') else: form = ProfileForm(instance=user, initial={ 'job_title': user.profile.job_title, 'url': user.profile.url, 'location': user.profile.location }) return render(request, 'core/settings.html', {'form':form})
def profile(request): profile = Profile.objects.filter(user=request.user).first() if request.method == 'POST': form = EditProfile(request.POST, instance=request.user) profle_form = ProfileForm(request.POST, request.FILES, instance=profile) if form.is_valid() and profle_form.is_valid(): user_form = form.save() custom_form = profle_form.save(False) custom_form.user = user_form custom_form.save() messages.success(request, 'user profile has been updated') return redirect('/') else: messages.error(request, 'Submission Error') if request.method == 'GET': form = EditProfile(instance=request.user) profle_form = ProfileForm(instance=profile) context = {'form': form, 'profile_form': profle_form} return render(request, 'profile.html', context)
def profile_edit(request): profile_inline_formset = inlineformset_factory(User, UserProfile, fields=('avatar',), extra=0, can_delete=False, min_num=1, max_num=1, validate_min=True, validate_max=True) if request.method == 'POST': form = ProfileForm(data=request.POST, instance=request.user) formset = profile_inline_formset(request.POST, request.FILES, instance=request.user) if form.is_valid(): perfil = form.save(commit=False) formset = profile_inline_formset(request.POST, request.FILES, instance=perfil) if formset.is_valid(): perfil.save() formset.save() return redirect('profile_edit') else: form = ProfileForm(instance=request.user) formset = profile_inline_formset(instance=request.user) return render(request, 'core/profile.html', {'form': form, 'formset': formset, })
def createProfile(request): instance = get_object_or_404(UserProfile, user=request.user) data = {'bio': instance.bio, 'picture': instance.picture} profile = UserProfile.objects.all().get(user=request.user) if request.method == "POST": form_instance = ProfileForm(request.POST, request.FILES, instance=instance, initial=data) if (form_instance.is_valid()): instance = form_instance.save(commit=False) instance.user = request.user username = instance.user.username instance.save() return redirect('profile', username=username) else: form_instance = ProfileForm() context = { "form": form_instance, "profile": profile, "is_user": checkAuth(request), "user": request.user, } return render(request, 'chat/createProfile.html', context=context)
def settings(request): stip_user = request.user profile = stip_user.sns_profile if request.method == 'POST': form = ProfileForm(request.POST) country = request.POST['country'] form.fields[ 'administrative_area'].choices = Region.get_administrative_areas_choices( country) if form.is_valid(): stip_user.screen_name = form.cleaned_data.get('screen_name') stip_user.timezone = form.cleaned_data.get('timezone') stip_user.affiliation = form.cleaned_data.get('affiliation') stip_user.job_title = form.cleaned_data.get('job_title') stip_user.url = form.cleaned_data.get('url') stip_user.location = form.cleaned_data.get('location') stip_user.description = form.cleaned_data.get('description') stip_user.tlp = form.cleaned_data.get('tlp') code = form.cleaned_data.get('administrative_area') try: region = Region.objects.get(code=code) administraive_code = region.code administraive_area = region.administrative_area country_code = region.country_code except BaseException: region = None administraive_code = None administraive_area = None country_code = form.cleaned_data.get('country') stip_user.region = region stip_user.administrative_code = administraive_code stip_user.administrative_area = administraive_area stip_user.country_code = country_code stip_user.ci = form.cleaned_data.get('ci') stip_user.language = form.cleaned_data.get('language') profile.scan_csv = form.cleaned_data.get('scan_csv') profile.scan_pdf = form.cleaned_data.get('scan_pdf') profile.scan_post = form.cleaned_data.get('scan_post') profile.scan_txt = form.cleaned_data.get('scan_txt') profile.threat_actors = form.cleaned_data.get('threat_actors') profile.indicator_white_list = form.cleaned_data.get( 'indicator_white_list') profile.phantom_host = form.cleaned_data.get('phantom_host') profile.phantom_source_name = form.cleaned_data.get( 'phantom_source_name') profile.phantom_playbook_name = form.cleaned_data.get( 'phantom_playbook_name') phantom_auth_token = form.cleaned_data.get('phantom_auth_token') if len(phantom_auth_token) != 0: profile.phantom_auth_token = form.cleaned_data.get( 'phantom_auth_token') profile.splunk_host = form.cleaned_data.get('splunk_host') profile.splunk_api_port = form.cleaned_data.get('splunk_api_port') profile.splunk_web_port = form.cleaned_data.get('splunk_web_port') profile.splunk_username = form.cleaned_data.get('splunk_username') splunk_password = form.cleaned_data.get('splunk_password') if len(splunk_password) != 0: profile.splunk_password = splunk_password profile.splunk_scheme = form.cleaned_data.get('splunk_scheme') profile.splunk_query = form.cleaned_data.get('splunk_query') stip_user.save() profile.save() messages.add_message(request, messages.SUCCESS, _('Your profile was successfully edited.')) lang = stip_user.language translation.activate(lang) request.session['_language'] = lang else: if stip_user.region is None: country = stip_user.country_code code = '' else: country = stip_user.region.country_code code = stip_user.region.code form = ProfileForm(instance=profile, initial={ 'screen_name': stip_user.screen_name, 'affiliation': stip_user.affiliation, 'job_title': stip_user.job_title, 'url': stip_user.url, 'location': stip_user.location, 'description': stip_user.description, 'tlp': stip_user.tlp, 'country': country, 'administrative_area': code, 'ci': stip_user.ci, 'language': stip_user.language, 'scan_csv': profile.scan_csv, 'scan_pdf': profile.scan_pdf, 'scan_post': profile.scan_post, 'scan_txt': profile.scan_txt, 'threat_actors': profile.threat_actors, 'indicator_white_list': profile.indicator_white_list, 'timezone': stip_user.timezone, 'phantom_host': profile.phantom_host, 'phantom_source_name': profile.phantom_source_name, 'phantom_playbook_name': profile.phantom_playbook_name, 'phantom_auth_token': profile.phantom_auth_token, 'splunk_host': profile.splunk_host, 'splunk_api_port': profile.splunk_api_port, 'splunk_web_port': profile.splunk_web_port, 'splunk_username': profile.splunk_username, 'splunk_password': profile.splunk_password, 'splunk_scheme': profile.splunk_scheme, 'splunk_query': profile.splunk_query, }) form.fields[ 'administrative_area'].choices = Region.get_administrative_areas_choices( country) return render(request, 'core/settings.html', {'form': form})
def view_profile(request, uname): profile_owner = get_object_or_404(User, username=uname) user_profile = profile_owner.get_profile() profile_edit_form = None # if the current user is the profile owner, give them the profile edit form if request.user == profile_owner: profile_edit_form = ProfileForm(instance=user_profile) # if this is a POST from the profile's owner, assume (for now) that the profile edit form was submitted and update accordingly if request.method == 'POST': profile_edit_form = ProfileForm(request.POST, request.FILES, instance=user_profile) if profile_edit_form.is_valid(): profile_edit_form.save() messages.success(request, 'Your profile changes have been saved') return redirect('view_profile', uname=uname) user_blurb = user_profile.blurb courses_created_by_user = profile_owner.courses_created.all() upvoted_lessons = LessonVote.objects.filter(user_profile=profile_owner).select_related('course') # courses_following = profile_owner.courses_following.select_related('category').all() # lessons_following = Lesson.objects.filter(course__in=courses_following) # lessons_following_completed = Lesson.objects.filter(course__in=courses_following, completers=profile_owner) # lessons = { les['id'] : les for les in lessons_following.values() } # completed_lessons = { les['id'] : les for les in lessons_following_completed.values() } # courses_following_with_progress = { c['id'] : c for c in courses_following.values() } # for c in courses_following: # courses_following_with_progress[c.id]['category_id'] = c.category.id # courses_following_with_progress[c.id]['category'] = c.category.name # course_progress = { c.id : { 'total' : 0, 'completed' : 0 } for c in courses_following } # for les_id, les in lessons.iteritems(): # course_progress[les['course_id']]['total'] = course_progress[les['course_id']]['total'] + 1 # if completed_lessons.has_key(les_id): # course_progress[les['course_id']]['completed'] = course_progress[les['course_id']]['completed'] + 1 # # for c_id, c in courses_following_with_progress.iteritems(): # c['total'] = course_progress.get(c_id, 0)['total'] # c['completed'] = course_progress.get(c_id, 0)['completed'] # courses_following = courses_following_with_progress.values() return render_to_response('view_profile.dtl', { 'profile_owner': profile_owner, 'user_blurb': user_blurb, 'courses_created_by_user': courses_created_by_user, # 'courses_following': courses_following, 'upvoted_lessons': upvoted_lessons, 'is_my_profile': (profile_owner == request.user), 'profile_edit_form': profile_edit_form }, context_instance=RequestContext(request))
def view_profile(request, uname): profile_owner = get_object_or_404(User, username=uname) user_profile = profile_owner.get_profile() profile_edit_form = None # if the current user is the profile owner, give them the profile edit form if request.user == profile_owner: profile_edit_form = ProfileForm(instance=user_profile) # if this is a POST from the profile's owner, assume (for now) that the profile edit form was submitted and update accordingly if request.method == 'POST': profile_edit_form = ProfileForm(request.POST, request.FILES, instance=user_profile) if profile_edit_form.is_valid(): profile_edit_form.save() messages.success(request, 'Your profile changes have been saved') return redirect('view_profile', uname=uname) user_blurb = user_profile.blurb courses_created_by_user = profile_owner.courses_created.all() upvoted_lessons = LessonVote.objects.filter( user_profile=profile_owner).select_related('course') # courses_following = profile_owner.courses_following.select_related('category').all() # lessons_following = Lesson.objects.filter(course__in=courses_following) # lessons_following_completed = Lesson.objects.filter(course__in=courses_following, completers=profile_owner) # lessons = { les['id'] : les for les in lessons_following.values() } # completed_lessons = { les['id'] : les for les in lessons_following_completed.values() } # courses_following_with_progress = { c['id'] : c for c in courses_following.values() } # for c in courses_following: # courses_following_with_progress[c.id]['category_id'] = c.category.id # courses_following_with_progress[c.id]['category'] = c.category.name # course_progress = { c.id : { 'total' : 0, 'completed' : 0 } for c in courses_following } # for les_id, les in lessons.iteritems(): # course_progress[les['course_id']]['total'] = course_progress[les['course_id']]['total'] + 1 # if completed_lessons.has_key(les_id): # course_progress[les['course_id']]['completed'] = course_progress[les['course_id']]['completed'] + 1 # # for c_id, c in courses_following_with_progress.iteritems(): # c['total'] = course_progress.get(c_id, 0)['total'] # c['completed'] = course_progress.get(c_id, 0)['completed'] # courses_following = courses_following_with_progress.values() return render_to_response( 'view_profile.dtl', { 'profile_owner': profile_owner, 'user_blurb': user_blurb, 'courses_created_by_user': courses_created_by_user, # 'courses_following': courses_following, 'upvoted_lessons': upvoted_lessons, 'is_my_profile': (profile_owner == request.user), 'profile_edit_form': profile_edit_form }, context_instance=RequestContext(request))