Example #1
0
def feedback(request):
    body = request.data
    if 'recaptcha_token' in body and utils.verify_recaptcha(
            request.user.email, body['recaptcha_token'],
            'feedback') == ResponseCodes.verify_recaptcha_failed:
        return JsonResponse(create_response(
            data=None,
            success=False,
            error_code=ResponseCodes.verify_recaptcha_failed),
                            safe=False)

    text = body['text']
    star = body['star']
    user = request.user
    Feedback.objects.create(user=user, text=text, star=star)
    send_notification_email_to_admins('feedback')
    return JsonResponse(create_response(data=None), safe=False)
Example #2
0
def feedback(request):
    body = request.data
    if 'recaptcha_token' in body and utils.verify_recaptcha(
            request.user.email, body['recaptcha_token'],
            'feedback') == ResponseCodes.verify_recaptcha_failed:
        return JsonResponse(create_response(
            data=None,
            success=False,
            error_code=ResponseCodes.verify_recaptcha_failed),
                            safe=False)

    text = body['text']
    star = body['star']
    user = request.user
    if user.username.startswith('demo'):
        from_demo_account = True
    else:
        from_demo_account = False
    Feedback.objects.create(user=user,
                            text=text,
                            star=star,
                            from_demo_account=from_demo_account)
    send_notification_email_to_admins('feedback', feedback.user.college.id)
    return JsonResponse(create_response(data=None), safe=False)
