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 index(request): lisUserProfile = {} # listParentCategory = Category.objects(parentCategory = None) lisCategory =Category.objects() # print('begin') # for user in listCategory: # if user.parentCategory is not None : # print(user.parentCategory.categoryName) # print('finish') if len(lisCategory) > 0: category=lisCategory[0] c = {'lisUserProfile':lisUserProfile,'listCategory':lisCategory,'category':category} else: c = {'lisUserProfile':lisUserProfile,'listCategory':lisCategory} if request.method == 'GET': return render(request, 'myapp/search-mentor.html', c) elif request.method == 'POST': try: lisUserProfile={} listCurriculumn={} lisCategory =Category.objects() user=User.objects.get(username=str(request.user)) mentor=Mentor.objects(user=user.id) keyword = request.POST['search'] parentCategory = request.POST['parentCategory']; childrenCategory =request.POST['childrenCategory']; #search data # users = User.objects(Q(first_name__icontains=keyword) | Q(last_name__icontains=keyword)) # lisUserProfile = UserProfile.objects(user_id__in=users,is_mentor=True) # categoryObject =Category.objects.get(id=childrenCategory) if len(mentor)>0: mt=mentor[0] listCurriculumn =Curriculumn.objects(category=childrenCategory ,name__icontains=keyword).order_by('published_date') else: listCurriculumn =Curriculumn.objects(category=childrenCategory ,name__icontains=keyword).order_by('published_date') # listCurriculumn =Curriculumn.objects(name__icontains=keyword) c = {'lisUserProfile':lisUserProfile,'listCurriculumn':listCurriculumn,'listCategory':lisCategory,'search':keyword,'parentCategory':parentCategory,'childrenCategory':childrenCategory} #get data from mongodb c.update(csrf(request)) c.update(context_processors.user(request)) return render_to_response("myapp/search-mentor.html", c) except Exception: lisUserProfile={} listCurriculumn={} lisCategory =Category.objects() user=User.objects.get(username=str(request.user)) mentor=Mentor.objects(user=user.id) keyword = request.POST['search'] parentCategory = request.POST['parentCategory']; if len(mentor)>0: mt=mentor[0] listCurriculumn =Curriculumn.objects(name__icontains=keyword).order_by('published_date') else: listCurriculumn =Curriculumn.objects(name__icontains=keyword).order_by('published_date') c = {'lisUserProfile':lisUserProfile,'listCurriculumn':listCurriculumn,'listCategory':lisCategory,'search':keyword,'parentCategory':parentCategory} c.update(csrf(request)) c.update(context_processors.user(request)) return render_to_response("myapp/search-mentor.html", c) return render(request, 'myapp/search-mentor.html', c)
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', {})
def index(request): if request.method == "GET": vCourse_id = request.GET["course_id"] author_id = request.GET["user_id"] author = User.objects.get(id=author_id) cl = Curriculumn.objects(id=vCourse_id) has_curriculum = False try: is_mentor = request.session["is_mentor"] except Exception as e: is_mentor = False user = User.objects.get(username=str(request.user)) student = Student.objects(user=user.id) status = "1" if len(cl): has_curriculum = True clTaken = 0 clLike = 0 mtTaken = 0 mtLike = 0 actTaken = 0 actLike = 0 mtTotal = 0 actTotal = 0 is_joined = False if len(student): progress = CurriculumnStudyProgress.objects(curriculumn=cl[0].id, student=student[0].id) if len(progress) > 0: is_joined = True lscl = [] lscl = cl[0] for i in lscl.__getattribute__("material"): i.note = "0" try: is_like = StatisticDetail.objects(object_id=str(i.id), status=status, user=user.id) if len(is_like): i.note = "1" i.__getattribute__("statistic").currentLikeNumber -= 1 except Exception as e: print(e) avatar = "" try: avatar = request.session["avatar"] except Exception as e: request.session["avatar"] = "" print(e) context = { "cl": lscl, "is_joined": is_joined, "user_id": request.user, "course_id": vCourse_id, "author_id": author_id, "author": author.username, "is_mentor": is_mentor, "clTaken": clTaken, "clLike": clLike, "mtTaken": mtTaken, "mtLike": mtLike, "actTaken": actTaken, "actLike": actLike, "mtTotal": mtTotal, "actTotal": actTotal, "has_curriculum": has_curriculum, "avatar": avatar, } return render(request, "myapp/course-detail.html", context) elif request.method == "POST": if request.POST["posttype"] == "likeMaterial": user = User.objects.get(username=str(request.user)) print(request.POST["posttype"]) materialId = request.POST["materialId"] status = request.POST["status"] # like or dislike # update status=0 for recors last with status=1,user_id,material sdLast = StatisticDetail.objects(user=user.id, object_id=str(materialId), status="1").order_by( "published_date" )[:10] if len(sdLast): for sdUpdate in sdLast: sdUpdate.status = "0" sdUpdate.save() else: print("no update ") sdNew = StatisticDetail() sdNew.user = user sdNew.object_id = str(materialId) if status == "0": sdNew.status = "1" else: sdNew.status = "0" sdNew.save() # update or insert statistic stCurent = Statistic.objects(object_id=str(materialId)).order_by("create_date")[:1] stNew = Statistic() if len(stCurent) > 0: stNew = stCurent[0] if status == "1": stNew.currentLikeNumber -= 1 else: stNew.currentLikeNumber += 1 stNew.type = "1" stNew.save() else: if status == "0": stNew.currentLikeNumber = 1 else: stNew.currentLikeNumber = 0 stNew.object_id = str(materialId) stNew.type = "1" stNew.save() # update material mtCurrent = Material.objects.get(id=materialId) mtCurrent.statistic = stNew mtCurrent.note = "0" mtCurrent.save() return HttpResponse(json.dumps({"formdata": materialId}), content_type="application/json") elif request.POST["posttype"] == "frmJoincourse": curriculum_id = request.POST["curriculum_id"] user_id = request.POST["user_id"] print(curriculum_id) curriculum = Curriculumn.objects.get(id=curriculum_id) user = User.objects.get(username=str(request.user)) student = Student.objects(user=user.id) planstart = request.POST["planstart"] planend = request.POST["planend"] impression = request.POST["impression"] description = request.POST["description"] # Save CurriculumnStudyProgress csp = CurriculumnStudyProgress() if len(student): st = student[0] csp.student = st csp.curriculumn = curriculum csp.PlanStartDate = datetime.strptime(planstart, "%m/%d/%Y") csp.PlanEndDate = datetime.strptime(planend, "%m/%d/%Y") csp.impression = Impression.objects.get(showpiority=impression) csp.description = description csp.save() # UPDATE Curriculumn print(user.id) curri = Curriculumn() curri = Curriculumn.objects.get(id=curriculum_id) curri.joined_user.append(user) curri.save() # Save CurriculumnLog lisProgressType = ProgressType.objects().order_by("rate") progressType = lisProgressType[0] curriLog = CurriculumnLog() curriLog.curriculumn = curri curriLog.process = progressType curriLog.data = "[]" curriLog.user_id = user curriLog.save() # SHOW Record cl = Curriculumn.objects(id=curriculum_id) has_curriculum = False is_mentor = False mentor = Mentor.objects(user=user.id) if len(mentor): is_mentor = True if len(cl): has_curriculum = True print(has_curriculum) clTaken = 0 clLike = 0 mtTaken = 0 mtLike = 0 actTaken = 0 actLike = 0 mtTotal = 0 actTotal = 0 try: for c in cl: if c.statistic.currentTakenNumber: clTaken += c.statistic.currentTakenNumber if c.statistic.currentLikeNumber: clLike += c.statistic.currentLikeNumber for mt in c.material: if mt.statistic.currentTakenNumber: mtTaken += mt.statistic.currentTakenNumber if mt.statistic.currentLikeNumber: mtLike += mt.statistic.currentLikeNumber mtTotal += 1 for act in c.action: if act.statistic.currentTakenNumber: actTaken += act.statistic.currentTakenNumber if act.statistic.currentLikeNumber: actLike += act.statistic.currentLikeNumber actTotal += 1 except Exception as e: print(e) is_joined = False if len(student): progress = CurriculumnStudyProgress.objects(curriculumn=cl[0].id, student=student[0].id) if len(progress) > 0: is_joined = True context = { "cl": cl[0], "is_joined": is_joined, "user_id": request.user, "course_id": curriculum_id, "author": user.username, "is_mentor": is_mentor, "clTaken": clTaken, "clLike": clLike, "mtTaken": mtTaken, "mtLike": mtLike, "actTaken": actTaken, "actLike": actLike, "mtTotal": mtTotal, "actTotal": actTotal, "has_curriculum": has_curriculum, } return HttpResponseRedirect("course-detail?course_id=" + curriculum_id + "&user_id=" + user_id) elif request.POST["posttype"] == "deleteComment": try: comment_status = "0" comment_id = request.POST["hd_comment_id"] course_id = request.POST["hd_course_id"] author_id = request.POST["hd_author_course_id"] user_id = request.session["_auth_user_id"] cmt = Comment.objects.get(id=comment_id) cmt.status = comment_status cmt.save() status = "1" author = User.objects.get(id=author_id) cl = Curriculumn.objects(id=course_id) has_curriculum = False is_mentor = request.session["is_mentor"] user = User.objects.get(username=str(request.user)) student = Student.objects.get(user=user.id) clTaken = 0 clLike = 0 mtTaken = 0 mtLike = 0 actTaken = 0 actLike = 0 mtTotal = 0 actTotal = 0 try: for c in cl: if c.statistic.currentTakenNumber: c.statistic.currentTakenNumber = 10 clTaken += c.statistic.currentTakenNumber if c.statistic.currentLikeNumber: clLike += c.statistic.currentLikeNumber for mt in c.material: if mt.statistic.currentTakenNumber: mtTaken += mt.statistic.currentTakenNumber if mt.statistic.currentLikeNumber: mtLike += mt.statistic.currentLikeNumber mtTotal += 1 print(mt.name) for act in c.action: if act.statistic.currentTakenNumber: actTaken += act.statistic.currentTakenNumber if act.statistic.currentLikeNumber: actLike += act.statistic.currentLikeNumber actTotal += 1 except Exception as e: print(e) progress = CurriculumnStudyProgress.objects(curriculumn=cl[0].id, student=student.id) is_joined = False if len(progress) > 0: is_joined = True lscl = [] lscl = cl[0] for i in lscl.__getattribute__("material"): i.note = "0" try: is_like = StatisticDetail.objects(object_id=str(i.id), status=status, user=user.id) if len(is_like): i.note = "1" i.__getattribute__("statistic").currentLikeNumber -= 1 except Exception as e: print(e) context = { "cl": lscl, "is_joined": is_joined, "user_id": request.user, "course_id": course_id, "author_id": author_id, "author": author.username, "is_mentor": is_mentor, "clTaken": clTaken, "clLike": clLike, "mtTaken": mtTaken, "mtLike": mtLike, "actTaken": actTaken, "actLike": actLike, "mtTotal": mtTotal, "actTotal": actTotal, "has_curriculum": has_curriculum, } except Exception as e: print(e) finally: return render(request, "myapp/course-detail.html", context) elif request.POST["posttype"] == "editComment": try: comment = request.POST["txtcommentName"] comment_id = request.POST["hd_comment_id"] course_id = request.POST["hd_course_id"] author_id = request.POST["hd_author_course_id"] user_id = request.session["_auth_user_id"] cmt = Comment.objects.get(id=comment_id) cmt.content = comment cmt.save() status = "1" author = User.objects.get(id=author_id) cl = Curriculumn.objects(id=course_id) has_curriculum = False is_mentor = request.session["is_mentor"] user = User.objects.get(username=str(request.user)) student = Student.objects.get(user=user.id) clTaken = 0 clLike = 0 mtTaken = 0 mtLike = 0 actTaken = 0 actLike = 0 mtTotal = 0 actTotal = 0 try: for c in cl: if c.statistic.currentTakenNumber: c.statistic.currentTakenNumber = 10 clTaken += c.statistic.currentTakenNumber if c.statistic.currentLikeNumber: clLike += c.statistic.currentLikeNumber for mt in c.material: if mt.statistic.currentTakenNumber: mtTaken += mt.statistic.currentTakenNumber if mt.statistic.currentLikeNumber: mtLike += mt.statistic.currentLikeNumber mtTotal += 1 print(mt.name) for act in c.action: if act.statistic.currentTakenNumber: actTaken += act.statistic.currentTakenNumber if act.statistic.currentLikeNumber: actLike += act.statistic.currentLikeNumber actTotal += 1 except Exception as e: print(e) progress = CurriculumnStudyProgress.objects(curriculumn=cl[0].id, student=student.id) is_joined = False if len(progress) > 0: is_joined = True lscl = [] lscl = cl[0] for i in lscl.__getattribute__("material"): i.note = "0" try: is_like = StatisticDetail.objects(object_id=str(i.id), status=status, user=user.id) if len(is_like): i.note = "1" i.__getattribute__("statistic").currentLikeNumber -= 1 except Exception as e: print(e) context = { "cl": lscl, "is_joined": is_joined, "user_id": request.user, "course_id": course_id, "author_id": author_id, "author": author.username, "is_mentor": is_mentor, "clTaken": clTaken, "clLike": clLike, "mtTaken": mtTaken, "mtLike": mtLike, "actTaken": actTaken, "actLike": actLike, "mtTotal": mtTotal, "actTotal": actTotal, "has_curriculum": has_curriculum, } except Exception as e: print(e) finally: return render(request, "myapp/course-detail.html", context) else: comment = request.POST["txtComment"] course_id = request.POST["hd_course_id"] material_id = request.POST["hd_material_id"] user_id = request.session["_auth_user_id"] author_id = request.POST["hd_author_course_id"] ur = request.user cmt = Comment() cmt.user = request.user cmt.content = comment cmt.save() cl = Curriculumn.objects.get(id=course_id) mt = Material.objects.get(id=material_id) mt.comment.append(cmt) mt.save() cl.save() status = "1" print(author_id) author = User.objects.get(id=author_id) cl = Curriculumn.objects(id=course_id) has_curriculum = False is_mentor = request.session["is_mentor"] user = User.objects.get(username=str(request.user)) student = Student.objects.get(user=user.id) clTaken = 0 clLike = 0 mtTaken = 0 mtLike = 0 actTaken = 0 actLike = 0 mtTotal = 0 actTotal = 0 try: for c in cl: if c.statistic.currentTakenNumber: c.statistic.currentTakenNumber = 10 clTaken += c.statistic.currentTakenNumber if c.statistic.currentLikeNumber: clLike += c.statistic.currentLikeNumber for mt in c.material: if mt.statistic.currentTakenNumber: mtTaken += mt.statistic.currentTakenNumber if mt.statistic.currentLikeNumber: mtLike += mt.statistic.currentLikeNumber mtTotal += 1 print(mt.name) for act in c.action: if act.statistic.currentTakenNumber: actTaken += act.statistic.currentTakenNumber if act.statistic.currentLikeNumber: actLike += act.statistic.currentLikeNumber actTotal += 1 except Exception as e: print(e) progress = CurriculumnStudyProgress.objects(curriculumn=cl[0].id, student=student.id) is_joined = False if len(progress) > 0: is_joined = True lscl = [] lscl = cl[0] for i in lscl.__getattribute__("material"): i.note = "0" try: is_like = StatisticDetail.objects(object_id=str(i.id), status=status, user=user.id) if len(is_like): i.note = "1" i.__getattribute__("statistic").currentLikeNumber -= 1 except Exception as e: print(e) context = { "cl": lscl, "is_joined": is_joined, "user_id": request.user, "course_id": course_id, "author_id": author_id, "author": author.username, "is_mentor": is_mentor, "clTaken": clTaken, "clLike": clLike, "mtTaken": mtTaken, "mtLike": mtLike, "actTaken": actTaken, "actLike": actLike, "mtTotal": mtTotal, "actTotal": actTotal, "has_curriculum": has_curriculum, } return render(request, "myapp/course-detail.html", context)