def profile_view(request): category = Category.objects.all() context = {} profile_serializer = ProfileSerializer( category, many=True, context={'account': get_user(request)}) context['status'] = 'successful' context['count'] = profile_serializer.data return Response(context)
def get(self, request): user = get_user(request) if user: if not has_permission(request.user, user): print("status: 400", error_data(profile="Private")) return JsonResponse(error_data(profile="Private"), status=400) data = get_paginated_data( data=BoardSerializer(user.boards.all().order_by('-id'), requester=request.user, many=True).data, page=request.query_params.get('page'), limit=request.query_params.get('limit'), url=F"/social/boards/?username={user.username}") return JsonResponse(data) else: print("status: 400", error_data(profile="NotExist")) return JsonResponse(error_data(profile="NotExist"), status=400)
def list_courses(request): required_courses = Course.objects.filter(is_required=True) departments = Department.objects.all() course_selections = CourseSelection.objects.filter(user=get_user(request)) selected_courses = { course_selection.course for course_selection in course_selections } paginator = Paginator(departments, 8) page = request.GET.get('page') departments = paginator.get_page(page) context = { 'required_courses': required_courses, 'departments': departments, 'selected_courses': selected_courses, } return render(request, 'library/courses.html', context)
def remove_course_selection(request): if request.is_ajax() and request.method == 'POST': user = get_user(request) course = get_object_or_404(Course, id=request.POST.get('course_id')) course_selection = CourseSelection.objects.get(user=user, course=course) course_selection.delete() messages.success(request, 'Successfully remove course from your selection.') response_obj = { 'status': 'success', 'course_id': request.POST.get('course_id'), } else: messages.error( request, 'Could not add course to your selections. Please try again.') response_obj = { 'status': 'error', } response_json = json.dumps(response_obj) return HttpResponse(response_json, content_type='text/json')
def task_view(request, slug): context = {} try: category = Category.objects.get(slug=slug) task = Task.objects.filter(status=STATUS_PUBLISH, category__slug=slug) serializer = TaskSerializer(task, many=True, context={'account': get_user(request)}) context['status'] = 'successful' context['tasks'] = serializer.data context['category_title'] = category.title context['organisers'] = OrganiserSerializer( category.organiser_set.all(), many=True, ).data except Exception as e: print(e) context['status'] = 'unsuccessful' context['error'] = 'invalid slug' return Response(context)
def get(self, request): tag = request.query_params.get("tag") if tag: users = list(Post.objects.all().filter( user__profile__is_private=False).values_list('user', flat=True)) users += list(request.user.profile.followings.all().values_list( 'user', flat=True)) users.append(request.user.pk) data = get_paginated_data(data=PostSerializer( Post.objects.filter(tags__name=tag, user_id__in=set(users)).order_by('-date'), many=True, requester=request.user).data, page=request.query_params.get('page'), limit=request.query_params.get('limit'), url=F"/social/posts/?tag={tag}") return JsonResponse(data) user = get_user(request) if user: if not has_permission(request.user, user): print("status: 400", error_data(profile="Private")) return JsonResponse(error_data(profile="Private"), status=400) data = get_paginated_data( data=PostSerializer( Post.objects.filter(user_id=user.pk).order_by('-date'), many=True, requester=request.user).data, page=request.query_params.get('page'), limit=request.query_params.get('limit'), url=F"/social/posts/?username={user.username}") return JsonResponse(data) else: print("status: 400", error_data(profile="NotExist")) return JsonResponse(error_data(profile="NotExist"), status=400)
def add_course_selection(request): if request.is_ajax() and request.method == 'POST': already_selected = False course_id = request.POST.get('course_id') user = get_user(request) course = get_object_or_404(Course, id=course_id) course_selections = CourseSelection.objects.filter(user=user) for course_selection in course_selections: if course == course_selection.course: already_selected = True break if not already_selected: new_course_selection = CourseSelection() new_course_selection.user = user new_course_selection.course = course new_course_selection.save() messages.success(request, 'Successfully added course to your selections.') response_obj = { 'status': 'success', 'course_id': course_id, } else: messages.error(request, 'You have already selected this course.') response_obj = { 'status': 'error', } else: messages.error( request, 'Could not add course to your selections. Please try again.') response_obj = { 'status': 'error', } response_json = json.dumps(response_obj) return HttpResponse(response_json, content_type='text/json')
def submission(request): context = {} try: task = Task.objects.get(uuid=request.data.get('uuid')) submission = Submission.objects.create( task=task, account=get_user(request), file=request.FILES.get('file', None), github_link=request.data.get('github_link', None)) context['status'] = 'successful' context[ 'file'] = submission.file.url if not task.is_github else submission.github_link context['submission'] = SubmissionSerializer(submission).data email = Email() email.send_submission_email(request, submission) except IntegrityError as e: context['status'] = 'unsuccessful' context['error'] = 'You might have already submitted' except Exception as e: context['status'] = 'unsuccessful' context['error'] = e.__str__() return Response(context)