Example #3
0
def blogs(request):
    user_profile = request.user
    if request.method == "GET":
        if user_profile.user_type.name == 'Career Service' and get_boolean_from_request(request, 'waiting'):
            queryset = Blog.objects.filter(is_approved=False, is_publish=True, is_rejected=False, college=user_profile.college)
        elif request.user.user_type.name == 'Career Service' and request.GET.get('type', '') != '':
            queryset = Blog.objects.filter(Q(is_publish=True, is_rejected=False, is_approved=True) | Q(publisher_profile=request.user),
                publisher_profile__user_type__id=int(request.GET.get('type')), college=request.user.college)
        else:
            if user_profile.user_type.name == 'Career Service':
                student = get_boolean_from_request(request, 'student')
                if student:
                    user_type = UserType.objects.get(name='Student')
                else:
                    user_type = UserType.objects.get(name='Alumni')
                queryset = Blog.objects.filter(is_approved=True, is_publish=True, college=user_profile.college, user_types__in=[user_type])
            else:
                if user_profile.user_type.name == 'Public':
                    queryset = Blog.objects.filter(Q(is_approved=True, is_publish=True) | Q(publisher_profile=request.user),
                                                   Q(user_types__in=[user_profile.user_type])
                                                   | Q(publisher_profile__is_superuser=True))
                else:
                    queryset = Blog.objects.filter(Q(is_approved=True, is_publish=True) | Q(publisher_profile=request.user),
                                                   Q(user_types__in=[user_profile.user_type],college=user_profile.college)
                                                   | Q(publisher_profile__is_superuser=True))
        queryset = queryset.filter(publisher_profile__isnull=False)
        paginator = pagination.CustomPagination()
        blogs = paginator.paginate_queryset(queryset, request)
        serialized_blogs = BlogSnippetSerializer(
            instance=blogs, many=True, context={'user': request.user}).data
        return JsonResponse(create_response(data=serialized_blogs, paginator=paginator), safe=False)
    else:
        if not user_profile.user_type.blog_creation_enabled:
            return JsonResponse(create_response(data=None, success=False, error_code=ResponseCodes.not_supported_user),
                                safe=False)
        if request.method == "POST":
            body = request.data
            blog = Blog()
            blog.college = request.user.college
            blog.publisher_profile = request.user
            if 'header_image' in body:
                file = body['header_image']
                ext = file.name.split('.')[-1]
                filename = "%s.%s" % (uuid.uuid4(), ext)
                blog.header_image.save(filename, file, save=True)
            if 'title' in body:
                title = body['title']
                blog.title = title
            if 'content' in body:
                content = body['content']
                blog.content = content
            if 'snippet' in body:
                snippet = body['snippet'][:130] + '...'
                blog.snippet = snippet
            if 'is_publish' in body:
                is_publish = get_boolean_from_request(request, 'is_publish', 'POST')
                blog.is_publish = is_publish
            if request.user.user_type.name == 'Career Service':
                user_types = body['user_types'].split(',')
                blog.save()
                for type in user_types:
                    user_type = UserType.objects.get(pk=int(type))
                    blog.user_types.add(user_type)
                blog.is_approved = True
            else:
                blog.user_types.add(request.user.user_type)
                if blog.is_publish:
                    send_notification_email_to_admins('blog', blog.college.id)
                blog.is_approved = False

            blog.save()
            return JsonResponse(create_response(data={"id": blog.id}), safe=False)
        elif request.method == "PUT":
            body = request.data
            blog = Blog.objects.get(pk=body['blog_id'], publisher_profile=request.user)
            if 'title' in body:
                blog.title = body['title']
            if 'content' in body:
                blog.content = body['content']
                blog.snippet = body['snippet'][:130] + '...'
            if 'header_image' in body:
                file = body['header_image']
                ext = file.name.split('.')[-1]
                filename = "%s.%s" % (uuid.uuid4(), ext)
                blog.header_image.save(filename, file, save=True)
            if 'is_publish' in body:
                blog.is_publish = get_boolean_from_request(request, 'is_publish', 'POST')
            if request.user.user_type.name == 'Career Service':
                user_types = body['user_types'].split(',')
                blog.user_types.clear()
                for type in user_types:
                    user_type = UserType.objects.get(pk=int(type))
                    blog.user_types.add(user_type)
                blog.is_approved = True
            else:
                if blog.is_publish:
                    send_notification_email_to_admins('blog', blog.college.id)
                blog.is_approved = False
            blog.updated_at = timezone.now()
            blog.save()

            return JsonResponse(create_response(data={"id": blog.id}), safe=False)
        elif request.method == "PATCH":
            if request.user.user_type.name == 'Career Service':
                body = request.data
                blog = Blog.objects.get(pk=body['blog_id'])
                approved = body['approved']
                blog.is_approved = approved
                blog.is_rejected = not approved
                blog.save()
                return JsonResponse(create_response(data=None), safe=False)
            else:
                return JsonResponse(
                    create_response(data=None, success=False, error_code=ResponseCodes.not_supported_user),
                    safe=False)
        elif request.method == "DELETE":
            body = request.data
            blog = Blog.objects.get(pk=body['blog_id'], publisher_profile=request.user)
            blog.delete()
            return JsonResponse(create_response(data=None), safe=False)
