def choose_role(request): if request.method == "POST": post = json.loads(request.body) tmp_role = post.get("role") if tmp_role == "Mentor" and request.user.profile.role == "": dt = datetime.datetime.strptime(settings.HMP_CHECK_TIME, '%Y-%m-%d') user_review_num = request.user.courseuser_set.annotate( length=Length("text")).filter(length__gt=15, date__gt=dt).count() if user_review_num < 3: return _error_response( "You don't have enough course comments.") request.user.profile.role = "Mentor" request.user.save() elif tmp_role == "Mentee" and request.user.profile.role == "": # return _error_response("Mentee registration not yet started.") request.user.profile.role = "Mentee" request.user.save() else: return _error_response("Unknown Reason") return _success_response({ "role": request.user.profile.role, }) if request.method == "GET": return _get_not_allowed()
def edit_user(request): if request.method == "POST": username = request.POST.get("username") if not username: return _error_response("No username provided") user = User.objects.filter(username=username).first() if not user: return _error_response("User does not exist.") if user != request.user: return _error_response("You can't edit other people's profile.") profile_form = ProfileNewForm(request.POST, request.FILES, instance=user.profile) user_form = UserForm(request.POST, request.FILES, instance=user) if profile_form.is_valid() and user_form.is_valid(): # print("profile Form cleaned data", profile_form.cleaned_data) # print("user Form cleaned data", user_form.cleaned_data) profile_form.save() user_form.save() return _success_response() else: print("User Edit Error:", user_form.errors) print("Profile Edit Error:", profile_form.errors) if request.method == "GET": return _get_not_allowed()
def report_bug(request): if request.method == "POST": post = json.loads(request.body) title, text = post.get("title"), post.get("text") if len(title) > 0 and len(text) > 0: Bug.objects.create(user=request.user, title=title, text=text) return _success_response() else: return _error_response("Missing title or text field") return _error_response("Get method not allowed")
def edit_plannable_profile(request, username): if request.method == "POST": try: user = User.objects.get(username=username) except: return _error_response("User doesn't exist") post = json.loads(request.body) user_agent = request.META['HTTP_USER_AGENT'] action = post["action"] # Rename profile and delete all previous versions if action == "rename": oldName, newName, content = post["oldName"], post["newName"], post["profile"] try: profile = PlanProfile.objects.get(user=user ,name=oldName) except: return _error_response("Profile doesn't exist") new_profile = PlanProfile.objects.filter(user=user, name=newName).first() # newName already exists, delete old profile and add new version to new profile it if new_profile != None: for p_v in profile.planprofileversion_set.all(): p_v.delete() profile.delete() PlanProfileVersion.objects.create(version=new_profile.latest + 1, content=content, plan_profile=new_profile, user_agent=user_agent) new_profile.latest += 1 new_profile.save() return _success_response({"versions":get_versions_of_profile(new_profile)}) # Delete(detach) previous versions for p_v in profile.planprofileversion_set.all(): p_v.delete() PlanProfileVersion.objects.create(version=1, content=content, plan_profile=profile, user_agent=user_agent) profile.name = newName profile.latest = 1 profile.save() return _success_response({"versions":get_versions_of_profile(profile)}) # Delete profile and corresponding versions elif action == "delete": if "name" not in post: return _error_response("Missing name field") name = post["name"] try: profile = PlanProfile.objects.get(user=user ,name=name) except: return _error_response("Profile doesn't exist") for p_v in profile.planprofileversion_set.all(): p_v.delete() profile.delete() return _success_response() else: return _error_response("Action not recognized") if request.method == "GET": return _get_not_allowed()
def get_profile(request): username = request.GET.get('username') if not username: return _error_response() editable = request.user.username == username user = User.objects.filter(username=username).first() if not user: return _error_response() return _success_response({ "editable": editable, "user": user_json(user, request, personal_profile=True), })
def submit_review(request): if request.method == "POST": post = json.loads(request.body) text, rating_course, rating_instructor = post["text"], post["rating_course"], post["rating_instructor"] course_instructor_pk = post["course_instructor_pk"] if len(text.strip()) == 0 or rating_course == 0 or rating_instructor == 0: return _error_response() course_instructor = get_object_or_404(CourseInstructor, pk=course_instructor_pk) cs_user_query = CourseUser.objects.filter(course=course_instructor.course, user=request.user) if cs_user_query.first() != None: cs_user = cs_user_query.first() cs_user.text = text cs_user.instructor = course_instructor.instructor cs_user.take = "taken" cs_user.rating_course = rating_course cs_user.rating_instructor = rating_instructor cs_user.course_instructor = course_instructor cs_user.save() else: CourseUser.objects.create(course=course_instructor.course, user=request.user, instructor=course_instructor.instructor, course_instructor=course_instructor, text=text, take="taken", rating_course=rating_course, rating_instructor=rating_instructor) return _success_response() if request.method == "GET": return _get_not_allowed()
def del_fav(request): if request.method == "POST": post = json.loads(request.body) user_pk = post.get("user_pk") try: to_user = User.objects.get(pk=user_pk) except: return _error_response("User does not exist") if Follow.objects.filter(follower=request.user, followee=to_user).exists(): Follow.objects.remove_follower(follower=request.user, followee=to_user) return _success_response() else: return _error_response("Following relationship does not exist") if request.method == "GET": return _get_not_allowed()
def user_del_skill(request): if request.method == "POST": post = json.loads(request.body) skill_id = post.get("id") skill = Skill.objects.filter(pk=skill_id).first() if not skill: return _error_response("Skill doesn't exist") skill_relation = SkillRelation.objects.filter(user=request.user, skill=skill).first() if skill_relation: skill_relation.delete() if skill.users.count() == 0 and skill.type == "Custom": skill.delete() return _success_response() else: return _error_response("You don't have this skill") if request.method == "GET": return _get_not_allowed()
def get_plannable_profile(request, username): if request.method == "POST": try: user = User.objects.get(username=username) except: return _error_response("User doesn't exist") post = json.loads(request.body) ret_profile = [] # Return a specific profile if "name" in post: profile = PlanProfile.objects.filter(name=post["name"], user=user).first() if profile == None: return _error_response("Profile doesn't exist") # Return a specific version of the profile if "version" in post: try: version = int(post["version"]) except: return _error_response("Version should be a number") real_profile = profile.planprofileversion_set.filter(version=version).first() # Return the latest version of the profile else: real_profile = profile.planprofileversion_set.filter(version=profile.latest).first() ret_profile.append({ "versions":get_versions_of_profile(profile), "profile":real_profile.content, }) # Return all profiles else: profiles = PlanProfile.objects.filter(user=user) for profile in profiles: ret_profile.append({ "versions":get_versions_of_profile(profile), "profile":profile.planprofileversion_set.filter(version=profile.latest).first().content, }) return JsonResponse({ "success":True, "message":"Nice", "profiles":ret_profile, }, safe=False) if request.method == "GET": return _get_not_allowed()
def save_plannable_profile(request, username): if request.method == "POST": try: user = User.objects.get(username=username) except: return _error_response("User doesn't exist") post = json.loads(request.body) user_agent = request.META['HTTP_USER_AGENT'] profiles = post["profiles"] versions = [] for profile in profiles: name, content = profile["name"], profile["profile"] plan_profile = PlanProfile.objects.filter(user=user, name=name).first() new_flag = plan_profile == None if new_flag: plan_profile = PlanProfile.objects.create(user=user, name=name) # Force to create a new version if "new" in profile or new_flag: PlanProfileVersion.objects.create(version=plan_profile.latest + 1, content=content, plan_profile=plan_profile, user_agent=user_agent) plan_profile.latest += 1 plan_profile.save() # Server decide whether to create a new version else: latest_version = plan_profile.planprofileversion_set.filter(version=plan_profile.latest).first() if latest_version == None: return _error_response("Dirty profile") diff = timezone.now() - latest_version.modified # Latest version is more than 5min old, create new version if diff.total_seconds() > 300: PlanProfileVersion.objects.create(version=plan_profile.latest + 1, content=content, plan_profile=plan_profile, user_agent=user_agent) plan_profile.latest += 1 plan_profile.save() # Latest version is still young, update latest version else: latest_version.user_agent = user_agent latest_version.content = content latest_version.save() versions.append(get_versions_of_profile(plan_profile)) return _success_response({"versions":versions}) if request.method == "GET": return _get_not_allowed()
def get_skill(request): skill_id = request.GET.get("id") try: skill = Skill.objects.get(pk=skill_id) users = skill.users.all() return _success_response({ "skill": skill_json(skill), "users": [user_json(user, request) for user in users] }) except: return _error_response("Skill doesn't exist.")
def add_fav(request): if request.method == "POST": post = json.loads(request.body) user_pk = post.get("user_pk") try: to_user = User.objects.get(pk=user_pk) except: return _error_response("User does not exist") if request.user.profile.role == '' or request.user.profile.role == to_user.profile.role: return _error_response("False Role") if not Follow.objects.filter( follower=request.user, followee=to_user).exists() and Follow.objects.filter( follower=request.user).count() < 3: Follow.objects.add_follower(request.user, to_user) return _success_response() return _error_response( "You either have already followed this person or have already followed three users" ) if request.method == "GET": return _get_not_allowed()
def choose_roommate_role(request): if request.method == "POST": post = json.loads(request.body) rm = post.get("rm") if rm != True and rm != False: return _error_response("Unknown Rm") request.user.profile.rm = rm request.user.save() return _success_response({ "rm": request.user.profile.rm, }) if request.method == "GET": return _get_not_allowed()
def match_user(request): if request.method == "POST": post = json.loads(request.body) user_1_pk, user_2_pk = post["user_1"], post["user_2"] try: user_1, user_2 = User.objects.get(pk=user_1_pk), User.objects.get( pk=user_2_pk) user_1.profile.matched = True user_1.save() user_2.profile.matched = True user_2.save() return _success_response() except: return _error_response("User doesn't exist") if request.method == "GET": return _get_not_allowed()
def course_search_result(request): query = request.GET.get("query") query_time = request.GET.get("time") search_course = js_boolean(request.GET.get('cs')) search_instructor = js_boolean(request.GET.get('instr')) if not query or not query_time: return _error_response("Query or Time not provided") search_queryset = course_and_instructor_retrieve(query, search_course, search_instructor) course_result = [] for query in search_queryset: if query[1] == "instructor": course_result.append(get_json_of_instructor(query[0])) if query[1] == "course": course_result.append(get_json_of_course(query[0])) return _success_response({ "course_result": course_result, "time":query_time, })
def user_add_skill(request): if request.method == "POST": post = json.loads(request.body) skill_id, skill_name = post.get("id"), post.get("name") skill = Skill.objects.filter(Q(pk=skill_id) | Q(name=skill_name)).first() if not skill: skill = Skill.objects.create(name=skill_name, intro="", type="Custom") # skill = Skill.objects.filter(pk = skill_id).first() # if not skill: # skill = Skill.objects.filter(name = skill_name).first() # if not skill: # skill = Skill.objects.create(name=skill_name, intro="", type="Custom") if SkillRelation.objects.filter(user=request.user, skill=skill).first(): return _error_response("User-skill relation exists") SkillRelation.objects.create(user=request.user, skill=skill) return _success_response({ "id": skill.id, }) if request.method == "GET": return _get_not_allowed()