def index(request): form = DocumentForm() if request.method == 'GET': user=User.objects.get(username=str(request.user)) thisupro = UserProfile.objects(user_id=user)[:1] context={"UserProfile":thisupro[0],'form': form} return render(request, 'myapp/account-setting.html', context) elif request.method == 'POST': form = DocumentForm() user=User.objects.get(username=str(request.user)) thisupro = UserProfile.objects(user_id=user)[:1] fromType = request.POST['formType'] if fromType == "frmImage" : err_message="" try: form = DocumentForm(request.POST, request.FILES) if form.is_valid(): handle_uploaded_image(request.user,request.FILES['image'],request) except Exception as e: print(e) err_message = e context={"UserProfile":thisupro[0],'form': form} return render(request, 'myapp/account-setting.html', context) elif fromType == "frmProfile" : err_message="" success="" try: txtJob = request.POST['txtJob'] txtCompany = request.POST['txtCompany'] txtWorking = request.POST['txtWorking'] txtEducation = request.POST['txtEducation'] txtSkill = request.POST['txtSkill'] txaAbout = request.POST['txaAbout'] user=User.objects.get(username=str(request.user)) thisupro = UserProfile.objects(user_id=user)[:1] if len(thisupro)>0: supro=thisupro[0] supro.job_title = txtJob supro.company = txtCompany supro.work_field = txtWorking supro.edu = txtEducation supro.skill = txtSkill supro.about = txaAbout supro.save() success="success" except Exception as e: print(e) err_message = e success=e context={"UserProfile":thisupro[0],'form': form} return render(request, 'myapp/account-setting.html', context)
def save_profile_picture(strategy, user, response, details,is_new=False,*args,**kwargs): if strategy.backend.name == 'facebook': imgpro = 'http://graph.facebook.com/{0}/picture'.format(response['id']) elif strategy.backend.name == 'twitter': imgpro = response.get('profile_image_url', '').replace('_normal', '') elif strategy.backend.name == 'google-oauth2' and "picture" in response: imgpro = response['picture'] else: imgpro = "" try: if imgpro: try: thisprofile = UserProfile.objects(user_id=user) #ThaiNN Please review this code if len(thisprofile) == 0: upro = UserProfile() upro.user_id = user upro.images = imgpro upro.save() else: mypro = thisprofile[0] mypro.images = imgpro mypro.save() except Exception as e: print(e) except HTTPError: pass
def signinsns(request): is_mentor = False is_join = False defaultUserImage = "/images/avatar/default.png" if 'next' in request.GET: nextpage = request.GET['next'] else: nextpage = "" user1=User.objects.get(username=str(request.user)) thismentor = Mentor.objects(user=user1.id) thisstudent = Student.objects(user=user1.id) thiscurrijoined = Curriculumn.objects() username=str(request.user) avatar = "" user_images = "" try: thisprofile = UserProfile.objects(user_id=user1) request.session['user_images'] = thisprofile[0].images user_images= request.session['user_images'] except Exception as e: upro = UserProfile() upro.user_id = user1 upro.images = defaultUserImage upro.save() request.session['user_images'] = defaultUserImage print(e) for cl in thiscurrijoined: if username in cl.joined_user: is_join = True if len(thisstudent) == 0: studentnew = Student() studentnew.user = user1 studentnew.save() if len(thismentor) > 0: is_mentor = True request.session['is_mentor'] = is_mentor context = { 'avatar':avatar, 'user_images':user_images } if nextpage: return HttpResponseRedirect(nextpage,context) else: if is_mentor: return HttpResponseRedirect('/mentorview',context ) else: if is_join: return HttpResponseRedirect('/student-home',context) else: return HttpResponseRedirect('/search-mentor',context)
def handle_uploaded_image(user,i,request): try: imageImage = Image.open(i) except: print("Image was broken or not an image") size = (128, 128) imageImage.thumbnail(size) user=User.objects.get(username=str(request.user)) thisuprolist = UserProfile.objects(user_id=user)[:1] userprofile = thisuprolist[0] userprofile.fileImage.put(imageImage, content_type='image/jpeg') userprofile.save() #folder_path = os.path.abspath(os.path.join(os.path.join(os.path.dirname(__file__),os.pardir),os.pardir))+"/common/upload/"; #if os.path.isdir(folder_path) == False: # os.makedirs(folder_path) #with open(os.path.abspath(os.path.join(os.path.join(os.path.dirname(__file__),os.pardir),os.pardir))+"/common/upload/"+user.username+"-avatar.jpg", 'wb+') as destination: # imageImage.save(destination) #request.session['user_images'] = "/upload/" +user.username+"-avatar.jpg"
def index(request): username = "" password = "" defaultUserImage = "/images/avatar/default.png" if 'next' in request.GET: nextpage = request.GET['next'] else: nextpage = "" if request.method == 'GET': return render(request, 'myapp/signin.html', {}) elif request.method == 'POST': username = request.POST['txtUserName'] password = request.POST['txtPassWord'] try: user = User.objects.get(username=username) if user.check_password(password): logout(request) user.backend = 'mongoengine.django.auth.MongoEngineBackend' login(request, user) user_images = "" try: profile = UserProfile.objects(user_id=user) user_images = profile[0].images request.session['user_images'] = "/upload/" +user.username+"-avatar.jpg" except Exception as e: upro = UserProfile() upro.user_id = user upro.images = defaultUserImage upro.save() request.session['user_images'] = defaultUserImage print(e) context = { 'user_images':user_images, } if nextpage: return HttpResponseRedirect(nextpage,context) else: return HttpResponseRedirect('/custom-debit-detail?type=loan') else: c = { 'error_message':"User name or password does not correct", 'username':username, 'password':password, } c.update(csrf(request)) c.update(context_processors.user(request)) return render_to_response("myapp/signin.html", c) except Exception as e: c = { 'error_message':e, } c.update(csrf(request)) c.update(context_processors.user(request)) return render_to_response("myapp/signin.html", c) return render(request, 'myapp/signin.html', {})
def index(request): username = "" password = "" is_mentor=False is_join = False defaultUserImage = "/images/avatar/default.png" username=str(request.user) thiscurrijoined1 = Curriculumn.objects() for cl in thiscurrijoined1: if username in cl.joined_user: is_join = True if request.method == 'GET': return render(request, 'myapp/signin.html', {}) elif request.method == 'POST': username = request.POST['txtUserName'] password = request.POST['txtPassWord'] if 'next' in request.GET: nextpage = request.GET['next'] else: nextpage = "" try: user = User.objects.get(username=username) transaction = UserLogin() transaction.user = user transaction.save() if user.check_password(password): logout(request) user.backend = 'mongoengine.django.auth.MongoEngineBackend' login(request, user) mentor = Mentor.objects(user=user) if len(mentor) > 0: is_mentor = True request.session['is_mentor'] = is_mentor user_images = "" try: profile = UserProfile.objects(user_id=user) user_images = profile[0].images request.session['user_images'] = "/upload/" +user.username+"-avatar.jpg" except Exception as e: upro = UserProfile() upro.user_id = user upro.images = defaultUserImage upro.save() request.session['user_images'] = defaultUserImage print(e) context = { 'user_images':user_images, } if nextpage: return HttpResponseRedirect(nextpage,context) else: if is_mentor: return HttpResponseRedirect('/mentorview',context) else: if is_join: return HttpResponseRedirect('/student-home',context) else: return HttpResponseRedirect('/search-mentor',context) #return HttpResponseRedirect('/search-mentor') else: c = { 'error_message':"User name or password does not correct", 'username':username, 'password':password, } c.update(csrf(request)) c.update(context_processors.user(request)) return render_to_response("myapp/signin.html", c) except Exception as e: c = { 'error_message':e, } c.update(csrf(request)) c.update(context_processors.user(request)) return render_to_response("myapp/signin.html", c) return render(request, 'myapp/signin.html', {})