Example #4
0
def blogs(request):
    user_profile = Profile.objects.get(user=request.user)
    if request.method == "GET":
        mine = request.GET.get('mine')
        if mine is None:
            if user_profile.user_type >= int(Profile.UserTypes.student):
                queryset = Blog.objects.filter(
                    Q(is_approved=True) | Q(publisher_profile=request.user))
            else:
                queryset = Blog.objects.filter(is_approved=True,
                                               is_public=True)
        else:
            queryset = Blog.objects.filter(publisher_profile=request.user)
        paginator = pagination.CustomPagination()
        blogs = paginator.paginate_queryset(queryset, request)
        serialized_blogs = BlogSnippetSerializer(instance=blogs,
                                                 many=True,
                                                 context={
                                                     'user': request.user
                                                 }).data
        return JsonResponse(create_response(data=serialized_blogs,
                                            paginator=paginator),
                            safe=False)
    else:
        if user_profile.user_type < int(Profile.UserTypes.student):
            return JsonResponse(create_response(
                data=None,
                success=False,
                error_code=ResponseCodes.not_supported_user),
                                safe=False)
        if request.method == "POST":
            body = request.data
            blog = Blog()
            if 'header_image' in body:
                file = body['header_image']
                ext = file.name.split('.')[-1]
                filename = "%s.%s" % (uuid.uuid4(), ext)
                blog.header_image.save(filename, file, save=True)
            if 'title' in body:
                title = body['title']
                blog.title = title
            if 'content' in body:
                content = body['content']
                blog.content = content
            if 'snippet' in body:
                snippet = body['snippet'][:130] + '...'
                blog.snippet = snippet
            if 'is_publish' in body:
                is_publish = get_boolean_from_request(request, 'is_publish',
                                                      request.method)
                blog.is_publish = is_publish
                send_notification_email_to_admins('blog')
            if 'is_public' in body:
                is_public = get_boolean_from_request(request, 'is_public',
                                                     request.method)
                blog.is_public = is_public
            blog.publisher_profile = request.user

            blog.save()
            return JsonResponse(create_response(data={"id": blog.id}),
                                safe=False)
        elif request.method == "PUT":
            body = request.data
            blog = Blog.objects.get(pk=body['blog_id'],
                                    publisher_profile=request.user)
            if 'title' in body:
                blog.title = body['title']
            if 'content' in body:
                blog.content = body['content']
                blog.snippet = body['snippet'][:130] + '...'
            if 'header_image' in body:
                file = body['header_image']
                ext = file.name.split('.')[-1]
                filename = "%s.%s" % (uuid.uuid4(), ext)
                blog.header_image.save(filename, file, save=True)
            if 'is_publish' in body:
                blog.is_publish = get_boolean_from_request(
                    request, 'is_publish', request.method)
            if 'is_public' in body:
                blog.is_public = get_boolean_from_request(
                    request, 'is_public', request.method)
            blog.is_approved = False
            blog.updated_at = datetime.now()
            blog.save()
            send_notification_email_to_admins('blog')
            return JsonResponse(create_response(data={"id": blog.id}),
                                safe=False)
        elif request.method == "DELETE":
            body = request.data
            blog = Blog.objects.get(pk=body['blog_id'],
                                    publisher_profile=request.user)
            blog.delete()
            return JsonResponse(create_response(data=None), safe=False)
