def contrib_viewall(request): """ View to display all contributions """ is_loggedin, username = get_session_variables(request) contrib_list = Contribution.objects.all() contrib_org = {} if contrib_list: for contrib in contrib_list: if contrib.org_name not in contrib_org.keys(): contrib_org[contrib.org_name] = 0 for contrib in contrib_list: contrib_org[contrib.org_name] += 1 if contrib_list: return render_to_response('achievement/contrib_viewall.html', \ {'is_loggedin':logged_in(request), \ 'username':username, \ 'contrib_list':contrib_list, 'contrib_org':contrib_org}, \ RequestContext(request)) else: return render_to_response('achievement/noview.html', \ {'is_loggedin':is_loggedin, \ 'username':username, \ 'type': 'Contribution'}, \ RequestContext(request))
def insert_intern(request): """ View to add internship details Models used: Achievement, Intern """ try: is_loggedin, username = get_session_variables(request) # User is not logged in if not logged_in(request): return HttpResponseRedirect('/register/login') # User is logged in else: if request.method == 'POST': form = AddInternForm(request.POST) # Invalid form imput if not form.is_valid(): error = "Invalid inputs" return render_to_response('achievement/new_intern.html', \ {'form':form, \ 'error':error, \ 'is_loggedin':is_loggedin, \ 'username':username}, \ RequestContext(request)) # Form is valid else: # Get the new achievement_id achievement_id = get_achievement_id(request) achievement_type = "Intern" # Saving inputs achievement_obj = Achievement(achievement_id, \ achievement_type, \ username) achievement_obj.save() contribution_obj = form.save(commit=False) contribution_obj.achievement_id = achievement_obj contribution_obj.achieve_typ = achievement_type user_obj = get_object_or_404(User_info, username=username) contribution_obj.username = user_obj contribution_obj.save() return render_to_response('achievement/success.html', \ {'achievement_type':achievement_type, \ 'is_loggedin':is_loggedin, \ 'username':username}, \ RequestContext(request)) # Method is not POST else: return render_to_response('achievement/new_intern.html', \ {'form': AddInternForm, \ 'is_loggedin':is_loggedin, \ 'username':username}, \ RequestContext(request)) except KeyError: return error_key(request)
def insert_intern(request): """ View to add internship details Models used: Achievement, Intern """ try: is_loggedin, username = get_session_variables(request) # User is not logged in if not logged_in(request): return HttpResponseRedirect('/register/login') # User is logged in else: if request.method == 'POST': form = AddInternForm(request.POST) # Invalid form imput if not form.is_valid(): error = "Invalid inputs" return render_to_response('achievement/new_intern.html', \ {'form':form, \ 'error':error, \ 'is_loggedin':is_loggedin, \ 'username':username}, \ RequestContext(request)) # Form is valid else: # Get the new achievement_id achievement_id = get_achievement_id(request) achievement_type = "Intern" # Saving inputs achievement_obj = Achievement(achievement_id, \ achievement_type, \ username) achievement_obj.save() contribution_obj = form.save(commit = False) contribution_obj.achievement_id = achievement_obj contribution_obj.achieve_typ = achievement_type user_obj = get_object_or_404(User_info, username = username) contribution_obj.username = user_obj contribution_obj.save() return render_to_response('achievement/success.html', \ {'achievement_type':achievement_type, \ 'is_loggedin':is_loggedin, \ 'username':username}, \ RequestContext(request)) # Method is not POST else: return render_to_response('achievement/new_intern.html', \ {'form': AddInternForm, \ 'is_loggedin':is_loggedin, \ 'username':username}, \ RequestContext(request)) except KeyError: return error_key(request)
def update_profile(request): try: is_loggedin, username = get_session_variables(request) # User is not logged in if not logged_in(request): return HttpResponseRedirect('/register/login') else: user_details = get_object_or_404(User_info, username=username) init_user_details = user_details.__dict__ #If method is not POST if request.method != 'POST': #return form with old details return render_to_response('register/update_profile.html',\ {'form':UpdateProfileForm(init_user_details),\ 'is_loggedin':is_loggedin, 'username':username},\ RequestContext(request)) # If method is POST else: profile_update_form = UpdateProfileForm(request.POST) # Form is not valid if not profile_update_form.is_valid(): #return form with old details print profile_update_form.cleaned_data return render_to_response('register/update_profile.html',\ {'form':UpdateProfileForm(init_user_details),\ 'is_loggedin':is_loggedin, 'username':username},\ RequestContext(request)) # Form is valid: else: user_details_form = profile_update_form.save(commit=False) user_details_obj = get_object_or_404(User_info, username=username) user_details_obj.firstname = user_details_form.firstname user_details_obj.lastname = user_details_form.lastname user_details_obj.gender = user_details_form.gender user_details_obj.contact = user_details_form.contact user_details_obj.role = user_details_form.role user_details_obj.blog_url = user_details_form.blog_url user_details_obj.twitter_id = user_details_form.twitter_id user_details_obj.bitbucket_id = user_details_form.topcoder_handle user_details_obj.github_id = user_details_form.github_id user_details_obj.bitbucket_id = user_details_form.bitbucket_id user_details_obj.typing_speed = user_details_form.typing_speed user_details_obj.interest = user_details_form.interest user_details_obj.expertise = user_details_form.expertise user_details_obj.goal = user_details_form.goal #user_details_obj.email = user_details_form.email user_details_obj.save() redirect_url = "/register/profile/" + username + "/" return HttpResponseRedirect(redirect_url) except KeyError: return error_key(request)
def update_profile(request): try: is_loggedin, username = get_session_variables(request) # User is not logged in if not logged_in(request): return HttpResponseRedirect('/register/login') else: user_details = get_object_or_404(User_info, username = username) init_user_details = user_details.__dict__ #If method is not POST if request.method != 'POST': #return form with old details return render_to_response('register/update_profile.html',\ {'form':UpdateProfileForm(init_user_details),\ 'is_loggedin':is_loggedin, 'username':username},\ RequestContext(request)) # If method is POST else: profile_update_form = UpdateProfileForm(request.POST) # Form is not valid if not profile_update_form.is_valid(): #return form with old details print profile_update_form.cleaned_data return render_to_response('register/update_profile.html',\ {'form':UpdateProfileForm(init_user_details),\ 'is_loggedin':is_loggedin, 'username':username},\ RequestContext(request)) # Form is valid: else: user_details_form = profile_update_form.save(commit = False) user_details_obj = get_object_or_404(User_info, username = username) user_details_obj.firstname = user_details_form.firstname user_details_obj.lastname = user_details_form.lastname user_details_obj.gender = user_details_form.gender user_details_obj.contact = user_details_form.contact user_details_obj.role = user_details_form.role user_details_obj.blog_url = user_details_form.blog_url user_details_obj.twitter_id = user_details_form.twitter_id user_details_obj.bitbucket_id = user_details_form.topcoder_handle user_details_obj.github_id = user_details_form.github_id user_details_obj.bitbucket_id = user_details_form.bitbucket_id user_details_obj.typing_speed = user_details_form.typing_speed user_details_obj.interest= user_details_form.interest user_details_obj.expertise = user_details_form.expertise user_details_obj.goal = user_details_form.goal #user_details_obj.email = user_details_form.email user_details_obj.save() redirect_url = "/register/profile/"+username+"/" return HttpResponseRedirect(redirect_url) except KeyError: return error_key(request)
def update_article(request, achievement_id): """ View to update the artciel information """ try: is_loggedin, username = get_session_variables(request) # User is not logged in if not logged_in(request): return HttpResponseRedirect('/register/login') else: article = get_object_or_404(Article, achievement_id=achievement_id) init_article = article.__dict__ #If method is not POST if request.method != 'POST': #return form with old details return render_to_response('achievement/update_article.html',\ {'form':UpdateArticleForm(init_article),\ 'is_loggedin':is_loggedin, 'username':username},\ RequestContext(request)) # If method is POST else: article_update_form = UpdateArticleForm(request.POST) # Form is not valid if not article_update_form.is_valid(): #return form with old details return render_to_response('achievement/update_article.html',\ {'form':UpdateArticleForm(init_article),\ 'is_loggedin':is_loggedin, 'username':username},\ RequestContext(request)) # Form is valid: else: article_update = article_update_form.save(commit=False) update_article_obj = get_object_or_404(Article, username=username) update_article_obj.area = article_update.area update_article_obj.magazine_name = article_update.magazine_name update_article_obj.title = article_update.title update_article_obj.publication_date = article_update.publication_date update_article_obj.save() return render_to_response('achievement/success.html', \ {'achievement_type':'Update Article', \ 'is_loggedin':is_loggedin, \ 'username':username}, \ RequestContext(request)) except KeyError: return error_key(request)
def update_intern(request, achievement_id): """ View to update the artciel information """ try: is_loggedin, username = get_session_variables(request) # User is not logged in if not logged_in(request): return HttpResponseRedirect('/register/login') else: internship = get_object_or_404(Intern, achievement_id=achievement_id) init_internship = internship.__dict__ #If method is not POST if request.method != 'POST': #return form with old details return render_to_response('achievement/update_intern.html',\ {'form':UpdateInternForm(init_internship),\ 'is_loggedin':is_loggedin, 'username':username},\ RequestContext(request)) # If method is POST else: intern_update_form = UpdateInternForm(request.POST) # Form is not valid if not intern_update_form.is_valid(): #return form with old details return render_to_response('achievement/update_intern.html',\ {'form':UpdateInternForm(init_internship),\ 'is_loggedin':is_loggedin, 'username':username},\ RequestContext(request)) # Form is valid: else: intern_update = intern_update_form.save(commit=False) intern_article_obj = get_object_or_404(Intern, username=username) intern_article_obj.place = intern_update.place intern_article_obj.intern_type = intern_update.intern_type intern_article_obj.period = intern_update.period intern_article_obj.save() return render_to_response('achievement/success.html', \ {'achievement_type':'Update Internship', \ 'is_loggedin':is_loggedin, \ 'username':username}, \ RequestContext(request)) except KeyError: return error_key(request)
def icpc_viewall(request): """ View to display all ICPCers. """ is_loggedin, username = get_session_variables(request) icpc_participants_list = [] p_list = [] icpc_list = ACM_ICPC_detail.objects.all().order_by('ranking') if icpc_list: for icpc_obj in icpc_list: team = icpc_obj.team_name member1 = [icpc_obj.participant1_name, \ get_username_from_email(icpc_obj.participant1_email)] member2 = [icpc_obj.participant2_name, \ get_username_from_email(icpc_obj.participant2_email)] member3 = [icpc_obj.participant3_name, \ get_username_from_email(icpc_obj.participant3_email)] icpc_participant_list = [icpc_obj, member1, member2, member3] icpc_participants_list.append(icpc_participant_list) return render_to_response('achievement/icpc_viewall.html', \ {'is_loggedin':logged_in(request), \ 'username':username, \ 'icpc_list':icpc_list,\ 'icpc_participants_list':icpc_participants_list}, RequestContext(request)) else: return render_to_response('achievement/noview.html', \ {'is_loggedin':logged_in(request), \ 'username':username, \ 'type': 'ACM ICPC Contest'}, \ RequestContext(request))
def icpc_viewall(request): """ View to display all ICPCers. """ is_loggedin, username = get_session_variables(request) icpc_participants_list = [] p_list= [] icpc_list = ACM_ICPC_detail.objects.all().order_by('ranking') if icpc_list: for icpc_obj in icpc_list: team = icpc_obj.team_name member1 = [icpc_obj.participant1_name, \ get_username_from_email(icpc_obj.participant1_email)] member2 = [icpc_obj.participant2_name, \ get_username_from_email(icpc_obj.participant2_email)] member3 = [icpc_obj.participant3_name, \ get_username_from_email(icpc_obj.participant3_email)] icpc_participant_list = [icpc_obj, member1,member2,member3] icpc_participants_list.append(icpc_participant_list) return render_to_response('achievement/icpc_viewall.html', \ {'is_loggedin':logged_in(request), \ 'username':username, \ 'icpc_list':icpc_list,\ 'icpc_participants_list':icpc_participants_list}, RequestContext(request)) else: return render_to_response('achievement/noview.html', \ {'is_loggedin':logged_in(request), \ 'username':username, \ 'type': 'ACM ICPC Contest'}, \ RequestContext(request))
def update_contribution(request, achievement_id): try: is_loggedin, username = get_session_variables(request) # User is not logged in if not logged_in(request): return HttpResponseRedirect('/register/login') else: #achievement_id = get_object_or_404(Achievement, username = user_name) contribution = get_object_or_404(Contribution, achievement_id=achievement_id) init_contribution = contribution.__dict__ #If method is not POST if request.method != 'POST': #return form with old details return render_to_response('achievement/update_contrib.html',\ {'form':UpdateContributionForm(init_contribution),\ 'is_loggedin':is_loggedin, 'username':username},\ RequestContext(request)) # If method is POST else: contribution_update_form = UpdateContributionForm(request.POST) # Form is not valid if not contribution_update_form.is_valid(): #return form with old details return render_to_response('achievement/update_contrib.html',\ {'form':UpdateContributionForm(init_contribution),\ 'is_loggedin':is_loggedin, 'username':username},\ RequestContext(request)) # Form is valid: else: contribution_update = contribution_update_form.save( commit=False) update_contribution_obj = get_object_or_404( Contribution, username=username) update_contribution_obj.bug_id = contribution_update.bug_id update_contribution_obj.org_name = contribution_update.org_name update_contribution_obj.bug_url = contribution_update.bug_url update_contribution_obj.bug_description = contribution_update.bug_description update_contribution_obj.save() return render_to_response('achievement/success.html', \ {'achievement_type':'Update Contribution', \ 'is_loggedin':is_loggedin, \ 'username':username}, \ RequestContext(request)) except KeyError: return error_key(request)
def update_article(request,achievement_id): """ View to update the artciel information """ try: is_loggedin, username = get_session_variables(request) # User is not logged in if not logged_in(request): return HttpResponseRedirect('/register/login') else: article = get_object_or_404(Article, achievement_id = achievement_id) init_article = article.__dict__ #If method is not POST if request.method != 'POST': #return form with old details return render_to_response('achievement/update_article.html',\ {'form':UpdateArticleForm(init_article),\ 'is_loggedin':is_loggedin, 'username':username},\ RequestContext(request)) # If method is POST else: article_update_form = UpdateArticleForm(request.POST) # Form is not valid if not article_update_form.is_valid(): #return form with old details return render_to_response('achievement/update_article.html',\ {'form':UpdateArticleForm(init_article),\ 'is_loggedin':is_loggedin, 'username':username},\ RequestContext(request)) # Form is valid: else: article_update = article_update_form.save(commit = False) update_article_obj = get_object_or_404(Article, username = username) update_article_obj.area = article_update.area update_article_obj.magazine_name = article_update.magazine_name update_article_obj.title = article_update.title update_article_obj.publication_date = article_update.publication_date update_article_obj.save() return render_to_response('achievement/success.html', \ {'achievement_type':'Update Article', \ 'is_loggedin':is_loggedin, \ 'username':username}, \ RequestContext(request)) except KeyError: return error_key(request)
def update_intern(request,achievement_id): """ View to update the artciel information """ try: is_loggedin, username = get_session_variables(request) # User is not logged in if not logged_in(request): return HttpResponseRedirect('/register/login') else: internship = get_object_or_404(Intern, achievement_id = achievement_id) init_internship = internship.__dict__ #If method is not POST if request.method != 'POST': #return form with old details return render_to_response('achievement/update_intern.html',\ {'form':UpdateInternForm(init_internship),\ 'is_loggedin':is_loggedin, 'username':username},\ RequestContext(request)) # If method is POST else: intern_update_form = UpdateInternForm(request.POST) # Form is not valid if not intern_update_form.is_valid(): #return form with old details return render_to_response('achievement/update_intern.html',\ {'form':UpdateInternForm(init_internship),\ 'is_loggedin':is_loggedin, 'username':username},\ RequestContext(request)) # Form is valid: else: intern_update = intern_update_form.save(commit = False) intern_article_obj = get_object_or_404(Intern, username = username) intern_article_obj.place = intern_update.place intern_article_obj.intern_type = intern_update.intern_type intern_article_obj.period = intern_update.period intern_article_obj.save() return render_to_response('achievement/success.html', \ {'achievement_type':'Update Internship', \ 'is_loggedin':is_loggedin, \ 'username':username}, \ RequestContext(request)) except KeyError: return error_key(request)
def mypage(request): """ An editable profile page for the user """ if not logged_in(request): return HttpResponseRedirect('/register/login') else: is_loggedin, username = get_session_variables(request) name = User_info.objects.get(username=username) return render_to_response( \ 'register/mypages.html', {'username':username, \ 'firstname':name.firstname, \ 'is_loggedin':is_loggedin, \ 'lastname':name.lastname,},\ RequestContext(request))
def update_contribution(request,achievement_id): try: is_loggedin, username = get_session_variables(request) # User is not logged in if not logged_in(request): return HttpResponseRedirect('/register/login') else: #achievement_id = get_object_or_404(Achievement, username = user_name) contribution = get_object_or_404(Contribution, achievement_id = achievement_id) init_contribution = contribution.__dict__ #If method is not POST if request.method != 'POST': #return form with old details return render_to_response('achievement/update_contrib.html',\ {'form':UpdateContributionForm(init_contribution),\ 'is_loggedin':is_loggedin, 'username':username},\ RequestContext(request)) # If method is POST else: contribution_update_form = UpdateContributionForm(request.POST) # Form is not valid if not contribution_update_form.is_valid(): #return form with old details return render_to_response('achievement/update_contrib.html',\ {'form':UpdateContributionForm(init_contribution),\ 'is_loggedin':is_loggedin, 'username':username},\ RequestContext(request)) # Form is valid: else: contribution_update = contribution_update_form.save(commit = False) update_contribution_obj = get_object_or_404(Contribution, username = username) update_contribution_obj.bug_id = contribution_update.bug_id update_contribution_obj.org_name = contribution_update.org_name update_contribution_obj.bug_url = contribution_update.bug_url update_contribution_obj.bug_description = contribution_update.bug_description update_contribution_obj.save() return render_to_response('achievement/success.html', \ {'achievement_type':'Update Contribution', \ 'is_loggedin':is_loggedin, \ 'username':username}, \ RequestContext(request)) except KeyError: return error_key(request)
def update_profile_pic(request): try: is_loggedin, username = get_session_variables(request) # User is not logged in if not logged_in(request): return HttpResponseRedirect('/register/login') else: user_details = get_object_or_404(User_info, username=username) init_user_details = user_details.__dict__ #If method is not POST if request.method != 'POST': #return form with old details return render_to_response('register/update_profile_pic.html',\ {'form':UpdateProfileForm(init_user_details),\ 'is_loggedin':is_loggedin, 'username':username},\ RequestContext(request)) # If method is POST else: user_object = get_object_or_404(User_info, \ username=username) if 'image' in request.FILES: try: to_delete = ProfileImage.objects.filter( username=username) for obj in to_delete: obj.delete() except ProfileImage.DoesNotExist: pass profile_image = request.FILES['image'] profile_image_object = ProfileImage \ (image=profile_image, \ username=user_object) profile_image_object.image.name = username + \ ".jpg" profile_image_object.save() redirect_url = "/register/profile/" + username + "/" return HttpResponseRedirect(redirect_url) except KeyError: return error_key(request)
def update_profile_pic(request): try: is_loggedin, username = get_session_variables(request) # User is not logged in if not logged_in(request): return HttpResponseRedirect('/register/login') else: user_details = get_object_or_404(User_info, username = username) init_user_details = user_details.__dict__ #If method is not POST if request.method != 'POST': #return form with old details return render_to_response('register/update_profile_pic.html',\ {'form':UpdateProfileForm(init_user_details),\ 'is_loggedin':is_loggedin, 'username':username},\ RequestContext(request)) # If method is POST else: user_object = get_object_or_404(User_info, \ username=username) if 'image' in request.FILES: try: to_delete = ProfileImage.objects.filter(username=username) for obj in to_delete: obj.delete() except ProfileImage.DoesNotExist: pass profile_image = request.FILES['image'] profile_image_object = ProfileImage \ (image=profile_image, \ username=user_object) profile_image_object.image.name = username + \ ".jpg" profile_image_object.save() redirect_url = "/register/profile/"+username+"/" return HttpResponseRedirect(redirect_url) except KeyError: return error_key(request)
def newregister(request): """ Make a new registration, inserting into User_info and ProfileImage models. """ try: # If the user is already loggedin never show the login page if logged_in(request): return render_to_response('register/logged_in.html', RequestContext(request)) # Upon Register button click if request.method == 'POST': form = NewRegisterForm(request.POST, request.FILES) # Form has all valid entries if form.is_valid(): cleaned_reg_data = form.cleaned_data inp_username = cleaned_reg_data['username'] inp_password = cleaned_reg_data['password'] inp_email = cleaned_reg_data['email'] # Saving the user inputs into table new_register = form.save(commit=False) new_register.password = hash_func(inp_password) \ .hexdigest() new_register.save() user_object = get_object_or_404(User_info, \ username=inp_username) # Optional image upload processing and saving if 'image' in request.FILES: profile_image = request.FILES['image'] profile_image_object = ProfileImage \ (image=profile_image, \ username=user_object) profile_image_object.image.name = inp_username + \ ".jpg" profile_image_object.save() # Setting the session variables request.session['username'] = cleaned_reg_data['username'] request.session['is_loggedin'] = True request.session['email'] = cleaned_reg_data['email'] sendmail_after_userreg(inp_username, inp_password, inp_email) notify_new_user(inp_username, inp_email) return render_to_response('register/register_success.html', {'is_loggedin':logged_in(request), \ 'username':request.session['username']}, \ RequestContext(request)) # Invalid form inputs else: error = "Invalid inputs" return render_to_response('register/newregister.html', {'form': form, 'error':error}, RequestContext(request)) return render_to_response('register/newregister.html', {'form': NewRegisterForm}, RequestContext(request)) except KeyError: return error_key(request)
def login(request): """ A view to evaluate login form """ try: # If the user is already loggedin never show the login page if logged_in(request): return render_to_response('register/logged_in.html', \ RequestContext(request)) # Upon signin button click if request.method=='POST': form = LoginForm(request.POST) # Form has all valid entries if form.is_valid(): cleaned_login_data = form.cleaned_data inp_username = cleaned_login_data['username'] inp_password = cleaned_login_data['password'] hashed_password = hash_func(inp_password).hexdigest() user_tuple = User_info.objects.all().filter \ (username = inp_username) # There exist an entry in table with the given username if user_tuple: actual_pwd = user_tuple[0].password # Password matches: session validation if actual_pwd == hashed_password: request.session['is_loggedin'] = True request.session['username'] = inp_username request.session['email'] = user_tuple[0].email return HttpResponseRedirect('/') # Invalid password else: error = "Invalid password. Is it really you, " + \ str(inp_username) + "?" return render_to_response('register/login.html', \ {'form':form, 'error':error}, RequestContext(request)) # There's no entry in the table with the given username else: error = "User doesn't exist!" return render_to_response('register/login.html', \ {'form':form, 'error':error}, RequestContext(request)) # Invalid form inputs else: error = "Invalid username and password" return render_to_response('register/login.html', {'form':form, 'error':error}, RequestContext(request)) # 'GET' request i.e refresh else: # User is logged in and hence redirect to home page if 'is_loggedin' in request.session and \ request.session['is_loggedin']: return HttpResponseRedirect('/') # User is not logged in and refresh the page else: form=LoginForm() return render_to_response('register/login.html', {'form':form}, RequestContext(request)) except KeyError: return error_key(request)
def newregister(request): """ Make a new registration, inserting into User_info and ProfileImage models. """ try: # If the user is already loggedin never show the login page if logged_in(request): return render_to_response('register/logged_in.html', RequestContext(request)) # Upon Register button click if request.method == 'POST': form = NewRegisterForm(request.POST, request.FILES) # Form has all valid entries if form.is_valid(): cleaned_reg_data = form.cleaned_data inp_username = cleaned_reg_data['username'] inp_password = cleaned_reg_data['password'] inp_email = cleaned_reg_data['email'] # Saving the user inputs into table new_register = form.save(commit=False) new_register.password = hash_func(inp_password) \ .hexdigest() new_register.save() user_object = get_object_or_404(User_info, \ username=inp_username) # Optional image upload processing and saving if 'image' in request.FILES: profile_image = request.FILES['image'] profile_image_object = ProfileImage \ (image=profile_image, \ username=user_object) profile_image_object.image.name = inp_username + \ ".jpg" profile_image_object.save() # Setting the session variables request.session['username'] = cleaned_reg_data['username'] request.session['is_loggedin'] = True request.session['email'] = cleaned_reg_data['email'] sendmail_after_userreg(inp_username, inp_password, inp_email) notify_new_user(inp_username, inp_email) return render_to_response('register/register_success.html', {'is_loggedin':logged_in(request), \ 'username':request.session['username']}, \ RequestContext(request)) # Invalid form inputs else: error = "Invalid inputs" return render_to_response('register/newregister.html', { 'form': form, 'error': error }, RequestContext(request)) return render_to_response('register/newregister.html', {'form': NewRegisterForm}, RequestContext(request)) except KeyError: return error_key(request)
def login(request): """ A view to evaluate login form """ try: # If the user is already loggedin never show the login page if logged_in(request): return render_to_response('register/logged_in.html', \ RequestContext(request)) # Upon signin button click if request.method == 'POST': form = LoginForm(request.POST) # Form has all valid entries if form.is_valid(): cleaned_login_data = form.cleaned_data inp_username = cleaned_login_data['username'] inp_password = cleaned_login_data['password'] hashed_password = hash_func(inp_password).hexdigest() user_tuple = User_info.objects.all().filter \ (username = inp_username) # There exist an entry in table with the given username if user_tuple: actual_pwd = user_tuple[0].password # Password matches: session validation if actual_pwd == hashed_password: request.session['is_loggedin'] = True request.session['username'] = inp_username request.session['email'] = user_tuple[0].email return HttpResponseRedirect('/') # Invalid password else: error = "Invalid password. Is it really you, " + \ str(inp_username) + "?" return render_to_response('register/login.html', \ {'form':form, 'error':error}, RequestContext(request)) # There's no entry in the table with the given username else: error = "User doesn't exist!" return render_to_response('register/login.html', \ {'form':form, 'error':error}, RequestContext(request)) # Invalid form inputs else: error = "Invalid username and password" return render_to_response('register/login.html', { 'form': form, 'error': error }, RequestContext(request)) # 'GET' request i.e refresh else: # User is logged in and hence redirect to home page if 'is_loggedin' in request.session and \ request.session['is_loggedin']: return HttpResponseRedirect('/') # User is not logged in and refresh the page else: form = LoginForm() return render_to_response('register/login.html', {'form': form}, RequestContext(request)) except KeyError: return error_key(request)