Example #5
0
def reviews(request):
    body = request.data
    user = request.user
    if 'recaptcha_token' in body and utils.verify_recaptcha(
            user, body['recaptcha_token'],
            'review') == ResponseCodes.verify_recaptcha_failed:
        return JsonResponse(create_response(
            data=None,
            success=False,
            error_code=ResponseCodes.verify_recaptcha_failed),
                            safe=False)
    if request.method == "GET" and user.user_type.name == 'Career Service':
        reviews_list = Review.objects.filter(is_published=False,
                                             is_rejected=False,
                                             user__college=user.college)
        return JsonResponse(create_response(
            data=ReviewSerializer(instance=reviews_list, many=True).data),
                            safe=False)
    elif request.method == "PATCH":
        if request.user.user_type.name == 'Career Service':
            body = request.data
            review = Review.objects.get(pk=body['review_id'])
            approved = body['approved']
            review.is_published = approved
            review.is_rejected = not approved
            review.save()
            return JsonResponse(create_response(data=None), safe=False)
        else:
            return JsonResponse(create_response(
                data=None,
                success=False,
                error_code=ResponseCodes.not_supported_user),
                                safe=False)
    elif request.method == "GET":
        company_id = request.GET.get('company_id')
        position_id = request.GET.get('position_id')
        all_reviews = get_boolean_from_request(request, 'all_reviews')
        review_id = request.GET.get('review_id')
        if review_id is not None:
            reviews_list = Review.objects.filter(pk=review_id,
                                                 user=request.user)
            if reviews_list.count() == 0:
                return JsonResponse(create_response(
                    data=None,
                    success=False,
                    error_code=ResponseCodes.record_not_found),
                                    safe=False)
            return JsonResponse(create_response(data=ReviewSerializer(
                instance=reviews_list[0], many=False).data),
                                safe=False)
        elif company_id is None and position_id is None:
            return JsonResponse(create_response(
                data=None,
                success=False,
                error_code=ResponseCodes.invalid_parameters),
                                safe=False)
        if company_id is None:
            reviews_list = Review.objects.filter(Q(is_published=True)
                                                 | Q(user=request.user),
                                                 position__pk=position_id)
        elif position_id is None:
            reviews_list = Review.objects.filter(Q(is_published=True)
                                                 | Q(user=request.user),
                                                 company__pk=company_id)
        else:
            if all_reviews:
                reviews_list = Review.objects.filter(is_published=True,
                                                     position__pk=position_id,
                                                     company__pk=company_id)
            else:
                reviews_list = Review.objects.filter(user=request.user,
                                                     position__pk=position_id,
                                                     company__pk=company_id)
                if reviews_list.count() > 0:
                    return JsonResponse(create_response(data=ReviewSerializer(
                        instance=reviews_list[0], many=False).data),
                                        safe=False)
                else:
                    return JsonResponse(create_response(data=None), safe=False)
        return JsonResponse(create_response(
            data=ReviewSerializer(instance=reviews_list, many=True).data),
                            safe=False)
    else:
        if 'company_id' not in body or 'position_id' not in body:
            return JsonResponse(create_response(
                data=None,
                success=False,
                error_code=ResponseCodes.invalid_parameters),
                                safe=False)

        company = Company.objects.get(pk=body['company_id'])
        position = JobPosition.objects.get(pk=body['position_id'])
        if request.method == "PUT":
            review = Review.objects.get(pk=body['review_id'])
            if review.user.pk != user.pk:
                return JsonResponse(create_response(
                    data=None,
                    success=False,
                    error_code=ResponseCodes.record_not_found),
                                    safe=False)
            review.update_date = timezone.now()
        elif request.method == "POST":
            review = Review()
        review.company = company
        review.position = position
        review.user = request.user
        if 'pros' in body:
            review.pros = body['pros']
        if 'cons' in body:
            review.cons = body['cons']
        if 'interview_notes' in body:
            review.interview_notes = body['interview_notes']
        if 'overall_company_experience' in body:
            review.overall_company_experience = body[
                'overall_company_experience']
        if 'interview_difficulty' in body:
            review.interview_difficulty = body['interview_difficulty']
        if 'overall_interview_experience' in body:
            review.overall_interview_experience = body[
                'overall_interview_experience']
        if 'anonymous' in body:
            review.anonymous = body['anonymous']
        if 'emp_auths' in body:
            for a in body['emp_auths']:
                if 'value' in a:
                    auth = EmploymentAuth.objects.get(pk=a['id'])
                    review.save()
                    if CompanyEmploymentAuth.objects.filter(
                            review=review, employment_auth=auth).count() == 0:
                        c_auth = CompanyEmploymentAuth(review=review,
                                                       employment_auth=auth,
                                                       value=a['value'])
                        c_auth.save()
                    else:
                        c_auth = CompanyEmploymentAuth.objects.get(
                            review=review, employment_auth=auth)
                        c_auth.value = a['value']
                        c_auth.save()
        if 'emp_status_id' in body:
            review.emp_status = EmploymentStatus.objects.get(
                pk=body['emp_status_id'])
        if 'source_type_id' in body:
            review.source_type = SourceType.objects.get(
                pk=body['source_type_id'])

        # publish review if there is no content to approve
        if 'pros' not in body and 'cons' not in body and 'interview_notes' not in body:
            review.is_published = True
        else:
            review.is_published = False
            send_notification_email_to_admins('review', review.user.college.id)

        review.save()
        response = {
            'review':
            ReviewSerializer(instance=review, many=False).data,
            'company':
            CompanySerializer(instance=company,
                              many=False,
                              context={
                                  'user': request.user,
                                  'position': position
                              }).data
        }
        return JsonResponse(create_response(data=response), safe=False)
Example #6
0
def events(request):
    if request.method == "GET":
        if request.user.user_type.name == 'Career Service' and get_boolean_from_request(request, 'waiting'):
            queryset = Event.objects.filter(is_approved=False, is_publish=True, is_rejected=False, college=request.user.college).order_by('-updated_at')
        elif request.user.user_type.name == 'Career Service' and request.GET.get('type', '') != '':
            queryset = Event.objects.filter(Q(is_publish=True, is_rejected=False, is_approved=True) | Q(host_user=request.user),
                                            host_user__user_type__id=int(request.GET.get('type')), college=request.user.college).order_by('-updated_at')
        else:
            attended = get_boolean_from_request(request, 'attended')
            if not attended:
                user_profile = request.user
                if user_profile.user_type.name == 'Career Service':
                    student = get_boolean_from_request(request, 'student')
                    if student:
                        user_type = UserType.objects.get(name='Student')
                    else:
                        user_type = UserType.objects.get(name='Alumni')
                    queryset = Event.objects.filter(is_approved=True, is_publish=True, college=user_profile.college, user_types__in=[user_type])
                else:
                    if user_profile.user_type.name == 'Public':
                        queryset = Event.objects.filter(Q(is_approved=True, is_publish=True) | Q(host_user=request.user),
                                                        Q(user_types__in=[user_profile.user_type])
                                                        | Q(host_user__is_superuser=True))
                    else:
                        queryset = Event.objects.filter(Q(is_approved=True, is_publish=True) | Q(host_user=request.user),
                                                        Q(user_types__in=[user_profile.user_type], college=user_profile.college)
                                                        | Q(host_user__is_superuser=True))
            else:
                attended_events = EventAttendee.objects.filter(user=request.user)
                queryset = Event.objects.all().filter(id__in=[e.event.id for e in attended_events])
        queryset = queryset.filter(host_user__isnull=False)
        paginator = pagination.CustomPagination()
        event_list = paginator.paginate_queryset(queryset, request)
        serialized_events = EventSimpleSerializer(
            instance=event_list, many=True, context={'user': request.user, 'detailed': False}).data
        return JsonResponse(create_response(data=serialized_events, paginator=paginator), safe=False)
    elif request.method == "DELETE":
        body = request.data
        event = Event.objects.get(pk=body['event_id'], host_user=request.user)
        event.delete()
        return JsonResponse(create_response(data=None), safe=False)
    elif request.method == "PATCH":
        if request.user.user_type.name == 'Career Service':
            body = request.data
            event = Event.objects.get(pk=body['event_id'])
            approved = body['approved']
            event.is_approved = approved
            event.is_rejected = not approved
            event.save()
            return JsonResponse(create_response(data=None), safe=False)
        else:
            return JsonResponse(
                create_response(data=None, success=False, error_code=ResponseCodes.not_supported_user),
                safe=False)
    else:
        user_profile = request.user
        if not user_profile.user_type.event_creation_enabled:
            return JsonResponse(create_response(data=None, success=False, error_code=ResponseCodes.not_supported_user),
                                safe=False)
        body = request.data
        if request.method == "POST":
            event = Event()

            if request.user.user_type.name != 'Career Service':
                event.user_types.add(request.user.user_type)
            else:
                event.save()
                user_types = body['user_types'].split(',')
                for type in user_types:
                    user_type = UserType.objects.get(pk=type)
                    event.user_types.add(user_type)
            event.college = request.user.college
        else:
            event = Event.objects.get(pk=body['event_id'])
            event.updated_at = timezone.now()
            if request.user.user_type.name == 'Career Service':
                event.user_types.clear()
                user_types = body['user_types'].split(',')
                for type in user_types:
                    user_type = UserType.objects.get(pk=type)
                    event.user_types.add(user_type)

        event.host_user = request.user
        if 'title' in body:
            event.title = body['title']
        if 'short_description' in body:
            event.short_description = body['short_description']
        if 'details' in body:
            event.details = body['details']
        if 'location_lat' in body:
            event.location_lat = body['location_lat']
        if 'location_lon' in body:
            event.location_lon = body['location_lon']
        if 'location_title' in body:
            event.location_title = body['location_title']
        if 'location_address' in body:
            event.location_address = body['location_address']
        if 'event_date_start' in body:
            event.event_date_start = body['event_date_start']
        if 'event_date_end' in body:
            event.event_date_end = body['event_date_end']
        if 'event_type_id' in body:
            event.event_type = EventType.objects.get(pk=int(body['event_type_id']))
        if 'spot_count' in body:
            event.spot_count = int(body['spot_count'])
        if 'header_image' in body:
            file = body['header_image']
            ext = file.name.split('.')[-1]
            filename = "%s.%s" % (uuid.uuid4(), ext)
            event.header_image.save(filename, file, save=True)
        if 'is_publish' in body:
            event.is_publish = get_boolean_from_request(request, 'is_publish', 'POST')
        if request.user.user_type.name == 'Career Service':
            event.is_approved = True
        else:
            if event.is_publish:
                send_notification_email_to_admins('event', event.college.id)
            event.is_approved = False
        event.save()
        return JsonResponse(create_response(data={"id": event.id}), safe=False)
Example #7
0
def events(request):
    if request.method == "GET":
        attended = get_boolean_from_request(request, 'attended')
        if not attended:
            user_profile = request.user
            if user_profile.user_type >= int(User.UserTypes.student):
                queryset = Event.objects.filter(is_approved=True)
            else:
                queryset = Event.objects.filter(is_approved=True, is_public=True)
        else:
            attended_events = EventAttendee.objects.filter(user=request.user)
            queryset = Event.objects.all().filter(id__in=[e.event.id for e in attended_events])
        queryset = queryset.filter(host_user__isnull=False)
        paginator = pagination.CustomPagination()
        event_list = paginator.paginate_queryset(queryset, request)
        serialized_events = EventSimpleSerializer(
            instance=event_list, many=True, context={'user': request.user, 'detailed': False}).data
        return JsonResponse(create_response(data=serialized_events, paginator=paginator), safe=False)
    elif request.method == "DELETE":
        user_profile = request.user
        if user_profile.user_type < int(User.UserTypes.student):
            return JsonResponse(create_response(data=None, success=False, error_code=ResponseCodes.not_supported_user),
                                safe=False)

        body = request.data
        event = Event.objects.get(pk=body['event_id'], host_user=request.user)
        event.delete()
        return JsonResponse(create_response(data=None), safe=False)
    else:
        user_profile = request.user
        if user_profile.user_type < int(User.UserTypes.student):
            return JsonResponse(create_response(data=None, success=False, error_code=ResponseCodes.not_supported_user),
                                safe=False)
        body = request.data
        if request.method == "POST":
            event = Event()
        else:
            event = Event.objects.get(pk=body['event_id'])
            event.updated_at = datetime.now()

        event.host_user = request.user
        if 'title' in body:
            event.title = body['title']
        if 'short_description' in body:
            event.short_description = body['short_description']
        if 'details' in body:
            event.details = body['details']
        if 'location_lat' in body:
            event.location_lat = body['location_lat']
        if 'location_lon' in body:
            event.location_lon = body['location_lon']
        if 'location_address' in body:
            event.location_address = body['location_address']
        if 'event_date_start' in body:
            event.event_date_start = body['event_date_start']
        if 'event_date_end' in body:
            event.event_date_end = body['event_date_end']
        if 'event_type_id' in body:
            event.event_type = EventType.objects.get(pk=body['event_type_id'])
        if 'spot_count' in body:
            event.spot_count = body['spot_count']
        if 'header_image' in body:
            file = body['header_image']
            ext = file.name.split('.')[-1]
            filename = "%s.%s" % (uuid.uuid4(), ext)
            event.header_image.save(filename, file, save=True)
        if 'is_public' in body:
            event.is_public = get_boolean_from_request(request, 'is_public')
        if 'is_publish' in body:
            event.is_publish = get_boolean_from_request(request, 'is_publish')
        event.is_approved = False
        event.save()
        send_notification_email_to_admins('event')
        return JsonResponse(create_response(data={"id": event.id}), safe=False)